Skip to content

Commit 1c6d686

Browse files
authored
chore: replace PPRemoveAbsolutePaths with generic fixture template data (#2265)
* chore: replace PPRemoveAbsolutePaths with generic fixture template data * chore: update to goldie v2.7.1
1 parent dac5aa1 commit 1c6d686

24 files changed

+83
-76
lines changed

Taskfile.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ tasks:
5353
generate:fixtures:
5454
desc: Runs tests and generates golden fixture files
5555
aliases: [gen:fixtures, g:fixtures]
56+
env:
57+
GOLDIE_UPDATE: 'true'
58+
GOLDIE_TEMPLATE: 'true'
5659
cmds:
5760
- find ./testdata -name '*.golden' -delete
58-
- go test -update ./...
61+
- go test ./...
5962

6063
install:mockery:
6164
desc: Installs mockgen; a tool to generate mock files

executor_test.go

Lines changed: 8 additions & 7 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 {
@@ -984,6 +985,6 @@ func TestIncludeChecksum(t *testing.T) {
984985
task.WithDir("testdata/includes_checksum/incorrect"),
985986
),
986987
WithSetupError(),
987-
WithPostProcessFn(PPRemoveAbsolutePaths),
988+
WithFixtureTemplating(),
988989
)
989990
}

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
}

go.sum

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ github.com/ProtonMail/go-crypto v1.1.6 h1:ZcV+Ropw6Qn0AX9brlQLAUXfqLBc7Bl+f/DmNx
1111
github.com/ProtonMail/go-crypto v1.1.6/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
1212
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
1313
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
14-
github.com/alecthomas/chroma/v2 v2.18.0 h1:6h53Q4hW83SuF+jcsp7CVhLsMozzvQvO8HBbKQW+gn4=
15-
github.com/alecthomas/chroma/v2 v2.18.0/go.mod h1:RVX6AvYm4VfYe/zsk7mjHueLDZor3aWCNE14TFlepBk=
1614
github.com/alecthomas/chroma/v2 v2.19.0 h1:Im+SLRgT8maArxv81mULDWN8oKxkzboH07CHesxElq4=
1715
github.com/alecthomas/chroma/v2 v2.19.0/go.mod h1:RVX6AvYm4VfYe/zsk7mjHueLDZor3aWCNE14TFlepBk=
1816
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
@@ -111,8 +109,6 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
111109
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
112110
github.com/sajari/fuzzy v1.0.0 h1:+FmwVvJErsd0d0hAPlj4CxqxUtQY/fOoY0DwX4ykpRY=
113111
github.com/sajari/fuzzy v1.0.0/go.mod h1:OjYR6KxoWOe9+dOlXeiCJd4dIbED4Oo8wpS89o0pwOo=
114-
github.com/sebdah/goldie/v2 v2.5.5 h1:rx1mwF95RxZ3/83sdS4Yp7t2C5TCokvWP4TBRbAyEWY=
115-
github.com/sebdah/goldie/v2 v2.5.5/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
116112
github.com/sebdah/goldie/v2 v2.7.1 h1:PkBHymaYdtvEkZV7TmyqKxdmn5/Vcj+8TpATWZjnG5E=
117113
github.com/sebdah/goldie/v2 v2.7.1/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
118114
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
@@ -121,8 +117,6 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG
121117
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
122118
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
123119
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
124-
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
125-
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
126120
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
127121
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
128122
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -147,8 +141,6 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbR
147141
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
148142
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
149143
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
150-
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
151-
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
152144
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
153145
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
154146
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -160,13 +152,9 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
160152
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
161153
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
162154
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
163-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
164-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
165155
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
166156
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
167157
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
168-
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
169-
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
170158
golang.org/x/term v0.33.0 h1:NuFncQrRcaRvVmgRkvM3j/F00gWIAlcmlB8ACEKmGIg=
171159
golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0=
172160
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -183,7 +171,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
183171
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
184172
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
185173
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
186-
mvdan.cc/sh/v3 v3.11.0 h1:q5h+XMDRfUGUedCqFFsjoFjrhwf2Mvtt1rkMvVz0blw=
187-
mvdan.cc/sh/v3 v3.11.0/go.mod h1:LRM+1NjoYCzuq/WZ6y44x14YNAI0NK7FLPeQSaFagGg=
188174
mvdan.cc/sh/v3 v3.12.0 h1:ejKUR7ONP5bb+UGHGEG/k9V5+pRVIyD+LsZz7o8KHrI=
189175
mvdan.cc/sh/v3 v3.12.0/go.mod h1:Se6Cj17eYSn+sNooLZiEUnNNmNxg0imoYlTu4CyaGyg=

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:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
task: The checksum of the Taskfile at "/testdata/includes_checksum/included.yml" does not match!
1+
task: The checksum of the Taskfile at "{{.TEST_DIR}}/testdata/includes_checksum/included.yml" does not match!
22
got: "c97f39eb96fe3fa5fe2a610d244b8449897b06f0c93821484af02e0999781bf5"
33
want: "foo"

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
}

0 commit comments

Comments
 (0)