package beside import ( "fmt" "os" "path/filepath" "testing" ) // The captured copy of a repository is laid out once per test process: a second call, in another test, // gets the same directory (issue 1379, AC3). Laying it out per test made go/catalogue's tests take 2.14 s // against the unit level's 2 s bound; once, 0.81 s. func TestTheCapturedCopyIsLaidOutOnce(t *testing.T) { t.Setenv(Env, "") var first, second string t.Run("one", func(t *testing.T) { first = Dir(t, "mesh-catalog") }) t.Run("two", func(t *testing.T) { second = Dir(t, "mesh-catalog") }) if first == "" || first != second { t.Fatalf("the captured copy was laid out twice: %q and %q", first, second) } if _, err := os.Stat(filepath.Join(first, "modules")); err != nil { t.Fatalf("the copy holds no modules directory: %v", err) } // And it outlives the test that first asked, which a t.TempDir would not. if _, err := os.Stat(first); err != nil { t.Fatalf("the copy is gone after the first test ended: %v", err) } } // With MESH_CHECK_BESIDE naming a directory that lacks the repository, the test fails naming it (AC4; the // rule is beside's own, unchanged). Dir fails the test it is given, so it is given a recorder. func TestABesideDirectoryWithoutTheRepositoryFailsNamingIt(t *testing.T) { empty := t.TempDir() t.Setenv(Env, empty) r := &recorder{TB: t} func() { defer func() { _ = recover() }() Dir(r, "mesh-catalog") }() if !r.failed { t.Fatal("a repository missing beside the check did not fail the test") } if want := "mesh-catalog is not beside this check in " + Env + "=" + empty; len(r.msg) < len(want) || r.msg[:len(want)] != want { t.Fatalf("the failure does not name the repository and the directory: %q", r.msg) } } type recorder struct { testing.TB failed bool msg string } func (r *recorder) Fatalf(format string, args ...any) { r.failed = true r.msg = sprintf(format, args...) panic(stop{}) } type stop struct{} func sprintf(format string, args ...any) string { return fmt.Sprintf(format, args...) }