mirror of
https://github.com/go-task/task.git
synced 2024-12-12 10:45:49 +02:00
fix error in evaluating dynamic variables with newly created directory
This commit is contained in:
parent
41a71e1dee
commit
93dcb20e12
@ -47,10 +47,10 @@ func RunCommand(ctx context.Context, opts *RunCommandOptions) error {
|
||||
|
||||
r, err := interp.New(
|
||||
interp.Params("-e"),
|
||||
interp.Dir(opts.Dir),
|
||||
interp.Env(expand.ListEnviron(environ...)),
|
||||
interp.OpenHandler(openHandler),
|
||||
interp.StdIO(opts.Stdin, opts.Stdout, opts.Stderr),
|
||||
dirOption(opts.Dir),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -87,3 +87,24 @@ func openHandler(ctx context.Context, path string, flag int, perm os.FileMode) (
|
||||
}
|
||||
return interp.DefaultOpenHandler()(ctx, path, flag, perm)
|
||||
}
|
||||
|
||||
func dirOption(path string) interp.RunnerOption {
|
||||
return func(r *interp.Runner) error {
|
||||
err := interp.Dir(path)(r)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If the specified directory doesn't exist, it will be created later.
|
||||
// Therefore, even if `interp.Dir` method returns an error, the
|
||||
// directory path should be set only when the directory cannot be found.
|
||||
if absPath, _ := filepath.Abs(path); absPath != "" {
|
||||
if _, err := os.Stat(absPath); os.IsNotExist(err) {
|
||||
r.Dir = absPath
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
27
task_test.go
27
task_test.go
@ -877,6 +877,33 @@ func TestWhenDirAttributeItCreatesMissingAndRunsInThatDir(t *testing.T) {
|
||||
_ = os.RemoveAll(toBeCreated)
|
||||
}
|
||||
|
||||
func TestDynamicVariablesRunOnTheNewCreatedDir(t *testing.T) {
|
||||
const expected = "created"
|
||||
const dir = "testdata/dir/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)
|
||||
}
|
||||
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.RemoveAll(toBeCreated)
|
||||
}
|
||||
|
||||
func TestDynamicVariablesShouldRunOnTheTaskDir(t *testing.T) {
|
||||
tt := fileContentTest{
|
||||
Dir: "testdata/dir/dynamic_var",
|
||||
|
10
testdata/dir/dynamic_var_on_created_dir/Taskfile.yml
vendored
Normal file
10
testdata/dir/dynamic_var_on_created_dir/Taskfile.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
dir: created
|
||||
cmds:
|
||||
- echo {{.TASK_DIR}}
|
||||
vars:
|
||||
TASK_DIR:
|
||||
sh: echo $(pwd)
|
Loading…
Reference in New Issue
Block a user