]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: updates go get to return exit status 0 for test file only pkgs
authorJess Frazelle <me@jessfraz.com>
Fri, 20 May 2016 23:17:27 +0000 (16:17 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 23 Aug 2016 23:49:34 +0000 (23:49 +0000)
Updates the behavior of `go get` to return exit status 0 when a
requested package only contains test files.

Fixes #15093

Change-Id: I76b80517d58748090f5e8c6f41178361e2d7ca54
Reviewed-on: https://go-review.googlesource.com/23314
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/build.go
src/cmd/go/get.go
src/cmd/go/go_test.go

index 1c9d3b2ba20f45ec15dbd8a0c0c9c0e11b787610..bb76465ce7292bde2a81042b1d33642bf03e5bfb 100644 (file)
@@ -579,6 +579,10 @@ func libname(args []string, pkgs []*Package) (string, error) {
 }
 
 func runInstall(cmd *Command, args []string) {
+       installPackages(args, false)
+}
+
+func installPackages(args []string, forGet bool) {
        if gobin != "" && !filepath.IsAbs(gobin) {
                fatalf("cannot install, GOBIN must be an absolute path")
        }
@@ -606,6 +610,8 @@ func runInstall(cmd *Command, args []string) {
 
        var b builder
        b.init()
+       // Set the behavior for `go get` to not error on packages with test files only.
+       b.testFilesOnlyOK = forGet
        var a *action
        if buildBuildmode == "shared" {
                if libName, err := libname(args, pkgs); err != nil {
@@ -696,6 +702,8 @@ type builder struct {
        flagCache   map[string]bool      // a cache of supported compiler flags
        print       func(args ...interface{}) (int, error)
 
+       testFilesOnlyOK bool // do not error if the packages only have test files
+
        output    sync.Mutex
        scriptDir string // current directory in printed script
 
@@ -1279,6 +1287,8 @@ func (b *builder) do(root *action) {
                if err != nil {
                        if err == errPrintedOutput {
                                setExitStatus(2)
+                       } else if _, ok := err.(*build.NoGoError); ok && len(a.p.TestGoFiles) > 0 && b.testFilesOnlyOK {
+                               // Ignore the "no buildable Go source files" error for a package with only test files.
                        } else {
                                errorf("%s", err)
                        }
@@ -1365,7 +1375,7 @@ func (b *builder) build(a *action) (err error) {
        }
 
        defer func() {
-               if err != nil && err != errPrintedOutput {
+               if _, ok := err.(*build.NoGoError); err != nil && err != errPrintedOutput && !(ok && b.testFilesOnlyOK && len(a.p.TestGoFiles) > 0) {
                        err = fmt.Errorf("go build %s: %v", a.p.ImportPath, err)
                }
        }()
index d30d612c72712f85d18acfbd178851d2fc6d8eb0..05b6cb787ecb2fd4b5c1a88fcf721ce80197e522 100644 (file)
@@ -139,7 +139,7 @@ func runGet(cmd *Command, args []string) {
                return
        }
 
-       runInstall(cmd, args)
+       installPackages(args, true)
 }
 
 // downloadPaths prepares the list of paths to pass to download.
index 66c641347cb27b5e8fd68d7aaf282c354b64a07b..5d7e2e9f3aa90d2e19fad1149965f93756ca4a5f 100644 (file)
@@ -1430,6 +1430,17 @@ func TestGoGetNonPkg(t *testing.T) {
        tg.grepStderr("golang.org/x/tools: no buildable Go source files", "missing error")
 }
 
+func TestGoGetTestOnlyPkg(t *testing.T) {
+       testenv.MustHaveExternalNetwork(t)
+
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.tempDir("gopath")
+       tg.setenv("GOPATH", tg.path("gopath"))
+       tg.run("get", "golang.org/x/tour/content")
+       tg.run("get", "-t", "golang.org/x/tour/content")
+}
+
 func TestInstalls(t *testing.T) {
        if testing.Short() {
                t.Skip("don't install into GOROOT in short mode")