Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#363 respect ignore errors #855

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
d := d

g.Go(func() error {
err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars, IgnoreError: t.IgnoreError})
if err != nil {
return err
}
Expand Down Expand Up @@ -226,8 +226,8 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
reacquire := e.releaseConcurrencyLimit()
defer reacquire()

err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
if err != nil {
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars, IgnoreError: cmd.IgnoreError})
if err != nil && !t.IgnoreError {
return err
}
return nil
Expand Down Expand Up @@ -264,7 +264,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
Stdout: stdOut,
Stderr: stdErr,
})
if execext.IsExitError(err) && cmd.IgnoreError {
if execext.IsExitError(err) && t.IgnoreError {
e.Logger.VerboseErrf(logger.Yellow, "task: [%s] command error ignored: %v", t.Name(), err)
return nil
}
Expand Down
59 changes: 26 additions & 33 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,34 @@ func TestTaskIgnoreErrors(t *testing.T) {

assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task-should-pass"}))
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "task-should-fail"}))
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "inner-task-should-pass"}))
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "inner-task-should-fail"}))
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "cmd-should-pass"}))
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "cmd-should-fail"}))
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "dep-task-should-pass"}))
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "dep-task-should-fail"}))
}

func TestTaskIgnoreErrorsExtended(t *testing.T) {
const dir = "testdata/ignore_errors_2"
var buff bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
}
assert.NoError(t, e.Setup())

assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "fail"}))
assert.Contains(t, strings.TrimSpace(buff.String()), `task: [fail] echo Failing task
Failing task
task: [fail] exit 100`)

assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "ignore-error-task-without-template"}))
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "ignore-error-cmd-without-template"}))
// assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "ignore-error-task-with-template"}))
// assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "ignore-error-cmd-with-template"}))

}

func TestExpand(t *testing.T) {
Expand Down Expand Up @@ -1338,36 +1364,3 @@ func TestErrorCode(t *testing.T) {
assert.True(t, ok, "cannot cast returned error to *task.TaskRunError")
assert.Equal(t, 42, casted.ExitCode(), "unexpected exit code from task")
}

func TestEvaluateSymlinksInPaths(t *testing.T) {
const dir = "testdata/evaluate_symlinks_in_paths"
var buff bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: false,
}
assert.NoError(t, e.Setup())
err := e.Run(context.Background(), taskfile.Call{Task: "default"})
assert.NoError(t, err)
assert.NotEqual(t, `task: Task "default" is up to date`, strings.TrimSpace(buff.String()))
buff.Reset()
err = e.Run(context.Background(), taskfile.Call{Task: "test-sym"})
assert.NoError(t, err)
assert.NotEqual(t, `task: Task "test-sym" is up to date`, strings.TrimSpace(buff.String()))
buff.Reset()
err = e.Run(context.Background(), taskfile.Call{Task: "default"})
assert.NoError(t, err)
assert.NotEqual(t, `task: Task "default" is up to date`, strings.TrimSpace(buff.String()))
buff.Reset()
err = e.Run(context.Background(), taskfile.Call{Task: "default"})
assert.NoError(t, err)
assert.Equal(t, `task: Task "default" is up to date`, strings.TrimSpace(buff.String()))
buff.Reset()
err = e.Run(context.Background(), taskfile.Call{Task: "reset"})
assert.NoError(t, err)
buff.Reset()
err = os.RemoveAll(dir + "/.task")
assert.NoError(t, err)
}
5 changes: 3 additions & 2 deletions taskfile/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package taskfile

// Call is the parameters to a task call
type Call struct {
Task string
Vars *Vars
Task string
Vars *Vars
IgnoreError bool
}
27 changes: 26 additions & 1 deletion testdata/ignore_errors/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,36 @@ tasks:
cmds:
- exit 1

inner-task:
cmds:
- exit 1

inner-task-should-pass:
cmds:
- task: inner-task
ignore_error: true

inner-task-should-fail:
cmds:
- task: inner-task

cmd-should-pass:
cmds:
- cmd: exit 1
ignore_error: true
ignore_error: true

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

dep-task-should-pass:
deps: [inner-task]
cmds:
- echo 'test ignore_error'
ignore_error: true

dep-task-should-fail:
deps: [inner-task]
cmds:
- echo 'test ignore_error'

40 changes: 40 additions & 0 deletions testdata/ignore_errors_2/Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: "3"

tasks:

pass:
cmd: exit 0

fail:
cmds:
- echo Failing task
- exit 100

ignore-error-task-without-template:
cmds:
- task: fail
- echo After ignore_error
ignore_error: true # FIXED

ignore-error-cmd-without-template:
cmds:
- cmd: exit 101
- echo After ignore_error
ignore_error: true # FIXED


ignore-error-task-with-template:
cmds:
- cmd: echo IGNORE="$IGNORE"
- cmd: echo '{{if .IGNORE}}true{{else}}false{{end}}'
- task: fail
- echo After ignore_error
# ignore_error: '{{if .IGNORE}}true{{else}}false{{end}}'

ignore-error-cmd-with-template:
cmds:
- cmd: echo IGNORE="$IGNORE"
- cmd: echo '{{if .IGNORE}}true{{else}}false{{end}}'
- cmd: exit 102
- echo After ignore_error
# ignore_error: '{{if .IGNORE}}true{{else}}false{{end}}'
4 changes: 2 additions & 2 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
Interactive: origTask.Interactive,
Method: r.Replace(origTask.Method),
Prefix: r.Replace(origTask.Prefix),
IgnoreError: origTask.IgnoreError,
IgnoreError: origTask.IgnoreError || call.IgnoreError,
Run: r.Replace(origTask.Run),
IncludeVars: origTask.IncludeVars,
IncludedTaskfileVars: origTask.IncludedTaskfileVars,
Expand Down Expand Up @@ -103,7 +103,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
Silent: cmd.Silent,
Cmd: r.Replace(cmd.Cmd),
Vars: r.ReplaceVars(cmd.Vars),
IgnoreError: cmd.IgnoreError,
IgnoreError: cmd.IgnoreError || call.IgnoreError,
Defer: cmd.Defer,
})
}
Expand Down