Commit ce143f25 authored by Russ Cox's avatar Russ Cox

cmd/go: add test -o flag to control where test binary is written

While we are here, remove undocumented, meaningless test -file flag.

Fixes #7724.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews, iant
https://golang.org/cl/149070043
parent 13a2c1ca
...@@ -552,6 +552,30 @@ if [ ! -x strings.test ]; then ...@@ -552,6 +552,30 @@ if [ ! -x strings.test ]; then
fi fi
rm -f strings.prof strings.test rm -f strings.prof strings.test
TEST go test -cpuprofile -o controls binary location
./testgo test -cpuprofile strings.prof -o mystrings.test strings || ok=false
if [ ! -x mystrings.test ]; then
echo "go test -cpuprofile -o mystrings.test did not create mystrings.test"
ok=false
fi
rm -f strings.prof mystrings.test
TEST go test -c -o controls binary location
./testgo test -c -o mystrings.test strings || ok=false
if [ ! -x mystrings.test ]; then
echo "go test -c -o mystrings.test did not create mystrings.test"
ok=false
fi
rm -f mystrings.test
TEST go test -o writes binary
./testgo test -o mystrings.test strings || ok=false
if [ ! -x mystrings.test ]; then
echo "go test -o mystrings.test did not create mystrings.test"
ok=false
fi
rm -f mystrings.test
TEST symlinks do not confuse go list '(issue 4568)' TEST symlinks do not confuse go list '(issue 4568)'
old=$(pwd) old=$(pwd)
tmp=$(cd /tmp && pwd -P) tmp=$(cd /tmp && pwd -P)
......
...@@ -66,16 +66,23 @@ non-test installation. ...@@ -66,16 +66,23 @@ non-test installation.
In addition to the build flags, the flags handled by 'go test' itself are: In addition to the build flags, the flags handled by 'go test' itself are:
-c Compile the test binary to pkg.test but do not run it. -c
(Where pkg is the last element of the package's import path.) Compile the test binary to pkg.test but do not run it
(where pkg is the last element of the package's import path).
The file name can be changed with the -o flag.
-exec xprog
Run the test binary using xprog. The behavior is the same as
in 'go run'. See 'go help run' for details.
-i -i
Install packages that are dependencies of the test. Install packages that are dependencies of the test.
Do not run the test. Do not run the test.
-exec xprog -o file
Run the test binary using xprog. The behavior is the same as Compile the test binary to the named file.
in 'go run'. See 'go help run' for details. The test still runs (unless -c or -i is specified).
The test binary also accepts flags that control execution of the test; these The test binary also accepts flags that control execution of the test; these
flags are also accessible by 'go test'. See 'go help testflag' for details. flags are also accessible by 'go test'. See 'go help testflag' for details.
...@@ -123,6 +130,7 @@ control the execution of any test: ...@@ -123,6 +130,7 @@ control the execution of any test:
-blockprofile block.out -blockprofile block.out
Write a goroutine blocking profile to the specified file Write a goroutine blocking profile to the specified file
when all tests are complete. when all tests are complete.
Writes test binary as -c would.
-blockprofilerate n -blockprofilerate n
Control the detail provided in goroutine blocking profiles by Control the detail provided in goroutine blocking profiles by
...@@ -154,8 +162,7 @@ control the execution of any test: ...@@ -154,8 +162,7 @@ control the execution of any test:
Sets -cover. Sets -cover.
-coverprofile cover.out -coverprofile cover.out
Write a coverage profile to the specified file after all tests Write a coverage profile to the file after all tests have passed.
have passed.
Sets -cover. Sets -cover.
-cpu 1,2,4 -cpu 1,2,4
...@@ -165,10 +172,11 @@ control the execution of any test: ...@@ -165,10 +172,11 @@ control the execution of any test:
-cpuprofile cpu.out -cpuprofile cpu.out
Write a CPU profile to the specified file before exiting. Write a CPU profile to the specified file before exiting.
Writes test binary as -c would.
-memprofile mem.out -memprofile mem.out
Write a memory profile to the specified file after all tests Write a memory profile to the file after all tests have passed.
have passed. Writes test binary as -c would.
-memprofilerate n -memprofilerate n
Enable more precise (and expensive) memory profiles by setting Enable more precise (and expensive) memory profiles by setting
...@@ -275,10 +283,10 @@ var ( ...@@ -275,10 +283,10 @@ var (
testCoverMode string // -covermode flag testCoverMode string // -covermode flag
testCoverPaths []string // -coverpkg flag testCoverPaths []string // -coverpkg flag
testCoverPkgs []*Package // -coverpkg flag testCoverPkgs []*Package // -coverpkg flag
testO string // -o flag
testProfile bool // some profiling flag testProfile bool // some profiling flag
testNeedBinary bool // profile needs to keep binary around testNeedBinary bool // profile needs to keep binary around
testV bool // -v flag testV bool // -v flag
testFiles []string // -file flag(s) TODO: not respected
testTimeout string // -timeout flag testTimeout string // -timeout flag
testArgs []string testArgs []string
testBench bool testBench bool
...@@ -310,6 +318,9 @@ func runTest(cmd *Command, args []string) { ...@@ -310,6 +318,9 @@ func runTest(cmd *Command, args []string) {
if testC && len(pkgs) != 1 { if testC && len(pkgs) != 1 {
fatalf("cannot use -c flag with multiple packages") fatalf("cannot use -c flag with multiple packages")
} }
if testO != "" && len(pkgs) != 1 {
fatalf("cannot use -o flag with multiple packages")
}
if testProfile && len(pkgs) != 1 { if testProfile && len(pkgs) != 1 {
fatalf("cannot use test profile flag with multiple packages") fatalf("cannot use test profile flag with multiple packages")
} }
...@@ -781,17 +792,24 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -781,17 +792,24 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
a.objdir = testDir + string(filepath.Separator) a.objdir = testDir + string(filepath.Separator)
a.objpkg = filepath.Join(testDir, "main.a") a.objpkg = filepath.Join(testDir, "main.a")
a.target = filepath.Join(testDir, testBinary) + exeSuffix a.target = filepath.Join(testDir, testBinary) + exeSuffix
pmainAction := a buildAction = a
if testC || testNeedBinary { if testC || testNeedBinary {
// -c or profiling flag: create action to copy binary to ./test.out. // -c or profiling flag: create action to copy binary to ./test.out.
runAction = &action{ target := filepath.Join(cwd, testBinary+exeSuffix)
if testO != "" {
target = testO
if !filepath.IsAbs(target) {
target = filepath.Join(cwd, target)
}
}
buildAction = &action{
f: (*builder).install, f: (*builder).install,
deps: []*action{pmainAction}, deps: []*action{buildAction},
p: pmain, p: pmain,
target: filepath.Join(cwd, testBinary+exeSuffix), target: target,
} }
pmainAction = runAction // in case we are running the test runAction = buildAction // make sure runAction != nil even if not running test
} }
if testC { if testC {
printAction = &action{p: p, deps: []*action{runAction}} // nop printAction = &action{p: p, deps: []*action{runAction}} // nop
...@@ -799,7 +817,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -799,7 +817,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
// run test // run test
runAction = &action{ runAction = &action{
f: (*builder).runTest, f: (*builder).runTest,
deps: []*action{pmainAction}, deps: []*action{buildAction},
p: p, p: p,
ignoreFail: true, ignoreFail: true,
} }
...@@ -815,7 +833,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -815,7 +833,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
} }
} }
return pmainAction, runAction, printAction, nil return buildAction, runAction, printAction, nil
} }
func testImportStack(top string, p *Package, target string) []string { func testImportStack(top string, p *Package, target string) []string {
......
...@@ -65,9 +65,9 @@ type testFlagSpec struct { ...@@ -65,9 +65,9 @@ type testFlagSpec struct {
var testFlagDefn = []*testFlagSpec{ var testFlagDefn = []*testFlagSpec{
// local. // local.
{name: "c", boolVar: &testC}, {name: "c", boolVar: &testC},
{name: "file", multiOK: true},
{name: "cover", boolVar: &testCover}, {name: "cover", boolVar: &testCover},
{name: "coverpkg"}, {name: "coverpkg"},
{name: "o"},
// build flags. // build flags.
{name: "a", boolVar: &buildA}, {name: "a", boolVar: &buildA},
...@@ -153,6 +153,9 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -153,6 +153,9 @@ func testFlags(args []string) (packageNames, passToTest []string) {
// bool flags. // bool flags.
case "a", "c", "i", "n", "x", "v", "race", "cover", "work": case "a", "c", "i", "n", "x", "v", "race", "cover", "work":
setBoolFlag(f.boolVar, value) setBoolFlag(f.boolVar, value)
case "o":
testO = value
testNeedBinary = true
case "p": case "p":
setIntFlag(&buildP, value) setIntFlag(&buildP, value)
case "exec": case "exec":
...@@ -184,8 +187,6 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -184,8 +187,6 @@ func testFlags(args []string) (packageNames, passToTest []string) {
buildContext.BuildTags = strings.Fields(value) buildContext.BuildTags = strings.Fields(value)
case "compiler": case "compiler":
buildCompiler{}.Set(value) buildCompiler{}.Set(value)
case "file":
testFiles = append(testFiles, value)
case "bench": case "bench":
// record that we saw the flag; don't care about the value // record that we saw the flag; don't care about the value
testBench = true testBench = true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment