fix(tests): simplify cross-platform golden file comparison

Instead of manually handling template substitution, normalize the output
before passing it to AssertWithTemplate. This keeps goldie's features
(diff, -update flag) intact while ensuring cross-platform compatibility.
This commit is contained in:
Valentin Maerten
2026-01-31 10:35:35 +01:00
parent 7a76bcd4e6
commit 92fe5170ed
3 changed files with 4 additions and 20 deletions

View File

@@ -163,9 +163,8 @@ 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(tt.fixtureDir),
goldie.WithFixtureDir(filepath.Join(e.Dir, "testdata")),
goldie.WithEqualFn(NormalizedEqual),
)

View File

@@ -125,9 +125,8 @@ 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(tt.fixtureDir),
goldie.WithFixtureDir(filepath.Join(e.Dir, "testdata")),
goldie.WithEqualFn(NormalizedEqual),
)

View File

@@ -19,7 +19,6 @@ import (
"strings"
"sync"
"testing"
"text/template"
"time"
"github.com/Masterminds/semver/v3"
@@ -49,7 +48,6 @@ type (
postProcessFns []PostProcessFn
fixtureTemplateData map[string]any
fixtureTemplatingEnabled bool
fixtureDir string
}
)
@@ -96,20 +94,8 @@ func (tt *TaskTest) writeFixture(
if tt.fixtureTemplateData != nil {
maps.Copy(fixtureTemplateData, tt.fixtureTemplateData)
}
// 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)))
}
// Normalize output before comparison (CRLF→LF, backslash→forward slash)
g.AssertWithTemplate(t, goldenFileName, fixtureTemplateData, normalizeLineEndings(b))
} else {
g.Assert(t, goldenFileName, b)
}