From b94ab8d186bbb5b6bbc19827fb517757fa95ae46 Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Mon, 11 May 2026 15:10:54 +1000 Subject: [PATCH] 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 --- .../root-internal-tests/src/go.mod | 3 +++ .../root-internal-tests/src/main.go | 13 +++++++++++++ .../root-internal-tests/src/main_test.go | 16 ++++++++++++++++ .../root-internal-tests/src/nested/nested.go | 5 +++++ .../src/nested/nested_test.go | 9 +++++++++ .../root-internal-tests/test.expected | 7 +++++++ .../root-internal-tests/test.py | 5 +++++ .../root-internal-tests/test.ql | 15 +++++++++++++++ 8 files changed, 73 insertions(+) create mode 100644 go/ql/integration-tests/root-internal-tests/src/go.mod create mode 100644 go/ql/integration-tests/root-internal-tests/src/main.go create mode 100644 go/ql/integration-tests/root-internal-tests/src/main_test.go create mode 100644 go/ql/integration-tests/root-internal-tests/src/nested/nested.go create mode 100644 go/ql/integration-tests/root-internal-tests/src/nested/nested_test.go create mode 100644 go/ql/integration-tests/root-internal-tests/test.expected create mode 100644 go/ql/integration-tests/root-internal-tests/test.py create mode 100644 go/ql/integration-tests/root-internal-tests/test.ql diff --git a/go/ql/integration-tests/root-internal-tests/src/go.mod b/go/ql/integration-tests/root-internal-tests/src/go.mod new file mode 100644 index 00000000000..12e11856e55 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/src/go.mod @@ -0,0 +1,3 @@ +module example.com/testpkg + +go 1.26 diff --git a/go/ql/integration-tests/root-internal-tests/src/main.go b/go/ql/integration-tests/root-internal-tests/src/main.go new file mode 100644 index 00000000000..fff083caa0a --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/src/main.go @@ -0,0 +1,13 @@ +package main + +func PublicFunc() int { + return 42 +} + +func privateFunc() int { + return 24 +} + +func main() { + PublicFunc() +} diff --git a/go/ql/integration-tests/root-internal-tests/src/main_test.go b/go/ql/integration-tests/root-internal-tests/src/main_test.go new file mode 100644 index 00000000000..7c38d61d4c8 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/src/main_test.go @@ -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") + } +} diff --git a/go/ql/integration-tests/root-internal-tests/src/nested/nested.go b/go/ql/integration-tests/root-internal-tests/src/nested/nested.go new file mode 100644 index 00000000000..427af1e44b6 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/src/nested/nested.go @@ -0,0 +1,5 @@ +package nested + +func NestedFunc() string { + return "nested" +} diff --git a/go/ql/integration-tests/root-internal-tests/src/nested/nested_test.go b/go/ql/integration-tests/root-internal-tests/src/nested/nested_test.go new file mode 100644 index 00000000000..a7e063c6185 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/src/nested/nested_test.go @@ -0,0 +1,9 @@ +package nested + +import "testing" + +func TestNestedFunc(t *testing.T) { + if NestedFunc() != "nested" { + t.Error("NestedFunc failed") + } +} diff --git a/go/ql/integration-tests/root-internal-tests/test.expected b/go/ql/integration-tests/root-internal-tests/test.expected new file mode 100644 index 00000000000..9f3d7b762c7 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/test.expected @@ -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 | diff --git a/go/ql/integration-tests/root-internal-tests/test.py b/go/ql/integration-tests/root-internal-tests/test.py new file mode 100644 index 00000000000..a8f376e3397 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/test.py @@ -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"]) diff --git a/go/ql/integration-tests/root-internal-tests/test.ql b/go/ql/integration-tests/root-internal-tests/test.ql new file mode 100644 index 00000000000..234fd1a0420 --- /dev/null +++ b/go/ql/integration-tests/root-internal-tests/test.ql @@ -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() + ) +}