From 5aa619023a5c156c2ed2e2c7f64716d514b84a17 Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Tue, 5 Sep 2023 23:26:25 +0000 Subject: [PATCH] fix: getSpecialVars --- task_test.go | 55 +++++++++++---------- testdata/special_vars/Taskfile.yml | 15 ++---- testdata/special_vars/included/Taskfile.yml | 10 ++-- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/task_test.go b/task_test.go index 9f918bd23e..ab86728c36 100644 --- a/task_test.go +++ b/task_test.go @@ -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) { diff --git a/testdata/special_vars/Taskfile.yml b/testdata/special_vars/Taskfile.yml index 61623074b7..271356cf95 100644 --- a/testdata/special_vars/Taskfile.yml +++ b/testdata/special_vars/Taskfile.yml @@ -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}} diff --git a/testdata/special_vars/included/Taskfile.yml b/testdata/special_vars/included/Taskfile.yml index 881a9c3e8f..5eb75b1594 100644 --- a/testdata/special_vars/included/Taskfile.yml +++ b/testdata/special_vars/included/Taskfile.yml @@ -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}}