From 7a76bcd4e63fc8e03b4953c39f6ece83302101d9 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sat, 31 Jan 2026 10:31:13 +0100 Subject: [PATCH] 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. --- executor_test.go | 3 ++- formatter_test.go | 3 ++- task_test.go | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/executor_test.go b/executor_test.go index f72104a6..eb1e1c34 100644 --- a/executor_test.go +++ b/executor_test.go @@ -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), ) diff --git a/formatter_test.go b/formatter_test.go index b92c8d55..0078b9bb 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -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), ) diff --git a/task_test.go b/task_test.go index a98d781c..07365868 100644 --- a/task_test.go +++ b/task_test.go @@ -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) }