| 1 |
package instrumentor_test |
| 2 |
|
| 3 |
// Golden-file tests for the coverage instrumentor. |
| 4 |
// |
| 5 |
// Both coverage back-ends -- the AST-rewriting instrumentor and the |
| 6 |
// source-editing instrumentor -- are exercised against the same inputs. The |
| 7 |
// instrumented *source* they emit differs (the AST path reprints and adds |
| 8 |
// per-node //line directives; the source-editing path edits in place), so each |
| 9 |
// has its own source golden. Their *symbol tables*, by contrast, must be |
| 10 |
// identical/ |
| 11 |
// |
| 12 |
// Each subdirectory of testdata/golden/ is one case: |
| 13 |
// |
| 14 |
// testdata/golden/<case>/input.go -- a small .go file exercising one structure |
| 15 |
// testdata/golden/<case>/expected.source_editing.golden -- source-editing instrumented output |
| 16 |
// testdata/golden/<case>/expected.ast_rewriting.golden -- AST-rewriting instrumented output |
| 17 |
// testdata/golden/<case>/expected.sym.golden -- the symbol table both must emit (normalized) |
| 18 |
// |
| 19 |
// Regenerate goldens after changing inputs or (intentionally) changing |
| 20 |
// instrumentor output: |
| 21 |
// |
| 22 |
// go test ./scanners/coverage/instrumentor -run TestInstrumentGolden -update |
| 23 |
|
| 24 |
import ( |
| 25 |
"flag" |
| 26 |
"os" |
| 27 |
"path/filepath" |
| 28 |
"regexp" |
| 29 |
"strings" |
| 30 |
"testing" |
| 31 |
|
| 32 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 33 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/instrumentor" |
| 34 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/symboltable" |
| 35 |
|
| 36 |
qt "github.com/go-quicktest/qt" |
| 37 |
) |
| 38 |
|
| 39 |
var update = flag.Bool("update", false, "regenerate golden files") |
| 40 |
|
| 41 |
// testShimPkg is the notifier import path the instrumentor adds. Fixed so |
| 42 |
// the generated import line is deterministic across runs and machines. |
| 43 |
const testShimPkg = "antithesis.notifier/instrumentation" |
| 44 |
|
| 45 |
func TestMain(m *testing.M) { |
| 46 |
// Send logs to /dev/null to keep test output clean and avoid nil panics |
| 47 |
common.NewLogWriter(os.DevNull, common.Normal) |
| 48 |
os.Exit(m.Run()) |
| 49 |
} |
| 50 |
|
| 51 |
type instrumenter interface { |
| 52 |
Instrument(path string) (string, error) |
| 53 |
} |
| 54 |
|
| 55 |
var backends = []struct { |
| 56 |
name string |
| 57 |
golden string |
| 58 |
create func(basePath, shimPkg string, table *symboltable.SymbolTable) instrumenter |
| 59 |
}{ |
| 60 |
{ |
| 61 |
name: "source_editing", |
| 62 |
golden: "expected.source_editing.golden", |
| 63 |
create: func(b, s string, t *symboltable.SymbolTable) instrumenter { |
| 64 |
return instrumentor.CreateSourceEditingInstrumentor(b, s, t) |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "ast_rewriting", |
| 69 |
golden: "expected.ast_rewriting.golden", |
| 70 |
create: func(b, s string, t *symboltable.SymbolTable) instrumenter { |
| 71 |
return instrumentor.CreateInstrumentor(b, s, t) |
| 72 |
}, |
| 73 |
}, |
| 74 |
} |
| 75 |
|
| 76 |
func TestInstrumentGolden(t *testing.T) { |
| 77 |
inputs, err := filepath.Glob("testdata/golden/*/input.go") |
| 78 |
qt.Assert(t, qt.IsNil(err)) |
| 79 |
qt.Assert(t, qt.IsTrue(len(inputs) > 0)) |
| 80 |
|
| 81 |
for _, inputPath := range inputs { |
| 82 |
caseDir := filepath.Dir(inputPath) |
| 83 |
name := filepath.Base(caseDir) |
| 84 |
t.Run(name, func(t *testing.T) { |
| 85 |
symGolden := filepath.Join(caseDir, "expected.sym.golden") |
| 86 |
|
| 87 |
var symbols []string |
| 88 |
for _, be := range backends { |
| 89 |
res := runInstrument(t, inputPath, be.create) |
| 90 |
checkGolden(t, filepath.Join(caseDir, be.golden), res.source) |
| 91 |
checkGolden(t, symGolden, res.symbols) |
| 92 |
symbols = append(symbols, res.symbols) |
| 93 |
} |
| 94 |
qt.Check(t, qt.Equals(symbols[1], symbols[0]), |
| 95 |
qt.Commentf("%s and %s must emit identical symbol tables", backends[0].name, backends[1].name)) |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
type instrumentResult struct { |
| 101 |
source string // the instrumented source ("" if the file was skipped/copied) |
| 102 |
symbols string // the symbol table file, with non-deterministic bits normalized |
| 103 |
} |
| 104 |
|
| 105 |
func runInstrument(t *testing.T, inputPath string, create func(basePath, shimPkg string, table *symboltable.SymbolTable) instrumenter) instrumentResult { |
| 106 |
t.Helper() |
| 107 |
|
| 108 |
src, err := os.ReadFile(inputPath) |
| 109 |
qt.Assert(t, qt.IsNil(err)) |
| 110 |
|
| 111 |
dir := t.TempDir() |
| 112 |
tmpInput := filepath.Join(dir, "input.go") |
| 113 |
qt.Assert(t, qt.IsNil(os.WriteFile(tmpInput, src, 0644))) |
| 114 |
|
| 115 |
symPath := filepath.Join(dir, "test.sym.tsv") |
| 116 |
sym, err := symboltable.CreateSymbolTableFile(symPath, "testmodule") |
| 117 |
qt.Assert(t, qt.IsNil(err)) |
| 118 |
|
| 119 |
out, err := create(dir, testShimPkg, sym).Instrument(tmpInput) |
| 120 |
qt.Assert(t, qt.IsNil(err)) |
| 121 |
|
| 122 |
// Flush the symbol table before reading it back. |
| 123 |
qt.Assert(t, qt.IsNil(sym.Close())) |
| 124 |
symBytes, err := os.ReadFile(symPath) |
| 125 |
qt.Assert(t, qt.IsNil(err)) |
| 126 |
|
| 127 |
return instrumentResult{ |
| 128 |
source: out, |
| 129 |
symbols: normalizeSymbols(string(symBytes), tmpInput), |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
var instrumentorLineRe = regexp.MustCompile(`(?m)^# instrumentor = .*$`) |
| 134 |
|
| 135 |
func normalizeSymbols(s, tmpInput string) string { |
| 136 |
s = strings.ReplaceAll(s, tmpInput, "input.go") |
| 137 |
s = instrumentorLineRe.ReplaceAllString(s, "# instrumentor = INSTRUMENTOR") |
| 138 |
return s |
| 139 |
} |
| 140 |
|
| 141 |
// checkGolden compares got against the golden file, or (with -update) writes |
| 142 |
// got to it. |
| 143 |
func checkGolden(t *testing.T, goldenPath, got string) { |
| 144 |
t.Helper() |
| 145 |
|
| 146 |
if *update { |
| 147 |
qt.Assert(t, qt.IsNil(os.WriteFile(goldenPath, []byte(got), 0644))) |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
want, err := os.ReadFile(goldenPath) |
| 152 |
if os.IsNotExist(err) { |
| 153 |
t.Fatalf("golden %s does not exist; run: go test ./... -run TestInstrumentGolden -update", goldenPath) |
| 154 |
} |
| 155 |
qt.Assert(t, qt.IsNil(err)) |
| 156 |
qt.Check(t, qt.Equals(got, string(want))) |
| 157 |
} |