Debian Codegraph @

tools/antithesis-go-toolexec/policy_test.go

raw
1 package main
2
3 import (
4 "strings"
5 "testing"
6
7 qt "github.com/go-quicktest/qt"
8 )
9
10 // The argument lists below are real `go build -toolexec` compile invocations,
11 // captured from go1.26.6 with $WORK abbreviated.
12 func TestSourceFiles(t *testing.T) {
13 tests := []struct {
14 name string
15 args string
16 files []string
17 }{{
18 name: "single file",
19 args: "-o $W/b001/_pkg_.a -trimpath $W/b001=> -p main -lang=go1.24 -complete -buildid abc/abc -goversion go1.26.6 -c=24 -nolocalimports -importcfg $W/b001/importcfg -pack ./main.go",
20 files: []string{"./main.go"},
21 }, {
22 name: "several files",
23 args: "-o $W/b006/_pkg_.a -p internal/goarch -lang=go1.26 -std -complete -importcfg $W/b006/importcfg -pack /goroot/src/internal/goarch/goarch.go /goroot/src/internal/goarch/goarch_amd64.go /goroot/src/internal/goarch/zgoarch_amd64.go",
24 files: []string{"/goroot/src/internal/goarch/goarch.go", "/goroot/src/internal/goarch/goarch_amd64.go", "/goroot/src/internal/goarch/zgoarch_amd64.go"},
25 }, {
26 name: "embedcfg does not look like a source file",
27 args: "-o $W/b001/_pkg_.a -p main -lang=go1.24 -complete -importcfg $W/b001/importcfg -embedcfg $W/b001/embedcfg -pack /src/main.go",
28 files: []string{"/src/main.go"},
29 }, {
30 name: "no source files at all",
31 args: "-V=full",
32 files: nil,
33 }}
34
35 for _, test := range tests {
36 t.Run(test.name, func(t *testing.T) {
37 args := strings.Fields(test.args)
38 var got []string
39 for _, file := range sourceFiles(args) {
40 qt.Assert(t, qt.Equals(args[file.Index], file.Path))
41 got = append(got, file.Path)
42 }
43 qt.Assert(t, qt.DeepEquals(got, test.files))
44 })
45 }
46 }
47
48 func TestSkipFile(t *testing.T) {
49 excluded := map[string]bool{"/src/vendored": true, "/src/one.go": true}
50 cfg := &Config{Exclusions: excluded}
51
52 qt.Assert(t, qt.IsTrue(skipFile(cfg, "/src/one.go")))
53 qt.Assert(t, qt.IsTrue(skipFile(cfg, "/src/vendored/deep/two.go")))
54 qt.Assert(t, qt.IsTrue(skipFile(cfg, "/src/api.pb.go")))
55 qt.Assert(t, qt.IsFalse(skipFile(cfg, "/src/three.go")))
56 // A directory sharing a prefix with an exclusion is not itself excluded.
57 qt.Assert(t, qt.IsFalse(skipFile(cfg, "/src/vendored_other/four.go")))
58
59 qt.Assert(t, qt.IsFalse(skipFile(cfg, "/src/five_test.go")))
60 cfg.SkipTestFiles = true
61 qt.Assert(t, qt.IsTrue(skipFile(cfg, "/src/five_test.go")))
62 }
63
64 func TestInScope(t *testing.T) {
65 cfg := &Config{Instrument: []string{"example.com/app"}}
66
67 qt.Assert(t, qt.IsTrue(inScope(cfg, "example.com/app", "/src/a.go")))
68 qt.Assert(t, qt.IsTrue(inScope(cfg, "example.com/app/worker", "/src/a.go")))
69 // A prefix match must respect path boundaries.
70 qt.Assert(t, qt.IsFalse(inScope(cfg, "example.com/application", "/src/a.go")))
71 qt.Assert(t, qt.IsFalse(inScope(cfg, "example.com/other", "/src/a.go")))
72 }
73
74 func TestIsSDKPackage(t *testing.T) {
75 qt.Assert(t, qt.IsTrue(isSDKPackage("github.com/antithesishq/antithesis-sdk-go")))
76 qt.Assert(t, qt.IsTrue(isSDKPackage("github.com/antithesishq/antithesis-sdk-go/instrumentation")))
77 qt.Assert(t, qt.IsFalse(isSDKPackage("github.com/antithesishq/antithesis-sdk-goodies")))
78 qt.Assert(t, qt.IsFalse(isSDKPackage("example.com/app")))
79 }
80
81 func TestIsVersionProbe(t *testing.T) {
82 qt.Assert(t, qt.IsTrue(isVersionProbe([]string{"-V=full"})))
83 qt.Assert(t, qt.IsFalse(isVersionProbe(nil)))
84 // The C compiler is probed differently, and its output is hashed rather than
85 // parsed; we must leave that invocation alone.
86 qt.Assert(t, qt.IsFalse(isVersionProbe([]string{"-###", "-x", "c", "-c", "-"})))
87 qt.Assert(t, qt.IsFalse(isVersionProbe([]string{"-o", "x.a", "-pack", "a.go"})))
88 }
89
90 func TestWithoutToolexec(t *testing.T) {
91 qt.Assert(t, qt.Equals(withoutToolexec("-toolexec=/bin/wrap -mod=mod"), "-mod=mod"))
92 qt.Assert(t, qt.Equals(withoutToolexec("-mod=mod"), "-mod=mod"))
93 qt.Assert(t, qt.Equals(withoutToolexec(""), ""))
94 }
95
96 func TestEscapeImportPath(t *testing.T) {
97 for _, importPath := range []string{"app", "example.com/app/worker", "a/b/c"} {
98 qt.Assert(t, qt.Equals(unescapeImportPath(escapeImportPath(importPath)), importPath))
99 }
100 }