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:
@ -51,6 +51,7 @@ func NewExecutorTest(t *testing.T, opts ...ExecutorTestOption) {
|
||||
vars: map[string]any{},
|
||||
TaskTest: TaskTest{
|
||||
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 {
|
||||
|
@ -45,6 +45,7 @@ func NewFormatterTest(t *testing.T, opts ...FormatterTestOption) {
|
||||
vars: map[string]any{},
|
||||
TaskTest: TaskTest{
|
||||
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(),
|
||||
)
|
||||
}
|
||||
|
66
task_test.go
66
task_test.go
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"maps"
|
||||
rand "math/rand/v2"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@ -45,7 +46,8 @@ type (
|
||||
name string
|
||||
experiments map[*experiments.Experiment]int
|
||||
postProcessFns []PostProcessFn
|
||||
fixtureTemplateData any
|
||||
fixtureTemplateData map[string]any
|
||||
fixtureTemplatingEnabled bool
|
||||
}
|
||||
)
|
||||
|
||||
@ -80,8 +82,19 @@ func (tt *TaskTest) writeFixture(
|
||||
if goldenFileSuffix != "" {
|
||||
goldenFileName += "-" + goldenFileSuffix
|
||||
}
|
||||
// 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 {
|
||||
g.AssertWithTemplate(t, goldenFileName, tt.fixtureTemplateData, b)
|
||||
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).
|
||||
|
@ -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"
|
@ -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
|
@ -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:
|
||||
|
@ -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"
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars
|
||||
{{.TEST_DIR}}/testdata/special_vars
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/included
|
||||
{{.TEST_DIR}}/testdata/special_vars/included
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/included/Taskfile.yml
|
||||
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars
|
||||
{{.TEST_DIR}}/testdata/special_vars
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/foo
|
||||
{{.TEST_DIR}}/testdata/special_vars/foo
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars
|
||||
{{.TEST_DIR}}/testdata/special_vars
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/Taskfile.yml
|
||||
{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars
|
||||
{{.TEST_DIR}}/testdata/special_vars
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/included
|
||||
{{.TEST_DIR}}/testdata/special_vars/included
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/included/Taskfile.yml
|
||||
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars
|
||||
{{.TEST_DIR}}/testdata/special_vars
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/foo
|
||||
{{.TEST_DIR}}/testdata/special_vars/foo
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars
|
||||
{{.TEST_DIR}}/testdata/special_vars
|
||||
|
@ -1 +1 @@
|
||||
/testdata/special_vars/Taskfile.yml
|
||||
{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml
|
||||
|
Reference in New Issue
Block a user