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

fix: exit codes for indirect tasks #1270

Merged
merged 2 commits into from
Jul 25, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Fixed variable propagation in multi-level includes (#778, #996, #1256 by
@hudclark).
- Fixed a bug where the `--exit-code` code flag was not returning the correct
exit code when calling commands indirectly (#1266, #1270 by @pd93).

## v3.27.1 - 2023-06-30

Expand Down
4 changes: 4 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
continue
}

if !call.Direct {
return err
}

return &errors.TaskRunError{TaskName: t.Task, Err: err}
}
}
Expand Down
44 changes: 31 additions & 13 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1823,21 +1823,39 @@ Hello bar

func TestErrorCode(t *testing.T) {
const dir = "testdata/error_code"

var buff bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: true,
tests := []struct {
name string
task string
expected int
}{
{
name: "direct task",
task: "direct",
expected: 42,
}, {
name: "indirect task",
task: "indirect",
expected: 42,
},
}
require.NoError(t, e.Setup())
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buff bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: true,
}
require.NoError(t, e.Setup())

err := e.Run(context.Background(), taskfile.Call{Task: "test-exit-code"})
require.Error(t, err)
casted, ok := err.(*errors.TaskRunError)
assert.True(t, ok, "cannot cast returned error to *task.TaskRunError")
assert.Equal(t, 42, casted.TaskExitCode(), "unexpected exit code from task")
err := e.Run(context.Background(), taskfile.Call{Task: test.task, Direct: true})
require.Error(t, err)
taskRunErr, ok := err.(*errors.TaskRunError)
assert.True(t, ok, "cannot cast returned error to *task.TaskRunError")
assert.Equal(t, test.expected, taskRunErr.TaskExitCode(), "unexpected exit code from task")
})
}
}

func TestEvaluateSymlinksInPaths(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion testdata/error_code/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
version: '3'

tasks:
test-exit-code:
direct:
cmds:
- exit 42

indirect:
cmds:
- task: direct