mirror of
https://github.com/go-task/task.git
synced 2026-02-24 12:10:47 +01:00
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:
@@ -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),
|
||||
)
|
||||
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
|
||||
|
||||
18
task_test.go
18
task_test.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user