Skip to content

Commit

Permalink
fix: taskfile directory (#1530)
Browse files Browse the repository at this point in the history
* fix: taskfile directory

* tests: add tests for special vars when running from a subdirectory
  • Loading branch information
pd93 authored Mar 4, 2024
1 parent 60d20c0 commit 3c05c9c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 43 deletions.
41 changes: 11 additions & 30 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import (

func (e *Executor) Setup() error {
e.setupLogger()
if err := e.setCurrentDir(); err != nil {
node, err := e.getRootNode()
if err != nil {
return err
}
if err := e.setupTempDir(); err != nil {
return err
}
if err := e.readTaskfile(); err != nil {
if err := e.readTaskfile(node); err != nil {
return err
}
e.setupFuzzyModel()
Expand All @@ -44,45 +45,25 @@ func (e *Executor) Setup() error {
if err := e.readDotEnvFiles(); err != nil {
return err
}

if err := e.doVersionChecks(); err != nil {
return err
}
e.setupDefaults()
e.setupConcurrencyState()

return nil
}

func (e *Executor) setCurrentDir() error {
// If the entrypoint is already set, we don't need to do anything
if e.Entrypoint != "" {
return nil
}

// Default the directory to the current working directory
if e.Dir == "" {
wd, err := os.Getwd()
if err != nil {
return err
}
e.Dir = wd
} else {
var err error
e.Dir, err = filepath.Abs(e.Dir)
if err != nil {
return err
}
}

return nil
}

func (e *Executor) readTaskfile() error {
func (e *Executor) getRootNode() (taskfile.Node, error) {
node, err := taskfile.NewRootNode(e.Dir, e.Entrypoint, e.Insecure)
if err != nil {
return err
return nil, err
}
e.Dir = node.BaseDir()
return node, err
}

func (e *Executor) readTaskfile(node taskfile.Node) error {
var err error
e.Taskfile, err = taskfile.Read(
node,
e.Insecure,
Expand Down
31 changes: 18 additions & 13 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestVars(t *testing.T) {

func TestSpecialVars(t *testing.T) {
const dir = "testdata/special_vars"
const subdir = "testdata/special_vars/subdir"
toAbs := func(rel string) string {
abs, err := filepath.Abs(rel)
assert.NoError(t, err)
Expand All @@ -122,28 +123,32 @@ func TestSpecialVars(t *testing.T) {
// Root
{target: "print-task", expected: "print-task"},
{target: "print-root-dir", expected: toAbs(dir)},
{target: "print-taskfile", expected: toAbs(dir) + "/Taskfile.yml"},
{target: "print-taskfile-dir", expected: toAbs(dir)},
{target: "print-task-version", expected: "unknown"},
// Included
{target: "included:print-task", expected: "included:print-task"},
{target: "included:print-root-dir", expected: toAbs(dir)},
{target: "included:print-taskfile", expected: toAbs(dir) + "/included/Taskfile.yml"},
{target: "included:print-taskfile-dir", expected: toAbs(dir) + "/included"},
{target: "included:print-task-version", expected: "unknown"},
}

for _, test := range tests {
t.Run(test.target, func(t *testing.T) {
var buff bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: true,
}
require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target}))
assert.Equal(t, test.expected+"\n", buff.String())
})
for _, dir := range []string{dir, subdir} {
for _, test := range tests {
t.Run(test.target, func(t *testing.T) {
var buff bytes.Buffer
e := &task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: true,
}
require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target}))
assert.Equal(t, test.expected+"\n", buff.String())
})
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions taskfile/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewRootNode(
entrypoint string,
insecure bool,
) (Node, error) {
dir = getDefaultDir(entrypoint, dir)
// Check if there is something to read on STDIN
stat, _ := os.Stdin.Stat()
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
Expand Down Expand Up @@ -68,3 +69,26 @@ func getScheme(uri string) string {
}
return ""
}

func getDefaultDir(entrypoint, dir string) string {
// If the entrypoint and dir are empty, we default the directory to the current working directory
if dir == "" {
if entrypoint == "" {
wd, err := os.Getwd()
if err != nil {
return ""
}
dir = wd
}
return dir
}

// If the directory is set, ensure it is an absolute path
var err error
dir, err = filepath.Abs(dir)
if err != nil {
return ""
}

return dir
}
1 change: 1 addition & 0 deletions testdata/special_vars/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ includes:
tasks:
print-task: echo {{.TASK}}
print-root-dir: echo {{.ROOT_DIR}}
print-taskfile: echo {{.TASKFILE}}
print-taskfile-dir: echo {{.TASKFILE_DIR}}
print-task-version: echo {{.TASK_VERSION}}
1 change: 1 addition & 0 deletions testdata/special_vars/included/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ version: '3'
tasks:
print-task: echo {{.TASK}}
print-root-dir: echo {{.ROOT_DIR}}
print-taskfile: echo {{.TASKFILE}}
print-taskfile-dir: echo {{.TASKFILE_DIR}}
print-task-version: echo {{.TASK_VERSION}}
Empty file.

0 comments on commit 3c05c9c

Please sign in to comment.