diff --git a/task_test.go b/task_test.go index 33c3a3099b..14a1b3d63e 100644 --- a/task_test.go +++ b/task_test.go @@ -37,6 +37,18 @@ type SyncBuffer struct { mu sync.Mutex } +func (sb *SyncBuffer) String() string { + sb.mu.Lock() + defer sb.mu.Unlock() + return sb.buf.String() +} + +func (sb *SyncBuffer) Reset() { + sb.mu.Lock() + defer sb.mu.Unlock() + sb.buf.Reset() +} + func (sb *SyncBuffer) Write(p []byte) (n int, err error) { sb.mu.Lock() defer sb.mu.Unlock() @@ -179,7 +191,7 @@ func TestSpecialVars(t *testing.T) { for _, dir := range []string{dir, subdir} { for _, test := range tests { t.Run(test.target, func(t *testing.T) { - var buff bytes.Buffer + var buff SyncBuffer e := &task.Executor{ Dir: dir, Stdout: &buff, @@ -188,7 +200,7 @@ func TestSpecialVars(t *testing.T) { } require.NoError(t, e.Setup()) require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target})) - assert.Equal(t, test.expected+"\n", buff.String()) + assert.Equal(t, test.expected+"\n", buff.buf.String()) }) } } @@ -286,7 +298,7 @@ func TestStatus(t *testing.T) { } } - var buff bytes.Buffer + var buff SyncBuffer e := &task.Executor{ Dir: dir, TempDir: task.TempDir{ @@ -322,7 +334,7 @@ func TestStatus(t *testing.T) { require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "gen-bar"})) // We're silent, so no output should have been produced. - assert.Empty(t, buff.String()) + assert.Empty(t, buff.buf.String()) // Now, let's remove source file, and run the task again to to prepare // for the next test. @@ -371,7 +383,7 @@ func TestStatus(t *testing.T) { func TestPrecondition(t *testing.T) { const dir = "testdata/precondition" - var buff bytes.Buffer + var buff SyncBuffer e := &task.Executor{ Dir: dir, Stdout: &buff, @@ -486,7 +498,7 @@ func TestStatusChecksum(t *testing.T) { require.Error(t, err) } - var buff bytes.Buffer + var buff SyncBuffer tempdir := task.TempDir{ Remote: filepathext.SmartJoin(dir, ".task"), Fingerprint: filepathext.SmartJoin(dir, ".task"), @@ -1735,7 +1747,7 @@ func TestRunOnlyRunsJobsHashOnce(t *testing.T) { func TestRunOnceSharedDeps(t *testing.T) { const dir = "testdata/run_once_shared_deps" - var buff bytes.Buffer + var buff SyncBuffer e := task.Executor{ Dir: dir, Stdout: &buff, @@ -1746,10 +1758,10 @@ func TestRunOnceSharedDeps(t *testing.T) { require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "build"})) rx := regexp.MustCompile(`task: \[service-[a,b]:library:build\] echo "build library"`) - matches := rx.FindAllStringSubmatch(buff.String(), -1) + matches := rx.FindAllStringSubmatch(buff.buf.String(), -1) assert.Len(t, matches, 1) - assert.Contains(t, buff.String(), `task: [service-a:build] echo "build a"`) - assert.Contains(t, buff.String(), `task: [service-b:build] echo "build b"`) + assert.Contains(t, buff.buf.String(), `task: [service-a:build] echo "build a"`) + assert.Contains(t, buff.buf.String(), `task: [service-b:build] echo "build b"`) } func TestDeferredCmds(t *testing.T) { @@ -2412,8 +2424,8 @@ func TestForCmds(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - var stdOut bytes.Buffer - var stdErr bytes.Buffer + var stdOut SyncBuffer + var stdErr SyncBuffer e := task.Executor{ Dir: "testdata/for/cmds", Stdout: &stdOut, @@ -2423,7 +2435,7 @@ func TestForCmds(t *testing.T) { } require.NoError(t, e.Setup()) require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.name})) - assert.Equal(t, test.expectedOutput, stdOut.String()) + assert.Equal(t, test.expectedOutput, stdOut.buf.String()) }) } } diff --git a/taskfile/ast/task.go b/taskfile/ast/task.go index de6986c6b8..39543188b5 100644 --- a/taskfile/ast/task.go +++ b/taskfile/ast/task.go @@ -43,7 +43,7 @@ type Task struct { Watch bool Location *Location // Populated during merging - Namespace string + Namespace []string IncludeVars *Vars IncludedTaskfileVars *Vars } @@ -57,8 +57,10 @@ func (t *Task) Name() string { func (t *Task) LocalName() string { name := t.Task - name = strings.TrimPrefix(name, t.Namespace) - name = strings.TrimPrefix(name, ":") + for _, namespace := range t.Namespace { + name = strings.TrimPrefix(name, namespace) + name = strings.TrimPrefix(name, ":") + } return name } @@ -219,7 +221,7 @@ func (t *Task) DeepCopy() *Task { Platforms: deepcopy.Slice(t.Platforms), Location: t.Location.DeepCopy(), Requires: t.Requires.DeepCopy(), - Namespace: t.Namespace, + Namespace: append([]string{}, t.Namespace...), } return c } diff --git a/taskfile/ast/tasks.go b/taskfile/ast/tasks.go index cfe29a8da3..6cf356c55a 100644 --- a/taskfile/ast/tasks.go +++ b/taskfile/ast/tasks.go @@ -87,7 +87,7 @@ func (t1 *Tasks) Merge(t2 Tasks, include *Include, includedTaskfileVars *Vars) e } taskName = taskNameWithNamespace(name, include.Namespace) - task.Namespace = include.Namespace + task.Namespace = append([]string{include.Namespace}, task.Namespace...) task.Task = taskName }