| 1 |
package assertions |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"os/exec" |
| 6 |
"path/filepath" |
| 7 |
"sort" |
| 8 |
"testing" |
| 9 |
|
| 10 |
qt "github.com/go-quicktest/qt" |
| 11 |
|
| 12 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 13 |
) |
| 14 |
|
| 15 |
func init() { |
| 16 |
// Ensure the global Logger is initialized for tests. |
| 17 |
common.NewLogWriter("", 0) |
| 18 |
} |
| 19 |
|
| 20 |
func absTestdata(fixture string) string { |
| 21 |
p, err := filepath.Abs(filepath.Join("testdata", fixture)) |
| 22 |
if err != nil { |
| 23 |
panic(err) |
| 24 |
} |
| 25 |
return p |
| 26 |
} |
| 27 |
|
| 28 |
func TestSingleMain(t *testing.T) { |
| 29 |
dir := absTestdata("single_main") |
| 30 |
scanner := NewAssertionScanner(dir, dir) |
| 31 |
err := scanner.ScanAll() |
| 32 |
qt.Assert(t, qt.IsNil(err)) |
| 33 |
|
| 34 |
bins := scanner.binaries |
| 35 |
qt.Assert(t, qt.HasLen(bins, 1)) |
| 36 |
|
| 37 |
bc := bins[0] |
| 38 |
// single_main has 3 assertion calls: Always, Sometimes, Reachable |
| 39 |
qt.Check(t, qt.HasLen(bc.expects, 3)) |
| 40 |
|
| 41 |
msgs := collectMessages(bc.expects) |
| 42 |
qt.Check(t, qt.SliceContains(msgs, "always true")) |
| 43 |
qt.Check(t, qt.SliceContains(msgs, "sometimes true")) |
| 44 |
qt.Check(t, qt.SliceContains(msgs, "reached main")) |
| 45 |
} |
| 46 |
|
| 47 |
func TestMultiMain(t *testing.T) { |
| 48 |
dir := absTestdata("multi_main") |
| 49 |
scanner := NewAssertionScanner(dir, dir) |
| 50 |
err := scanner.ScanAll() |
| 51 |
qt.Assert(t, qt.IsNil(err)) |
| 52 |
|
| 53 |
bins := scanner.binaries |
| 54 |
qt.Assert(t, qt.HasLen(bins, 2)) |
| 55 |
|
| 56 |
// Sort by RelDir for deterministic assertions |
| 57 |
sort.Slice(bins, func(i, j int) bool { |
| 58 |
return bins[i].relDir < bins[j].relDir |
| 59 |
}) |
| 60 |
|
| 61 |
// cmd/a imports shared + aonly: 1 (a main) + 1 (shared) + 1 (aonly) = 3 assertions |
| 62 |
binA := bins[0] |
| 63 |
qt.Check(t, qt.Equals(binA.relDir, filepath.Join("cmd", "a"))) |
| 64 |
msgsA := collectMessages(binA.expects) |
| 65 |
qt.Check(t, qt.SliceContains(msgsA, "a main assertion")) |
| 66 |
qt.Check(t, qt.SliceContains(msgsA, "shared assertion")) |
| 67 |
qt.Check(t, qt.SliceContains(msgsA, "aonly assertion")) |
| 68 |
qt.Check(t, qt.HasLen(binA.expects, 3)) |
| 69 |
|
| 70 |
// cmd/b imports shared only: 1 (b main) + 1 (shared) = 2 assertions |
| 71 |
binB := bins[1] |
| 72 |
qt.Check(t, qt.Equals(binB.relDir, filepath.Join("cmd", "b"))) |
| 73 |
msgsB := collectMessages(binB.expects) |
| 74 |
qt.Check(t, qt.SliceContains(msgsB, "b main assertion")) |
| 75 |
qt.Check(t, qt.SliceContains(msgsB, "shared assertion")) |
| 76 |
qt.Check(t, qt.HasLen(binB.expects, 2)) |
| 77 |
|
| 78 |
// Cross-contamination check |
| 79 |
qt.Check(t, qt.Not(qt.SliceContains(msgsB, "aonly assertion"))) |
| 80 |
qt.Check(t, qt.Not(qt.SliceContains(msgsB, "a main assertion"))) |
| 81 |
qt.Check(t, qt.Not(qt.SliceContains(msgsA, "b main assertion"))) |
| 82 |
} |
| 83 |
|
| 84 |
func TestAliasedImport(t *testing.T) { |
| 85 |
dir := absTestdata("aliased_import") |
| 86 |
scanner := NewAssertionScanner(dir, dir) |
| 87 |
err := scanner.ScanAll() |
| 88 |
qt.Assert(t, qt.IsNil(err)) |
| 89 |
|
| 90 |
bins := scanner.binaries |
| 91 |
qt.Assert(t, qt.HasLen(bins, 1)) |
| 92 |
|
| 93 |
bc := bins[0] |
| 94 |
qt.Check(t, qt.HasLen(bc.expects, 2)) |
| 95 |
|
| 96 |
msgs := collectMessages(bc.expects) |
| 97 |
qt.Check(t, qt.SliceContains(msgs, "aliased always")) |
| 98 |
qt.Check(t, qt.SliceContains(msgs, "aliased unreachable")) |
| 99 |
} |
| 100 |
|
| 101 |
func TestNoMain(t *testing.T) { |
| 102 |
dir := absTestdata("no_main") |
| 103 |
scanner := NewAssertionScanner(dir, dir) |
| 104 |
err := scanner.ScanAll() |
| 105 |
qt.Assert(t, qt.IsNil(err)) |
| 106 |
|
| 107 |
bins := scanner.binaries |
| 108 |
qt.Check(t, qt.HasLen(bins, 0)) |
| 109 |
} |
| 110 |
|
| 111 |
func TestNoAssertions(t *testing.T) { |
| 112 |
dir := absTestdata("no_assertions") |
| 113 |
scanner := NewAssertionScanner(dir, dir) |
| 114 |
err := scanner.ScanAll() |
| 115 |
qt.Assert(t, qt.IsNil(err)) |
| 116 |
|
| 117 |
bins := scanner.binaries |
| 118 |
qt.Assert(t, qt.HasLen(bins, 1)) |
| 119 |
|
| 120 |
bc := bins[0] |
| 121 |
qt.Check(t, qt.HasLen(bc.expects, 0)) |
| 122 |
qt.Check(t, qt.HasLen(bc.guidance, 0)) |
| 123 |
|
| 124 |
// HasAssertionsDefined should be false |
| 125 |
qt.Check(t, qt.IsFalse(scanner.HasAssertionsDefined())) |
| 126 |
} |
| 127 |
|
| 128 |
// TestCatalogStability verifies that scanning a package which already |
| 129 |
// contains a generated catalog produces the same results as the first |
| 130 |
// scan. The catalog's assert.AssertRaw calls must not be picked up as |
| 131 |
// additional assertions. |
| 132 |
func TestCatalogStability(t *testing.T) { |
| 133 |
// Locate the repo root so we can set up a replace directive. |
| 134 |
repoRoot, err := filepath.Abs(filepath.Join("..", "..", "..", "..")) |
| 135 |
qt.Assert(t, qt.IsNil(err)) |
| 136 |
|
| 137 |
// The SDK's internal package uses cgo. Disable it so that the test |
| 138 |
// does not require a C compiler. |
| 139 |
t.Setenv("CGO_ENABLED", "0") |
| 140 |
|
| 141 |
// Copy the single_main fixture into a temp directory with its own module. |
| 142 |
tmpDir := t.TempDir() |
| 143 |
src, err := os.ReadFile(absTestdata("single_main/main.go")) |
| 144 |
qt.Assert(t, qt.IsNil(err)) |
| 145 |
err = os.WriteFile(filepath.Join(tmpDir, "main.go"), src, 0644) |
| 146 |
qt.Assert(t, qt.IsNil(err)) |
| 147 |
|
| 148 |
sdkModule := "github.com/antithesishq/antithesis-sdk-go" |
| 149 |
for _, cmd := range [][]string{ |
| 150 |
{"go", "mod", "init", "example.com/stability-test"}, |
| 151 |
{"go", "mod", "edit", "-require", sdkModule + "@v0.0.0"}, |
| 152 |
{"go", "mod", "edit", "-replace", sdkModule + "=" + repoRoot}, |
| 153 |
{"go", "mod", "tidy"}, |
| 154 |
{"go", "mod", "vendor"}, |
| 155 |
} { |
| 156 |
c := exec.Command(cmd[0], cmd[1:]...) |
| 157 |
c.Dir = tmpDir |
| 158 |
out, err := c.CombinedOutput() |
| 159 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("%s failed:\n%s", cmd, out)) |
| 160 |
} |
| 161 |
|
| 162 |
// First scan. |
| 163 |
scanner1 := NewAssertionScanner(tmpDir, tmpDir) |
| 164 |
err = scanner1.ScanAll() |
| 165 |
qt.Assert(t, qt.IsNil(err)) |
| 166 |
qt.Assert(t, qt.HasLen(scanner1.binaries, 1)) |
| 167 |
firstMsgs := collectMessages(scanner1.binaries[0].expects) |
| 168 |
firstGuidanceCount := len(scanner1.binaries[0].guidance) |
| 169 |
|
| 170 |
// Write the catalog into the temp directory (same as source). |
| 171 |
scanner1.WriteAssertionCatalogs("stability test") |
| 172 |
|
| 173 |
// Verify the catalog file was created. |
| 174 |
catalogPath := filepath.Join(tmpDir, common.GENERATED_CATALOG_FILE) |
| 175 |
_, err = os.Stat(catalogPath) |
| 176 |
qt.Assert(t, qt.IsNil(err)) |
| 177 |
|
| 178 |
// Second scan — the catalog is now present alongside main.go. |
| 179 |
scanner2 := NewAssertionScanner(tmpDir, tmpDir) |
| 180 |
err = scanner2.ScanAll() |
| 181 |
qt.Assert(t, qt.IsNil(err)) |
| 182 |
qt.Assert(t, qt.HasLen(scanner2.binaries, 1)) |
| 183 |
secondMsgs := collectMessages(scanner2.binaries[0].expects) |
| 184 |
secondGuidanceCount := len(scanner2.binaries[0].guidance) |
| 185 |
|
| 186 |
// The assertion counts must be identical. |
| 187 |
sort.Strings(firstMsgs) |
| 188 |
sort.Strings(secondMsgs) |
| 189 |
qt.Check(t, qt.DeepEquals(firstMsgs, secondMsgs)) |
| 190 |
qt.Check(t, qt.Equals(firstGuidanceCount, secondGuidanceCount)) |
| 191 |
} |
| 192 |
|
| 193 |
func collectMessages(expects []*AntExpect) []string { |
| 194 |
msgs := make([]string, len(expects)) |
| 195 |
for i, e := range expects { |
| 196 |
msgs[i] = e.Message |
| 197 |
} |
| 198 |
return msgs |
| 199 |
} |