2020-12-17 22:03:07 +00:00
|
|
|
// +build !nacl,!js,gc
|
2014-11-06 20:14:08 +00:00
|
|
|
// run
|
|
|
|
|
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Run the linkx test.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-21 18:35:02 +00:00
|
|
|
"bytes"
|
2014-11-06 20:14:08 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-06-29 17:03:11 +00:00
|
|
|
"strings"
|
2014-11-06 20:14:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-01-29 03:50:03 +00:00
|
|
|
// test(" ") // old deprecated & removed syntax
|
2015-05-21 18:35:02 +00:00
|
|
|
test("=") // new syntax
|
|
|
|
}
|
|
|
|
|
|
|
|
func test(sep string) {
|
2014-12-31 21:25:52 +00:00
|
|
|
// Successful run
|
2015-05-21 18:35:02 +00:00
|
|
|
cmd := exec.Command("go", "run", "-ldflags=-X main.tbd"+sep+"hello -X main.overwrite"+sep+"trumped -X main.nosuchsymbol"+sep+"neverseen", "linkx.go")
|
|
|
|
var out, errbuf bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &errbuf
|
|
|
|
err := cmd.Run()
|
2014-11-06 20:14:08 +00:00
|
|
|
if err != nil {
|
2015-05-21 18:35:02 +00:00
|
|
|
fmt.Println(errbuf.String())
|
|
|
|
fmt.Println(out.String())
|
2014-11-06 20:14:08 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-10-03 17:49:28 +00:00
|
|
|
want := "hello\nhello\nhello\ntrumped\ntrumped\ntrumped\n"
|
2015-05-21 18:35:02 +00:00
|
|
|
got := out.String()
|
2014-11-06 20:14:08 +00:00
|
|
|
if got != want {
|
|
|
|
fmt.Printf("got %q want %q\n", got, want)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-12-31 21:25:52 +00:00
|
|
|
|
|
|
|
// Issue 8810
|
|
|
|
cmd = exec.Command("go", "run", "-ldflags=-X main.tbd", "linkx.go")
|
|
|
|
_, err = cmd.CombinedOutput()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("-X linker flag should not accept keys without values")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-06-29 17:03:11 +00:00
|
|
|
|
|
|
|
// Issue 9621
|
|
|
|
cmd = exec.Command("go", "run", "-ldflags=-X main.b=false -X main.x=42", "linkx.go")
|
|
|
|
outx, err := cmd.CombinedOutput()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("-X linker flag should not overwrite non-strings")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
outstr := string(outx)
|
|
|
|
if !strings.Contains(outstr, "main.b") {
|
2016-04-20 14:29:43 +00:00
|
|
|
fmt.Printf("-X linker flag did not diagnose overwrite of main.b:\n%s\n", outstr)
|
2015-06-29 17:03:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if !strings.Contains(outstr, "main.x") {
|
2016-04-20 14:29:43 +00:00
|
|
|
fmt.Printf("-X linker flag did not diagnose overwrite of main.x:\n%s\n", outstr)
|
2015-06-29 17:03:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-11-06 20:14:08 +00:00
|
|
|
}
|