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
250280type fixtureTemplateDataTestOption struct {
251- data any
281+ k string
282+ v any
252283}
253284
254285func (opt * fixtureTemplateDataTestOption ) applyToExecutorTest (t * ExecutorTest ) {
255- t .fixtureTemplateData = opt .data
286+ t .fixtureTemplatingEnabled = true
287+ t .fixtureTemplateData [opt .k ] = opt .v
256288}
257289
258290func (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.
266299type 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).
0 commit comments