From 3507fa40f1f71f6e7425a304e12865feb37cba3f Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Fri, 22 Jul 2022 02:16:14 +0000 Subject: [PATCH] feat: add internal to included files --- task_test.go | 40 ++++++++++++++++++++++++ taskfile/included_taskfile.go | 3 ++ taskfile/merge.go | 4 ++- taskfile/read/taskfile.go | 5 +-- testdata/includes_internal/Taskfile.yml | 16 ++++++++++ testdata/includes_internal/Taskfile2.yml | 7 +++++ 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 testdata/includes_internal/Taskfile.yml create mode 100644 testdata/includes_internal/Taskfile2.yml diff --git a/task_test.go b/task_test.go index 4c252067..9b933dbb 100644 --- a/task_test.go +++ b/task_test.go @@ -929,6 +929,46 @@ func TestIncludesRelativePath(t *testing.T) { assert.Contains(t, buff.String(), "testdata/includes_rel_path/common") } +func TestIncludesInternal(t *testing.T) { + const dir = "testdata/internal_task" + tests := []struct { + name string + task string + expectedErr bool + expectedOutput string + }{ + {"included internal task via task", "task-1", false, "Hello, World!\n"}, + {"included internal task via dep", "task-2", false, "Hello, World!\n"}, + { + "included internal direct", + "included:task-3", + true, + "task: No tasks with description available. Try --list-all to list all tasks\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, + } + assert.NoError(t, e.Setup()) + + err := e.Run(context.Background(), taskfile.Call{Task: test.task}) + if test.expectedErr { + assert.Error(t, err, test.expectedErr) + } else { + assert.NoError(t, err) + } + assert.Equal(t, test.expectedOutput, buff.String()) + }) + } +} + func TestInternalTask(t *testing.T) { const dir = "testdata/internal_task" tests := []struct { diff --git a/taskfile/included_taskfile.go b/taskfile/included_taskfile.go index ec4ade08..fe83bd7d 100644 --- a/taskfile/included_taskfile.go +++ b/taskfile/included_taskfile.go @@ -16,6 +16,7 @@ type IncludedTaskfile struct { Taskfile string Dir string Optional bool + Internal bool AdvancedImport bool Vars *Vars BaseDir string // The directory from which the including taskfile was loaded; used to resolve relative paths @@ -101,6 +102,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err Taskfile string Dir string Optional bool + Internal bool Vars *Vars } if err := unmarshal(&includedTaskfile); err != nil { @@ -109,6 +111,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err it.Taskfile = includedTaskfile.Taskfile it.Dir = includedTaskfile.Dir it.Optional = includedTaskfile.Optional + it.Internal = includedTaskfile.Internal it.AdvancedImport = true it.Vars = includedTaskfile.Vars return nil diff --git a/taskfile/merge.go b/taskfile/merge.go index a5731c71..12f3feae 100644 --- a/taskfile/merge.go +++ b/taskfile/merge.go @@ -9,7 +9,7 @@ import ( const NamespaceSeparator = ":" // Merge merges the second Taskfile into the first -func Merge(t1, t2 *Taskfile, namespaces ...string) error { +func Merge(t1, t2 *Taskfile, internal bool, namespaces ...string) error { if t1.Version != t2.Version { return fmt.Errorf(`task: Taskfiles versions should match. First is "%s" but second is "%s"`, t1.Version, t2.Version) } @@ -43,6 +43,8 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error { // have serious side-effects in the future, since we're editing // the original references instead of deep copying them. + v.Internal = internal + t1.Tasks[taskNameWithNamespace(k, namespaces...)] = v for _, dep := range v.Deps { diff --git a/taskfile/read/taskfile.go b/taskfile/read/taskfile.go index 285df4b7..59d70cc1 100644 --- a/taskfile/read/taskfile.go +++ b/taskfile/read/taskfile.go @@ -78,6 +78,7 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { Taskfile: tr.Replace(includedTask.Taskfile), Dir: tr.Replace(includedTask.Dir), Optional: includedTask.Optional, + Internal: includedTask.Internal, AdvancedImport: includedTask.AdvancedImport, Vars: includedTask.Vars, BaseDir: includedTask.BaseDir, @@ -147,7 +148,7 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { } } - if err = taskfile.Merge(t, includedTaskfile, namespace); err != nil { + if err = taskfile.Merge(t, includedTaskfile, includedTask.Internal, namespace); err != nil { return err } return nil @@ -163,7 +164,7 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { if err != nil { return nil, err } - if err = taskfile.Merge(t, osTaskfile); err != nil { + if err = taskfile.Merge(t, osTaskfile, false); err != nil { return nil, err } } diff --git a/testdata/includes_internal/Taskfile.yml b/testdata/includes_internal/Taskfile.yml new file mode 100644 index 00000000..64121323 --- /dev/null +++ b/testdata/includes_internal/Taskfile.yml @@ -0,0 +1,16 @@ +version: '3' + +includes: + included: + taskfile: Taskfile2.yml + internal: true + +tasks: + + task-1: + cmds: + - task: included:default + + task-2: + deps: + - included:default diff --git a/testdata/includes_internal/Taskfile2.yml b/testdata/includes_internal/Taskfile2.yml new file mode 100644 index 00000000..dce136f0 --- /dev/null +++ b/testdata/includes_internal/Taskfile2.yml @@ -0,0 +1,7 @@ +version: '3' + +tasks: + + task-3: + cmds: + - echo "Hello, World!"