Skip to content

Commit

Permalink
fix: getSpecialVars
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Jan 4, 2024
1 parent 3830e42 commit 5aa6190
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
55 changes: 30 additions & 25 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,42 @@ func TestVars(t *testing.T) {

func TestSpecialVars(t *testing.T) {
const dir = "testdata/special_vars"
const target = "default"

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: target}))

toAbs := func(rel string) string {
abs, err := filepath.Abs(rel)
require.NoError(t, err)
assert.NoError(t, err)
return abs
}

output := buff.String()

// Root Taskfile
assert.Contains(t, output, "root/TASK=print")
assert.Contains(t, output, "root/ROOT_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "root/TASKFILE_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "root/TASK_VERSION=unknown")
tests := []struct {
target string
expected string
}{
// Root
{target: "print-task", expected: "print-task"},
{target: "print-root-dir", expected: toAbs(dir)},
{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-dir", expected: toAbs(dir) + "/included"},
{target: "included:print-task-version", expected: "unknown"},
}

// Included Taskfile
assert.Contains(t, output, "included/TASK=included:print")
assert.Contains(t, output, "included/ROOT_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "included/TASKFILE_DIR="+toAbs("testdata/special_vars/included"))
assert.Contains(t, output, "included/TASK_VERSION=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())
})
}
}

func TestConcurrency(t *testing.T) {
Expand Down
15 changes: 4 additions & 11 deletions testdata/special_vars/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ includes:
dir: ./included

tasks:
default:
cmds:
- task: print
- task: included:print

print:
cmds:
- echo root/TASK={{.TASK}}
- echo root/ROOT_DIR={{.ROOT_DIR}}
- echo root/TASKFILE_DIR={{.TASKFILE_DIR}}
- echo root/TASK_VERSION={{.TASK_VERSION}}
print-task: echo {{.TASK}}
print-root-dir: echo {{.ROOT_DIR}}
print-taskfile-dir: echo {{.TASKFILE_DIR}}
print-task-version: echo {{.TASK_VERSION}}
10 changes: 4 additions & 6 deletions testdata/special_vars/included/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
version: '3'

tasks:
print:
cmds:
- echo included/TASK={{.TASK}}
- echo included/ROOT_DIR={{.ROOT_DIR}}
- echo included/TASKFILE_DIR={{.TASKFILE_DIR}}
- echo included/TASK_VERSION={{.TASK_VERSION}}
print-task: echo {{.TASK}}
print-root-dir: echo {{.ROOT_DIR}}
print-taskfile-dir: echo {{.TASKFILE_DIR}}
print-task-version: echo {{.TASK_VERSION}}

0 comments on commit 5aa6190

Please sign in to comment.