1
0
mirror of https://github.com/golang/go synced 2024-11-19 20:34:42 -07:00
go/test/linkx_run.go
Russ Cox 12795c02f3 cmd/link: deprecate -X name value in favor of -X name=value
People invoking the linker directly already have to change their scripts
to use the new "go tool link", so this is a good time to make the -X flag
behave like all other Go flags and take just a single argument.

The old syntax will continue to be accepted (it is rewritten into the new
syntax before flag parsing). Maybe some day we will be able to retire it.

Even if we never retire the old syntax, having the new syntax at least
makes the rewriting much less of a kludge.

Change-Id: I91e8df94f4c22b2186e81d7f1016b8767d777eac
Reviewed-on: https://go-review.googlesource.com/10310
Reviewed-by: Rob Pike <r@golang.org>
2015-06-05 04:55:41 +00:00

53 lines
1.1 KiB
Go

// +build !nacl
// 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 (
"bytes"
"fmt"
"os"
"os/exec"
)
func main() {
test(" ") // old deprecated syntax
test("=") // new syntax
}
func test(sep string) {
// Successful run
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()
if err != nil {
fmt.Println(errbuf.String())
fmt.Println(out.String())
fmt.Println(err)
os.Exit(1)
}
want := "hello\ntrumped\n"
got := out.String()
if got != want {
fmt.Printf("got %q want %q\n", got, want)
os.Exit(1)
}
// 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)
}
}