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 {
|
func (e *Executor) Setup() error {
|
||||||
e.setupLogger()
|
e.setupLogger()
|
||||||
if err := e.setCurrentDir(); err != nil {
|
node, err := e.getRootNode()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := e.setupTempDir(); err != nil {
|
if err := e.setupTempDir(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := e.readTaskfile(); err != nil {
|
if err := e.readTaskfile(node); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
e.setupFuzzyModel()
|
e.setupFuzzyModel()
|
||||||
@ -44,45 +45,25 @@ func (e *Executor) Setup() error {
|
|||||||
if err := e.readDotEnvFiles(); err != nil {
|
if err := e.readDotEnvFiles(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.doVersionChecks(); err != nil {
|
if err := e.doVersionChecks(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
e.setupDefaults()
|
e.setupDefaults()
|
||||||
e.setupConcurrencyState()
|
e.setupConcurrencyState()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Executor) setCurrentDir() error {
|
func (e *Executor) getRootNode() (taskfile.Node, 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 {
|
|
||||||
node, err := taskfile.NewRootNode(e.Dir, e.Entrypoint, e.Insecure)
|
node, err := taskfile.NewRootNode(e.Dir, e.Entrypoint, e.Insecure)
|
||||||
if err != nil {
|
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(
|
e.Taskfile, err = taskfile.Read(
|
||||||
node,
|
node,
|
||||||
e.Insecure,
|
e.Insecure,
|
||||||
|
31
task_test.go
31
task_test.go
@ -109,6 +109,7 @@ func TestVars(t *testing.T) {
|
|||||||
|
|
||||||
func TestSpecialVars(t *testing.T) {
|
func TestSpecialVars(t *testing.T) {
|
||||||
const dir = "testdata/special_vars"
|
const dir = "testdata/special_vars"
|
||||||
|
const subdir = "testdata/special_vars/subdir"
|
||||||
toAbs := func(rel string) string {
|
toAbs := func(rel string) string {
|
||||||
abs, err := filepath.Abs(rel)
|
abs, err := filepath.Abs(rel)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@ -122,28 +123,32 @@ func TestSpecialVars(t *testing.T) {
|
|||||||
// Root
|
// Root
|
||||||
{target: "print-task", expected: "print-task"},
|
{target: "print-task", expected: "print-task"},
|
||||||
{target: "print-root-dir", expected: toAbs(dir)},
|
{target: "print-root-dir", expected: toAbs(dir)},
|
||||||
|
{target: "print-taskfile", expected: toAbs(dir) + "/Taskfile.yml"},
|
||||||
{target: "print-taskfile-dir", expected: toAbs(dir)},
|
{target: "print-taskfile-dir", expected: toAbs(dir)},
|
||||||
{target: "print-task-version", expected: "unknown"},
|
{target: "print-task-version", expected: "unknown"},
|
||||||
// Included
|
// Included
|
||||||
{target: "included:print-task", expected: "included:print-task"},
|
{target: "included:print-task", expected: "included:print-task"},
|
||||||
{target: "included:print-root-dir", expected: toAbs(dir)},
|
{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-taskfile-dir", expected: toAbs(dir) + "/included"},
|
||||||
{target: "included:print-task-version", expected: "unknown"},
|
{target: "included:print-task-version", expected: "unknown"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, dir := range []string{dir, subdir} {
|
||||||
t.Run(test.target, func(t *testing.T) {
|
for _, test := range tests {
|
||||||
var buff bytes.Buffer
|
t.Run(test.target, func(t *testing.T) {
|
||||||
e := &task.Executor{
|
var buff bytes.Buffer
|
||||||
Dir: dir,
|
e := &task.Executor{
|
||||||
Stdout: &buff,
|
Dir: dir,
|
||||||
Stderr: &buff,
|
Stdout: &buff,
|
||||||
Silent: true,
|
Stderr: &buff,
|
||||||
}
|
Silent: true,
|
||||||
require.NoError(t, e.Setup())
|
}
|
||||||
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target}))
|
require.NoError(t, e.Setup())
|
||||||
assert.Equal(t, test.expected+"\n", buff.String())
|
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,
|
entrypoint string,
|
||||||
insecure bool,
|
insecure bool,
|
||||||
) (Node, error) {
|
) (Node, error) {
|
||||||
|
dir = getDefaultDir(entrypoint, dir)
|
||||||
// Check if there is something to read on STDIN
|
// Check if there is something to read on STDIN
|
||||||
stat, _ := os.Stdin.Stat()
|
stat, _ := os.Stdin.Stat()
|
||||||
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
|
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
|
||||||
@ -68,3 +69,26 @@ func getScheme(uri string) string {
|
|||||||
}
|
}
|
||||||
return ""
|
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:
|
tasks:
|
||||||
print-task: echo {{.TASK}}
|
print-task: echo {{.TASK}}
|
||||||
print-root-dir: echo {{.ROOT_DIR}}
|
print-root-dir: echo {{.ROOT_DIR}}
|
||||||
|
print-taskfile: echo {{.TASKFILE}}
|
||||||
print-taskfile-dir: echo {{.TASKFILE_DIR}}
|
print-taskfile-dir: echo {{.TASKFILE_DIR}}
|
||||||
print-task-version: echo {{.TASK_VERSION}}
|
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:
|
tasks:
|
||||||
print-task: echo {{.TASK}}
|
print-task: echo {{.TASK}}
|
||||||
print-root-dir: echo {{.ROOT_DIR}}
|
print-root-dir: echo {{.ROOT_DIR}}
|
||||||
|
print-taskfile: echo {{.TASKFILE}}
|
||||||
print-taskfile-dir: echo {{.TASKFILE_DIR}}
|
print-taskfile-dir: echo {{.TASKFILE_DIR}}
|
||||||
print-task-version: echo {{.TASK_VERSION}}
|
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