From 8c0d6eabdc82114e1a0739e923793f13354373db Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Sat, 2 Nov 2024 22:13:11 +0300 Subject: [PATCH] not allow undeclared or empty taskfile in include --- task_test.go | 29 +++++++++++++++++++ taskfile/reader.go | 4 +++ .../inlined_taskfile_empty/Taskfile.yml | 9 ++++++ .../inlined_taskfile_null/Taskfile.yml | 9 ++++++ .../taskfile_empty/Taskfile.yml | 10 +++++++ .../taskfile_null/Taskfile.yml | 10 +++++++ 6 files changed, 71 insertions(+) create mode 100644 testdata/includes_missing_taskfile/inlined_taskfile_empty/Taskfile.yml create mode 100644 testdata/includes_missing_taskfile/inlined_taskfile_null/Taskfile.yml create mode 100644 testdata/includes_missing_taskfile/taskfile_empty/Taskfile.yml create mode 100644 testdata/includes_missing_taskfile/taskfile_null/Taskfile.yml diff --git a/task_test.go b/task_test.go index 599277c8c5..53e7433521 100644 --- a/task_test.go +++ b/task_test.go @@ -1527,6 +1527,35 @@ func TestIncludesInterpolation(t *testing.T) { } } +func TestIncludesMissingTaskfile(t *testing.T) { + const dir = "testdata/includes_missing_taskfile" + const error = "no taskfile is specified for include \"included\"" + + tests := []struct { + name string + task string + }{ + {"inlined_taskfile_empty", "empty inline taskfile"}, + {"inlined_taskfile_null", "null inline taskfile"}, + {"taskfile_empty", "empty taskfile"}, + {"taskfile_null", "null taskfile"}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + var buff bytes.Buffer + e := task.Executor{ + Dir: filepath.Join(dir, test.name), + Stdout: &buff, + Stderr: &buff, + Silent: true, + } + err := e.Setup() + assert.ErrorContains(t, err, error) + }) + } +} + func TestIncludedTaskfileVarMerging(t *testing.T) { const dir = "testdata/included_taskfile_var_merging" tests := []struct { diff --git a/taskfile/reader.go b/taskfile/reader.go index 69f75b1d19..0d48f400a2 100644 --- a/taskfile/reader.go +++ b/taskfile/reader.go @@ -121,6 +121,10 @@ func (r *Reader) include(node Node) error { return err } + if include.Taskfile == "" { + return errors.New(fmt.Sprintf("no taskfile is specified for include %q", include.Namespace)) + } + entrypoint, err := node.ResolveEntrypoint(include.Taskfile) if err != nil { return err diff --git a/testdata/includes_missing_taskfile/inlined_taskfile_empty/Taskfile.yml b/testdata/includes_missing_taskfile/inlined_taskfile_empty/Taskfile.yml new file mode 100644 index 0000000000..58f3516ced --- /dev/null +++ b/testdata/includes_missing_taskfile/inlined_taskfile_empty/Taskfile.yml @@ -0,0 +1,9 @@ +version: '3' + +includes: + included: '' + +tasks: + task-1: + cmds: + - task: included:default diff --git a/testdata/includes_missing_taskfile/inlined_taskfile_null/Taskfile.yml b/testdata/includes_missing_taskfile/inlined_taskfile_null/Taskfile.yml new file mode 100644 index 0000000000..d48191c23d --- /dev/null +++ b/testdata/includes_missing_taskfile/inlined_taskfile_null/Taskfile.yml @@ -0,0 +1,9 @@ +version: '3' + +includes: + included: + +tasks: + task-1: + cmds: + - task: included:default diff --git a/testdata/includes_missing_taskfile/taskfile_empty/Taskfile.yml b/testdata/includes_missing_taskfile/taskfile_empty/Taskfile.yml new file mode 100644 index 0000000000..7805221521 --- /dev/null +++ b/testdata/includes_missing_taskfile/taskfile_empty/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +includes: + included: + taskfile: '' + +tasks: + task-1: + cmds: + - task: included:default diff --git a/testdata/includes_missing_taskfile/taskfile_null/Taskfile.yml b/testdata/includes_missing_taskfile/taskfile_null/Taskfile.yml new file mode 100644 index 0000000000..5c0a933a64 --- /dev/null +++ b/testdata/includes_missing_taskfile/taskfile_null/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +includes: + included: + taskfile: + +tasks: + task-1: + cmds: + - task: included:default