mirror of
https://github.com/github/codeql.git
synced 2026-05-14 11:19:27 +02:00
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:
3
go/ql/integration-tests/root-internal-tests/src/go.mod
Normal file
3
go/ql/integration-tests/root-internal-tests/src/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/testpkg
|
||||
|
||||
go 1.26
|
||||
13
go/ql/integration-tests/root-internal-tests/src/main.go
Normal file
13
go/ql/integration-tests/root-internal-tests/src/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
func PublicFunc() int {
|
||||
return 42
|
||||
}
|
||||
|
||||
func privateFunc() int {
|
||||
return 24
|
||||
}
|
||||
|
||||
func main() {
|
||||
PublicFunc()
|
||||
}
|
||||
16
go/ql/integration-tests/root-internal-tests/src/main_test.go
Normal file
16
go/ql/integration-tests/root-internal-tests/src/main_test.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package nested
|
||||
|
||||
func NestedFunc() string {
|
||||
return "nested"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package nested
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNestedFunc(t *testing.T) {
|
||||
if NestedFunc() != "nested" {
|
||||
t.Error("NestedFunc failed")
|
||||
}
|
||||
}
|
||||
@@ -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 |
|
||||
5
go/ql/integration-tests/root-internal-tests/test.py
Normal file
5
go/ql/integration-tests/root-internal-tests/test.py
Normal 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"])
|
||||
15
go/ql/integration-tests/root-internal-tests/test.ql
Normal file
15
go/ql/integration-tests/root-internal-tests/test.ql
Normal 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()
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user