Skip to content

Commit 7b55165

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

21 files changed

+41
-53
lines changed

executor_test.go

Lines changed: 2 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,6 @@ func TestEmptyTaskfile(t *testing.T) {
232233
task.WithDir("testdata/empty_taskfile"),
233234
),
234235
WithSetupError(),
235-
WithPostProcessFn(PPRemoveAbsolutePaths),
236236
)
237237
}
238238

@@ -367,7 +367,6 @@ func TestSpecialVars(t *testing.T) {
367367
task.WithVersionCheck(true),
368368
),
369369
WithTask(test),
370-
WithPostProcessFn(PPRemoveAbsolutePaths),
371370
)
372371
}
373372
}
@@ -551,7 +550,6 @@ func TestStatus(t *testing.T) {
551550
task.WithVerbose(true),
552551
),
553552
WithTask("gen-silent-baz"),
554-
WithPostProcessFn(PPRemoveAbsolutePaths),
555553
)
556554
}
557555

@@ -777,7 +775,6 @@ func TestForCmds(t *testing.T) {
777775
task.WithForce(true),
778776
),
779777
WithTask(test.name),
780-
WithPostProcessFn(PPRemoveAbsolutePaths),
781778
}
782779
if test.wantErr {
783780
opts = append(opts, WithRunError())
@@ -822,7 +819,6 @@ func TestForDeps(t *testing.T) {
822819
task.WithOutputStyle(ast.Output{Name: "group"}),
823820
),
824821
WithTask(test.name),
825-
WithPostProcessFn(PPRemoveAbsolutePaths),
826822
WithPostProcessFn(PPSortedLines),
827823
}
828824
if test.wantErr {

formatter_test.go

Lines changed: 2 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,12 @@ 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-
}),
239233
)
240234
}

task_test.go

Lines changed: 18 additions & 20 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"
@@ -45,7 +46,7 @@ type (
4546
name string
4647
experiments map[*experiments.Experiment]int
4748
postProcessFns []PostProcessFn
48-
fixtureTemplateData any
49+
fixtureTemplateData map[string]any
4950
}
5051
)
5152

@@ -80,11 +81,18 @@ func (tt *TaskTest) writeFixture(
8081
if goldenFileSuffix != "" {
8182
goldenFileName += "-" + goldenFileSuffix
8283
}
84+
// Create a set of data to be made available to every test fixture
85+
wd, err := os.Getwd()
86+
require.NoError(t, err)
87+
fixtureTemplateData := map[string]any{
88+
"TEST_NAME": t.Name(),
89+
"TEST_DIR": wd,
90+
}
91+
// If the test has additional template data, copy it into the map
8392
if tt.fixtureTemplateData != nil {
84-
g.AssertWithTemplate(t, goldenFileName, tt.fixtureTemplateData, b)
85-
} else {
86-
g.Assert(t, goldenFileName, b)
93+
maps.Copy(fixtureTemplateData, tt.fixtureTemplateData)
8794
}
95+
g.AssertWithTemplate(t, goldenFileName, fixtureTemplateData, b)
8896
}
8997

9098
// writeFixtureBuffer is a wrapper for writing the main output of the task to a
@@ -243,20 +251,21 @@ func (opt *setupErrorTestOption) applyToFormatterTest(t *FormatterTest) {
243251
// template. Useful if the golden file can change depending on the test.
244252
// Example template: {{ .Value }}
245253
// Example data definition: struct{ Value string }{Value: "value"}
246-
func WithFixtureTemplateData(data any) TestOption {
247-
return &fixtureTemplateDataTestOption{data: data}
254+
func WithFixtureTemplateData(k string, v any) TestOption {
255+
return &fixtureTemplateDataTestOption{k, v}
248256
}
249257

250258
type fixtureTemplateDataTestOption struct {
251-
data any
259+
k string
260+
v any
252261
}
253262

254263
func (opt *fixtureTemplateDataTestOption) applyToExecutorTest(t *ExecutorTest) {
255-
t.fixtureTemplateData = opt.data
264+
t.fixtureTemplateData[opt.k] = opt.v
256265
}
257266

258267
func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest) {
259-
t.fixtureTemplateData = opt.data
268+
t.fixtureTemplateData[opt.k] = opt.v
260269
}
261270

262271
// Post-processing
@@ -265,17 +274,6 @@ func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest)
265274
// fixture before the file is written.
266275
type PostProcessFn func(*testing.T, []byte) []byte
267276

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-
279277
// PPSortedLines sorts the lines of the output of the task. This is useful when
280278
// the order of the output is not important, but the output is expected to be
281279
// 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)