From 0d3652b793f6dc238667db3ea7939f76e49948c8 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 2 Mar 2025 18:37:46 +0900 Subject: [PATCH] test/sandbox/assert: wrap printf Signed-off-by: Ophestra --- test/sandbox/assert.go | 4 +++- test/sandbox/assert_test.go | 5 ++++- test/sandbox/mount_test.go | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/sandbox/assert.go b/test/sandbox/assert.go index 150bef4..2decde8 100644 --- a/test/sandbox/assert.go +++ b/test/sandbox/assert.go @@ -8,9 +8,11 @@ import ( var ( assert = log.New(os.Stderr, "sandbox: ", 0) + printfFunc = assert.Printf fatalfFunc = assert.Fatalf ) +func printf(format string, v ...any) { printfFunc(format, v...) } func fatalf(format string, v ...any) { fatalfFunc(format, v...) } func MustAssertMounts(name, hostMountsFile, wantFile string) { @@ -53,7 +55,7 @@ func MustAssertMounts(name, hostMountsFile, wantFile string) { e, &want[i]) } - assert.Printf("%s", e) + printf("%s", e) i++ }); err != nil { fatalf("cannot iterate mounts: %v", err) diff --git a/test/sandbox/assert_test.go b/test/sandbox/assert_test.go index f2b7f99..3727f57 100644 --- a/test/sandbox/assert_test.go +++ b/test/sandbox/assert_test.go @@ -1,3 +1,6 @@ package sandbox -func ReplaceFatal(f func(format string, v ...any)) { fatalfFunc = f } +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 } diff --git a/test/sandbox/mount_test.go b/test/sandbox/mount_test.go index 425ce6d..7407412 100644 --- a/test/sandbox/mount_test.go +++ b/test/sandbox/mount_test.go @@ -111,7 +111,8 @@ overlay /.fortify/sbin/fortify overlay ro,nosuid,nodev,relatime,lowerdir=/mnt-ro }) t.Run(tc.name+" assert", func(t *testing.T) { - sandbox.ReplaceFatal(t.Fatalf) + oldFatal := sandbox.SwapFatal(t.Fatalf) + t.Cleanup(func() { sandbox.SwapFatal(oldFatal) }) wantFile := path.Join(t.TempDir(), "want.json") if f, err := os.OpenFile(wantFile, os.O_CREATE|os.O_WRONLY, 0400); err != nil {