feat(override): allow adding includes in a Taskfile override
This commit is contained in:
parent
337677d9cc
commit
fd07f9ddfe
4 changed files with 42 additions and 26 deletions
|
|
@ -7,8 +7,9 @@ import (
|
|||
"github.com/go-task/task/v3/errors"
|
||||
)
|
||||
|
||||
// MergeOverride applies a local override: vars and env merge by key, dotenv
|
||||
// files take priority over the main ones, tasks are replaced whole.
|
||||
// MergeOverride applies a local override: includes are added, vars and env
|
||||
// merge by key, dotenv files take priority over the main ones, tasks are
|
||||
// replaced whole.
|
||||
func (tf *Taskfile) MergeOverride(override *Taskfile) error {
|
||||
// An older version is fine, so bumping the shared Taskfile doesn't break
|
||||
// existing overrides.
|
||||
|
|
@ -19,6 +20,12 @@ func (tf *Taskfile) MergeOverride(override *Taskfile) error {
|
|||
Message: fmt.Sprintf("must be a v%d version no newer than the main Taskfile (%s)", tf.Version.Major(), tf.Version),
|
||||
}
|
||||
}
|
||||
for namespace, include := range override.Includes.All() {
|
||||
if _, ok := tf.Includes.Get(namespace); ok {
|
||||
return fmt.Errorf("task: Taskfile override %q can't redefine include %q", override.Location, namespace)
|
||||
}
|
||||
tf.Includes.Set(namespace, include)
|
||||
}
|
||||
if tf.Vars == nil {
|
||||
tf.Vars = NewVars()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ func (tf *taskfileOverride) UnmarshalYAML(node *yaml.Node) error {
|
|||
for i := 0; i < len(node.Content); i += 2 {
|
||||
key := node.Content[i]
|
||||
switch key.Value {
|
||||
case "version", "vars", "env", "dotenv", "tasks":
|
||||
case "version", "includes", "vars", "env", "dotenv", "tasks":
|
||||
default:
|
||||
return errors.NewTaskfileDecodeError(nil, key).
|
||||
WithMessage("field %q is not allowed in a Taskfile override", key.Value)
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ func TestAutomaticOverrideErrors(t *testing.T) {
|
|||
{"older major version", "version: '2'", "(2.0.0) must be a v3 version"},
|
||||
{"sequence", "- version: '3'", "taskfile override"},
|
||||
}
|
||||
for _, key := range []string{"includes", "unknown", "<<"} {
|
||||
for _, key := range []string{"output", "unknown", "<<"} {
|
||||
tests = append(tests, struct{ name, content, message string }{key, "version: '3'\n" + key + ": null", "not allowed"})
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
|
@ -169,29 +169,34 @@ func TestAutomaticOverrideReadError(t *testing.T) {
|
|||
|
||||
func TestAutomaticOverrideIncludes(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, conflict := range []bool{false, true} {
|
||||
t.Run(fmt.Sprint(conflict), func(t *testing.T) {
|
||||
const main = "version: '3'\nvars: {INCLUDE: missing}\nincludes:\n docs: '{{.INCLUDE}}/Taskfile.yml'\n"
|
||||
tests := []struct {
|
||||
name, main, override, task, taskfile, err string
|
||||
}{
|
||||
{name: "var picks include", main: main, override: "vars: {INCLUDE: docs}\n", task: "docs:build", taskfile: "docs/Taskfile.yml"},
|
||||
{name: "task conflicts with include", main: main, override: "vars: {INCLUDE: docs}\ntasks:\n docs:build: echo local\n", err: "docs:build"},
|
||||
{name: "added include", main: "version: '3'\n", override: "includes:\n extra: extra/Taskfile.yml\n", task: "extra:build", taskfile: "extra/Taskfile.yml"},
|
||||
{name: "redefined include", main: main, override: "includes:\n docs: extra/Taskfile.yml\n", err: `can't redefine include "docs"`},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
writeOverrideFixture(t, dir, "Taskfile.yml", "version: '3'\nvars: {INCLUDE: missing}\nincludes:\n docs: '{{.INCLUDE}}/Taskfile.yml'\n")
|
||||
override := "version: '3'\nvars: {INCLUDE: docs}\n"
|
||||
if conflict {
|
||||
override += "tasks:\n docs:build: echo local\n"
|
||||
writeOverrideFixture(t, dir, "Taskfile.yml", tt.main)
|
||||
writeOverrideFixture(t, dir, "Taskfile.override.yml", "version: '3'\n"+tt.override)
|
||||
for _, sub := range []string{"docs", "extra"} {
|
||||
writeOverrideFixture(t, dir, sub+"/Taskfile.yml", "version: '3'\ntasks:\n build: echo "+sub+"\n")
|
||||
writeOverrideFixture(t, dir, sub+"/Taskfile.override.yml", "invalid YAML: [")
|
||||
}
|
||||
writeOverrideFixture(t, dir, "Taskfile.override.yml", override)
|
||||
included := writeOverrideFixture(t, dir, "docs/Taskfile.yml", "version: '3'\ntasks:\n build: echo included\n")
|
||||
writeOverrideFixture(t, dir, "docs/Taskfile.override.yml", "invalid YAML: [")
|
||||
tf, err := readOverrideFixture(t, dir, WithAutomaticOverride(true))
|
||||
if conflict {
|
||||
var conflictErr *errors.TaskNameFlattenConflictError
|
||||
require.ErrorAs(t, err, &conflictErr)
|
||||
assert.Equal(t, "docs:build", conflictErr.TaskName)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
build, ok := tf.Tasks.Get("docs:build")
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, included, build.Location.Taskfile)
|
||||
if tt.err != "" {
|
||||
require.ErrorContains(t, err, tt.err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
task, ok := tf.Tasks.Get(tt.task)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, filepath.Join(dir, tt.taskfile), task.Location.Taskfile)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -182,9 +182,13 @@ vars:
|
|||
`task dev` now prints `PORT=4000`. Add `Taskfile.override.yml` to `.gitignore`
|
||||
so it stays on your machine.
|
||||
|
||||
An override can set `vars`, `env`, `dotenv` and `tasks`. Its `version` can't be
|
||||
newer than the main Taskfile's, so `version: '3'` keeps working when the main
|
||||
Taskfile bumps its version:
|
||||
An override can set `includes`, `vars`, `env`, `dotenv` and `tasks`. Its
|
||||
`version` can't be newer than the main Taskfile's, so `version: '3'` keeps
|
||||
working when the main Taskfile bumps its version:
|
||||
|
||||
- Includes are added, for example your own tasks under
|
||||
`includes: {me: ~/tasks/Taskfile.yml}`. Redefining an include of the main
|
||||
Taskfile is an error; to point it elsewhere, have its path read a variable.
|
||||
|
||||
- Variables and environment variables are merged: yours win, the others are
|
||||
kept.
|
||||
|
|
@ -195,7 +199,7 @@ Taskfile bumps its version:
|
|||
value of a shared task, have the main Taskfile read it from a variable, then
|
||||
override that variable.
|
||||
|
||||
Anything else at the root, such as `includes` or `output`, is rejected.
|
||||
Anything else at the root, such as `output` or `silent`, is rejected.
|
||||
|
||||
::: tip
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue