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:
74
task_test.go
74
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).
|
||||
|
Reference in New Issue
Block a user