]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add test -o flag to control where test binary is written
authorRuss Cox <rsc@golang.org>
Fri, 26 Sep 2014 17:50:02 +0000 (13:50 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 26 Sep 2014 17:50:02 +0000 (13:50 -0400)
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

src/cmd/go/test.bash
src/cmd/go/test.go
src/cmd/go/testflag.go

index 80af61ae0edf83852a1fff446aa9fda0e39260c2..bc2ce710a0f3e62823c15f726cad89ce3dd980d8 100755 (executable)
@@ -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)
index c135b89c841181c088d8da87f2233b603129b3dc..100ef5fa82d83039c6b8f5c0154ff9dfbb3d7405 100644 (file)
@@ -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.
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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")
        }
@@ -781,17 +792,24 @@ 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
@@ -799,7 +817,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
                // run test
                runAction = &action{
                        f:          (*builder).runTest,
-                       deps:       []*action{pmainAction},
+                       deps:       []*action{buildAction},
                        p:          p,
                        ignoreFail: true,
                }
@@ -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 {
index 73f311e5f69e6e3a99c610e40b066ea353315f91..6da74b99677f92be2f09f15809d13130d47d8a06 100644 (file)
@@ -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},
@@ -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":
@@ -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