From 5ae825109edafa5e1ec3a2d59e2ea7161cbeafdd Mon Sep 17 00:00:00 2001 From: PJ Date: Sat, 22 Aug 2026 21:23:54 +0530 Subject: [PATCH] test: one run-directory writer for the packages that share it internal/tracecorpus and cmd/internal-tools/exploration-reach declared the same 24-line writeRun, identical apart from whether the seed was a parameter. The package's charter is on it: a helper goes here once it has callers in two packages, and stays in a package's own tests until then. --- .../exploration-reach/reach_test.go | 24 ++--------- internal/testsupport/runs.go | 41 +++++++++++++++++++ internal/tracecorpus/corpus_test.go | 25 ++--------- 3 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 internal/testsupport/runs.go diff --git a/cmd/internal-tools/exploration-reach/reach_test.go b/cmd/internal-tools/exploration-reach/reach_test.go index 5f56331..af5e10c 100644 --- a/cmd/internal-tools/exploration-reach/reach_test.go +++ b/cmd/internal-tools/exploration-reach/reach_test.go @@ -5,6 +5,7 @@ import ( "github.com/priyanshujain/sanderling/internal/driver/ioscompanion" "github.com/priyanshujain/sanderling/internal/hierarchy" + "github.com/priyanshujain/sanderling/internal/testsupport" "github.com/priyanshujain/sanderling/internal/trace" "github.com/priyanshujain/sanderling/internal/tracecorpus" ) @@ -136,27 +137,8 @@ func measureRun(t *testing.T, seed int64, dumps ...string) Reach { func writeRun(t *testing.T, seed int64, dumps ...string) string { t.Helper() - directory := t.TempDir() - writer, err := trace.NewWriter(directory) - if err != nil { - t.Fatal(err) - } - if err := writer.WriteMeta(trace.Meta{Seed: seed, Platform: "web"}); err != nil { - t.Fatal(err) - } - for index, dump := range dumps { - tree, err := hierarchy.Parse(dump) - if err != nil { - t.Fatal(err) - } - if err := writer.WriteStep(trace.Step{Index: index + 1, Hierarchy: tree}); err != nil { - t.Fatal(err) - } - } - if err := writer.Close(); err != nil { - t.Fatal(err) - } - return directory + return testsupport.WriteRunFromDumps( + t, t.TempDir(), trace.Meta{Seed: seed, Platform: "web"}, dumps...) } func appendFinalize(t *testing.T, directory string, index int) { diff --git a/internal/testsupport/runs.go b/internal/testsupport/runs.go new file mode 100644 index 0000000..c8c4d54 --- /dev/null +++ b/internal/testsupport/runs.go @@ -0,0 +1,41 @@ +// Package testsupport holds test helpers that more than one package needs. +// It exists because the suite's helper layer grew by copying: a helper written +// inside one package's tests is invisible to the next package that needs it, so +// the next package writes it again. Anything here has at least two callers in +// different packages; a helper with one belongs in that package's own tests. +package testsupport + +import ( + "testing" + + "github.com/priyanshujain/sanderling/internal/hierarchy" + "github.com/priyanshujain/sanderling/internal/trace" +) + +// WriteRunFromDumps writes a complete run directory: meta.json, then one step +// per hierarchy dump numbered from 1. It goes through trace.NewWriter rather +// than composing the files by hand so a fixture cannot claim a trace_version +// the writer would not have produced. +func WriteRunFromDumps(t *testing.T, directory string, meta trace.Meta, dumps ...string) string { + t.Helper() + writer, err := trace.NewWriter(directory) + if err != nil { + t.Fatal(err) + } + if err := writer.WriteMeta(meta); err != nil { + t.Fatal(err) + } + for index, dump := range dumps { + tree, err := hierarchy.Parse(dump) + if err != nil { + t.Fatal(err) + } + if err := writer.WriteStep(trace.Step{Index: index + 1, Hierarchy: tree}); err != nil { + t.Fatal(err) + } + } + if err := writer.Close(); err != nil { + t.Fatal(err) + } + return directory +} diff --git a/internal/tracecorpus/corpus_test.go b/internal/tracecorpus/corpus_test.go index 19bf4fc..3637fe4 100644 --- a/internal/tracecorpus/corpus_test.go +++ b/internal/tracecorpus/corpus_test.go @@ -7,7 +7,7 @@ import ( "strings" "testing" - "github.com/priyanshujain/sanderling/internal/hierarchy" + "github.com/priyanshujain/sanderling/internal/testsupport" "github.com/priyanshujain/sanderling/internal/trace" ) @@ -73,27 +73,8 @@ func TestLoadRefusesElementsWithNoStoredShape(t *testing.T) { func writeRun(t *testing.T, dumps ...string) string { t.Helper() - directory := t.TempDir() - writer, err := trace.NewWriter(directory) - if err != nil { - t.Fatal(err) - } - if err := writer.WriteMeta(trace.Meta{Seed: 7, Platform: "web"}); err != nil { - t.Fatal(err) - } - for index, dump := range dumps { - tree, err := hierarchy.Parse(dump) - if err != nil { - t.Fatal(err) - } - if err := writer.WriteStep(trace.Step{Index: index + 1, Hierarchy: tree}); err != nil { - t.Fatal(err) - } - } - if err := writer.Close(); err != nil { - t.Fatal(err) - } - return directory + return testsupport.WriteRunFromDumps( + t, t.TempDir(), trace.Meta{Seed: 7, Platform: "web"}, dumps...) } func downgrade(t *testing.T, path string) {