Skip to content

chore: replace PPRemoveAbsolutePaths with generic fixture template data #2265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -232,7 +233,7 @@ func TestEmptyTaskfile(t *testing.T) {
task.WithDir("testdata/empty_taskfile"),
),
WithSetupError(),
WithPostProcessFn(PPRemoveAbsolutePaths),
WithFixtureTemplating(),
)
}

Expand Down Expand Up @@ -367,7 +368,7 @@ func TestSpecialVars(t *testing.T) {
task.WithVersionCheck(true),
),
WithTask(test),
WithPostProcessFn(PPRemoveAbsolutePaths),
WithFixtureTemplating(),
)
}
}
Expand Down Expand Up @@ -551,7 +552,7 @@ func TestStatus(t *testing.T) {
task.WithVerbose(true),
),
WithTask("gen-silent-baz"),
WithPostProcessFn(PPRemoveAbsolutePaths),
WithFixtureTemplating(),
)
}

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 3 additions & 8 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -222,19 +223,13 @@ 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"),
),
WithListOptions(task.ListOptions{
FormatTaskListAsJSON: true,
}),
WithFixtureTemplateData(struct {
TaskfileLocation string
}{
TaskfileLocation: fp,
}),
WithFixtureTemplating(),
)
}
74 changes: 48 additions & 26 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/fs"
"maps"
rand "math/rand/v2"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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
}
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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:
4 changes: 2 additions & 2 deletions testdata/json_list_format/testdata/TestJsonListFormat.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/included
{{.TEST_DIR}}/testdata/special_vars/included
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/included/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/foo
{{.TEST_DIR}}/testdata/special_vars/foo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/included
{{.TEST_DIR}}/testdata/special_vars/included
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/included/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/foo
{{.TEST_DIR}}/testdata/special_vars/foo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/testdata/special_vars/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml
Loading