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>
This commit is contained in:
Arieh Schneier
2026-05-11 15:10:54 +10:00
parent 3ef4a5836c
commit b94ab8d186
8 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
module example.com/testpkg
go 1.26

View File

@@ -0,0 +1,13 @@
package main
func PublicFunc() int {
return 42
}
func privateFunc() int {
return 24
}
func main() {
PublicFunc()
}

View File

@@ -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")
}
}

View File

@@ -0,0 +1,5 @@
package nested
func NestedFunc() string {
return "nested"
}

View File

@@ -0,0 +1,9 @@
package nested
import "testing"
func TestNestedFunc(t *testing.T) {
if NestedFunc() != "nested" {
t.Error("NestedFunc failed")
}
}

View File

@@ -0,0 +1,7 @@
#select
| src/main_test.go:0:0:0:0 | src/main_test.go |
| src/nested/nested_test.go:0:0:0:0 | src/nested/nested_test.go |
testFunctions
| TestNestedFunc | src/nested/nested_test.go |
| TestPrivateFunc | src/main_test.go |
| TestPublicFunc | src/main_test.go |

View File

@@ -0,0 +1,5 @@
import os
def test(codeql, go):
# Test that root internal test files are extracted when nested packages have tests
codeql.database.create(source_root="src", extractor_option = ["extract_tests=true"])

View File

@@ -0,0 +1,15 @@
import go
// Verify that root internal test files are extracted
// when nested packages also have tests
from File f
where f.getBaseName().matches("%_test.go")
select f.getRelativePath()
query predicate testFunctions(string name, string file) {
exists(FuncDecl fn |
fn.getName().matches("Test%") and
name = fn.getName() and
file = fn.getFile().getRelativePath()
)
}