Commit 5d8d3d52 authored by Paul Jolly's avatar Paul Jolly Committed by Ian Lance Taylor

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: default avatarIan Lance Taylor <iant@golang.org>
parent 3ac17f86
...@@ -3179,6 +3179,22 @@ func TestGoGenerateEnv(t *testing.T) { ...@@ -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) { func TestGoGenerateBadImports(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("skipping because windows has no echo command") t.Skip("skipping because windows has no echo command")
......
...@@ -153,8 +153,18 @@ func runGenerate(cmd *base.Command, args []string) { ...@@ -153,8 +153,18 @@ func runGenerate(cmd *base.Command, args []string) {
} }
// Even if the arguments are .go files, this loop suffices. // Even if the arguments are .go files, this loop suffices.
for _, pkg := range load.Packages(args) { for _, pkg := range load.Packages(args) {
pkgName := pkg.Name
for _, file := range pkg.InternalGoFiles() { 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 break
} }
} }
......
...@@ -1294,7 +1294,13 @@ func (p *Package) mkAbs(list []string) []string { ...@@ -1294,7 +1294,13 @@ func (p *Package) mkAbs(list []string) []string {
// InternalGoFiles returns the list of Go files being built for the package, // InternalGoFiles returns the list of Go files being built for the package,
// using absolute paths. // using absolute paths.
func (p *Package) InternalGoFiles() []string { 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, // InternalGoFiles returns the list of all Go files possibly relevant for the package,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment