diff --git a/formatter_test.go b/formatter_test.go index 51f10d92..2db067b2 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -218,3 +218,23 @@ 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, + }), + ) +} diff --git a/help.go b/help.go index 02656d13..2c687a97 100644 --- a/help.go +++ b/help.go @@ -149,6 +149,7 @@ func (e *Executor) ToEditorOutput(tasks []*ast.Task, noStatus bool) (*editors.Ta g.Go(func() error { o.Tasks[i] = editors.Task{ Name: tasks[i].Name(), + Task: tasks[i].Task, Desc: tasks[i].Desc, Summary: tasks[i].Summary, Aliases: aliases, diff --git a/internal/editors/output.go b/internal/editors/output.go index 42b893a1..9a6090b3 100644 --- a/internal/editors/output.go +++ b/internal/editors/output.go @@ -9,6 +9,7 @@ type ( // Task describes a single task Task struct { Name string `json:"name"` + Task string `json:"task"` Desc string `json:"desc"` Summary string `json:"summary"` Aliases []string `json:"aliases"` diff --git a/task_test.go b/task_test.go index 711d6724..9ae65847 100644 --- a/task_test.go +++ b/task_test.go @@ -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 diff --git a/testdata/json_list_format/Taskfile.yml b/testdata/json_list_format/Taskfile.yml new file mode 100644 index 00000000..5c7db94b --- /dev/null +++ b/testdata/json_list_format/Taskfile.yml @@ -0,0 +1,6 @@ +version: '3' + +tasks: + foo: + label: "foobar" + desc: "task description" diff --git a/testdata/json_list_format/testdata/TestJsonListFormat.golden b/testdata/json_list_format/testdata/TestJsonListFormat.golden new file mode 100644 index 00000000..44f05284 --- /dev/null +++ b/testdata/json_list_format/testdata/TestJsonListFormat.golden @@ -0,0 +1,18 @@ +{ + "tasks": [ + { + "name": "foobar", + "task": "foo", + "desc": "task description", + "summary": "", + "aliases": [], + "up_to_date": false, + "location": { + "line": 4, + "column": 3, + "taskfile": "{{ .TaskfileLocation }}" + } + } + ], + "location": "{{ .TaskfileLocation }}" +} diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index e5094a9a..55ab128f 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -104,6 +104,7 @@ structure: "tasks": [ { "name": "", + "task": "", "desc": "", "summary": "", "up_to_date": false,