Skip to content

Commit 5d8d3d5

Browse files
myitcvianlancetaylor
authored andcommitted
cmd/go: make generate pass correct GOPACKAGE to XTest files
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 golang#24594 Change-Id: I96b6e5777ec511cdcf1a6267a43f4d8c544c4af3 Reviewed-on: https://go-review.googlesource.com/103415 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 3ac17f8 commit 5d8d3d5

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/cmd/go/go_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,6 +3179,22 @@ func TestGoGenerateEnv(t *testing.T) {
31793179
}
31803180
}
31813181

3182+
func TestGoGenerateXTestPkgName(t *testing.T) {
3183+
if runtime.GOOS == "windows" {
3184+
t.Skip("skipping because windows has no echo command")
3185+
}
3186+
3187+
tg := testgo(t)
3188+
defer tg.cleanup()
3189+
tg.parallel()
3190+
tg.tempFile("env_test.go", "package main_test\n\n//go:generate echo $GOPACKAGE")
3191+
tg.run("generate", tg.path("env_test.go"))
3192+
want := "main_test"
3193+
if got := strings.TrimSpace(tg.getStdout()); got != want {
3194+
t.Errorf("go generate in XTest file got package name %q; want %q", got, want)
3195+
}
3196+
}
3197+
31823198
func TestGoGenerateBadImports(t *testing.T) {
31833199
if runtime.GOOS == "windows" {
31843200
t.Skip("skipping because windows has no echo command")

src/cmd/go/internal/generate/generate.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,18 @@ func runGenerate(cmd *base.Command, args []string) {
153153
}
154154
// Even if the arguments are .go files, this loop suffices.
155155
for _, pkg := range load.Packages(args) {
156+
pkgName := pkg.Name
157+
156158
for _, file := range pkg.InternalGoFiles() {
157-
if !generate(pkg.Name, file) {
159+
if !generate(pkgName, file) {
160+
break
161+
}
162+
}
163+
164+
pkgName += "_test"
165+
166+
for _, file := range pkg.InternalXGoFiles() {
167+
if !generate(pkgName, file) {
158168
break
159169
}
160170
}

src/cmd/go/internal/load/pkg.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,13 @@ func (p *Package) mkAbs(list []string) []string {
12941294
// InternalGoFiles returns the list of Go files being built for the package,
12951295
// using absolute paths.
12961296
func (p *Package) InternalGoFiles() []string {
1297-
return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
1297+
return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles))
1298+
}
1299+
1300+
// InternalXGoFiles returns the list of Go files being built for the XTest package,
1301+
// using absolute paths.
1302+
func (p *Package) InternalXGoFiles() []string {
1303+
return p.mkAbs(p.XTestGoFiles)
12981304
}
12991305

13001306
// InternalGoFiles returns the list of all Go files possibly relevant for the package,

0 commit comments

Comments
 (0)