forked from rosa/hakurei
This makes package check portable, and removes nonportable behaviour from package pkg, pipewire, and system. All other packages remain nonportable due to their nature. No latency increase was observed due to this change on amd64 and arm64 linux. Signed-off-by: Ophestra <cat@gensokyo.uk>
35 lines
871 B
Go
35 lines
871 B
Go
//go:build testtool
|
|
|
|
package sandbox
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"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 = filepath.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
|
|
}
|