Skip to content

Commit

Permalink
IgnoreError
Browse files Browse the repository at this point in the history
* Document ignore_error
* ignore_error only for commands
  • Loading branch information
Tobias Salzmann committed Aug 1, 2018
1 parent 0560060 commit 108cb91
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 56 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions internal/taskfile/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 7 additions & 12 deletions internal/taskfile/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
})
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
28 changes: 4 additions & 24 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
})
}
24 changes: 10 additions & 14 deletions testdata/ignore_errors/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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
tasks:
cmd-should-pass:
cmds:
- cmd: exit 1
ignore_error: true

cmd-should-fail:
cmds:
- cmd: exit 1

0 comments on commit 108cb91

Please sign in to comment.