Skip to content

Commit

Permalink
cmd/go: add test -o flag to control where test binary is written
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsc committed Sep 26, 2014
1 parent 13a2c1c commit ce143f2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
24 changes: 24 additions & 0 deletions src/cmd/go/test.bash
Expand Up @@ -552,6 +552,30 @@ if [ ! -x strings.test ]; then
fi
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)'
old=$(pwd)
tmp=$(cd /tmp && pwd -P)
Expand Down
52 changes: 35 additions & 17 deletions src/cmd/go/test.go
Expand Up @@ -66,16 +66,23 @@ non-test installation.
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.
(Where pkg is the last element of the package's import path.)
-c
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
Install packages that are dependencies of the test.
Do not run the test.
-exec xprog
Run the test binary using xprog. The behavior is the same as
in 'go run'. See 'go help run' for details.
-o file
Compile the test binary to the named file.
The test still runs (unless -c or -i is specified).
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.
Expand Down Expand Up @@ -123,6 +130,7 @@ control the execution of any test:
-blockprofile block.out
Write a goroutine blocking profile to the specified file
when all tests are complete.
Writes test binary as -c would.
-blockprofilerate n
Control the detail provided in goroutine blocking profiles by
Expand Down Expand Up @@ -154,8 +162,7 @@ control the execution of any test:
Sets -cover.
-coverprofile cover.out
Write a coverage profile to the specified file after all tests
have passed.
Write a coverage profile to the file after all tests have passed.
Sets -cover.
-cpu 1,2,4
Expand All @@ -165,10 +172,11 @@ control the execution of any test:
-cpuprofile cpu.out
Write a CPU profile to the specified file before exiting.
Writes test binary as -c would.
-memprofile mem.out
Write a memory profile to the specified file after all tests
have passed.
Write a memory profile to the file after all tests have passed.
Writes test binary as -c would.
-memprofilerate n
Enable more precise (and expensive) memory profiles by setting
Expand Down Expand Up @@ -275,10 +283,10 @@ var (
testCoverMode string // -covermode flag
testCoverPaths []string // -coverpkg flag
testCoverPkgs []*Package // -coverpkg flag
testO string // -o flag
testProfile bool // some profiling flag
testNeedBinary bool // profile needs to keep binary around
testV bool // -v flag
testFiles []string // -file flag(s) TODO: not respected
testTimeout string // -timeout flag
testArgs []string
testBench bool
Expand Down Expand Up @@ -310,6 +318,9 @@ func runTest(cmd *Command, args []string) {
if testC && len(pkgs) != 1 {
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 {
fatalf("cannot use test profile flag with multiple packages")
}
Expand Down Expand Up @@ -781,25 +792,32 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
a.objdir = testDir + string(filepath.Separator)
a.objpkg = filepath.Join(testDir, "main.a")
a.target = filepath.Join(testDir, testBinary) + exeSuffix
pmainAction := a
buildAction = a

if testC || testNeedBinary {
// -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,
deps: []*action{pmainAction},
deps: []*action{buildAction},
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 {
printAction = &action{p: p, deps: []*action{runAction}} // nop
} else {
// run test
runAction = &action{
f: (*builder).runTest,
deps: []*action{pmainAction},
deps: []*action{buildAction},
p: p,
ignoreFail: true,
}
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/go/testflag.go
Expand Up @@ -65,9 +65,9 @@ type testFlagSpec struct {
var testFlagDefn = []*testFlagSpec{
// local.
{name: "c", boolVar: &testC},
{name: "file", multiOK: true},
{name: "cover", boolVar: &testCover},
{name: "coverpkg"},
{name: "o"},

// build flags.
{name: "a", boolVar: &buildA},
Expand Down Expand Up @@ -153,6 +153,9 @@ func testFlags(args []string) (packageNames, passToTest []string) {
// bool flags.
case "a", "c", "i", "n", "x", "v", "race", "cover", "work":
setBoolFlag(f.boolVar, value)
case "o":
testO = value
testNeedBinary = true
case "p":
setIntFlag(&buildP, value)
case "exec":
Expand Down Expand Up @@ -184,8 +187,6 @@ func testFlags(args []string) (packageNames, passToTest []string) {
buildContext.BuildTags = strings.Fields(value)
case "compiler":
buildCompiler{}.Set(value)
case "file":
testFiles = append(testFiles, value)
case "bench":
// record that we saw the flag; don't care about the value
testBench = true
Expand Down

0 comments on commit ce143f2

Please sign in to comment.