From 1261272b7da37810f0f07e39f6e07bcd2ff5db5b Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Sun, 12 May 2024 19:29:20 +0000 Subject: [PATCH] fix: included variable merging --- task_test.go | 28 +++++++++++++++++++ taskfile/ast/tasks.go | 2 +- .../Taskfile.yaml | 12 ++++++++ .../bar/Taskfile.yaml | 11 ++++++++ .../foo/Taskfile.yaml | 11 ++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 testdata/included_taskfile_var_merging/Taskfile.yaml create mode 100644 testdata/included_taskfile_var_merging/bar/Taskfile.yaml create mode 100644 testdata/included_taskfile_var_merging/foo/Taskfile.yaml diff --git a/task_test.go b/task_test.go index 3286eb46de..94c11ed78c 100644 --- a/task_test.go +++ b/task_test.go @@ -1237,6 +1237,34 @@ func TestIncludesInterpolation(t *testing.T) { } } +func TestIncludedTaskfileVarMerging(t *testing.T) { + const dir = "testdata/included_taskfile_var_merging" + tests := []struct { + name string + task string + expectedOutput string + }{ + {"foo", "foo:pwd", "included_taskfile_var_merging/foo\n"}, + {"bar", "bar:pwd", "included_taskfile_var_merging/bar\n"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + var buff bytes.Buffer + e := task.Executor{ + Dir: dir, + Stdout: &buff, + Stderr: &buff, + Silent: true, + } + require.NoError(t, e.Setup()) + + err := e.Run(context.Background(), &ast.Call{Task: test.task}) + require.NoError(t, err) + assert.Contains(t, buff.String(), test.expectedOutput) + }) + } +} + func TestInternalTask(t *testing.T) { const dir = "testdata/internal_task" tests := []struct { diff --git a/taskfile/ast/tasks.go b/taskfile/ast/tasks.go index 79d7606d43..44dcd9d87a 100644 --- a/taskfile/ast/tasks.go +++ b/taskfile/ast/tasks.go @@ -90,7 +90,7 @@ func (t1 *Tasks) Merge(t2 Tasks, include *Include, includedTaskfileVars *Vars) { task.IncludeVars = &Vars{} } task.IncludeVars.Merge(include.Vars, nil) - task.IncludedTaskfileVars = includedTaskfileVars + task.IncludedTaskfileVars = includedTaskfileVars.DeepCopy() } // Add the task to the merged taskfile diff --git a/testdata/included_taskfile_var_merging/Taskfile.yaml b/testdata/included_taskfile_var_merging/Taskfile.yaml new file mode 100644 index 0000000000..6ea01adaac --- /dev/null +++ b/testdata/included_taskfile_var_merging/Taskfile.yaml @@ -0,0 +1,12 @@ +version: "3" + +includes: + foo: + taskfile: ./foo/Taskfile.yaml + bar: + taskfile: ./bar/Taskfile.yaml + +tasks: + stub: + cmds: + - echo 0 diff --git a/testdata/included_taskfile_var_merging/bar/Taskfile.yaml b/testdata/included_taskfile_var_merging/bar/Taskfile.yaml new file mode 100644 index 0000000000..afc5a94bdc --- /dev/null +++ b/testdata/included_taskfile_var_merging/bar/Taskfile.yaml @@ -0,0 +1,11 @@ +version: "3" + +vars: + DIR: bar + +tasks: + pwd: + dir: ./{{ .DIR }} + cmds: + - echo "{{ .DIR }}" + - pwd diff --git a/testdata/included_taskfile_var_merging/foo/Taskfile.yaml b/testdata/included_taskfile_var_merging/foo/Taskfile.yaml new file mode 100644 index 0000000000..691755756c --- /dev/null +++ b/testdata/included_taskfile_var_merging/foo/Taskfile.yaml @@ -0,0 +1,11 @@ +version: "3" + +vars: + DIR: foo + +tasks: + pwd: + dir: ./{{ .DIR }} + cmds: + - echo "{{ .DIR }}" + - pwd