1
0
mirror of https://github.com/go-task/task.git synced 2025-01-20 04:59:37 +02:00

fix: exit codes for indirect tasks

This commit is contained in:
Pete Davison 2023-07-19 23:08:22 +00:00 committed by Andrey Nering
parent 659ba317c1
commit 6059ce2ac4
3 changed files with 40 additions and 14 deletions

View File

@ -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}
}
}

View File

@ -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) {

View File

@ -1,6 +1,10 @@
version: '3'
tasks:
test-exit-code:
direct:
cmds:
- exit 42
indirect:
cmds:
- task: direct