Skip to content

Commit

Permalink
fix: interpolate vars in defer (#1814)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Nering <andreynering@users.noreply.github.com>
  • Loading branch information
vmaerten and andreynering authored Sep 19, 2024
1 parent 0be0579 commit 1bf8505
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,13 @@ func (e *Executor) runDeferred(t *ast.Task, call *ast.Call, i int, deferredExitC
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

origTask, err := e.GetTask(call)
if err != nil {
return
}

cmd := t.Cmds[i]
vars, _ := e.Compiler.FastGetVariables(t, call)
vars, _ := e.Compiler.FastGetVariables(origTask, call)
cache := &templater.Cache{Vars: vars}
extra := map[string]any{}

Expand Down
4 changes: 2 additions & 2 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ func TestExitCodeZero(t *testing.T) {
require.NoError(t, e.Setup())

require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "exit-zero"}))
assert.Equal(t, "EXIT_CODE=", strings.TrimSpace(buff.String()))
assert.Equal(t, "FOO=bar - EXIT_CODE=", strings.TrimSpace(buff.String()))
}

func TestExitCodeOne(t *testing.T) {
Expand All @@ -1802,7 +1802,7 @@ func TestExitCodeOne(t *testing.T) {
require.NoError(t, e.Setup())

require.Error(t, e.Run(context.Background(), &ast.Call{Task: "exit-one"}))
assert.Equal(t, "EXIT_CODE=1", strings.TrimSpace(buff.String()))
assert.Equal(t, "FOO=bar - EXIT_CODE=1", strings.TrimSpace(buff.String()))
}

func TestIgnoreNilElements(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions testdata/exit_code/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ vars:

tasks:
exit-zero:
vars:
FOO: bar
cmds:
- defer: echo {{.PREFIX}}{{.EXIT_CODE}}
- defer: echo FOO={{.FOO}} - {{.PREFIX}}{{.EXIT_CODE}}
- exit 0

exit-one:
vars:
FOO: bar
cmds:
- defer: echo {{.PREFIX}}{{.EXIT_CODE}}
- defer: echo FOO={{.FOO}} - {{.PREFIX}}{{.EXIT_CODE}}
- exit 1

0 comments on commit 1bf8505

Please sign in to comment.