mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
Merge branch 'dballweg/vars_in_includedtaskfile' of https://github.com/dballweg/task into dballweg-dballweg/vars_in_includedtaskfile
This commit is contained in:
29
task_test.go
29
task_test.go
@@ -1218,3 +1218,32 @@ Bye!
|
|||||||
t.Log(buff.String())
|
t.Log(buff.String())
|
||||||
assert.Equal(t, strings.TrimSpace(buff.String()), expectedOutputOrder)
|
assert.Equal(t, strings.TrimSpace(buff.String()), expectedOutputOrder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIncludedVars(t *testing.T) {
|
||||||
|
const dir = "testdata/include_with_vars"
|
||||||
|
var buff bytes.Buffer
|
||||||
|
e := task.Executor{
|
||||||
|
Dir: dir,
|
||||||
|
Stdout: &buff,
|
||||||
|
Stderr: &buff,
|
||||||
|
}
|
||||||
|
assert.NoError(t, e.Setup())
|
||||||
|
|
||||||
|
expectedOutputOrder := strings.TrimSpace(`
|
||||||
|
task: [included1:task1] echo "VAR_1 is included1-var1"
|
||||||
|
VAR_1 is included1-var1
|
||||||
|
task: [included1:task1] echo "VAR_2 is incldued-default-var2"
|
||||||
|
VAR_2 is incldued-default-var2
|
||||||
|
task: [included2:task1] echo "VAR_1 is included2-var1"
|
||||||
|
VAR_1 is included2-var1
|
||||||
|
task: [included2:task1] echo "VAR_2 is incldued-default-var2"
|
||||||
|
VAR_2 is incldued-default-var2
|
||||||
|
task: [included3:task1] echo "VAR_1 is included-default-var1"
|
||||||
|
VAR_1 is included-default-var1
|
||||||
|
task: [included3:task1] echo "VAR_2 is incldued-default-var2"
|
||||||
|
VAR_2 is incldued-default-var2
|
||||||
|
`)
|
||||||
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task1"}))
|
||||||
|
t.Log(buff.String())
|
||||||
|
assert.Equal(t, strings.TrimSpace(buff.String()), expectedOutputOrder)
|
||||||
|
}
|
||||||
|
@@ -12,6 +12,7 @@ type IncludedTaskfile struct {
|
|||||||
Dir string
|
Dir string
|
||||||
Optional bool
|
Optional bool
|
||||||
AdvancedImport bool
|
AdvancedImport bool
|
||||||
|
Vars *Vars
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncludedTaskfiles represents information about included tasksfiles
|
// IncludedTaskfiles represents information about included tasksfiles
|
||||||
@@ -94,6 +95,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err
|
|||||||
Taskfile string
|
Taskfile string
|
||||||
Dir string
|
Dir string
|
||||||
Optional bool
|
Optional bool
|
||||||
|
Vars *Vars
|
||||||
}
|
}
|
||||||
if err := unmarshal(&includedTaskfile); err != nil {
|
if err := unmarshal(&includedTaskfile); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -102,5 +104,6 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err
|
|||||||
it.Dir = includedTaskfile.Dir
|
it.Dir = includedTaskfile.Dir
|
||||||
it.Optional = includedTaskfile.Optional
|
it.Optional = includedTaskfile.Optional
|
||||||
it.AdvancedImport = true
|
it.AdvancedImport = true
|
||||||
|
it.Vars = includedTaskfile.Vars
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -62,6 +62,7 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
|
|||||||
Dir: tr.Replace(includedTask.Dir),
|
Dir: tr.Replace(includedTask.Dir),
|
||||||
Optional: includedTask.Optional,
|
Optional: includedTask.Optional,
|
||||||
AdvancedImport: includedTask.AdvancedImport,
|
AdvancedImport: includedTask.AdvancedImport,
|
||||||
|
Vars: includedTask.Vars,
|
||||||
}
|
}
|
||||||
if err := tr.Err(); err != nil {
|
if err := tr.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -109,6 +110,12 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, task := range includedTaskfile.Tasks {
|
for _, task := range includedTaskfile.Tasks {
|
||||||
|
if includedTask.Vars != nil {
|
||||||
|
if task.Vars == nil {
|
||||||
|
task.Vars = &taskfile.Vars{}
|
||||||
|
}
|
||||||
|
task.Vars.Merge(includedTask.Vars)
|
||||||
|
}
|
||||||
if !filepath.IsAbs(task.Dir) {
|
if !filepath.IsAbs(task.Dir) {
|
||||||
task.Dir = filepath.Join(includedTask.Dir, task.Dir)
|
task.Dir = filepath.Join(includedTask.Dir, task.Dir)
|
||||||
}
|
}
|
||||||
|
20
testdata/include_with_vars/Taskfile.yml
vendored
Normal file
20
testdata/include_with_vars/Taskfile.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
includes:
|
||||||
|
included1:
|
||||||
|
taskfile: include/Taskfile.include.yml
|
||||||
|
vars:
|
||||||
|
VAR_1: included1-var1
|
||||||
|
included2:
|
||||||
|
taskfile: include/Taskfile.include.yml
|
||||||
|
vars:
|
||||||
|
VAR_1: included2-var1
|
||||||
|
included3:
|
||||||
|
taskfile: include/Taskfile.include.yml
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
task1:
|
||||||
|
cmds:
|
||||||
|
- task: included1:task1
|
||||||
|
- task: included2:task1
|
||||||
|
- task: included3:task1
|
11
testdata/include_with_vars/include/Taskfile.include.yml
vendored
Normal file
11
testdata/include_with_vars/include/Taskfile.include.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
vars:
|
||||||
|
VAR_1: included-default-var1
|
||||||
|
VAR_2: incldued-default-var2
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
task1:
|
||||||
|
cmds:
|
||||||
|
- echo "VAR_1 is {{.VAR_1}}"
|
||||||
|
- echo "VAR_2 is {{.VAR_2}}"
|
Reference in New Issue
Block a user