test/sandbox/assert: wrap printf
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
d8e9d71f87
commit
0d3652b793
@ -8,9 +8,11 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
assert = log.New(os.Stderr, "sandbox: ", 0)
|
assert = log.New(os.Stderr, "sandbox: ", 0)
|
||||||
|
printfFunc = assert.Printf
|
||||||
fatalfFunc = assert.Fatalf
|
fatalfFunc = assert.Fatalf
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func printf(format string, v ...any) { printfFunc(format, v...) }
|
||||||
func fatalf(format string, v ...any) { fatalfFunc(format, v...) }
|
func fatalf(format string, v ...any) { fatalfFunc(format, v...) }
|
||||||
|
|
||||||
func MustAssertMounts(name, hostMountsFile, wantFile string) {
|
func MustAssertMounts(name, hostMountsFile, wantFile string) {
|
||||||
@ -53,7 +55,7 @@ func MustAssertMounts(name, hostMountsFile, wantFile string) {
|
|||||||
e, &want[i])
|
e, &want[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Printf("%s", e)
|
printf("%s", e)
|
||||||
i++
|
i++
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
fatalf("cannot iterate mounts: %v", err)
|
fatalf("cannot iterate mounts: %v", err)
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
package sandbox
|
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 }
|
||||||
|
@ -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) {
|
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")
|
wantFile := path.Join(t.TempDir(), "want.json")
|
||||||
if f, err := os.OpenFile(wantFile, os.O_CREATE|os.O_WRONLY, 0400); err != nil {
|
if f, err := os.OpenFile(wantFile, os.O_CREATE|os.O_WRONLY, 0400); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user