Skip to content

Commit ddd7d22

Browse files
committed
Use testing.T.ArtifactDir with Spin
This is much cleaner than the manual cleanup I did before, and provides the option to retain the generated files even on successful test runs. (On the other hand, test failures clean up the files rather than leaving them around for you to inspect, but Spin is consistent enough that a re-run should be fine.)
1 parent 8f7d1b2 commit ddd7d22

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

internal/watch/model_test.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ var modelFile string
2626
// processes that violates the model's assertions.
2727
//
2828
// This harness automates this process within the standard Go testing framework,
29-
// building and executing the model checker in a temporary directory and
30-
// displaying the details of any generated trail.
29+
// building the model checker into a test artifact directory and displaying the
30+
// details of any generated trail.
3131
//
3232
// Use the "modeltest" build tag to include this harness in a test run, and
33-
// include the "-v" flag to display the model checker's output. For example:
33+
// include the "-v" flag to display the model checker's output:
3434
//
35-
// go test -v -tags modeltest -run Model ./internal/watch
35+
// go test ./internal/watch -v -tags modeltest -run Model
36+
//
37+
// Include the "-artifacts" flag to persist the "pan" binary and any "*.trail"
38+
// file rather than let Go's test framework delete them automatically.
3639
//
3740
// [Spin]: https://spinroot.com/
3841
func TestModel(t *testing.T) {
@@ -42,24 +45,7 @@ func TestModel(t *testing.T) {
4245
}
4346
}
4447

45-
tmpdir, err := os.MkdirTemp("", "hypcast-spin-*")
46-
if err != nil {
47-
t.Fatalf("failed to create spin compilation directory: %v", err)
48-
}
49-
50-
t.Logf("compiling model under %v", tmpdir)
51-
t.Chdir(tmpdir)
52-
t.Cleanup(func() {
53-
if t.Failed() {
54-
t.Logf("keeping %v due to test failure", tmpdir)
55-
return
56-
}
57-
if err := os.RemoveAll(tmpdir); err == nil {
58-
t.Logf("cleaned up %v", tmpdir)
59-
} else {
60-
t.Logf("failed to clean up %v: %v", tmpdir, err)
61-
}
62-
})
48+
t.Chdir(t.ArtifactDir())
6349

6450
spin := exec.Command("spin", "-a", "/dev/stdin")
6551
spin.Stdin = strings.NewReader(modelFile)
@@ -74,7 +60,7 @@ func TestModel(t *testing.T) {
7460
t.Fatalf("failed to compile pan.c: %v", err)
7561
}
7662

77-
pan := exec.Command(filepath.Join(tmpdir, "pan"))
63+
pan := exec.Command(filepath.Join(t.ArtifactDir(), "pan"))
7864
pan.Stdout, pan.Stderr = os.Stdout, os.Stderr
7965
if err := pan.Run(); err != nil {
8066
t.Fatalf("failed to run pan: %v", err)

0 commit comments

Comments
 (0)