1
0
mirror of https://github.com/go-task/task.git synced 2025-06-23 00:38:19 +02:00

chore: replace PPRemoveAbsolutePaths with generic fixture template data

This commit is contained in:
Pete Davison
2025-05-21 14:32:37 +00:00
parent ec4e68d601
commit 85d3754ac0
21 changed files with 77 additions and 59 deletions

View File

@ -50,7 +50,8 @@ func NewExecutorTest(t *testing.T, opts ...ExecutorTestOption) {
task: "default", task: "default",
vars: map[string]any{}, vars: map[string]any{},
TaskTest: TaskTest{ TaskTest: TaskTest{
experiments: map[*experiments.Experiment]int{}, experiments: map[*experiments.Experiment]int{},
fixtureTemplateData: map[string]any{},
}, },
} }
// Apply the functional options // Apply the functional options
@ -232,7 +233,7 @@ func TestEmptyTaskfile(t *testing.T) {
task.WithDir("testdata/empty_taskfile"), task.WithDir("testdata/empty_taskfile"),
), ),
WithSetupError(), WithSetupError(),
WithPostProcessFn(PPRemoveAbsolutePaths), WithFixtureTemplating(),
) )
} }
@ -367,7 +368,7 @@ func TestSpecialVars(t *testing.T) {
task.WithVersionCheck(true), task.WithVersionCheck(true),
), ),
WithTask(test), WithTask(test),
WithPostProcessFn(PPRemoveAbsolutePaths), WithFixtureTemplating(),
) )
} }
} }
@ -551,7 +552,7 @@ func TestStatus(t *testing.T) {
task.WithVerbose(true), task.WithVerbose(true),
), ),
WithTask("gen-silent-baz"), WithTask("gen-silent-baz"),
WithPostProcessFn(PPRemoveAbsolutePaths), WithFixtureTemplating(),
) )
} }
@ -777,7 +778,7 @@ func TestForCmds(t *testing.T) {
task.WithForce(true), task.WithForce(true),
), ),
WithTask(test.name), WithTask(test.name),
WithPostProcessFn(PPRemoveAbsolutePaths), WithFixtureTemplating(),
} }
if test.wantErr { if test.wantErr {
opts = append(opts, WithRunError()) opts = append(opts, WithRunError())
@ -822,7 +823,7 @@ func TestForDeps(t *testing.T) {
task.WithOutputStyle(ast.Output{Name: "group"}), task.WithOutputStyle(ast.Output{Name: "group"}),
), ),
WithTask(test.name), WithTask(test.name),
WithPostProcessFn(PPRemoveAbsolutePaths), WithFixtureTemplating(),
WithPostProcessFn(PPSortedLines), WithPostProcessFn(PPSortedLines),
} }
if test.wantErr { if test.wantErr {

View File

@ -44,7 +44,8 @@ func NewFormatterTest(t *testing.T, opts ...FormatterTestOption) {
task: "default", task: "default",
vars: map[string]any{}, vars: map[string]any{},
TaskTest: TaskTest{ TaskTest: TaskTest{
experiments: map[*experiments.Experiment]int{}, experiments: map[*experiments.Experiment]int{},
fixtureTemplateData: map[string]any{},
}, },
} }
// Apply the functional options // Apply the functional options
@ -222,8 +223,6 @@ func TestListDescInterpolation(t *testing.T) {
func TestJsonListFormat(t *testing.T) { func TestJsonListFormat(t *testing.T) {
t.Parallel() t.Parallel()
fp, err := filepath.Abs("testdata/json_list_format/Taskfile.yml")
require.NoError(t, err)
NewFormatterTest(t, NewFormatterTest(t,
WithExecutorOptions( WithExecutorOptions(
task.WithDir("testdata/json_list_format"), task.WithDir("testdata/json_list_format"),
@ -231,10 +230,6 @@ func TestJsonListFormat(t *testing.T) {
WithListOptions(task.ListOptions{ WithListOptions(task.ListOptions{
FormatTaskListAsJSON: true, FormatTaskListAsJSON: true,
}), }),
WithFixtureTemplateData(struct { WithFixtureTemplating(),
TaskfileLocation string
}{
TaskfileLocation: fp,
}),
) )
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
"maps"
rand "math/rand/v2" rand "math/rand/v2"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -42,10 +43,11 @@ type (
FormatterTestOption FormatterTestOption
} }
TaskTest struct { TaskTest struct {
name string name string
experiments map[*experiments.Experiment]int experiments map[*experiments.Experiment]int
postProcessFns []PostProcessFn postProcessFns []PostProcessFn
fixtureTemplateData any fixtureTemplateData map[string]any
fixtureTemplatingEnabled bool
} }
) )
@ -80,8 +82,19 @@ func (tt *TaskTest) writeFixture(
if goldenFileSuffix != "" { if goldenFileSuffix != "" {
goldenFileName += "-" + goldenFileSuffix goldenFileName += "-" + goldenFileSuffix
} }
if tt.fixtureTemplateData != nil { // Create a set of data to be made available to every test fixture
g.AssertWithTemplate(t, goldenFileName, tt.fixtureTemplateData, b) 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 { } else {
g.Assert(t, goldenFileName, b) g.Assert(t, goldenFileName, b)
} }
@ -239,24 +252,44 @@ func (opt *setupErrorTestOption) applyToFormatterTest(t *FormatterTest) {
t.wantSetupError = true t.wantSetupError = true
} }
// WithFixtureTemplateData sets up data defined in the golden file using golang // WithFixtureTemplating enables templating for the golden fixture files with
// template. Useful if the golden file can change depending on the test. // the default set of data. This is useful if the golden file is dynamic in some
// Example template: {{ .Value }} // way (e.g. contains user-specific directories). To add more data, see
// Example data definition: struct{ Value string }{Value: "value"} // WithFixtureTemplateData.
func WithFixtureTemplateData(data any) TestOption { func WithFixtureTemplating() TestOption {
return &fixtureTemplateDataTestOption{data: data} 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 { type fixtureTemplateDataTestOption struct {
data any k string
v any
} }
func (opt *fixtureTemplateDataTestOption) applyToExecutorTest(t *ExecutorTest) { 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) { func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest) {
t.fixtureTemplateData = opt.data t.fixtureTemplatingEnabled = true
t.fixtureTemplateData[opt.k] = opt.v
} }
// Post-processing // Post-processing
@ -265,17 +298,6 @@ func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest)
// fixture before the file is written. // fixture before the file is written.
type PostProcessFn func(*testing.T, []byte) []byte 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 // 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 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). // the same each time the task is run (e.g. when running tasks in parallel).

View File

@ -1 +1 @@
task: Missing schema version in Taskfile "/testdata/empty_taskfile/Taskfile.yml" task: Missing schema version in Taskfile "{{.TEST_DIR}}/testdata/empty_taskfile/Taskfile.yml"

View File

@ -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 matrix reference ".NOT_A_LIST" must resolve to a list

View File

@ -1,2 +1,2 @@
matrix reference ".NOT_A_LIST" must resolve to a list 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:

View File

@ -10,9 +10,9 @@
"location": { "location": {
"line": 4, "line": 4,
"column": 3, "column": 3,
"taskfile": "{{ .TaskfileLocation }}" "taskfile": "{{.TEST_DIR}}/testdata/json_list_format/Taskfile.yml"
} }
} }
], ],
"location": "{{ .TaskfileLocation }}" "location": "{{.TEST_DIR}}/testdata/json_list_format/Taskfile.yml"
} }

View File

@ -1 +1 @@
/testdata/special_vars {{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/included {{.TEST_DIR}}/testdata/special_vars/included

View File

@ -1 +1 @@
/testdata/special_vars/included/Taskfile.yml {{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml

View File

@ -1 +1 @@
/testdata/special_vars {{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/foo {{.TEST_DIR}}/testdata/special_vars/foo

View File

@ -1 +1 @@
/testdata/special_vars {{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/Taskfile.yml {{.TEST_DIR}}/testdata/special_vars/Taskfile.yml

View File

@ -1 +1 @@
/testdata/special_vars {{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/included {{.TEST_DIR}}/testdata/special_vars/included

View File

@ -1 +1 @@
/testdata/special_vars/included/Taskfile.yml {{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml

View File

@ -1 +1 @@
/testdata/special_vars {{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/foo {{.TEST_DIR}}/testdata/special_vars/foo

View File

@ -1 +1 @@
/testdata/special_vars {{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/Taskfile.yml {{.TEST_DIR}}/testdata/special_vars/Taskfile.yml