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