1
0
mirror of https://github.com/go-task/task.git synced 2025-02-13 13:59:32 +02:00

Issue #519: Allow includes to be optional

This commit is contained in:
Sally Young 2021-08-11 17:28:44 +01:00
parent 50e5813222
commit 8f80fc4e2c
No known key found for this signature in database
GPG Key ID: 1909E5FC18EE050E
8 changed files with 93 additions and 1 deletions

View File

@ -154,6 +154,24 @@ includes:
> This was a deliberate decision to keep use and implementation simple. > This was a deliberate decision to keep use and implementation simple.
> If you disagree, open an GitHub issue and explain your use case. =) > If you disagree, open an GitHub issue and explain your use case. =)
### Optional includes
Includes marked as optional will allow Task to continue execution as normal if
the included file is missing.
```yaml
version: '3'
includes:
tests:
taskfile: ./tests/Taskfile.yml
optional: true
tasks:
greet:
cmds:
- echo "This command can still be successfully executed if ./tests/Taskfile.yml does not exist"
```
## Task directory ## Task directory
By default, tasks will be executed in the directory where the Taskfile is By default, tasks will be executed in the directory where the Taskfile is

View File

@ -755,6 +755,41 @@ func TestIncludesCallingRoot(t *testing.T) {
tt.Run(t) tt.Run(t)
} }
func TestIncludesOptional(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/includes_optional",
Target: "default",
TrimSpace: true,
Files: map[string]string{
"called_dep.txt": "called_dep",
}}
tt.Run(t)
}
func TestIncludesOptionalImplicitFalse(t *testing.T) {
e := task.Executor{
Dir: "testdata/includes_optional_implicit_false",
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
err := e.Setup()
assert.Error(t, err)
assert.Equal(t, "stat testdata/includes_optional_implicit_false/TaskfileOptional.yml: no such file or directory", err.Error())
}
func TestIncludesOptionalExplicitFalse(t *testing.T) {
e := task.Executor{
Dir: "testdata/includes_optional_explicit_false",
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
err := e.Setup()
assert.Error(t, err)
assert.Equal(t, "stat testdata/includes_optional_explicit_false/TaskfileOptional.yml: no such file or directory", err.Error())
}
func TestSummary(t *testing.T) { func TestSummary(t *testing.T) {
const dir = "testdata/summary" const dir = "testdata/summary"

View File

@ -10,6 +10,7 @@ import (
type IncludedTaskfile struct { type IncludedTaskfile struct {
Taskfile string Taskfile string
Dir string Dir string
Optional bool
AdvancedImport bool AdvancedImport bool
} }
@ -92,12 +93,14 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err
var includedTaskfile struct { var includedTaskfile struct {
Taskfile string Taskfile string
Dir string Dir string
Optional bool
} }
if err := unmarshal(&includedTaskfile); err != nil { if err := unmarshal(&includedTaskfile); err != nil {
return err return err
} }
it.Dir = includedTaskfile.Dir
it.Taskfile = includedTaskfile.Taskfile it.Taskfile = includedTaskfile.Taskfile
it.Dir = includedTaskfile.Dir
it.Optional = includedTaskfile.Optional
it.AdvancedImport = true it.AdvancedImport = true
return nil return nil
} }

View File

@ -42,6 +42,7 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
includedTask = taskfile.IncludedTaskfile{ includedTask = taskfile.IncludedTaskfile{
Taskfile: tr.Replace(includedTask.Taskfile), Taskfile: tr.Replace(includedTask.Taskfile),
Dir: tr.Replace(includedTask.Dir), Dir: tr.Replace(includedTask.Dir),
Optional: includedTask.Optional,
AdvancedImport: includedTask.AdvancedImport, AdvancedImport: includedTask.AdvancedImport,
} }
if err := tr.Err(); err != nil { if err := tr.Err(); err != nil {
@ -56,6 +57,9 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
} }
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil && includedTask.Optional {
return nil
}
if err != nil { if err != nil {
return err return err
} }

1
testdata/includes_optional/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.txt

11
testdata/includes_optional/Taskfile.yml vendored Normal file
View File

@ -0,0 +1,11 @@
version: '3'
includes:
included:
taskfile: TaskfileOptional.yml
optional: true
tasks:
default:
cmds:
- echo "called_dep" > called_dep.txt

View File

@ -0,0 +1,11 @@
version: '3'
includes:
included:
taskfile: TaskfileOptional.yml
optional: false
tasks:
default:
cmds:
- echo "Hello, world!"

View File

@ -0,0 +1,9 @@
version: '3'
includes:
included: TaskfileOptional.yml
tasks:
default:
cmds:
- echo "Hello, world!"