]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: split out cmd/go/internal/test
authorRuss Cox <rsc@golang.org>
Wed, 18 Jan 2017 17:56:37 +0000 (12:56 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 3 Feb 2017 20:31:41 +0000 (20:31 +0000)
This is one CL in a long sequence of changes to break up the
go command from one package into a plausible group of packages.

This sequence is concerned only with moving code, not changing
or cleaning up code. There will still be more cleanup after this sequence.

The entire sequence will be submitted together: it is not a goal
for the tree to build at every step.

For #18653.

Change-Id: I2d0ccdb84814537ab8b8842aa1b5f5bc0a88a0fc
Reviewed-on: https://go-review.googlesource.com/36198
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/go/bug.go
src/cmd/go/generate.go
src/cmd/go/internal/base/env.go
src/cmd/go/internal/test/test.go [moved from src/cmd/go/test.go with 98% similarity]
src/cmd/go/internal/test/testflag.go [moved from src/cmd/go/testflag.go with 98% similarity]
src/cmd/go/internal/work/build.go
src/cmd/go/main.go
src/cmd/go/run.go
src/cmd/go/tool.go
src/cmd/go/vcs.go

index be64fe80ddffe91c829fe4f133a49dd80967d283..239c27e4260893c6cf435f634be5820487665701 100644 (file)
@@ -6,8 +6,6 @@ package main
 
 import (
        "bytes"
-       "cmd/go/internal/base"
-       "cmd/go/internal/cfg"
        "fmt"
        "io"
        "io/ioutil"
@@ -17,6 +15,9 @@ import (
        "regexp"
        "runtime"
        "strings"
+
+       "cmd/go/internal/base"
+       "cmd/go/internal/cfg"
 )
 
 var cmdBug = &base.Command{
index 60d4f01e903e861f3049690a13046e2b8e2958a1..bc08e999007c6b432e35b35381e34f525eba2f35 100644 (file)
@@ -397,7 +397,7 @@ func (g *Generator) exec(words []string) {
        cmd.Stderr = os.Stderr
        // Run the command in the package directory.
        cmd.Dir = g.dir
-       cmd.Env = mergeEnvLists(g.env, cfg.OrigEnv)
+       cmd.Env = base.MergeEnvLists(g.env, cfg.OrigEnv)
        err := cmd.Run()
        if err != nil {
                g.errorf("running %q: %s", words[0], err)
index fb5956dfe34d2187e8fbf0e741f53b5bde212b48..fcade9d84e228400547fe3d9532b42ad8b655d32 100644 (file)
@@ -6,7 +6,7 @@ package base
 
 import "strings"
 
-// envForDir returns a copy of the environment
+// EnvForDir returns a copy of the environment
 // suitable for running in the given directory.
 // The environment is the current process's environment
 // but with an updated $PWD, so that an os.Getwd in the
similarity index 98%
rename from src/cmd/go/test.go
rename to src/cmd/go/internal/test/test.go
index c17507041e632729f8cd6f3560f7bab81d930b6b..366b19a7004672ef0aa18c0bf3fd41eee81463c9 100644 (file)
@@ -2,15 +2,10 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package test
 
 import (
        "bytes"
-       "cmd/go/internal/base"
-       "cmd/go/internal/cfg"
-       "cmd/go/internal/load"
-       "cmd/go/internal/str"
-       "cmd/go/internal/work"
        "errors"
        "fmt"
        "go/ast"
@@ -30,16 +25,22 @@ import (
        "time"
        "unicode"
        "unicode/utf8"
+
+       "cmd/go/internal/base"
+       "cmd/go/internal/cfg"
+       "cmd/go/internal/load"
+       "cmd/go/internal/str"
+       "cmd/go/internal/work"
 )
 
 // Break init loop.
 func init() {
-       cmdTest.Run = runTest
+       CmdTest.Run = runTest
 }
 
 const testUsage = "test [build/test flags] [packages] [build/test flags & test binary flags]"
 
-var cmdTest = &base.Command{
+var CmdTest = &base.Command{
        CustomFlags: true,
        UsageLine:   testUsage,
        Short:       "test packages",
@@ -112,7 +113,15 @@ The test binary also accepts flags that control execution of the test; these
 flags are also accessible by 'go test'.
 `
 
-var helpTestflag = &base.Command{
+// Usage prints the usage message for 'go test -h' and exits.
+func Usage() {
+       os.Stderr.WriteString(testUsage + "\n\n" +
+               strings.TrimSpace(testFlag1) + "\n\n\t" +
+               strings.TrimSpace(testFlag2) + "\n")
+       os.Exit(2)
+}
+
+var HelpTestflag = &base.Command{
        UsageLine: "testflag",
        Short:     "description of testing flags",
        Long: `
@@ -317,7 +326,7 @@ In the second example, the argument math is passed through to the test
 binary, instead of being interpreted as the package list.
 `
 
-var helpTestfunc = &base.Command{
+var HelpTestfunc = &base.Command{
        UsageLine: "testfunc",
        Short:     "description of testing functions",
        Long: `
@@ -408,7 +417,7 @@ func runTest(cmd *base.Command, args []string) {
        var pkgArgs []string
        pkgArgs, testArgs = testFlags(args)
 
-       findExecCmd() // initialize cached result
+       work.FindExecCmd() // initialize cached result
 
        work.InstrumentInit()
        work.BuildModeInit()
@@ -1107,7 +1116,7 @@ var noTestsToRun = []byte("\ntesting: warning: no tests to run\n")
 
 // builderRunTest is the action for running a test binary.
 func builderRunTest(b *work.Builder, a *work.Action) error {
-       args := str.StringList(findExecCmd(), a.Deps[0].Target, testArgs)
+       args := str.StringList(work.FindExecCmd(), a.Deps[0].Target, testArgs)
        a.TestOutput = new(bytes.Buffer)
 
        if cfg.BuildN || cfg.BuildX {
@@ -1127,7 +1136,7 @@ func builderRunTest(b *work.Builder, a *work.Action) error {
 
        cmd := exec.Command(args[0], args[1:]...)
        cmd.Dir = a.Package.Dir
-       cmd.Env = envForDir(cmd.Dir, cfg.OrigEnv)
+       cmd.Env = base.EnvForDir(cmd.Dir, cfg.OrigEnv)
        var buf bytes.Buffer
        if testStreamOutput {
                cmd.Stdout = os.Stdout
similarity index 98%
rename from src/cmd/go/testflag.go
rename to src/cmd/go/internal/test/testflag.go
index 4f519a4af6947c0a0a3080873325778d137b2e47..1c44af5a0053fce6b961aa7d20ef7a5696e0faa9 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package test
 
 import (
        "flag"
@@ -138,7 +138,6 @@ func testFlags(args []string) (packageNames, passToTest []string) {
                } else {
                        // Test-only flags.
                        // Arguably should be handled by f.flagValue, but aren't.
-                       var err error
                        switch f.name {
                        // bool flags.
                        case "c", "i", "v", "cover":
@@ -147,10 +146,11 @@ func testFlags(args []string) (packageNames, passToTest []string) {
                                testO = value
                                testNeedBinary = true
                        case "exec":
-                               execCmd, err = str.SplitQuotedFields(value)
+                               xcmd, err := str.SplitQuotedFields(value)
                                if err != nil {
                                        base.Fatalf("invalid flag argument for -%s: %v", f.name, err)
                                }
+                               work.ExecCmd = xcmd
                        case "bench":
                                // record that we saw the flag; don't care about the value
                                testBench = true
index 830015e94743109bd0a0ddabdbe66645c0d580be..63731e1dfa6ee77deaa871ac0a5ab4dffeafed9e 100644 (file)
@@ -3622,3 +3622,28 @@ func InstrumentInit() {
                cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "msan")
        }
 }
+
+// ExecCmd is the command to use to run user binaries.
+// Normally it is empty, meaning run the binaries directly.
+// If cross-compiling and running on a remote system or
+// simulator, it is typically go_GOOS_GOARCH_exec, with
+// the target GOOS and GOARCH substituted.
+// The -exec flag overrides these defaults.
+var ExecCmd []string
+
+// FindExecCmd derives the value of ExecCmd to use.
+// It returns that value and leaves ExecCmd set for direct use.
+func FindExecCmd() []string {
+       if ExecCmd != nil {
+               return ExecCmd
+       }
+       ExecCmd = []string{} // avoid work the second time
+       if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH {
+               return ExecCmd
+       }
+       path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
+       if err == nil {
+               ExecCmd = []string{path}
+       }
+       return ExecCmd
+}
index 64cde307c9ebf9a3331b84b9eda8c7ecdb403099..502e5716827847cf6320cc6daff4105a6fda45a6 100644 (file)
@@ -16,6 +16,7 @@ import (
        "cmd/go/internal/base"
        "cmd/go/internal/cfg"
        "cmd/go/internal/help"
+       "cmd/go/internal/test"
        "cmd/go/internal/work"
 )
 
@@ -33,7 +34,7 @@ func init() {
                work.CmdInstall,
                cmdList,
                cmdRun,
-               cmdTest,
+               test.CmdTest,
                cmdTool,
                cmdVersion,
                cmdVet,
@@ -45,8 +46,8 @@ func init() {
                help.HelpEnvironment,
                help.HelpImportPath,
                help.HelpPackages,
-               helpTestflag,
-               helpTestfunc,
+               test.HelpTestflag,
+               test.HelpTestfunc,
        }
 }
 
@@ -134,41 +135,8 @@ func init() {
 func mainUsage() {
        // special case "go test -h"
        if len(os.Args) > 1 && os.Args[1] == "test" {
-               os.Stderr.WriteString(testUsage + "\n\n" +
-                       strings.TrimSpace(testFlag1) + "\n\n\t" +
-                       strings.TrimSpace(testFlag2) + "\n")
-               os.Exit(2)
+               test.Usage()
        }
        help.PrintUsage(os.Stderr)
        os.Exit(2)
 }
-
-// envForDir returns a copy of the environment
-// suitable for running in the given directory.
-// The environment is the current process's environment
-// but with an updated $PWD, so that an os.Getwd in the
-// child will be faster.
-func envForDir(dir string, base []string) []string {
-       // Internally we only use rooted paths, so dir is rooted.
-       // Even if dir is not rooted, no harm done.
-       return mergeEnvLists([]string{"PWD=" + dir}, base)
-}
-
-// mergeEnvLists merges the two environment lists such that
-// variables with the same name in "in" replace those in "out".
-// This always returns a newly allocated slice.
-func mergeEnvLists(in, out []string) []string {
-       out = append([]string(nil), out...)
-NextVar:
-       for _, inkv := range in {
-               k := strings.SplitAfterN(inkv, "=", 2)[0]
-               for i, outkv := range out {
-                       if strings.HasPrefix(outkv, k) {
-                               out[i] = inkv
-                               continue NextVar
-                       }
-               }
-               out = append(out, inkv)
-       }
-       return out
-}
index 398a298fdc985c41dbaecd71bec37011cf835ae4..37209b366054afe3c99ae849c300af40a6fb8ae5 100644 (file)
@@ -7,8 +7,6 @@ package main
 import (
        "fmt"
        "os"
-       "os/exec"
-       "runtime"
        "strings"
 
        "cmd/go/internal/base"
@@ -18,23 +16,6 @@ import (
        "cmd/go/internal/work"
 )
 
-var execCmd []string // -exec flag, for run and test
-
-func findExecCmd() []string {
-       if execCmd != nil {
-               return execCmd
-       }
-       execCmd = []string{} // avoid work the second time
-       if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH {
-               return execCmd
-       }
-       path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
-       if err == nil {
-               execCmd = []string{path}
-       }
-       return execCmd
-}
-
 var cmdRun = &base.Command{
        UsageLine: "run [build flags] [-exec xprog] gofiles... [arguments...]",
        Short:     "compile and run Go program",
@@ -62,7 +43,7 @@ func init() {
        cmdRun.Run = runRun // break init loop
 
        work.AddBuildFlags(cmdRun)
-       cmdRun.Flag.Var((*base.StringsFlag)(&execCmd), "exec", "")
+       cmdRun.Flag.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "")
 }
 
 func printStderr(args ...interface{}) (int, error) {
@@ -136,7 +117,7 @@ func runRun(cmd *base.Command, args []string) {
 // buildRunProgram is the action for running a binary that has already
 // been compiled. We ignore exit status.
 func buildRunProgram(b *work.Builder, a *work.Action) error {
-       cmdline := str.StringList(findExecCmd(), a.Deps[0].Target, a.Args)
+       cmdline := str.StringList(work.FindExecCmd(), a.Deps[0].Target, a.Args)
        if cfg.BuildN || cfg.BuildX {
                b.Showcmd("", "%s", strings.Join(cmdline, " "))
                if cfg.BuildN {
index e6b3fd1e1b63f34770e71e00f005fedff2a2081b..2f08c419f3f51520a19215e30beda1112b53666c 100644 (file)
@@ -72,7 +72,7 @@ func runTool(cmd *base.Command, args []string) {
                Stdout: os.Stdout,
                Stderr: os.Stderr,
                // Set $GOROOT, mainly for go tool dist.
-               Env: mergeEnvLists([]string{"GOROOT=" + cfg.GOROOT}, os.Environ()),
+               Env: base.MergeEnvLists([]string{"GOROOT=" + cfg.GOROOT}, os.Environ()),
        }
        err := toolCmd.Run()
        if err != nil {
index bcb602c69bcbc566b14739a576e2a273982263bd..bb82deb3794363f7568b39d1b131dc10af887e6e 100644 (file)
@@ -6,7 +6,6 @@ package main
 
 import (
        "bytes"
-       "cmd/go/internal/cfg"
        "encoding/json"
        "errors"
        "fmt"
@@ -19,6 +18,9 @@ import (
        "regexp"
        "strings"
        "sync"
+
+       "cmd/go/internal/base"
+       "cmd/go/internal/cfg"
 )
 
 // A vcsCmd describes how to use a version control system
@@ -373,7 +375,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)
 
        cmd := exec.Command(v.cmd, args...)
        cmd.Dir = dir
-       cmd.Env = envForDir(cmd.Dir, os.Environ())
+       cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
        if cfg.BuildX {
                fmt.Printf("cd %s\n", dir)
                fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))