]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make generate pass correct GOPACKAGE to XTest files
authorPaul Jolly <paul@myitcv.io>
Thu, 29 Mar 2018 14:29:23 +0000 (15:29 +0100)
committerIan Lance Taylor <iant@golang.org>
Thu, 29 Mar 2018 17:58:14 +0000 (17:58 +0000)
The existing behaviour of go generate is to pass GOPACKAGE=p
to all package files, including XTest files. This however is
incorrect as the package name for the XTest files is p_test.

Fixes #24594

Change-Id: I96b6e5777ec511cdcf1a6267a43f4d8c544c4af3
Reviewed-on: https://go-review.googlesource.com/103415
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/generate/generate.go
src/cmd/go/internal/load/pkg.go

index c4939ed1d9d7c4609ae9a807a7be578758bf9a10..aa354027f400d304977401ee1019c4593201c2d2 100644 (file)
@@ -3179,6 +3179,22 @@ func TestGoGenerateEnv(t *testing.T) {
        }
 }
 
+func TestGoGenerateXTestPkgName(t *testing.T) {
+       if runtime.GOOS == "windows" {
+               t.Skip("skipping because windows has no echo command")
+       }
+
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.tempFile("env_test.go", "package main_test\n\n//go:generate echo $GOPACKAGE")
+       tg.run("generate", tg.path("env_test.go"))
+       want := "main_test"
+       if got := strings.TrimSpace(tg.getStdout()); got != want {
+               t.Errorf("go generate in XTest file got package name %q; want %q", got, want)
+       }
+}
+
 func TestGoGenerateBadImports(t *testing.T) {
        if runtime.GOOS == "windows" {
                t.Skip("skipping because windows has no echo command")
index 75c0d3b09d685788f72238bc4328ebd293957e8f..441f91aea83797ff0bb888abfeda1e8b383d3cfb 100644 (file)
@@ -153,8 +153,18 @@ func runGenerate(cmd *base.Command, args []string) {
        }
        // Even if the arguments are .go files, this loop suffices.
        for _, pkg := range load.Packages(args) {
+               pkgName := pkg.Name
+
                for _, file := range pkg.InternalGoFiles() {
-                       if !generate(pkg.Name, file) {
+                       if !generate(pkgName, file) {
+                               break
+                       }
+               }
+
+               pkgName += "_test"
+
+               for _, file := range pkg.InternalXGoFiles() {
+                       if !generate(pkgName, file) {
                                break
                        }
                }
index 0b82fc9f41fa89162c8b3fd107ac1aecbf80ca07..02cbb94bc7cd4d8e25328a14a68085c134afbb53 100644 (file)
@@ -1294,7 +1294,13 @@ func (p *Package) mkAbs(list []string) []string {
 // InternalGoFiles returns the list of Go files being built for the package,
 // using absolute paths.
 func (p *Package) InternalGoFiles() []string {
-       return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
+       return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles))
+}
+
+// InternalXGoFiles returns the list of Go files being built for the XTest package,
+// using absolute paths.
+func (p *Package) InternalXGoFiles() []string {
+       return p.mkAbs(p.XTestGoFiles)
 }
 
 // InternalGoFiles returns the list of all Go files possibly relevant for the package,