1
0
mirror of https://github.com/go-task/task.git synced 2025-06-02 23:27:37 +02:00

Allow ignore_error at task level

This commit is contained in:
Andrey Nering 2018-08-05 12:53:42 -03:00
parent c70343a5bc
commit feaf70922d
5 changed files with 63 additions and 26 deletions

View File

@ -18,4 +18,5 @@ type Task struct {
Silent bool Silent bool
Method string Method string
Prefix string Prefix string
IgnoreError bool `yaml:"ignore_error"`
} }

View File

@ -182,6 +182,12 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
if err2 := statusOnError(t); err2 != nil { if err2 := statusOnError(t); err2 != nil {
e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2) e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2)
} }
if _, ok := err.(interp.ExitCode); ok && t.IgnoreError {
e.Logger.VerboseErrf("task: task error ignored: %v", err)
continue
}
return &taskRunError{t.Task, err} return &taskRunError{t.Task, err}
} }
} }

View File

@ -416,6 +416,26 @@ func TestTaskVersion(t *testing.T) {
func TestTaskIgnoreErrors(t *testing.T) { func TestTaskIgnoreErrors(t *testing.T) {
const dir = "testdata/ignore_errors" const dir = "testdata/ignore_errors"
t.Run("task-should-pass", func(t *testing.T) {
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(taskfile.Call{Task: "task-should-pass"}))
})
t.Run("task-should-fail", func(t *testing.T) {
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.Setup())
assert.Error(t, e.Run(taskfile.Call{Task: "task-should-fail"}))
})
t.Run("cmd-should-pass", func(t *testing.T) { t.Run("cmd-should-pass", func(t *testing.T) {
e := task.Executor{ e := task.Executor{
Dir: dir, Dir: dir,

View File

@ -1,6 +1,15 @@
version: 2 version: '2'
tasks: tasks:
task-should-pass:
cmds:
- exit 1
ignore_error: true
task-should-fail:
cmds:
- exit 1
cmd-should-pass: cmd-should-pass:
cmds: cmds:
- cmd: exit 1 - cmd: exit 1

View File

@ -35,6 +35,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
Silent: origTask.Silent, Silent: origTask.Silent,
Method: r.Replace(origTask.Method), Method: r.Replace(origTask.Method),
Prefix: r.Replace(origTask.Prefix), Prefix: r.Replace(origTask.Prefix),
IgnoreError: origTask.IgnoreError,
} }
new.Dir, err = shell.Expand(new.Dir, nil) new.Dir, err = shell.Expand(new.Dir, nil)
if err != nil { if err != nil {