diff --git a/executor_test.go b/executor_test.go index 8f3c794c..6ecd08f5 100644 --- a/executor_test.go +++ b/executor_test.go @@ -50,7 +50,8 @@ func NewExecutorTest(t *testing.T, opts ...ExecutorTestOption) { task: "default", vars: map[string]any{}, TaskTest: TaskTest{ - experiments: map[*experiments.Experiment]int{}, + experiments: map[*experiments.Experiment]int{}, + fixtureTemplateData: map[string]any{}, }, } // Apply the functional options @@ -232,7 +233,7 @@ func TestEmptyTaskfile(t *testing.T) { task.WithDir("testdata/empty_taskfile"), ), WithSetupError(), - WithPostProcessFn(PPRemoveAbsolutePaths), + WithFixtureTemplating(), ) } @@ -367,7 +368,7 @@ func TestSpecialVars(t *testing.T) { task.WithVersionCheck(true), ), WithTask(test), - WithPostProcessFn(PPRemoveAbsolutePaths), + WithFixtureTemplating(), ) } } @@ -551,7 +552,7 @@ func TestStatus(t *testing.T) { task.WithVerbose(true), ), WithTask("gen-silent-baz"), - WithPostProcessFn(PPRemoveAbsolutePaths), + WithFixtureTemplating(), ) } @@ -777,7 +778,7 @@ func TestForCmds(t *testing.T) { task.WithForce(true), ), WithTask(test.name), - WithPostProcessFn(PPRemoveAbsolutePaths), + WithFixtureTemplating(), } if test.wantErr { opts = append(opts, WithRunError()) @@ -822,7 +823,7 @@ func TestForDeps(t *testing.T) { task.WithOutputStyle(ast.Output{Name: "group"}), ), WithTask(test.name), - WithPostProcessFn(PPRemoveAbsolutePaths), + WithFixtureTemplating(), WithPostProcessFn(PPSortedLines), } if test.wantErr { diff --git a/formatter_test.go b/formatter_test.go index 2db067b2..7221ff76 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -44,7 +44,8 @@ func NewFormatterTest(t *testing.T, opts ...FormatterTestOption) { task: "default", vars: map[string]any{}, TaskTest: TaskTest{ - experiments: map[*experiments.Experiment]int{}, + experiments: map[*experiments.Experiment]int{}, + fixtureTemplateData: map[string]any{}, }, } // Apply the functional options @@ -222,8 +223,6 @@ func TestListDescInterpolation(t *testing.T) { func TestJsonListFormat(t *testing.T) { t.Parallel() - fp, err := filepath.Abs("testdata/json_list_format/Taskfile.yml") - require.NoError(t, err) NewFormatterTest(t, WithExecutorOptions( task.WithDir("testdata/json_list_format"), @@ -231,10 +230,6 @@ func TestJsonListFormat(t *testing.T) { WithListOptions(task.ListOptions{ FormatTaskListAsJSON: true, }), - WithFixtureTemplateData(struct { - TaskfileLocation string - }{ - TaskfileLocation: fp, - }), + WithFixtureTemplating(), ) } diff --git a/task_test.go b/task_test.go index 9ae65847..a5f9e3b8 100644 --- a/task_test.go +++ b/task_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/fs" + "maps" rand "math/rand/v2" "net/http" "net/http/httptest" @@ -42,10 +43,11 @@ type ( FormatterTestOption } TaskTest struct { - name string - experiments map[*experiments.Experiment]int - postProcessFns []PostProcessFn - fixtureTemplateData any + name string + experiments map[*experiments.Experiment]int + postProcessFns []PostProcessFn + fixtureTemplateData map[string]any + fixtureTemplatingEnabled bool } ) @@ -80,8 +82,19 @@ func (tt *TaskTest) writeFixture( if goldenFileSuffix != "" { goldenFileName += "-" + goldenFileSuffix } - if tt.fixtureTemplateData != nil { - g.AssertWithTemplate(t, goldenFileName, tt.fixtureTemplateData, b) + // Create a set of data to be made available to every test fixture + wd, err := os.Getwd() + require.NoError(t, err) + if tt.fixtureTemplatingEnabled { + fixtureTemplateData := map[string]any{ + "TEST_NAME": t.Name(), + "TEST_DIR": wd, + } + // If the test has additional template data, copy it into the map + if tt.fixtureTemplateData != nil { + maps.Copy(fixtureTemplateData, tt.fixtureTemplateData) + } + g.AssertWithTemplate(t, goldenFileName, fixtureTemplateData, b) } else { g.Assert(t, goldenFileName, b) } @@ -239,24 +252,44 @@ func (opt *setupErrorTestOption) applyToFormatterTest(t *FormatterTest) { t.wantSetupError = true } -// WithFixtureTemplateData sets up data defined in the golden file using golang -// template. Useful if the golden file can change depending on the test. -// Example template: {{ .Value }} -// Example data definition: struct{ Value string }{Value: "value"} -func WithFixtureTemplateData(data any) TestOption { - return &fixtureTemplateDataTestOption{data: data} +// WithFixtureTemplating enables templating for the golden fixture files with +// the default set of data. This is useful if the golden file is dynamic in some +// way (e.g. contains user-specific directories). To add more data, see +// WithFixtureTemplateData. +func WithFixtureTemplating() TestOption { + return &fixtureTemplatingTestOption{} +} + +type fixtureTemplatingTestOption struct{} + +func (opt *fixtureTemplatingTestOption) applyToExecutorTest(t *ExecutorTest) { + t.fixtureTemplatingEnabled = true +} + +func (opt *fixtureTemplatingTestOption) applyToFormatterTest(t *FormatterTest) { + t.fixtureTemplatingEnabled = true +} + +// WithFixtureTemplateData adds data to the golden fixture file templates. Keys +// given here will override any existing values. This option will also enable +// global templating, so you do not need to call WithFixtureTemplating as well. +func WithFixtureTemplateData(key string, value any) TestOption { + return &fixtureTemplateDataTestOption{key, value} } type fixtureTemplateDataTestOption struct { - data any + k string + v any } func (opt *fixtureTemplateDataTestOption) applyToExecutorTest(t *ExecutorTest) { - t.fixtureTemplateData = opt.data + t.fixtureTemplatingEnabled = true + t.fixtureTemplateData[opt.k] = opt.v } func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest) { - t.fixtureTemplateData = opt.data + t.fixtureTemplatingEnabled = true + t.fixtureTemplateData[opt.k] = opt.v } // Post-processing @@ -265,17 +298,6 @@ func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest) // fixture before the file is written. type PostProcessFn func(*testing.T, []byte) []byte -// PPRemoveAbsolutePaths removes any absolute paths from the output of the task. -// This is useful when the task output contains paths that are can be different -// in different environments such as home directories. The function looks for -// any paths that contain the current working directory and truncates them. -func PPRemoveAbsolutePaths(t *testing.T, b []byte) []byte { - t.Helper() - wd, err := os.Getwd() - require.NoError(t, err) - return bytes.ReplaceAll(b, []byte(wd), nil) -} - // PPSortedLines sorts the lines of the output of the task. This is useful when // the order of the output is not important, but the output is expected to be // the same each time the task is run (e.g. when running tasks in parallel). diff --git a/testdata/empty_taskfile/testdata/TestEmptyTaskfile-err-setup.golden b/testdata/empty_taskfile/testdata/TestEmptyTaskfile-err-setup.golden index 270d5182..730f459b 100644 --- a/testdata/empty_taskfile/testdata/TestEmptyTaskfile-err-setup.golden +++ b/testdata/empty_taskfile/testdata/TestEmptyTaskfile-err-setup.golden @@ -1 +1 @@ -task: Missing schema version in Taskfile "/testdata/empty_taskfile/Taskfile.yml" \ No newline at end of file +task: Missing schema version in Taskfile "{{.TEST_DIR}}/testdata/empty_taskfile/Taskfile.yml" \ No newline at end of file diff --git a/testdata/for/cmds/testdata/TestForCmds-loop-matrix-ref-error-err-run.golden b/testdata/for/cmds/testdata/TestForCmds-loop-matrix-ref-error-err-run.golden index c04ae8c5..c013c8be 100644 --- a/testdata/for/cmds/testdata/TestForCmds-loop-matrix-ref-error-err-run.golden +++ b/testdata/for/cmds/testdata/TestForCmds-loop-matrix-ref-error-err-run.golden @@ -1,2 +1,2 @@ -task: Failed to parse /testdata/for/cmds/Taskfile.yml: +task: Failed to parse {{.TEST_DIR}}/testdata/for/cmds/Taskfile.yml: matrix reference ".NOT_A_LIST" must resolve to a list \ No newline at end of file diff --git a/testdata/for/deps/testdata/TestForDeps-loop-matrix-ref-error-err-run.golden b/testdata/for/deps/testdata/TestForDeps-loop-matrix-ref-error-err-run.golden index e43c1af3..a328c4fb 100644 --- a/testdata/for/deps/testdata/TestForDeps-loop-matrix-ref-error-err-run.golden +++ b/testdata/for/deps/testdata/TestForDeps-loop-matrix-ref-error-err-run.golden @@ -1,2 +1,2 @@ matrix reference ".NOT_A_LIST" must resolve to a list -task: Failed to parse /testdata/for/deps/Taskfile.yml: +task: Failed to parse {{.TEST_DIR}}/testdata/for/deps/Taskfile.yml: diff --git a/testdata/json_list_format/testdata/TestJsonListFormat.golden b/testdata/json_list_format/testdata/TestJsonListFormat.golden index 44f05284..5a17b3ed 100644 --- a/testdata/json_list_format/testdata/TestJsonListFormat.golden +++ b/testdata/json_list_format/testdata/TestJsonListFormat.golden @@ -10,9 +10,9 @@ "location": { "line": 4, "column": 3, - "taskfile": "{{ .TaskfileLocation }}" + "taskfile": "{{.TEST_DIR}}/testdata/json_list_format/Taskfile.yml" } } ], - "location": "{{ .TaskfileLocation }}" + "location": "{{.TEST_DIR}}/testdata/json_list_format/Taskfile.yml" } diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-root-dir.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-root-dir.golden index 53fb1b33..c2e48014 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-root-dir.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-root-dir.golden @@ -1 +1 @@ -/testdata/special_vars +{{.TEST_DIR}}/testdata/special_vars diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile-dir.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile-dir.golden index d1e887b1..8b508a90 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile-dir.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile-dir.golden @@ -1 +1 @@ -/testdata/special_vars/included +{{.TEST_DIR}}/testdata/special_vars/included diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile.golden index 193f3dcc..8b1cc4b2 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-included-print-taskfile.golden @@ -1 +1 @@ -/testdata/special_vars/included/Taskfile.yml +{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-root-dir.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-root-dir.golden index 53fb1b33..c2e48014 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-root-dir.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-root-dir.golden @@ -1 +1 @@ -/testdata/special_vars +{{.TEST_DIR}}/testdata/special_vars diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-task-dir.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-task-dir.golden index f9d92450..35d8dd28 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-task-dir.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-task-dir.golden @@ -1 +1 @@ -/testdata/special_vars/foo +{{.TEST_DIR}}/testdata/special_vars/foo diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile-dir.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile-dir.golden index 53fb1b33..c2e48014 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile-dir.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile-dir.golden @@ -1 +1 @@ -/testdata/special_vars +{{.TEST_DIR}}/testdata/special_vars diff --git a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile.golden b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile.golden index 1e59e1e4..7593e0b1 100644 --- a/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile.golden +++ b/testdata/special_vars/subdir/testdata/TestSpecialVars-testdata-special_vars-subdir-print-taskfile.golden @@ -1 +1 @@ -/testdata/special_vars/Taskfile.yml +{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-root-dir.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-root-dir.golden index 53fb1b33..c2e48014 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-root-dir.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-root-dir.golden @@ -1 +1 @@ -/testdata/special_vars +{{.TEST_DIR}}/testdata/special_vars diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile-dir.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile-dir.golden index d1e887b1..8b508a90 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile-dir.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile-dir.golden @@ -1 +1 @@ -/testdata/special_vars/included +{{.TEST_DIR}}/testdata/special_vars/included diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile.golden index 193f3dcc..8b1cc4b2 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-included-print-taskfile.golden @@ -1 +1 @@ -/testdata/special_vars/included/Taskfile.yml +{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-root-dir.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-root-dir.golden index 53fb1b33..c2e48014 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-root-dir.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-root-dir.golden @@ -1 +1 @@ -/testdata/special_vars +{{.TEST_DIR}}/testdata/special_vars diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-task-dir.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-task-dir.golden index f9d92450..35d8dd28 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-task-dir.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-task-dir.golden @@ -1 +1 @@ -/testdata/special_vars/foo +{{.TEST_DIR}}/testdata/special_vars/foo diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile-dir.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile-dir.golden index 53fb1b33..c2e48014 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile-dir.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile-dir.golden @@ -1 +1 @@ -/testdata/special_vars +{{.TEST_DIR}}/testdata/special_vars diff --git a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile.golden b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile.golden index 1e59e1e4..7593e0b1 100644 --- a/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile.golden +++ b/testdata/special_vars/testdata/TestSpecialVars-testdata-special_vars-print-taskfile.golden @@ -1 +1 @@ -/testdata/special_vars/Taskfile.yml +{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml