Skip to content

Commit 85d3754

Browse files
committed
chore: replace PPRemoveAbsolutePaths with generic fixture template data
1 parent ec4e68d commit 85d3754

21 files changed

+77
-59
lines changed

executor_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func NewExecutorTest(t *testing.T, opts ...ExecutorTestOption) {
5050
task: "default",
5151
vars: map[string]any{},
5252
TaskTest: TaskTest{
53-
experiments: map[*experiments.Experiment]int{},
53+
experiments: map[*experiments.Experiment]int{},
54+
fixtureTemplateData: map[string]any{},
5455
},
5556
}
5657
// Apply the functional options
@@ -232,7 +233,7 @@ func TestEmptyTaskfile(t *testing.T) {
232233
task.WithDir("testdata/empty_taskfile"),
233234
),
234235
WithSetupError(),
235-
WithPostProcessFn(PPRemoveAbsolutePaths),
236+
WithFixtureTemplating(),
236237
)
237238
}
238239

@@ -367,7 +368,7 @@ func TestSpecialVars(t *testing.T) {
367368
task.WithVersionCheck(true),
368369
),
369370
WithTask(test),
370-
WithPostProcessFn(PPRemoveAbsolutePaths),
371+
WithFixtureTemplating(),
371372
)
372373
}
373374
}
@@ -551,7 +552,7 @@ func TestStatus(t *testing.T) {
551552
task.WithVerbose(true),
552553
),
553554
WithTask("gen-silent-baz"),
554-
WithPostProcessFn(PPRemoveAbsolutePaths),
555+
WithFixtureTemplating(),
555556
)
556557
}
557558

