Skip to content

Commit b94ab8d

Browse files
AriehSchneierclaude
andcommitted
Add integration test for root internal test extraction
This test verifies that root internal test files (package foo, not foo_test) are correctly extracted when the repository has both: 1. Root-level internal tests (main_test.go with package main) 2. Nested packages with tests (nested/nested_test.go) This scenario reproduces the bug that was fixed: the old extractor would select the wrong package variant and miss root internal test files. The test ensures: - main_test.go (root internal test) is extracted - nested/nested_test.go (nested test) is extracted - All test functions from both files are present in the database This prevents regression of the bug fix. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 3ef4a58 commit b94ab8d

8 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/testpkg
2+
3+
go 1.26
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func PublicFunc() int {
4+
return 42
5+
}
6+
7+
func privateFunc() int {
8+
return 24
9+
}
10+
11+
func main() {
12+
PublicFunc()
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "testing"
4+
5+
// Root internal test - tests private functions
6+
func TestPrivateFunc(t *testing.T) {
7+
if privateFunc() != 24 {
8+
t.Error("privateFunc failed")
9+
}
10+
}
11+
12+
func TestPublicFunc(t *testing.T) {
13+
if PublicFunc() != 42 {
14+
t.Error("PublicFunc failed")
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package nested
2+
3+
func NestedFunc() string {
4+
return "nested"
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package nested
2+
3+
import "testing"
4+
5+
func TestNestedFunc(t *testing.T) {
6+
if NestedFunc() != "nested" {
7+
t.Error("NestedFunc failed")
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#select
2+
| src/main_test.go:0:0:0:0 | src/main_test.go |
3+
| src/nested/nested_test.go:0:0:0:0 | src/nested/nested_test.go |
4+
testFunctions
5+
| TestNestedFunc | src/nested/nested_test.go |
6+
| TestPrivateFunc | src/main_test.go |
7+
| TestPublicFunc | src/main_test.go |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
def test(codeql, go):
4+
# Test that root internal test files are extracted when nested packages have tests
5+
codeql.database.create(source_root="src", extractor_option = ["extract_tests=true"])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import go
2+
3+
// Verify that root internal test files are extracted
4+
// when nested packages also have tests
5+
from File f
6+
where f.getBaseName().matches("%_test.go")
7+
select f.getRelativePath()
8+
9+
query predicate testFunctions(string name, string file) {
10+
exists(FuncDecl fn |
11+
fn.getName().matches("Test%") and
12+
name = fn.getName() and
13+
file = fn.getFile().getRelativePath()
14+
)
15+
}

0 commit comments

Comments
 (0)