BuildInfo string // add this info to package main
TestmainGo *[]byte // content for _testmain.go
Embed map[string][]string // //go:embed comment mapping
+ OrigImportPath string // original import path before adding '_test' suffix
Asmflags []string // -asmflags for this package
Gcflags []string // -gcflags for this package
p.EmbedPatterns = pp.EmbedPatterns
p.TestEmbedPatterns = pp.TestEmbedPatterns
p.XTestEmbedPatterns = pp.XTestEmbedPatterns
+ p.Internal.OrigImportPath = pp.ImportPath
}
// A PackageError describes an error loading information about a package.
}
ptest.Internal.Embed = testEmbed
ptest.EmbedFiles = str.StringList(p.EmbedFiles, p.TestEmbedFiles)
+ ptest.Internal.OrigImportPath = p.Internal.OrigImportPath
ptest.collectDeps()
} else {
ptest = p
Imports: ximports,
RawImports: rawXTestImports,
- Asmflags: p.Internal.Asmflags,
- Gcflags: p.Internal.Gcflags,
- Ldflags: p.Internal.Ldflags,
- Gccgoflags: p.Internal.Gccgoflags,
- Embed: xtestEmbed,
+ Asmflags: p.Internal.Asmflags,
+ Gcflags: p.Internal.Gcflags,
+ Ldflags: p.Internal.Ldflags,
+ Gccgoflags: p.Internal.Gccgoflags,
+ Embed: xtestEmbed,
+ OrigImportPath: p.Internal.OrigImportPath,
},
}
if pxtestNeedsPtest {
Module: p.Module,
},
Internal: PackageInternal{
- Build: &build.Package{Name: "main"},
- BuildInfo: p.Internal.BuildInfo,
- Asmflags: p.Internal.Asmflags,
- Gcflags: p.Internal.Gcflags,
- Ldflags: p.Internal.Ldflags,
- Gccgoflags: p.Internal.Gccgoflags,
+ Build: &build.Package{Name: "main"},
+ BuildInfo: p.Internal.BuildInfo,
+ Asmflags: p.Internal.Asmflags,
+ Gcflags: p.Internal.Gcflags,
+ Ldflags: p.Internal.Ldflags,
+ Gccgoflags: p.Internal.Gccgoflags,
+ OrigImportPath: p.Internal.OrigImportPath,
},
}
rewriteDir := a.Package.Dir
if cfg.BuildTrimpath {
+ importPath := a.Package.Internal.OrigImportPath
if m := a.Package.Module; m != nil && m.Version != "" {
- rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(a.Package.ImportPath, m.Path)
+ rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(importPath, m.Path)
} else {
- rewriteDir = a.Package.ImportPath
+ rewriteDir = importPath
}
rewrite += a.Package.Dir + "=>" + rewriteDir + ";"
}
--- /dev/null
+[short] skip
+
+go test -trimpath -v .
+! stdout '[/\\]pkg_test[/\\]'
+stdout -count=3 '[/\\]pkg[/\\]'
+
+-- go.mod --
+module example.com/pkg
+
+go 1.17
+
+-- pkg.go --
+package pkg
+
+import "runtime"
+
+func PrintFile() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
+
+-- pkg_test.go --
+package pkg
+
+import "runtime"
+
+func PrintFileForTest() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
+
+-- pkg_x_test.go --
+package pkg_test
+
+import (
+ "runtime"
+ "testing"
+
+ "example.com/pkg"
+)
+
+func TestMain(m *testing.M) {
+ pkg.PrintFile()
+ pkg.PrintFileForTest()
+ PrintFileInXTest()
+}
+
+func PrintFileInXTest() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
--- /dev/null
+[short] skip
+
+go test -trimpath -v .
+! stdout '[/\\]pkg_test[/\\]'
+stdout -count=2 '[/\\]pkg[/\\]'
+
+-- go.mod --
+module example.com/pkg
+
+go 1.17
+
+-- main.go --
+package main
+
+import "runtime"
+
+func PrintFile() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
+
+-- main_test.go --
+package main
+
+import (
+ "runtime"
+ "testing"
+)
+
+func PrintFileForTest() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
+
+func TestMain(m *testing.M) {
+ PrintFile()
+ PrintFileForTest()
+}
--- /dev/null
+[short] skip
+
+go test -trimpath -v .
+! stdout '[/\\]pkg_test_test[/\\]'
+stdout -count=2 '[/\\]pkg_test[/\\]'
+
+-- go.mod --
+module example.com/pkg_test
+
+go 1.17
+
+-- pkg.go --
+package pkg_test
+
+import "runtime"
+
+func PrintFile() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
+
+-- pkg_x_test.go --
+package pkg_test_test
+
+import (
+ "runtime"
+ "testing"
+
+ "example.com/pkg_test"
+)
+
+func PrintFileForTest() {
+ _, file, _, _ := runtime.Caller(0)
+ println(file)
+}
+
+func TestMain(m *testing.M) {
+ pkg_test.PrintFile()
+ PrintFileForTest()
+}