1
0
mirror of https://github.com/golang/go synced 2024-11-23 14:20:05 -07:00

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 #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>
This commit is contained in:
Paul Jolly 2018-03-29 15:29:23 +01:00 committed by Ian Lance Taylor
parent 3ac17f86be
commit 5d8d3d52b0
3 changed files with 34 additions and 2 deletions

View 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")

View 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
}
}

View 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,