fix(tests): work around goldie AssertWithTemplate not using EqualFn

goldie's AssertWithTemplate doesn't respect the EqualFn option, so we
manually handle template substitution and use NormalizedEqual directly
for cross-platform comparison.
This commit is contained in:
Valentin Maerten
2026-01-31 10:31:13 +01:00
parent 0f6340858b
commit 7a76bcd4e6
3 changed files with 20 additions and 3 deletions

View File

@@ -163,8 +163,9 @@ func (tt *ExecutorTest) run(t *testing.T) {
e := task.NewExecutor(opts...)
// Create a golden fixture file for the output
tt.fixtureDir = filepath.Join(e.Dir, "testdata")
g := goldie.New(t,
goldie.WithFixtureDir(filepath.Join(e.Dir, "testdata")),
goldie.WithFixtureDir(tt.fixtureDir),
goldie.WithEqualFn(NormalizedEqual),
)

View File

@@ -125,8 +125,9 @@ func (tt *FormatterTest) run(t *testing.T) {
e := task.NewExecutor(opts...)
// Create a golden fixture file for the output
tt.fixtureDir = filepath.Join(e.Dir, "testdata")
g := goldie.New(t,
goldie.WithFixtureDir(filepath.Join(e.Dir, "testdata")),
goldie.WithFixtureDir(tt.fixtureDir),
goldie.WithEqualFn(NormalizedEqual),
)

View File

@@ -19,6 +19,7 @@ import (
"strings"
"sync"
"testing"
"text/template"
"time"
"github.com/Masterminds/semver/v3"
@@ -48,6 +49,7 @@ type (
postProcessFns []PostProcessFn
fixtureTemplateData map[string]any
fixtureTemplatingEnabled bool
fixtureDir string
}
)
@@ -94,7 +96,20 @@ func (tt *TaskTest) writeFixture(
if tt.fixtureTemplateData != nil {
maps.Copy(fixtureTemplateData, tt.fixtureTemplateData)
}
g.AssertWithTemplate(t, goldenFileName, fixtureTemplateData, b)
// Note: We manually handle template substitution and comparison
// because AssertWithTemplate doesn't respect the EqualFn option.
goldenFile := filepath.Join(tt.fixtureDir, goldenFileName+".golden")
goldenContent, err := os.ReadFile(goldenFile)
require.NoError(t, err)
tmpl, err := template.New("golden").Parse(string(goldenContent))
require.NoError(t, err)
var expected bytes.Buffer
require.NoError(t, tmpl.Execute(&expected, fixtureTemplateData))
if !NormalizedEqual(b, expected.Bytes()) {
t.Errorf("Result did not match the golden fixture.\nExpected:\n%s\nActual:\n%s",
string(normalizeLineEndings(expected.Bytes())),
string(normalizeLineEndings(b)))
}
} else {
g.Assert(t, goldenFileName, b)
}