| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"os/exec" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
var ( |
| 12 |
sharedToolexec string |
| 13 |
sharedGoCache string |
| 14 |
sharedRecorder string |
| 15 |
) |
| 16 |
|
| 17 |
func TestMain(m *testing.M) { |
| 18 |
os.Exit(func() int { |
| 19 |
work, err := os.MkdirTemp("", "antithesis-toolexec-test-") |
| 20 |
if err != nil { |
| 21 |
panic(err) |
| 22 |
} |
| 23 |
defer os.RemoveAll(work) |
| 24 |
|
| 25 |
sharedGoCache = filepath.Join(work, "gocache") |
| 26 |
sharedToolexec = filepath.Join(work, "antithesis-go-toolexec") |
| 27 |
build := exec.Command("go", "build", "-o", sharedToolexec, ".") |
| 28 |
build.Env = append(os.Environ(), "GOFLAGS=") |
| 29 |
if out, err := build.CombinedOutput(); err != nil { |
| 30 |
panic("building the wrapper failed: " + err.Error() + "\n" + string(out)) |
| 31 |
} |
| 32 |
|
| 33 |
sharedRecorder = filepath.Join(work, "recorder") |
| 34 |
if err := writeRecorderModuleTo(sharedRecorder); err != nil { |
| 35 |
panic(err) |
| 36 |
} |
| 37 |
return m.Run() |
| 38 |
}()) |
| 39 |
} |
| 40 |
|
| 41 |
// buildEnv is the environment every instrumented build in this package uses. |
| 42 |
// GOFLAGS is cleared of any inherited -toolexec, which would recurse. |
| 43 |
func buildEnv(symbolsDir, instrument string, extra ...string) []string { |
| 44 |
return append(append(os.Environ(), |
| 45 |
"GOFLAGS=-mod=mod", |
| 46 |
"GOCACHE="+sharedGoCache, |
| 47 |
"ANTITHESIS_INSTRUMENT="+instrument, |
| 48 |
"ANTITHESIS_SYMBOLS_DIR="+symbolsDir, |
| 49 |
"ANTITHESIS_SDK_MODULE_DIR="+sharedRecorder, |
| 50 |
), extra...) |
| 51 |
} |
| 52 |
|
| 53 |
// tablesForModule returns the symbol tables in symbolsDir that describe files |
| 54 |
// under moduleDir, ignoring any left by another case sharing the build cache. |
| 55 |
func tablesForModule(t *testing.T, symbolsDir, moduleDir string) []string { |
| 56 |
t.Helper() |
| 57 |
paths, err := filepath.Glob(filepath.Join(symbolsDir, "*.sym.tsv")) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
var mine []string |
| 62 |
for _, path := range paths { |
| 63 |
body, err := os.ReadFile(path) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
if strings.Contains(string(body), moduleDir+string(filepath.Separator)) { |
| 68 |
mine = append(mine, path) |
| 69 |
} |
| 70 |
} |
| 71 |
return mine |
| 72 |
} |