2021-05-31 02:53:40 +02:00
|
|
|
// Package golden asserts golden files contents.
|
|
|
|
package golden
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var update = flag.Bool("update", false, "update .golden files")
|
|
|
|
|
2021-11-11 14:37:58 +02:00
|
|
|
const golden = ".golden"
|
|
|
|
|
2021-05-31 02:53:40 +02:00
|
|
|
func RequireEqual(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2022-01-20 19:59:39 +02:00
|
|
|
RequireEqualExt(tb, out, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualExt(tb testing.TB, out []byte, ext string) {
|
|
|
|
tb.Helper()
|
2023-06-15 04:59:55 +02:00
|
|
|
doRequireEqual(tb, out, ext, golden, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualExtSubfolder(tb testing.TB, out []byte, ext string) {
|
|
|
|
tb.Helper()
|
|
|
|
doRequireEqual(tb, out, ext, golden, true)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualTxt(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2023-06-15 04:59:55 +02:00
|
|
|
doRequireEqual(tb, out, ".txt", golden, false)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualJSON(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2023-06-15 04:59:55 +02:00
|
|
|
doRequireEqual(tb, out, ".json", golden, false)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualRb(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2023-06-15 04:59:55 +02:00
|
|
|
doRequireEqual(tb, out, ".rb", golden, false)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualYaml(tb testing.TB, out []byte) {
|
2021-11-11 14:37:58 +02:00
|
|
|
tb.Helper()
|
2023-06-15 04:59:55 +02:00
|
|
|
doRequireEqual(tb, out, ".yaml", "", false)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-01 14:54:58 +02:00
|
|
|
func RequireReadFile(tb testing.TB, path string) []byte {
|
|
|
|
tb.Helper()
|
|
|
|
bts, err := os.ReadFile(path)
|
|
|
|
require.NoError(tb, err)
|
|
|
|
return bts
|
|
|
|
}
|
|
|
|
|
2023-06-15 04:59:55 +02:00
|
|
|
func doRequireEqual(tb testing.TB, out []byte, ext, suffix string, folder bool) {
|
2021-05-31 02:53:40 +02:00
|
|
|
tb.Helper()
|
|
|
|
|
2023-06-15 04:59:55 +02:00
|
|
|
golden := filepath.Join("testdata", tb.Name()+ext+suffix)
|
|
|
|
if folder {
|
|
|
|
golden = filepath.Join("testdata", tb.Name(), filepath.Base(tb.Name())+ext+suffix)
|
|
|
|
}
|
2021-05-31 02:53:40 +02:00
|
|
|
if *update {
|
|
|
|
require.NoError(tb, os.MkdirAll(filepath.Dir(golden), 0o755))
|
|
|
|
require.NoError(tb, os.WriteFile(golden, out, 0o655))
|
|
|
|
}
|
|
|
|
|
|
|
|
gbts, err := os.ReadFile(golden)
|
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
require.Equal(tb, string(gbts), string(out))
|
|
|
|
}
|