squadron/internal/testutils/snapshot.go
Kevin Franklin Kim 4b88ffbd15
feat: bump linter
2025-10-10 11:54:31 +02:00

41 lines
915 B
Go

package testutils
import (
"os"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Snapshot compares v with its snapshot file
func Snapshot(t *testing.T, name, yaml string) {
t.Helper()
snapshot := readSnapshot(t, name)
if *UpdateFlag || snapshot == "" {
writeSnapshot(t, name, yaml)
}
assert.YAMLEq(t, snapshot, yaml)
}
// writeSnapshot updates the snapshot file for a given test t.
func writeSnapshot(t *testing.T, name string, content string) {
t.Helper()
require.NoError(t, os.WriteFile(name, []byte(content), 0600), "failed to update snapshot", name)
}
// readSnapshot reads the snapshot file for a given test t.
func readSnapshot(t *testing.T, name string) string {
t.Helper()
g, err := os.ReadFile(name)
if !errors.Is(err, os.ErrNotExist) {
require.NoError(t, err, "failed reading file", name)
}
return string(g)
}