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()
|
|
|
|
doRequireEqual(tb, out, ext, golden)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualTxt(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2021-11-11 14:37:58 +02:00
|
|
|
doRequireEqual(tb, out, ".txt", golden)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualJSON(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2021-11-11 14:37:58 +02:00
|
|
|
doRequireEqual(tb, out, ".json", golden)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualRb(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2021-11-11 14:37:58 +02:00
|
|
|
doRequireEqual(tb, out, ".rb", golden)
|
2021-05-31 02:53:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:16:39 +02:00
|
|
|
func RequireEqualLua(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2021-11-11 14:37:58 +02:00
|
|
|
doRequireEqual(tb, out, ".lua", golden)
|
2021-09-29 01:16:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 02:53:40 +02:00
|
|
|
func RequireEqualYaml(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
2021-11-11 14:37:58 +02:00
|
|
|
doRequireEqual(tb, out, ".yml", golden)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequireEqualNakedYaml(tb testing.TB, out []byte) {
|
|
|
|
tb.Helper()
|
|
|
|
doRequireEqual(tb, out, ".yaml", "")
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:37:58 +02:00
|
|
|
func doRequireEqual(tb testing.TB, out []byte, ext, suffix string) {
|
2021-05-31 02:53:40 +02:00
|
|
|
tb.Helper()
|
|
|
|
|
2021-11-11 14:37:58 +02:00
|
|
|
golden := "testdata/" + 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))
|
|
|
|
}
|