From 108cb91d959e8e5c559b67802b804c9bbf4b8369 Mon Sep 17 00:00:00 2001 From: Tobias Salzmann Date: Wed, 1 Aug 2018 10:44:53 +0200 Subject: [PATCH] IgnoreError * Document ignore_error * ignore_error only for commands --- README.md | 29 +++++++++++++++++++++++++++++ internal/taskfile/call.go | 5 ++--- internal/taskfile/cmd.go | 19 +++++++------------ task.go | 6 +++--- task_test.go | 28 ++++------------------------ testdata/ignore_errors/Taskfile.yml | 24 ++++++++++-------------- 6 files changed, 55 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 9595e4a5..719aa31f 100644 --- a/README.md +++ b/README.md @@ -632,6 +632,35 @@ tasks: - echo "This will print nothing" > /dev/null ``` +## Ignore errors + +You have the option to ignore errors during command execution. +Given the following Taskfile: + +```yml +version: '2' + +tasks: + echo: + cmds: + - exit 1 + - echo "Hello World" +``` + +Task will abort the execution after running `exit 1` because the status code `1` stands for `EXIT_FAILURE`. +However it is possible to continue with execution using `ignore_errors`: + +```yml +version: '2' + +tasks: + echo: + cmds: + - cmd: exit 1 + ignore_errors: true + - echo "Hello World" +``` + ## Output syntax By default, Task just redirect the STDOUT and STDERR of the running commands diff --git a/internal/taskfile/call.go b/internal/taskfile/call.go index 3b3ac725..eec41031 100644 --- a/internal/taskfile/call.go +++ b/internal/taskfile/call.go @@ -2,7 +2,6 @@ package taskfile // Call is the parameters to a task call type Call struct { - Task string - Vars Vars - IgnoreError bool + Task string + Vars Vars } diff --git a/internal/taskfile/cmd.go b/internal/taskfile/cmd.go index 78fdd370..ea267811 100644 --- a/internal/taskfile/cmd.go +++ b/internal/taskfile/cmd.go @@ -16,9 +16,8 @@ type Cmd struct { // Dep is a task dependency type Dep struct { - Task string - Vars Vars - IgnoreError bool + Task string + Vars Vars } var ( @@ -42,7 +41,7 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error { var cmdStruct struct { Cmd string Silent bool - IgnoreError bool `yaml:"ignoreError"` + IgnoreError bool `yaml:"ignore_error"` } if err := unmarshal(&cmdStruct); err == nil && cmdStruct.Cmd != "" { c.Cmd = cmdStruct.Cmd @@ -51,14 +50,12 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } var taskCall struct { - Task string - Vars Vars - IgnoreError bool `yaml:"ignoreError"` + Task string + Vars Vars } if err := unmarshal(&taskCall); err == nil { c.Task = taskCall.Task c.Vars = taskCall.Vars - c.IgnoreError = taskCall.IgnoreError return nil } return ErrCantUnmarshalCmd @@ -72,14 +69,12 @@ func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } var taskCall struct { - Task string - Vars Vars - IgnoreError bool `yaml:"ignoreError"` + Task string + Vars Vars } if err := unmarshal(&taskCall); err == nil { d.Task = taskCall.Task d.Vars = taskCall.Vars - d.IgnoreError = taskCall.IgnoreError return nil } return ErrCantUnmarshalDep diff --git a/task.go b/task.go index 500e4112..d4fa70e7 100644 --- a/task.go +++ b/task.go @@ -188,7 +188,7 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error { d := d g.Go(func() error { - return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars, IgnoreError: d.IgnoreError}) + return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars}) }) } @@ -200,7 +200,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi switch { case cmd.Task != "": - return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars, IgnoreError: cmd.IgnoreError || call.IgnoreError}) + return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars}) case cmd.Cmd != "": if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) { e.Logger.Errf(cmd.Cmd) @@ -219,7 +219,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi Stdin: e.Stdin, Stdout: stdOut, Stderr: stdErr, - IgnoreErrorCode: cmd.IgnoreError || call.IgnoreError, + IgnoreErrorCode: cmd.IgnoreError, }) default: return nil diff --git a/task_test.go b/task_test.go index 4176eeab..2daa23e7 100644 --- a/task_test.go +++ b/task_test.go @@ -415,43 +415,23 @@ func TestTaskVersion(t *testing.T) { func TestTaskIgnoreErrors(t *testing.T) { const dir = "testdata/ignore_errors" - t.Run("CmdShouldPass", func(t *testing.T) { + t.Run("cmd-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: "CmdShouldPass"})) + assert.NoError(t, e.Run(taskfile.Call{Task: "cmd-should-pass"})) }) - t.Run("CmdShouldFail", func(t *testing.T) { + t.Run("cmd-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: "CmdShouldFail"})) - }) - - t.Run("TaskShouldPass", 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: "TaskShouldPass"})) - }) - - t.Run("TaskShouldFail", 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: "TaskShouldFail"})) + assert.Error(t, e.Run(taskfile.Call{Task: "cmd-should-fail"})) }) } diff --git a/testdata/ignore_errors/Taskfile.yml b/testdata/ignore_errors/Taskfile.yml index 3e5a67c5..57d86a6f 100644 --- a/testdata/ignore_errors/Taskfile.yml +++ b/testdata/ignore_errors/Taskfile.yml @@ -1,15 +1,11 @@ -CmdShouldPass: - cmds: - - cmd: UnknownCommandThatNeverWillExist - ignoreError: true -CmdShouldFail: - cmds: - - cmd: UnknownCommandThatNeverWillExist +version: 2 -TaskShouldPass: - cmds: - - task: CmdShouldFail - ignoreError: true -TaskShouldFail: - cmds: - - task: CmdShouldFail \ No newline at end of file +tasks: + cmd-should-pass: + cmds: + - cmd: exit 1 + ignore_error: true + + cmd-should-fail: + cmds: + - cmd: exit 1 \ No newline at end of file