From 9a82ea48f2708439ce6c2b56b79f907a4171cb9f Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Tue, 1 Oct 2024 15:41:17 +0100 Subject: [PATCH 1/3] Add test for cross-package references with test extraction --- go/ql/integration-tests/test-extraction/src/go.mod | 2 +- .../test-extraction/src/pkg1/def.go | 7 +++++++ .../test-extraction/src/pkg1/def_test.go | 5 +++++ .../test-extraction/src/pkg2/use.go | 12 ++++++++++++ .../integration-tests/test-extraction/test.expected | 4 ++++ 5 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 go/ql/integration-tests/test-extraction/src/pkg1/def.go create mode 100644 go/ql/integration-tests/test-extraction/src/pkg1/def_test.go create mode 100644 go/ql/integration-tests/test-extraction/src/pkg2/use.go diff --git a/go/ql/integration-tests/test-extraction/src/go.mod b/go/ql/integration-tests/test-extraction/src/go.mod index c4a9f55df6c..dae010718cf 100644 --- a/go/ql/integration-tests/test-extraction/src/go.mod +++ b/go/ql/integration-tests/test-extraction/src/go.mod @@ -1,3 +1,3 @@ -go 1.14 +go 1.18 module testsample diff --git a/go/ql/integration-tests/test-extraction/src/pkg1/def.go b/go/ql/integration-tests/test-extraction/src/pkg1/def.go new file mode 100644 index 00000000000..2492919f76d --- /dev/null +++ b/go/ql/integration-tests/test-extraction/src/pkg1/def.go @@ -0,0 +1,7 @@ +package pkg1 + +type Generic[T any] struct { + element T +} + +func TestMe() { } diff --git a/go/ql/integration-tests/test-extraction/src/pkg1/def_test.go b/go/ql/integration-tests/test-extraction/src/pkg1/def_test.go new file mode 100644 index 00000000000..f321ab577c9 --- /dev/null +++ b/go/ql/integration-tests/test-extraction/src/pkg1/def_test.go @@ -0,0 +1,5 @@ +package pkg1 + +func UsePkg1() { + TestMe() +} diff --git a/go/ql/integration-tests/test-extraction/src/pkg2/use.go b/go/ql/integration-tests/test-extraction/src/pkg2/use.go new file mode 100644 index 00000000000..8caabc3847e --- /dev/null +++ b/go/ql/integration-tests/test-extraction/src/pkg2/use.go @@ -0,0 +1,12 @@ +package pkg2 + +import ( + "testsample/pkg1" +) + +// This tests the case of cross-package generic type references +// in the presence of test extraction. We need to make sure we +// extract packages, including test variants, in the right order +// such that we've seen pkg1.Generic before we try to use it here. + +type Specialised = pkg1.Generic[string] diff --git a/go/ql/integration-tests/test-extraction/test.expected b/go/ql/integration-tests/test-extraction/test.expected index 9e1585fc5ef..c4135434743 100644 --- a/go/ql/integration-tests/test-extraction/test.expected +++ b/go/ql/integration-tests/test-extraction/test.expected @@ -1,8 +1,12 @@ #select +| src/pkg1/def.go:0:0:0:0 | src/pkg1/def.go | +| src/pkg1/def_test.go:0:0:0:0 | src/pkg1/def_test.go | +| src/pkg2/use.go:0:0:0:0 | src/pkg2/use.go | | src/testme.go:0:0:0:0 | src/testme.go | | src/testme_blackbox_test.go:0:0:0:0 | src/testme_blackbox_test.go | | src/testme_test.go:0:0:0:0 | src/testme_test.go | calls +| src/pkg1/def_test.go:4:2:4:9 | call to TestMe | src/pkg1/def.go:7:1:7:17 | function declaration | | src/testme_blackbox_test.go:10:18:10:44 | call to PublicFunction | src/testme.go:3:1:3:38 | function declaration | | src/testme_test.go:9:18:9:33 | call to PublicFunction | src/testme.go:3:1:3:38 | function declaration | | src/testme_test.go:14:19:14:35 | call to privateFunction | src/testme.go:5:1:5:39 | function declaration | From 6ef2aed3aa0d1f57602261f4e8a2de680a43d3aa Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Tue, 1 Oct 2024 15:52:30 +0100 Subject: [PATCH 2/3] Add blackbox, out-of-package test --- .../test-extraction/src/pkg1/def_blackbox_test.go | 9 +++++++++ go/ql/integration-tests/test-extraction/test.expected | 2 ++ go/ql/integration-tests/test-extraction/test.py | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 go/ql/integration-tests/test-extraction/src/pkg1/def_blackbox_test.go diff --git a/go/ql/integration-tests/test-extraction/src/pkg1/def_blackbox_test.go b/go/ql/integration-tests/test-extraction/src/pkg1/def_blackbox_test.go new file mode 100644 index 00000000000..09c9474567a --- /dev/null +++ b/go/ql/integration-tests/test-extraction/src/pkg1/def_blackbox_test.go @@ -0,0 +1,9 @@ +package pkg1_test + +import ( + "testsample/pkg1" +) + +func UsePkg1() { + pkg1.TestMe() +} diff --git a/go/ql/integration-tests/test-extraction/test.expected b/go/ql/integration-tests/test-extraction/test.expected index c4135434743..eda27075ad4 100644 --- a/go/ql/integration-tests/test-extraction/test.expected +++ b/go/ql/integration-tests/test-extraction/test.expected @@ -1,11 +1,13 @@ #select | src/pkg1/def.go:0:0:0:0 | src/pkg1/def.go | +| src/pkg1/def_blackbox_test.go:0:0:0:0 | src/pkg1/def_blackbox_test.go | | src/pkg1/def_test.go:0:0:0:0 | src/pkg1/def_test.go | | src/pkg2/use.go:0:0:0:0 | src/pkg2/use.go | | src/testme.go:0:0:0:0 | src/testme.go | | src/testme_blackbox_test.go:0:0:0:0 | src/testme_blackbox_test.go | | src/testme_test.go:0:0:0:0 | src/testme_test.go | calls +| src/pkg1/def_blackbox_test.go:8:2:8:14 | call to TestMe | src/pkg1/def.go:7:1:7:17 | function declaration | | src/pkg1/def_test.go:4:2:4:9 | call to TestMe | src/pkg1/def.go:7:1:7:17 | function declaration | | src/testme_blackbox_test.go:10:18:10:44 | call to PublicFunction | src/testme.go:3:1:3:38 | function declaration | | src/testme_test.go:9:18:9:33 | call to PublicFunction | src/testme.go:3:1:3:38 | function declaration | diff --git a/go/ql/integration-tests/test-extraction/test.py b/go/ql/integration-tests/test-extraction/test.py index 6419ae83f38..e25b4680114 100644 --- a/go/ql/integration-tests/test-extraction/test.py +++ b/go/ql/integration-tests/test-extraction/test.py @@ -1,7 +1,7 @@ import os def test_traced(codeql, go): - codeql.database.create(source_root="src", command="go test -c") + codeql.database.create(source_root="src", command="go test -c ./...") def test_autobuild(codeql, go): codeql.database.create(source_root="src", extractor_option = ["extract_tests=true"]) From 05d2e16de37fc6a004e9f86acd02bd87d37d3ac7 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Wed, 2 Oct 2024 15:25:36 +0100 Subject: [PATCH 3/3] autoformat --- go/ql/integration-tests/test-extraction/src/pkg1/def.go | 2 +- go/ql/integration-tests/test-extraction/test.expected | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go/ql/integration-tests/test-extraction/src/pkg1/def.go b/go/ql/integration-tests/test-extraction/src/pkg1/def.go index 2492919f76d..e768e81b89d 100644 --- a/go/ql/integration-tests/test-extraction/src/pkg1/def.go +++ b/go/ql/integration-tests/test-extraction/src/pkg1/def.go @@ -4,4 +4,4 @@ type Generic[T any] struct { element T } -func TestMe() { } +func TestMe() {} diff --git a/go/ql/integration-tests/test-extraction/test.expected b/go/ql/integration-tests/test-extraction/test.expected index eda27075ad4..77983c44807 100644 --- a/go/ql/integration-tests/test-extraction/test.expected +++ b/go/ql/integration-tests/test-extraction/test.expected @@ -7,8 +7,8 @@ | src/testme_blackbox_test.go:0:0:0:0 | src/testme_blackbox_test.go | | src/testme_test.go:0:0:0:0 | src/testme_test.go | calls -| src/pkg1/def_blackbox_test.go:8:2:8:14 | call to TestMe | src/pkg1/def.go:7:1:7:17 | function declaration | -| src/pkg1/def_test.go:4:2:4:9 | call to TestMe | src/pkg1/def.go:7:1:7:17 | function declaration | +| src/pkg1/def_blackbox_test.go:8:2:8:14 | call to TestMe | src/pkg1/def.go:7:1:7:16 | function declaration | +| src/pkg1/def_test.go:4:2:4:9 | call to TestMe | src/pkg1/def.go:7:1:7:16 | function declaration | | src/testme_blackbox_test.go:10:18:10:44 | call to PublicFunction | src/testme.go:3:1:3:38 | function declaration | | src/testme_test.go:9:18:9:33 | call to PublicFunction | src/testme.go:3:1:3:38 | function declaration | | src/testme_test.go:14:19:14:35 | call to privateFunction | src/testme.go:5:1:5:39 | function declaration |