From 5a08409a274b4baf2fbd4dae8a677603b7245a3d Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Wed, 2 Nov 2022 14:27:15 +0000 Subject: [PATCH] feat: run default task in included file when task is omitted --- task_test.go | 24 +++++++++++++++++++ taskfile/read/taskfile.go | 7 ++++++ .../includes_shadowed_default/Taskfile.yml | 10 ++++++++ .../includes_shadowed_default/Taskfile2.yml | 6 +++++ testdata/includes_shadowed_default/file.txt | 1 + .../includes_unshadowed_default/Taskfile.yml | 5 ++++ .../includes_unshadowed_default/Taskfile2.yml | 6 +++++ testdata/includes_unshadowed_default/file.txt | 1 + 8 files changed, 60 insertions(+) create mode 100644 testdata/includes_shadowed_default/Taskfile.yml create mode 100644 testdata/includes_shadowed_default/Taskfile2.yml create mode 100644 testdata/includes_shadowed_default/file.txt create mode 100644 testdata/includes_unshadowed_default/Taskfile.yml create mode 100644 testdata/includes_unshadowed_default/Taskfile2.yml create mode 100644 testdata/includes_unshadowed_default/file.txt diff --git a/task_test.go b/task_test.go index a2446bc5..30d90a54 100644 --- a/task_test.go +++ b/task_test.go @@ -1107,6 +1107,30 @@ func TestInternalTask(t *testing.T) { } } +func TestIncludesShadowedDefault(t *testing.T) { + tt := fileContentTest{ + Dir: "testdata/includes_shadowed_default", + Target: "included", + TrimSpace: true, + Files: map[string]string{ + "file.txt": "shadowed", + }, + } + tt.Run(t) +} + +func TestIncludesUnshadowedDefault(t *testing.T) { + tt := fileContentTest{ + Dir: "testdata/includes_unshadowed_default", + Target: "included", + TrimSpace: true, + Files: map[string]string{ + "file.txt": "included", + }, + } + tt.Run(t) +} + func TestSupportedFileNames(t *testing.T) { fileNames := []string{ "Taskfile.yml", diff --git a/taskfile/read/taskfile.go b/taskfile/read/taskfile.go index d0ee697b..fd567916 100644 --- a/taskfile/read/taskfile.go +++ b/taskfile/read/taskfile.go @@ -153,6 +153,13 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { if err = taskfile.Merge(t, includedTaskfile, &includedTask, namespace); err != nil { return err } + + if includedTaskfile.Tasks["default"] != nil && t.Tasks[namespace] == nil { + defaultTaskName := fmt.Sprintf("%s:default", namespace) + t.Tasks[defaultTaskName].Aliases = append(t.Tasks[defaultTaskName].Aliases, namespace) + t.Tasks[defaultTaskName].Aliases = append(t.Tasks[defaultTaskName].Aliases, includedTask.Aliases...) + } + return nil }) if err != nil { diff --git a/testdata/includes_shadowed_default/Taskfile.yml b/testdata/includes_shadowed_default/Taskfile.yml new file mode 100644 index 00000000..5588c962 --- /dev/null +++ b/testdata/includes_shadowed_default/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +includes: + included: + taskfile: Taskfile2.yml + +tasks: + included: + cmds: + - echo "shadowed" > file.txt diff --git a/testdata/includes_shadowed_default/Taskfile2.yml b/testdata/includes_shadowed_default/Taskfile2.yml new file mode 100644 index 00000000..e9447992 --- /dev/null +++ b/testdata/includes_shadowed_default/Taskfile2.yml @@ -0,0 +1,6 @@ +version: '3' + +tasks: + default: + cmds: + - echo "included" > file.txt diff --git a/testdata/includes_shadowed_default/file.txt b/testdata/includes_shadowed_default/file.txt new file mode 100644 index 00000000..92a8299c --- /dev/null +++ b/testdata/includes_shadowed_default/file.txt @@ -0,0 +1 @@ +shadowed diff --git a/testdata/includes_unshadowed_default/Taskfile.yml b/testdata/includes_unshadowed_default/Taskfile.yml new file mode 100644 index 00000000..017f9a8b --- /dev/null +++ b/testdata/includes_unshadowed_default/Taskfile.yml @@ -0,0 +1,5 @@ +version: '3' + +includes: + included: + taskfile: Taskfile2.yml diff --git a/testdata/includes_unshadowed_default/Taskfile2.yml b/testdata/includes_unshadowed_default/Taskfile2.yml new file mode 100644 index 00000000..e9447992 --- /dev/null +++ b/testdata/includes_unshadowed_default/Taskfile2.yml @@ -0,0 +1,6 @@ +version: '3' + +tasks: + default: + cmds: + - echo "included" > file.txt diff --git a/testdata/includes_unshadowed_default/file.txt b/testdata/includes_unshadowed_default/file.txt new file mode 100644 index 00000000..201a102c --- /dev/null +++ b/testdata/includes_unshadowed_default/file.txt @@ -0,0 +1 @@ +included