@@ -777,7 +778,7 @@ func TestForCmds(t *testing.T) {
777778
task.WithForce(true),
778779
),
779780
WithTask(test.name),
780-
WithPostProcessFn(PPRemoveAbsolutePaths),
781+
WithFixtureTemplating(),
781782
}
782783
if test.wantErr {
783784
opts = append(opts, WithRunError())
@@ -822,7 +823,7 @@ func TestForDeps(t *testing.T) {
822823
task.WithOutputStyle(ast.Output{Name: "group"}),
823824
),
824825
WithTask(test.name),
825-
WithPostProcessFn(PPRemoveAbsolutePaths),
826+
WithFixtureTemplating(),
826827
WithPostProcessFn(PPSortedLines),
827828
}
828829
if test.wantErr {

formatter_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func NewFormatterTest(t *testing.T, opts ...FormatterTestOption) {
4444
task: "default",
4545
vars: map[string]any{},
4646
TaskTest: TaskTest{
47-
experiments: map[*experiments.Experiment]int{},
47+
experiments: map[*experiments.Experiment]int{},
48+
fixtureTemplateData: map[string]any{},
4849
},
4950
}
5051
// Apply the functional options
@@ -222,19 +223,13 @@ func TestListDescInterpolation(t *testing.T) {
222223
func TestJsonListFormat(t *testing.T) {
223224
t.Parallel()
224225

225-
fp, err := filepath.Abs("testdata/json_list_format/Taskfile.yml")
226-
require.NoError(t, err)
227226
NewFormatterTest(t,
228227
WithExecutorOptions(
229228
task.WithDir("testdata/json_list_format"),
230229
),
231230
WithListOptions(task.ListOptions{
232231
FormatTaskListAsJSON: true,
233232
}),
234-
WithFixtureTemplateData(struct {
235-
TaskfileLocation string
236-
}{
237-
TaskfileLocation: fp,
238-
}),
233+
WithFixtureTemplating(),
239234
)
240235
}

task_test.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"io/fs"
9+
"maps"
910
rand "math/rand/v2"
1011
"net/http"
1112
"net/http/httptest"
@@ -42,10 +43,11 @@ type (
4243
FormatterTestOption
4344
}
4445
TaskTest struct {
45-
name string
46-
experiments map[*experiments.Experiment]int
47-
postProcessFns []PostProcessFn
48-
fixtureTemplateData any
46+
name string
47+
experiments map[*experiments.Experiment]int
48+
postProcessFns []PostProcessFn
49+
fixtureTemplateData map[string]any
50+
fixtureTemplatingEnabled bool
4951
}
5052
)
5153

@@ -80,8 +82,19 @@ func (tt *TaskTest) writeFixture(
8082
if goldenFileSuffix != "" {
8183
goldenFileName += "-" + goldenFileSuffix
8284
}
83-
if tt.fixtureTemplateData != nil {
84-
g.AssertWithTemplate(t, goldenFileName, tt.fixtureTemplateData, b)
85+
// Create a set of data to be made available to every test fixture
86+
wd, err := os.Getwd()
87+
require.NoError(t, err)
88+
if tt.fixtureTemplatingEnabled {
89+
fixtureTemplateData := map[string]any{
90+
"TEST_NAME": t.Name(),
91+
"TEST_DIR": wd,
92+
}
93+
// If the test has additional template data, copy it into the map
94+
if tt.fixtureTemplateData != nil {
95+
maps.Copy(fixtureTemplateData, tt.fixtureTemplateData)
96+
}
97+
g.AssertWithTemplate(t, goldenFileName, fixtureTemplateData, b)
8598
} else {
8699
g.Assert(t, goldenFileName, b)
87100
}
@@ -239,24 +252,44 @@ func (opt *setupErrorTestOption) applyToFormatterTest(t *FormatterTest) {
239252
t.wantSetupError = true
240253
}
241254

242-
// WithFixtureTemplateData sets up data defined in the golden file using golang
243-
// template. Useful if the golden file can change depending on the test.
244-
// Example template: {{ .Value }}
245-
// Example data definition: struct{ Value string }{Value: "value"}
246-
func WithFixtureTemplateData(data any) TestOption {
247-
return &fixtureTemplateDataTestOption{data: data}
255+
// WithFixtureTemplating enables templating for the golden fixture files with
256+
// the default set of data. This is useful if the golden file is dynamic in some
257+
// way (e.g. contains user-specific directories). To add more data, see
258+
// WithFixtureTemplateData.
259+
func WithFixtureTemplating() TestOption {
260+
return &fixtureTemplatingTestOption{}
261+
}
262+
263+
type fixtureTemplatingTestOption struct{}
264+
265+
func (opt *fixtureTemplatingTestOption) applyToExecutorTest(t *ExecutorTest) {
266+
t.fixtureTemplatingEnabled = true
267+
}
268+
269+
func (opt *fixtureTemplatingTestOption) applyToFormatterTest(t *FormatterTest) {
270+
t.fixtureTemplatingEnabled = true
271+
}
272+
273+
// WithFixtureTemplateData adds data to the golden fixture file templates. Keys
274+
// given here will override any existing values. This option will also enable
275+
// global templating, so you do not need to call WithFixtureTemplating as well.
276+
func WithFixtureTemplateData(key string, value any) TestOption {
277+
return &fixtureTemplateDataTestOption{key, value}
248278
}
249279

250280
type fixtureTemplateDataTestOption struct {
251-
data any
281+
k string
282+
v any
252283
}
253284

254285
func (opt *fixtureTemplateDataTestOption) applyToExecutorTest(t *ExecutorTest) {
255-
t.fixtureTemplateData = opt.data
286+
t.fixtureTemplatingEnabled = true
287+
t.fixtureTemplateData[opt.k] = opt.v
256288
}
257289

258290
func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest) {
259-
t.fixtureTemplateData = opt.data
291+
t.fixtureTemplatingEnabled = true
292+
t.fixtureTemplateData[opt.k] = opt.v
260293
}
261294

262295
// Post-processing
@@ -265,17 +298,6 @@ func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest)
265298
// fixture before the file is written.
266299
type PostProcessFn func(*testing.T, []byte) []byte
267300

268-
// PPRemoveAbsolutePaths removes any absolute paths from the output of the task.
269-
// This is useful when the task output contains paths that are can be different
270-
// in different environments such as home directories. The function looks for
271-
// any paths that contain the current working directory and truncates them.
272-
func PPRemoveAbsolutePaths(t *testing.T, b []byte) []byte {
273-
t.Helper()
274-
wd, err := os.Getwd()
275-
require.NoError(t, err)
276-
return bytes.ReplaceAll(b, []byte(wd), nil)
277-
}
278-
279301
// PPSortedLines sorts the lines of the output of the task. This is useful when
280302
// the order of the output is not important, but the output is expected to be
281303
// the same each time the task is run (e.g. when running tasks in parallel).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
task: Missing schema version in Taskfile "/testdata/empty_taskfile/Taskfile.yml"
1+
task: Missing schema version in Taskfile "{{.TEST_DIR}}/testdata/empty_taskfile/Taskfile.yml"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
task: Failed to parse /testdata/for/cmds/Taskfile.yml:
1+
task: Failed to parse {{.TEST_DIR}}/testdata/for/cmds/Taskfile.yml:
22
matrix reference ".NOT_A_LIST" must resolve to a list
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
matrix reference ".NOT_A_LIST" must resolve to a list
2-
task: Failed to parse /testdata/for/deps/Taskfile.yml:
2+
task: Failed to parse {{.TEST_DIR}}/testdata/for/deps/Taskfile.yml:

testdata/json_list_format/testdata/TestJsonListFormat.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"location": {
1111
"line": 4,
1212
"column": 3,
13-
"taskfile": "{{ .TaskfileLocation }}"
13+
"taskfile": "{{.TEST_DIR}}/testdata/json_list_format/Taskfile.yml"
1414
}
1515
}
1616
],
17-
"location": "{{ .TaskfileLocation }}"
17+
"location": "{{.TEST_DIR}}/testdata/json_list_format/Taskfile.yml"
1818
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/testdata/special_vars
1+
{{.TEST_DIR}}/testdata/special_vars
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/testdata/special_vars/included
1+
{{.TEST_DIR}}/testdata/special_vars/included
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/testdata/special_vars/included/Taskfile.yml
1+
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml

0 commit comments

Comments
 (0)