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

feat: add task name to json output (#2256)

This commit is contained in:
Aleksander Sh.
2025-05-20 22:37:57 +02:00
committed by GitHub
parent 89f29cb75b
commit bb5b045293
7 changed files with 76 additions and 4 deletions

View File

@ -42,9 +42,10 @@ type (
FormatterTestOption
}
TaskTest struct {
name string
experiments map[*experiments.Experiment]int
postProcessFns []PostProcessFn
name string
experiments map[*experiments.Experiment]int
postProcessFns []PostProcessFn
fixtureTemplateData any
}
)
@ -79,7 +80,11 @@ func (tt *TaskTest) writeFixture(
if goldenFileSuffix != "" {
goldenFileName += "-" + goldenFileSuffix
}
g.Assert(t, goldenFileName, b)
if tt.fixtureTemplateData != nil {
g.AssertWithTemplate(t, goldenFileName, tt.fixtureTemplateData, b)
} else {
g.Assert(t, goldenFileName, b)
}
}
// writeFixtureBuffer is a wrapper for writing the main output of the task to a
@ -234,6 +239,26 @@ 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}
}
type fixtureTemplateDataTestOption struct {
data any
}
func (opt *fixtureTemplateDataTestOption) applyToExecutorTest(t *ExecutorTest) {
t.fixtureTemplateData = opt.data
}
func (opt *fixtureTemplateDataTestOption) applyToFormatterTest(t *FormatterTest) {
t.fixtureTemplateData = opt.data
}
// Post-processing
// A PostProcessFn is a function that can be applied to the output of a test