From 17ad7060b338aa3b0780be93304206d19e96335c Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 15 Feb 2020 17:24:06 +0300 Subject: [PATCH] Added version validation and updated tests --- internal/taskfile/included_taskfile.go | 6 ++++-- internal/taskfile/read/taskfile.go | 8 +++++--- task.go | 8 ++++++++ task_test.go | 15 +++++++++++++++ testdata/incorrect_includes/Taskfile.yml | 10 ++++++++++ testdata/incorrect_includes/included/Taskfile.yml | 6 ++++++ 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 testdata/incorrect_includes/Taskfile.yml create mode 100644 testdata/incorrect_includes/included/Taskfile.yml diff --git a/internal/taskfile/included_taskfile.go b/internal/taskfile/included_taskfile.go index 21170e4d..346744f9 100644 --- a/internal/taskfile/included_taskfile.go +++ b/internal/taskfile/included_taskfile.go @@ -9,8 +9,9 @@ var ( // IncludedTaskfile represents information about included tasksfile type IncludedTaskfile struct { - Taskfile string - Dir string + Taskfile string + Dir string + AdvancedImport bool } // IncludedTaskfiles represents information about included tasksfiles @@ -31,6 +32,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err if err := unmarshal(&includedTaskfile); err == nil { it.Dir = includedTaskfile.Dir it.Taskfile = includedTaskfile.Taskfile + it.AdvancedImport = true return nil } diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index 8cf149d2..5f12446e 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -45,9 +45,11 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, ErrIncludedTaskfilesCantHaveIncludes } - for _, task := range includedTaskfile.Tasks { - if !filepath.IsAbs(task.Dir) { - task.Dir = filepath.Join(includedTask.Dir, task.Dir) + if includedTask.AdvancedImport { + for _, task := range includedTaskfile.Tasks { + if !filepath.IsAbs(task.Dir) { + task.Dir = filepath.Join(includedTask.Dir, task.Dir) + } } } diff --git a/task.go b/task.go index 8c1b14b7..5c4f07d6 100644 --- a/task.go +++ b/task.go @@ -207,6 +207,14 @@ func (e *Executor) Setup() error { } } + if v < 3 { + for _, taskfile := range e.Taskfile.Includes { + if taskfile.AdvancedImport { + return errors.New(`task: Import with additional parameters is only available starting on Taskfile version v3`) + } + } + } + e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks)) e.mkdirMutexMap = make(map[string]*sync.Mutex, len(e.Taskfile.Tasks)) for k := range e.Taskfile.Tasks { diff --git a/task_test.go b/task_test.go index 9bda7b93..c752e95b 100644 --- a/task_test.go +++ b/task_test.go @@ -551,6 +551,21 @@ func TestIncludes(t *testing.T) { tt.Run(t) } +func TestIncorrectVersionIncludes(t *testing.T) { + const dir = "testdata/incorrect_includes" + expectedError := "task: Import with additional parameters is only available starting on Taskfile version v3" + + var buff bytes.Buffer + e := task.Executor{ + Dir: dir, + Stdout: &buff, + Stderr: &buff, + Silent: true, + } + + assert.EqualError(t, e.Setup(), expectedError) +} + func TestIncludesEmptyMain(t *testing.T) { tt := fileContentTest{ Dir: "testdata/includes_empty", diff --git a/testdata/incorrect_includes/Taskfile.yml b/testdata/incorrect_includes/Taskfile.yml new file mode 100644 index 00000000..63f4745b --- /dev/null +++ b/testdata/incorrect_includes/Taskfile.yml @@ -0,0 +1,10 @@ +version: '2.6' + +includes: + included: + taskfile: ./included + +tasks: + default: + cmds: + - task: gen diff --git a/testdata/incorrect_includes/included/Taskfile.yml b/testdata/incorrect_includes/included/Taskfile.yml new file mode 100644 index 00000000..a50cc0ba --- /dev/null +++ b/testdata/incorrect_includes/included/Taskfile.yml @@ -0,0 +1,6 @@ +version: '2.6' + +tasks: + gen: + cmds: + - echo incorrect includes test