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

Create directory before evaluating dynamic arguments #1848

Open
wants to merge 3 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
9 changes: 5 additions & 4 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func (e *Executor) splitRegularAndWatchCalls(calls ...*ast.Call) (regularCalls [
// RunTask runs a task by its name
func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
t, err := e.FastCompiledTask(call)

if err := e.mkdir(t); err != nil {
e.Logger.Errf(logger.Red, "task: cannot make directory %q: %v\n", t.Dir, err)
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code placed here means that directories are created before any checks that might prevent a task from running.

if err != nil {
return err
}
Expand Down Expand Up @@ -245,10 +250,6 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
}
}

if err := e.mkdir(t); err != nil {
e.Logger.Errf(logger.Red, "task: cannot make directory %q: %v\n", t.Dir, err)
}

var deferredExitCode uint8

for i := range t.Cmds {
Expand Down
28 changes: 28 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,34 @@ func TestDynamicVariablesRunOnTheNewCreatedDir(t *testing.T) {
_ = os.RemoveAll(toBeCreated)
}

func TestExplicitDynamicVariablesRunOnTheNewCreatedDir(t *testing.T) {
const expected = "created"
const dir = "testdata/dir/explicit_dynamic_var_on_created_dir/"
const toBeCreated = dir + expected
const target = "default"
var out bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &out,
Stderr: &out,
}

// Ensure that the directory to be created doesn't actually exist.
_ = os.RemoveAll(toBeCreated)
if _, err := os.Stat(toBeCreated); err == nil {
t.Errorf("Directory should not exist: %v", err)
}

require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: target}))

got := strings.TrimSuffix(filepath.Base(out.String()), "\n")
assert.Equal(t, expected, got, "Mismatch in the working directory")

// Clean-up after ourselves only if no error.
_ = os.RemoveAll(toBeCreated)
}

func TestDynamicVariablesShouldRunOnTheTaskDir(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/dir/dynamic_var",
Expand Down
11 changes: 11 additions & 0 deletions testdata/dir/explicit_dynamic_var_on_created_dir/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

tasks:
default:
dir: created
vars:
TEST_VAR:
sh: echo test > created && echo created
cmds:
- |
echo "/{{.TEST_VAR}}"
Loading