From 1b7a9421740590f50dbf2d78b0b597999a192b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20M=2E=20Requena=20Plens?= Date: Thu, 8 Oct 2026 12:12:56 +0200 Subject: [PATCH] Type-check black box tests as go test builds them Since gobco resolves types, it type-checks a black box test in 'package x_test' with the source importer, which loads the package 'x' from its non-test files only. A black box test that uses a name from export_test.go, the usual way to hand an unexported name to such a test, therefore made gobco panic with "undefined", although 'go test' accepts the package. In 'go test', the package under test includes its own test files, and each package that the black box test imports and that imports 'x' is compiled again against this 'x'. Do the same when resolving the types: the black box test gets the 'x' that gobco has already type-checked from all of its files, and each imported package that depends on 'x' is type-checked again, once, against that 'x'. Replacing only the import of 'x' is not enough, as a package that passes types of 'x' to the black box test would still see the other 'x'. A package that is type-checked again takes its imports from the package that the source importer built, since asking the source importer again would run the go command once more for each import outside the standard library. A package that has no test files of its own gets the same types as before, as its black box test is still checked with the source importer alone. --- instrumenter.go | 203 ++++++++++++++++++++++++- instrumenter_test.go | 2 +- main_test.go | 19 +++ testdata/exporttest/black_box_test.go | 19 +++ testdata/exporttest/export_test.go | 9 ++ testdata/exporttest/exporttest.go | 13 ++ testdata/exporttest/filter/filter.go | 16 ++ testdata/exporttest/numbers/numbers.go | 13 ++ 8 files changed, 288 insertions(+), 6 deletions(-) create mode 100644 testdata/exporttest/black_box_test.go create mode 100644 testdata/exporttest/export_test.go create mode 100644 testdata/exporttest/exporttest.go create mode 100644 testdata/exporttest/filter/filter.go create mode 100644 testdata/exporttest/numbers/numbers.go diff --git a/instrumenter.go b/instrumenter.go index 8fe71c9..f9584c2 100644 --- a/instrumenter.go +++ b/instrumenter.go @@ -92,7 +92,7 @@ func (i *instrumenter) instrument(srcDir, singleFile, dstDir string) bool { // such as '//go:build 386' or '//go:embed'. mode := parser.ParseComments pkgsMap, err := parser.ParseDir(i.fset, srcDir, isRelevant, mode) - i.resolveTypes(pkgsMap) + i.resolveTypes(srcDir, pkgsMap) ok(err) pkgs := sortedPkgs(pkgsMap) @@ -110,9 +110,8 @@ func (i *instrumenter) instrument(srcDir, singleFile, dstDir string) bool { return true } -func (i *instrumenter) resolveTypes(pkgsMap map[string]*ast.Package) { - imp := importer.ForCompiler(i.fset, "source", nil) - conf := types.Config{Importer: imp} +func (i *instrumenter) resolveTypes(srcDir string, pkgsMap map[string]*ast.Package) { + imp := importer.ForCompiler(i.fset, "source", nil).(types.ImporterFrom) info := types.Info{ Types: make(map[ast.Expr]types.TypeAndValue), } @@ -134,20 +133,214 @@ func (i *instrumenter) resolveTypes(pkgsMap map[string]*ast.Package) { return true } - for _, pkg := range pkgsMap { + // The package under test, if it has test files of its own, + // such as an export_test.go that its black box test may use. + var tested *types.Package + + for _, pkg := range sortedPkgs(pkgsMap) { var files []*ast.File for _, file := range pkg.Files { files = append(files, file) } + conf := types.Config{Importer: imp} + if tested != nil && pkg.Name == tested.Name()+"_test" { + conf.Importer = newTestImporter(imp, i.fset, srcDir, tested, files) + } typePkg, err := conf.Check(pkg.Name, i.fset, files, &info) ok(err) i.pkg[pkg] = typePkg + if !strings.HasSuffix(pkg.Name, "_test") && hasTestFile(pkg) { + tested = typePkg + } for _, f := range files { ast.Inspect(f, rememberType) } } } +// testImporter imports the packages for a black box test 'x_test' +// in the same way as 'go test' builds them. +// There, the package under test 'x' includes its own test files, +// and each package that imports 'x', directly or indirectly, +// is compiled again against this 'x'. +// The source importer only knows the 'x' without the test files, +// in which the names from an export_test.go file are undefined. +type testImporter struct { + base types.ImporterFrom + fset *token.FileSet + tested *types.Package + path string // the import path of the package under test + depends map[*types.Package]bool + variants map[string]*types.Package +} + +// newTestImporter creates an importer for the black box test, +// which consists of the given files from the directory dir. +func newTestImporter( + base types.ImporterFrom, + fset *token.FileSet, + dir string, + tested *types.Package, + files []*ast.File, +) *testImporter { + // The import path of the package under test is needed in advance, + // as the black box test may import a package that depends on it + // before importing the package under test itself. + return &testImporter{ + base: base, + fset: fset, + tested: tested, + path: testedImportPath(dir, files), + depends: map[*types.Package]bool{}, + variants: map[string]*types.Package{}, + } +} + +func (imp *testImporter) Import(path string) (*types.Package, error) { + return imp.ImportFrom(path, ".", 0) +} + +func (imp *testImporter) ImportFrom(path, dir string, mode types.ImportMode) (*types.Package, error) { + pkg, err := imp.base.ImportFrom(path, dir, mode) + if err != nil { + return pkg, err + } + return imp.forTest(pkg, dir) +} + +// forTest returns the package that the test uses in place of pkg, +// which the source importer has imported from the directory dir. +func (imp *testImporter) forTest(pkg *types.Package, dir string) (*types.Package, error) { + if !imp.dependsOnTested(pkg) { + return pkg, nil + } + if pkg.Path() == imp.path { + return imp.tested, nil + } + return imp.variant(pkg, dir) +} + +// dependsOnTested returns whether pkg is the package under test +// or imports it, directly or indirectly. +func (imp *testImporter) dependsOnTested(pkg *types.Package) bool { + if pkg.Path() == imp.path { + return true + } + if dep, ok := imp.depends[pkg]; ok { + return dep + } + dep := false + for _, imported := range pkg.Imports() { + if imp.dependsOnTested(imported) { + dep = true + break + } + } + imp.depends[pkg] = dep + return dep +} + +// variant type-checks again the package that the source importer +// has imported as original, this time against the package under test +// that includes its test files. +// Each package is type-checked only once, +// so that all packages that import it see the same types. +// Like the source importer, it only needs the declarations, +// not the function bodies. +func (imp *testImporter) variant(original *types.Package, dir string) (*types.Package, error) { + path := original.Path() + if pkg := imp.variants[path]; pkg != nil { + return pkg, nil + } + + bp, err := build.Default.Import(path, dir, 0) + if err != nil { + return nil, err + } + var files []*ast.File + for _, name := range append(bp.GoFiles, bp.CgoFiles...) { + file, err := parser.ParseFile(imp.fset, filepath.Join(bp.Dir, name), nil, 0) + if err != nil { + return nil, err + } + files = append(files, file) + } + + imports := map[string]*types.Package{} + for _, imported := range original.Imports() { + imports[imported.Path()] = imported + } + conf := types.Config{ + Importer: &variantImporter{imp, imports}, + IgnoreFuncBodies: true, + FakeImportC: true, + } + pkg, err := conf.Check(path, imp.fset, files, nil) + if err != nil { + return nil, err + } + imp.variants[path] = pkg + return pkg, nil +} + +// variantImporter imports the packages for a package +// that is type-checked again against the package under test. +// The source importer has already imported them for the original package, +// so they are taken from there instead of asking the source importer again, +// which would ask the go command where each of them is located. +type variantImporter struct { + *testImporter + imports map[string]*types.Package +} + +func (imp *variantImporter) Import(path string) (*types.Package, error) { + return imp.ImportFrom(path, ".", 0) +} + +func (imp *variantImporter) ImportFrom(path, dir string, mode types.ImportMode) (*types.Package, error) { + if pkg := imp.imports[path]; pkg != nil { + return imp.forTest(pkg, dir) + } + return imp.testImporter.ImportFrom(path, dir, mode) +} + +// testedImportPath returns the import path by which the black box test +// imports the package under test from the directory dir, +// or "" if the black box test doesn't import it. +func testedImportPath(dir string, files []*ast.File) string { + seen := map[string]bool{} + for _, file := range files { + for _, spec := range file.Imports { + path, err := strconv.Unquote(spec.Path.Value) + ok(err) + if seen[path] { + continue + } + seen[path] = true + bp, err := build.Default.Import(path, dir, build.FindOnly) + if err == nil && sameDir(bp.Dir, dir) { + return bp.ImportPath + } + } + } + return "" +} + +func hasTestFile(pkg *ast.Package) bool { + for name := range pkg.Files { + if strings.HasSuffix(name, "_test.go") { + return true + } + } + return false +} + +func sameDir(a, b string) bool { + sa, errA := os.Stat(a) + sb, errB := os.Stat(b) + return errA == nil && errB == nil && os.SameFile(sa, sb) +} + func (i *instrumenter) instrumentFile(filename string, astFile *ast.File, dstDir string) { isTest := strings.HasSuffix(filename, "_test.go") if (i.coverTest || !isTest) && shouldBuild(filename) { diff --git a/instrumenter_test.go b/instrumenter_test.go index 5a6cb7a..9afc8aa 100644 --- a/instrumenter_test.go +++ b/instrumenter_test.go @@ -101,7 +101,7 @@ func Test_instrumenter(t *testing.T) { fileName := filepath.Clean(base + ".go") f := pkgs["instrumenter"].Files[fileName] assert(f != nil, fileName) - i.resolveTypes(pkgs) + i.resolveTypes(dir, pkgs) i.typePkg = i.pkg[pkgs["instrumenter"]] i.instrumentFileNode(f) diff --git a/main_test.go b/main_test.go index e1a1caf..cd05bbf 100644 --- a/main_test.go +++ b/main_test.go @@ -520,6 +520,25 @@ func Test_gobcoMain__blackBox(t *testing.T) { s.CheckEquals(stderr, "") } +// A black box test can use the names that an export_test.go file declares, +// as 'go test' compiles that file into the package under test. +// The packages that the black box test imports and that themselves import +// the package under test are compiled against that same package. +func Test_gobcoMain__exportTest(t *testing.T) { + s := NewSuite(t) + defer s.TearDownTest() + + stdout, stderr := s.RunMain(0, "gobco", "-cover-test", "testdata/exporttest") + + s.CheckEquals(s.GobcoLines(stdout), []string{ + "Condition coverage: 3/4", + "testdata/exporttest/black_box_test.go:16:5: " + + "condition \"len(evens) != 2\" " + + "was once false but never true", + }) + s.CheckEquals(stderr, "") +} + func Test_gobcoMain__condition(t *testing.T) { s := NewSuite(t) defer s.TearDownTest() diff --git a/testdata/exporttest/black_box_test.go b/testdata/exporttest/black_box_test.go new file mode 100644 index 0000000..c7b8f7e --- /dev/null +++ b/testdata/exporttest/black_box_test.go @@ -0,0 +1,19 @@ +package exporttest_test + +// The black box test imports two packages that depend on the package +// under test before it imports the package under test itself. +import "github.com/rillig/gobco/testdata/exporttest/filter" +import "github.com/rillig/gobco/testdata/exporttest/numbers" +import "github.com/rillig/gobco/testdata/exporttest" +import "testing" + +// IsEven is only declared in export_test.go. +// The list from the package numbers is passed to the package filter, +// which imports the package numbers as well. + +func TestKeep(t *testing.T) { + evens := filter.Keep(numbers.Of(1, 2, 3, 4), exporttest.IsEven) + if len(evens) != 2 { + t.Fail() + } +} diff --git a/testdata/exporttest/export_test.go b/testdata/exporttest/export_test.go new file mode 100644 index 0000000..142b273 --- /dev/null +++ b/testdata/exporttest/export_test.go @@ -0,0 +1,9 @@ +package exporttest + +// In an export test, the package name is the same as in the main code, +// but the file is only compiled into the test. +// It makes some of the unexported members available to the black box test. + +func IsEven(n Number) bool { + return n.isEven() +} diff --git a/testdata/exporttest/exporttest.go b/testdata/exporttest/exporttest.go new file mode 100644 index 0000000..18df1b4 --- /dev/null +++ b/testdata/exporttest/exporttest.go @@ -0,0 +1,13 @@ +package exporttest + +type Number struct { + value int +} + +func New(value int) Number { + return Number{value} +} + +func (n Number) isEven() bool { + return n.value%2 == 0 +} diff --git a/testdata/exporttest/filter/filter.go b/testdata/exporttest/filter/filter.go new file mode 100644 index 0000000..474aa7d --- /dev/null +++ b/testdata/exporttest/filter/filter.go @@ -0,0 +1,16 @@ +package filter + +import ( + "github.com/rillig/gobco/testdata/exporttest" + "github.com/rillig/gobco/testdata/exporttest/numbers" +) + +func Keep(list numbers.List, keep func(exporttest.Number) bool) numbers.List { + var kept numbers.List + for _, n := range list { + if keep(n) { + kept = append(kept, n) + } + } + return kept +} diff --git a/testdata/exporttest/numbers/numbers.go b/testdata/exporttest/numbers/numbers.go new file mode 100644 index 0000000..4c7f31d --- /dev/null +++ b/testdata/exporttest/numbers/numbers.go @@ -0,0 +1,13 @@ +package numbers + +import "github.com/rillig/gobco/testdata/exporttest" + +type List []exporttest.Number + +func Of(values ...int) List { + var list List + for _, value := range values { + list = append(list, exporttest.New(value)) + } + return list +}