From 0bd9b9e8fe61a96d7c63ec85c190cd32636fa659 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 2 Mar 2025 23:23:04 +0900 Subject: test/sandbox: assert filesystem json Signed-off-by: Ophestra --- test/sandbox/assert_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'test/sandbox/assert_test.go') diff --git a/test/sandbox/assert_test.go b/test/sandbox/assert_test.go index 3727f57f..c3fb5800 100644 --- a/test/sandbox/assert_test.go +++ b/test/sandbox/assert_test.go @@ -1,6 +1,32 @@ package sandbox +import ( + "encoding/json" + "os" + "path" + "testing" +) + type F func(format string, v ...any) func SwapPrint(f F) (old F) { old = printfFunc; printfFunc = f; return } func SwapFatal(f F) (old F) { old = fatalfFunc; fatalfFunc = f; return } + +func MustWantFile(t *testing.T, v any) (wantFile string) { + wantFile = path.Join(t.TempDir(), "want.json") + if f, err := os.OpenFile(wantFile, os.O_CREATE|os.O_WRONLY, 0400); err != nil { + t.Fatalf("cannot create %q: %v", wantFile, err) + } else if err = json.NewEncoder(f).Encode(v); err != nil { + t.Fatalf("cannot encode to %q: %v", wantFile, err) + } else if err = f.Close(); err != nil { + t.Fatalf("cannot close %q: %v", wantFile, err) + } + + t.Cleanup(func() { + if err := os.Remove(wantFile); err != nil { + t.Fatalf("cannot remove %q: %v", wantFile, err) + } + }) + + return +} -- cgit v1.3.1