1
0
mirror of https://github.com/go-task/task.git synced 2025-02-03 13:22:11 +02:00

When "dir:" attribute points to a non-existing dir, create it

Closes #209
This commit is contained in:
Marco Molteni 2019-06-04 18:58:22 +02:00
parent 1e93c38307
commit c663c5c507
3 changed files with 43 additions and 0 deletions

View File

@ -200,6 +200,15 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
}
}
// When using the dir: attribute it can happen that the directory doesn't exist.
// If so, we create it.
if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
if err := os.MkdirAll(t.Dir, 0755); err != nil {
e.Logger.Errf("cannot make directory %v: %v", t.Dir, err)
return err
}
}
for i := range t.Cmds {
if err := e.runCommand(ctx, t, call, i); err != nil {
if err2 := e.statusOnError(t); err2 != nil {

View File

@ -611,3 +611,29 @@ func TestWhenDirAttributeAndDirExistsItRunsInThatDir(t *testing.T) {
assert.Equal(t, expected, got, "Mismatch in the working directory")
}
func TestWhenDirAttributeItCreatesMissingAndRunsInThatDir(t *testing.T) {
const expected = "createme"
const dir = "testdata/dir/explicit_doesnt_exist/"
const toBeCreated = dir + expected
const target = "whereami"
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.Remove(toBeCreated)
if _, err := os.Stat(toBeCreated); err == nil {
t.Errorf("Directory should not exist: %v", err)
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.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.Remove(toBeCreated)
}

View File

@ -0,0 +1,8 @@
version: '2'
tasks:
whereami:
dir: createme
cmds:
- pwd
silent: true