1
0
mirror of https://github.com/go-task/task.git synced 2025-08-06 22:33:10 +02:00

chore: replace PPRemoveAbsolutePaths with generic fixture template data (#2265)

* chore: replace PPRemoveAbsolutePaths with generic fixture template data

* chore: update to goldie v2.7.1
This commit is contained in:
Pete Davison
2025-07-23 21:57:25 +01:00
committed by GitHub
parent dac5aa1954
commit 1c6d686356
24 changed files with 83 additions and 76 deletions

View File

@ -53,9 +53,12 @@ tasks:
generate:fixtures:
desc: Runs tests and generates golden fixture files
aliases: [gen:fixtures, g:fixtures]
env:
GOLDIE_UPDATE: 'true'
GOLDIE_TEMPLATE: 'true'
cmds:
- find ./testdata -name '*.golden' -delete
- go test -update ./...
- go test ./...
install:mockery:
desc: Installs mockgen; a tool to generate mock files

View File

@ -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
@ -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 {
@ -984,6 +985,6 @@ func TestIncludeChecksum(t *testing.T) {
task.WithDir("testdata/includes_checksum/incorrect"),
),
WithSetupError(),
WithPostProcessFn(PPRemoveAbsolutePaths),
WithFixtureTemplating(),
)
}

View File

@ -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
@ -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(),
)
}

14
go.sum
View File

@ -11,8 +11,6 @@ github.com/ProtonMail/go-crypto v1.1.6 h1:ZcV+Ropw6Qn0AX9brlQLAUXfqLBc7Bl+f/DmNx
github.com/ProtonMail/go-crypto v1.1.6/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.18.0 h1:6h53Q4hW83SuF+jcsp7CVhLsMozzvQvO8HBbKQW+gn4=
github.com/alecthomas/chroma/v2 v2.18.0/go.mod h1:RVX6AvYm4VfYe/zsk7mjHueLDZor3aWCNE14TFlepBk=
github.com/alecthomas/chroma/v2 v2.19.0 h1:Im+SLRgT8maArxv81mULDWN8oKxkzboH07CHesxElq4=
github.com/alecthomas/chroma/v2 v2.19.0/go.mod h1:RVX6AvYm4VfYe/zsk7mjHueLDZor3aWCNE14TFlepBk=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
@ -111,8 +109,6 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/sajari/fuzzy v1.0.0 h1:+FmwVvJErsd0d0hAPlj4CxqxUtQY/fOoY0DwX4ykpRY=
github.com/sajari/fuzzy v1.0.0/go.mod h1:OjYR6KxoWOe9+dOlXeiCJd4dIbED4Oo8wpS89o0pwOo=
github.com/sebdah/goldie/v2 v2.5.5 h1:rx1mwF95RxZ3/83sdS4Yp7t2C5TCokvWP4TBRbAyEWY=
github.com/sebdah/goldie/v2 v2.5.5/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sebdah/goldie/v2 v2.7.1 h1:PkBHymaYdtvEkZV7TmyqKxdmn5/Vcj+8TpATWZjnG5E=
github.com/sebdah/goldie/v2 v2.7.1/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
@ -121,8 +117,6 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -147,8 +141,6 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbR
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -160,13 +152,9 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
golang.org/x/term v0.33.0 h1:NuFncQrRcaRvVmgRkvM3j/F00gWIAlcmlB8ACEKmGIg=
golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@ -183,7 +171,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/sh/v3 v3.11.0 h1:q5h+XMDRfUGUedCqFFsjoFjrhwf2Mvtt1rkMvVz0blw=
mvdan.cc/sh/v3 v3.11.0/go.mod h1:LRM+1NjoYCzuq/WZ6y44x14YNAI0NK7FLPeQSaFagGg=
mvdan.cc/sh/v3 v3.12.0 h1:ejKUR7ONP5bb+UGHGEG/k9V5+pRVIyD+LsZz7o8KHrI=
mvdan.cc/sh/v3 v3.12.0/go.mod h1:Se6Cj17eYSn+sNooLZiEUnNNmNxg0imoYlTu4CyaGyg=

View File

@ -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).

View File

@ -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"

View File

@ -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

View File

@ -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:

View File

@ -1,3 +1,3 @@
task: The checksum of the Taskfile at "/testdata/includes_checksum/included.yml" does not match!
task: The checksum of the Taskfile at "{{.TEST_DIR}}/testdata/includes_checksum/included.yml" does not match!
got: "c97f39eb96fe3fa5fe2a610d244b8449897b06f0c93821484af02e0999781bf5"
want: "foo"

View File

@ -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"
}

View File

@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/included
{{.TEST_DIR}}/testdata/special_vars/included

View File

@ -1 +1 @@
/testdata/special_vars/included/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml

View File

@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/foo
{{.TEST_DIR}}/testdata/special_vars/foo

View File

@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml

View File

@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/included
{{.TEST_DIR}}/testdata/special_vars/included

View File

@ -1 +1 @@
/testdata/special_vars/included/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/included/Taskfile.yml

View File

@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/foo
{{.TEST_DIR}}/testdata/special_vars/foo

View File

@ -1 +1 @@
/testdata/special_vars
{{.TEST_DIR}}/testdata/special_vars

View File

@ -1 +1 @@
/testdata/special_vars/Taskfile.yml
{{.TEST_DIR}}/testdata/special_vars/Taskfile.yml