-
Notifications
You must be signed in to change notification settings - Fork 2k
Go: Fix extractor to extract root internal test files #21826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
owen-mc
merged 6 commits into
github:main
from
AriehSchneier:fix/go-extractor-root-test-files
May 12, 2026
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ef4a58
Fix Go extractor to extract root internal test files
AriehSchneier b94ab8d
Add integration test for root internal test extraction
AriehSchneier 151a332
Add Bazel build target for extractor_test.go
AriehSchneier aa1d322
Address PR feedback
AriehSchneier 0aaa7d0
Update expected test output
owen-mc 6b65866
Merge branch 'main' into fix/go-extractor-root-test-files
owen-mc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,44 @@ func init() { | |
| } | ||
| } | ||
|
|
||
| // isExactTestPackage checks if a package ID represents an exact test match. | ||
| // Returns true for IDs like "github.com/foo/bar [github.com/foo/bar.test]" | ||
| // Returns false for IDs like "github.com/foo/bar [github.com/foo/bar/nested.test]" | ||
| func isExactTestPackage(pkg *packages.Package) bool { | ||
| // Test packages have IDs in the format: "pkgpath [pkgpath.test]" | ||
| // or for nested test dependencies: "pkgpath [pkgpath/nested.test]" | ||
| if !strings.Contains(pkg.ID, " [") { | ||
| return false | ||
| } | ||
| expectedTestID := pkg.PkgPath + " [" + pkg.PkgPath + ".test]" | ||
| return pkg.ID == expectedTestID | ||
| } | ||
|
|
||
| // isBetterPackage determines if pkg is a better choice than current for extraction. | ||
| // Preferences: | ||
| // 1. Exact test package (e.g., "pkg [pkg.test]") over nested test dependencies | ||
| // 2. More Syntax nodes (more files to extract) | ||
| // 3. Longer ID string as tiebreaker | ||
| func isBetterPackage(pkg, current *packages.Package) bool { | ||
| pkgIsExact := isExactTestPackage(pkg) | ||
| currentIsExact := isExactTestPackage(current) | ||
|
|
||
| // Prefer exact test packages | ||
| if pkgIsExact != currentIsExact { | ||
| return pkgIsExact | ||
| } | ||
|
|
||
| // Prefer packages with more syntax nodes (more files) | ||
| pkgSyntaxCount := len(pkg.Syntax) | ||
| currentSyntaxCount := len(current.Syntax) | ||
| if pkgSyntaxCount != currentSyntaxCount { | ||
| return pkgSyntaxCount > currentSyntaxCount | ||
| } | ||
|
|
||
| // Fall back to string length | ||
| return len(pkg.ID) > len(current.ID) | ||
| } | ||
|
|
||
| // ExtractWithFlags extracts the packages specified by the given patterns and build flags | ||
| func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool, sourceRoot string) error { | ||
| startTime := time.Now() | ||
|
|
@@ -153,20 +191,22 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool, | |
|
|
||
| pkgsNotFound := make([]string, 0, len(pkgs)) | ||
|
|
||
| // Build a map from package paths to their longest IDs-- | ||
| // Build a map from package paths to their best IDs-- | ||
| // in the context of a `go test -c` compilation, we will see the same package more than | ||
| // once, with IDs like "abc.com/pkgname [abc.com/pkgname.test]" to distinguish the version | ||
| // that contains and is used by test code. | ||
| // For our purposes it is simplest to just ignore the non-test version, since the test | ||
| // version seems to be a superset of it. | ||
| longestPackageIds := make(map[string]string) | ||
| // We prefer the version with the most complete test coverage, which is typically: | ||
| // 1. The exact test package (e.g., "pkg [pkg.test]") over nested test dependencies | ||
| // 2. The package with the most Syntax nodes (most files to extract) | ||
| // 3. The longest ID string as a tiebreaker | ||
| bestPackageIds := make(map[string]*packages.Package) | ||
| packages.Visit(pkgs, nil, func(pkg *packages.Package) { | ||
| if longestIDSoFar, present := longestPackageIds[pkg.PkgPath]; present { | ||
| if len(pkg.ID) > len(longestIDSoFar) { | ||
| longestPackageIds[pkg.PkgPath] = pkg.ID | ||
| if bestSoFar, present := bestPackageIds[pkg.PkgPath]; present { | ||
| if isBetterPackage(pkg, bestSoFar) { | ||
| bestPackageIds[pkg.PkgPath] = pkg | ||
| } | ||
| } else { | ||
| longestPackageIds[pkg.PkgPath] = pkg.ID | ||
| bestPackageIds[pkg.PkgPath] = pkg | ||
| } | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me that this would be better as a separate function. Two pieces of evidence:
Note that it does not have to be an exported function, as I believe the test can still access it. |
||
|
|
||
|
|
@@ -257,15 +297,15 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool, | |
| // extract AST information for all packages | ||
| packages.Visit(pkgs, nil, func(pkg *packages.Package) { | ||
|
|
||
| // If this is a variant of a package that also occurs with a longer ID, skip it; | ||
| // If this is a variant of a package that also occurs with a better ID, skip it; | ||
| // otherwise we would extract the same file more than once including extracting the | ||
| // body of methods twice, causing database inconsistencies. | ||
| // | ||
| // We prefer the version with the longest ID because that is (so far as I know) always | ||
| // the version that defines more entities -- the only case I'm aware of being a test | ||
| // variant of a package, which includes test-only functions in addition to the complete | ||
| // contents of the main variant. | ||
| if pkg.ID != longestPackageIds[pkg.PkgPath] { | ||
| // We prefer the version with the most complete test coverage, prioritizing: | ||
| // 1. Exact test packages (e.g., "pkg [pkg.test]") over nested test dependencies | ||
| // 2. Packages with more Syntax nodes (more files to extract) | ||
| // 3. Longer ID strings as a tiebreaker | ||
| if pkg.ID != bestPackageIds[pkg.PkgPath].ID { | ||
| return | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| package extractor | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "testing" | ||
|
|
||
| "golang.org/x/tools/go/packages" | ||
| ) | ||
|
|
||
| func TestIsExactTestPackage(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| pkgID string | ||
| pkgPath string | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "exact test package", | ||
| pkgID: "github.com/foo/bar [github.com/foo/bar.test]", | ||
| pkgPath: "github.com/foo/bar", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "nested test package", | ||
| pkgID: "github.com/foo/bar [github.com/foo/bar/nested.test]", | ||
| pkgPath: "github.com/foo/bar", | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "deeply nested test package", | ||
| pkgID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", | ||
| pkgPath: "github.com/go-git/go-git/v6", | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "exact test package with version", | ||
| pkgID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", | ||
| pkgPath: "github.com/go-git/go-git/v6", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "non-test package", | ||
| pkgID: "github.com/foo/bar", | ||
| pkgPath: "github.com/foo/bar", | ||
| expected: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| pkg := &packages.Package{ | ||
| ID: tt.pkgID, | ||
| PkgPath: tt.pkgPath, | ||
| } | ||
| result := isExactTestPackage(pkg) | ||
| if result != tt.expected { | ||
| t.Errorf("isExactTestPackage(%q) = %v, want %v", tt.pkgID, result, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsBetterPackage(t *testing.T) { | ||
| // Helper to create a package with specified properties | ||
| makePkg := func(id, path string, syntaxCount int) *packages.Package { | ||
| syntax := make([]*ast.File, syntaxCount) | ||
| return &packages.Package{ | ||
| ID: id, | ||
| PkgPath: path, | ||
| Syntax: syntax, | ||
| } | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| pkg *packages.Package | ||
| current *packages.Package | ||
| expected bool // true if pkg is better than current | ||
| }{ | ||
| { | ||
| name: "exact test package beats nested test package", | ||
| pkg: makePkg( | ||
| "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", | ||
| "github.com/go-git/go-git/v6", | ||
| 39, // 19 production + 20 test files | ||
| ), | ||
| current: makePkg( | ||
| "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", | ||
| "github.com/go-git/go-git/v6", | ||
| 19, // production files only | ||
| ), | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "nested test package loses to exact test package", | ||
| pkg: makePkg( | ||
| "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", | ||
| "github.com/go-git/go-git/v6", | ||
| 19, | ||
| ), | ||
| current: makePkg( | ||
| "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", | ||
| "github.com/go-git/go-git/v6", | ||
| 39, | ||
| ), | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "more syntax nodes wins when both are exact tests", | ||
| pkg: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar.test]", | ||
| "github.com/foo/bar", | ||
| 50, | ||
| ), | ||
| current: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar.test]", | ||
| "github.com/foo/bar", | ||
| 30, | ||
| ), | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "fewer syntax nodes loses when both are exact tests", | ||
| pkg: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar.test]", | ||
| "github.com/foo/bar", | ||
| 30, | ||
| ), | ||
| current: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar.test]", | ||
| "github.com/foo/bar", | ||
| 50, | ||
| ), | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "more syntax nodes wins when both are nested tests", | ||
| pkg: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar/pkg1.test]", | ||
| "github.com/foo/bar", | ||
| 25, | ||
| ), | ||
| current: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar/pkg2.test]", | ||
| "github.com/foo/bar", | ||
| 20, | ||
| ), | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "longer ID wins when same syntax count", | ||
| pkg: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar/verylongpackagename.test]", | ||
| "github.com/foo/bar", | ||
| 20, | ||
| ), | ||
| current: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar/short.test]", | ||
| "github.com/foo/bar", | ||
| 20, | ||
| ), | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "test package beats non-test with same syntax count", | ||
| pkg: makePkg( | ||
| "github.com/foo/bar [github.com/foo/bar.test]", | ||
| "github.com/foo/bar", | ||
| 20, | ||
| ), | ||
| current: makePkg( | ||
| "github.com/foo/bar", | ||
| "github.com/foo/bar", | ||
| 20, | ||
| ), | ||
| expected: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := isBetterPackage(tt.pkg, tt.current) | ||
| if result != tt.expected { | ||
| t.Errorf("isBetterPackage() = %v, want %v\n pkg: %q (%d syntax nodes)\n current: %q (%d syntax nodes)", | ||
| result, tt.expected, | ||
| tt.pkg.ID, len(tt.pkg.Syntax), | ||
| tt.current.ID, len(tt.current.Syntax)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestPackageSelectionRealWorld simulates the real-world go-git scenario | ||
| func TestPackageSelectionRealWorld(t *testing.T) { | ||
| // Simulate the actual packages.Load result for go-git repository | ||
| // when EXTRACT_TESTS=true | ||
| pkgs := []*packages.Package{ | ||
| // Production package only | ||
| { | ||
| ID: "github.com/go-git/go-git/v6", | ||
| PkgPath: "github.com/go-git/go-git/v6", | ||
| Syntax: make([]*ast.File, 19), // 19 production files | ||
| }, | ||
| // Root test package - this is what we want! | ||
| { | ||
| ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", | ||
| PkgPath: "github.com/go-git/go-git/v6", | ||
| Syntax: make([]*ast.File, 39), // 19 production + 20 test files | ||
| }, | ||
| // Nested test dependency 1 | ||
| { | ||
| ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", | ||
| PkgPath: "github.com/go-git/go-git/v6", | ||
| Syntax: make([]*ast.File, 19), // production files only (dependency) | ||
| }, | ||
| // Nested test dependency 2 | ||
| { | ||
| ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/object.test]", | ||
| PkgPath: "github.com/go-git/go-git/v6", | ||
| Syntax: make([]*ast.File, 19), // production files only (dependency) | ||
| }, | ||
| } | ||
|
|
||
| // Simulate the bestPackageIds selection logic | ||
| bestPackageIds := make(map[string]*packages.Package) | ||
| for _, pkg := range pkgs { | ||
| if bestSoFar, present := bestPackageIds[pkg.PkgPath]; present { | ||
| if isBetterPackage(pkg, bestSoFar) { | ||
| bestPackageIds[pkg.PkgPath] = pkg | ||
| } | ||
| } else { | ||
| bestPackageIds[pkg.PkgPath] = pkg | ||
| } | ||
| } | ||
|
|
||
| // Verify the correct package was selected | ||
| selected := bestPackageIds["github.com/go-git/go-git/v6"] | ||
| expectedID := "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]" | ||
| expectedSyntaxCount := 39 | ||
|
|
||
| if selected.ID != expectedID { | ||
| t.Errorf("Wrong package selected!\n got: %q (%d syntax nodes)\n want: %q (%d syntax nodes)", | ||
| selected.ID, len(selected.Syntax), | ||
| expectedID, expectedSyntaxCount) | ||
| } | ||
|
|
||
| if len(selected.Syntax) != expectedSyntaxCount { | ||
| t.Errorf("Wrong syntax count: got %d, want %d", len(selected.Syntax), expectedSyntaxCount) | ||
| } | ||
|
|
||
| // Verify it's recognized as an exact test package | ||
| if !isExactTestPackage(selected) { | ||
| t.Errorf("Selected package %q should be recognized as exact test package", selected.ID) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module example.com/testpkg | ||
|
|
||
| go 1.26 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package main | ||
|
|
||
| func PublicFunc() int { | ||
| return 42 | ||
| } | ||
|
|
||
| func privateFunc() int { | ||
| return 24 | ||
| } | ||
|
|
||
| func main() { | ||
| PublicFunc() | ||
| } |
16 changes: 16 additions & 0 deletions
16
go/ql/integration-tests/root-internal-tests/src/main_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package main | ||
|
|
||
| import "testing" | ||
|
|
||
| // Root internal test - tests private functions | ||
| func TestPrivateFunc(t *testing.T) { | ||
| if privateFunc() != 24 { | ||
| t.Error("privateFunc failed") | ||
| } | ||
| } | ||
|
|
||
| func TestPublicFunc(t *testing.T) { | ||
| if PublicFunc() != 42 { | ||
| t.Error("PublicFunc failed") | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
go/ql/integration-tests/root-internal-tests/src/nested/nested.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package nested | ||
|
|
||
| func NestedFunc() string { | ||
| return "nested" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these lines needed? The check below will give the same result. The only possible benefit I can see is efficiency, not constructing the string, but it doesn't seem like that calling
strings.Containsinstead is definitely more performant.