2014-11-09 19:10:49 -07:00
|
|
|
// +build !nacl
|
2014-11-06 13:14:08 -07: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 12:35:02 -06:00
|
|
|
"bytes"
|
2014-11-06 13:14:08 -07:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-06-29 11:03:11 -06:00
|
|
|
"strings"
|
2014-11-06 13:14:08 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-01-28 20:50:03 -07:00
|
|
|
// test(" ") // old deprecated & removed syntax
|
2015-05-21 12:35:02 -06:00
|
|
|
test("=") // new syntax
|
|
|
|
}
|
|
|
|
|
|
|
|
func test(sep string) {
|
2014-12-31 14:25:52 -07:00
|
|
|
// Successful run
|
2015-05-21 12:35:02 -06: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 13:14:08 -07:00
|
|
|
if err != nil {
|
2015-05-21 12:35:02 -06:00
|
|
|
fmt.Println(errbuf.String())
|
|
|
|
fmt.Println(out.String())
|
2014-11-06 13:14:08 -07:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "hello\ntrumped\n"
|
2015-05-21 12:35:02 -06:00
|
|
|
got := out.String()
|
2014-11-06 13:14:08 -07:00
|
|
|
if got != want {
|
|
|
|
fmt.Printf("got %q want %q\n", got, want)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-12-31 14:25:52 -07: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 11:03:11 -06: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 08:29:43 -06:00
|
|
|
fmt.Printf("-X linker flag did not diagnose overwrite of main.b:\n%s\n", outstr)
|
2015-06-29 11:03:11 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if !strings.Contains(outstr, "main.x") {
|
2016-04-20 08:29:43 -06:00
|
|
|
fmt.Printf("-X linker flag did not diagnose overwrite of main.x:\n%s\n", outstr)
|
2015-06-29 11:03:11 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-11-06 13:14:08 -07:00
|
|
|
}
|