mirror of
https://github.com/go-task/task.git
synced 2025-03-05 15:05:42 +02:00
fix: taskfile directory (#1530)
* fix: taskfile directory * tests: add tests for special vars when running from a subdirectory
This commit is contained in:
parent
60d20c042e
commit
3c05c9c6e1
41
setup.go
41
setup.go
@ -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()
|
||||
@ -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,
|
||||
|
31
task_test.go
31
task_test.go
@ -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)
|
||||
@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
@ -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
testdata/special_vars/Taskfile.yml
vendored
1
testdata/special_vars/Taskfile.yml
vendored
@ -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
testdata/special_vars/included/Taskfile.yml
vendored
1
testdata/special_vars/included/Taskfile.yml
vendored
@ -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}}
|
||||
|
0
testdata/special_vars/subdir/.gitkeep
vendored
Normal file
0
testdata/special_vars/subdir/.gitkeep
vendored
Normal file
Loading…
x
Reference in New Issue
Block a user