2017-03-16 02:15:27 +02:00
|
|
|
package task_test
|
|
|
|
|
|
|
|
import (
|
2017-05-17 19:37:11 +02:00
|
|
|
"bytes"
|
2019-02-09 14:16:13 +02:00
|
|
|
"context"
|
2017-07-12 11:30:28 +02:00
|
|
|
"fmt"
|
2017-03-25 15:51:30 +02:00
|
|
|
"io/ioutil"
|
2017-03-16 02:15:27 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-25 15:51:30 +02:00
|
|
|
"strings"
|
2017-03-16 02:15:27 +02:00
|
|
|
"testing"
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2018-11-05 01:23:35 +02:00
|
|
|
"github.com/go-task/task/v2"
|
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2018-07-15 20:37:20 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2017-06-16 16:24:01 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-03-16 02:15:27 +02:00
|
|
|
)
|
|
|
|
|
2017-07-20 09:05:37 +02:00
|
|
|
// fileContentTest provides a basic reusable test-case for running a Taskfile
|
|
|
|
// and inspect generated files.
|
|
|
|
type fileContentTest struct {
|
|
|
|
Dir string
|
|
|
|
Target string
|
|
|
|
TrimSpace bool
|
|
|
|
Files map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fct fileContentTest) name(file string) string {
|
|
|
|
return fmt.Sprintf("target=%q,file=%q", fct.Target, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fct fileContentTest) Run(t *testing.T) {
|
|
|
|
for f := range fct.Files {
|
|
|
|
_ = os.Remove(filepath.Join(fct.Dir, f))
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &task.Executor{
|
|
|
|
Dir: fct.Dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup(), "e.Setup()")
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: fct.Target}), "e.Run(target)")
|
2017-07-20 09:05:37 +02:00
|
|
|
|
|
|
|
for name, expectContent := range fct.Files {
|
|
|
|
t.Run(fct.name(name), func(t *testing.T) {
|
|
|
|
b, err := ioutil.ReadFile(filepath.Join(fct.Dir, name))
|
|
|
|
assert.NoError(t, err, "Error reading file")
|
|
|
|
s := string(b)
|
|
|
|
if fct.TrimSpace {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectContent, s, "unexpected file content")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 13:04:58 +02:00
|
|
|
func TestEnv(t *testing.T) {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: "testdata/env",
|
|
|
|
Target: "default",
|
|
|
|
TrimSpace: false,
|
|
|
|
Files: map[string]string{
|
2019-01-02 16:05:40 +02:00
|
|
|
"local.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n",
|
|
|
|
"global.txt": "FOO='foo' BAR='overriden' BAZ='baz'\n",
|
2017-08-16 13:04:58 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:50:39 +02:00
|
|
|
func TestVarsV1(t *testing.T) {
|
2017-07-20 09:05:37 +02:00
|
|
|
tt := fileContentTest{
|
2018-02-18 14:50:39 +02:00
|
|
|
Dir: "testdata/vars/v1",
|
2017-07-20 09:05:37 +02:00
|
|
|
Target: "default",
|
|
|
|
TrimSpace: true,
|
|
|
|
Files: map[string]string{
|
2017-11-19 22:46:46 +02:00
|
|
|
// hello task:
|
2017-07-20 09:05:37 +02:00
|
|
|
"foo.txt": "foo",
|
|
|
|
"bar.txt": "bar",
|
|
|
|
"baz.txt": "baz",
|
|
|
|
"tmpl_foo.txt": "foo",
|
2017-11-19 22:46:46 +02:00
|
|
|
"tmpl_bar.txt": "<no value>",
|
2017-07-20 09:05:37 +02:00
|
|
|
"tmpl_foo2.txt": "foo2",
|
|
|
|
"tmpl_bar2.txt": "bar2",
|
|
|
|
"shtmpl_foo.txt": "foo",
|
|
|
|
"shtmpl_foo2.txt": "foo2",
|
2017-11-19 22:46:46 +02:00
|
|
|
"nestedtmpl_foo.txt": "{{.FOO}}",
|
2017-07-20 09:05:37 +02:00
|
|
|
"nestedtmpl_foo2.txt": "foo2",
|
|
|
|
"foo2.txt": "foo2",
|
|
|
|
"bar2.txt": "bar2",
|
|
|
|
"baz2.txt": "baz2",
|
|
|
|
"tmpl2_foo.txt": "<no value>",
|
|
|
|
"tmpl2_foo2.txt": "foo2",
|
|
|
|
"tmpl2_bar.txt": "<no value>",
|
2017-11-19 22:46:46 +02:00
|
|
|
"tmpl2_bar2.txt": "<no value>",
|
2017-07-20 09:05:37 +02:00
|
|
|
"shtmpl2_foo.txt": "<no value>",
|
|
|
|
"shtmpl2_foo2.txt": "foo2",
|
2017-11-19 22:46:46 +02:00
|
|
|
"nestedtmpl2_foo2.txt": "{{.FOO2}}",
|
2017-07-20 09:05:37 +02:00
|
|
|
"override.txt": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
// Ensure identical results when running hello task directly.
|
|
|
|
tt.Target = "hello"
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
2018-02-18 14:50:39 +02:00
|
|
|
|
|
|
|
func TestVarsV2(t *testing.T) {
|
2017-09-03 12:48:06 +02:00
|
|
|
tt := fileContentTest{
|
2018-02-18 14:50:39 +02:00
|
|
|
Dir: "testdata/vars/v2",
|
2017-09-03 12:48:06 +02:00
|
|
|
Target: "default",
|
2018-02-18 14:50:39 +02:00
|
|
|
TrimSpace: true,
|
2017-09-03 12:48:06 +02:00
|
|
|
Files: map[string]string{
|
2018-02-18 14:50:39 +02:00
|
|
|
"foo.txt": "foo",
|
|
|
|
"bar.txt": "bar",
|
|
|
|
"baz.txt": "baz",
|
|
|
|
"tmpl_foo.txt": "foo",
|
|
|
|
"tmpl_bar.txt": "bar",
|
|
|
|
"tmpl_foo2.txt": "foo2",
|
|
|
|
"tmpl_bar2.txt": "bar2",
|
|
|
|
"shtmpl_foo.txt": "foo",
|
|
|
|
"shtmpl_foo2.txt": "foo2",
|
|
|
|
"nestedtmpl_foo.txt": "<no value>",
|
|
|
|
"nestedtmpl_foo2.txt": "foo2",
|
|
|
|
"foo2.txt": "foo2",
|
|
|
|
"bar2.txt": "bar2",
|
|
|
|
"baz2.txt": "baz2",
|
|
|
|
"tmpl2_foo.txt": "<no value>",
|
|
|
|
"tmpl2_foo2.txt": "foo2",
|
|
|
|
"tmpl2_bar.txt": "<no value>",
|
|
|
|
"tmpl2_bar2.txt": "bar2",
|
|
|
|
"shtmpl2_foo.txt": "<no value>",
|
|
|
|
"shtmpl2_foo2.txt": "foo2",
|
|
|
|
"nestedtmpl2_foo2.txt": "<no value>",
|
|
|
|
"override.txt": "bar",
|
2018-03-04 20:39:14 +02:00
|
|
|
"nested.txt": "Taskvars-TaskfileVars-TaskVars",
|
2017-09-03 12:48:06 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
2018-02-18 14:50:39 +02:00
|
|
|
// Ensure identical results when running hello task directly.
|
|
|
|
tt.Target = "hello"
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultilineVars(t *testing.T) {
|
|
|
|
for _, dir := range []string{"testdata/vars/v1/multiline", "testdata/vars/v2/multiline"} {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: dir,
|
|
|
|
Target: "default",
|
|
|
|
TrimSpace: false,
|
|
|
|
Files: map[string]string{
|
|
|
|
// Note:
|
|
|
|
// - task does not strip a trailing newline from var entries
|
|
|
|
// - task strips one trailing newline from shell output
|
|
|
|
// - the cat command adds a trailing newline
|
|
|
|
"echo_foobar.txt": "foo\nbar\n",
|
|
|
|
"echo_n_foobar.txt": "foo\nbar\n",
|
|
|
|
"echo_n_multiline.txt": "\n\nfoo\n bar\nfoobar\n\nbaz\n\n",
|
|
|
|
"var_multiline.txt": "\n\nfoo\n bar\nfoobar\n\nbaz\n\n\n",
|
|
|
|
"var_catlines.txt": " foo bar foobar baz \n",
|
|
|
|
"var_enumfile.txt": "0:\n1:\n2:foo\n3: bar\n4:foobar\n5:\n6:baz\n7:\n8:\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
2017-09-03 12:48:06 +02:00
|
|
|
}
|
2017-07-20 09:05:37 +02:00
|
|
|
|
|
|
|
func TestVarsInvalidTmpl(t *testing.T) {
|
|
|
|
const (
|
2018-02-18 14:50:39 +02:00
|
|
|
dir = "testdata/vars/v1"
|
2017-07-20 09:05:37 +02:00
|
|
|
target = "invalid-var-tmpl"
|
|
|
|
expectError = "template: :1: unexpected EOF"
|
|
|
|
)
|
|
|
|
|
|
|
|
e := &task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup(), "e.Setup()")
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.EqualError(t, e.Run(context.Background(), taskfile.Call{Task: target}), expectError, "e.Run(target)")
|
2017-07-20 09:05:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParams(t *testing.T) {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: "testdata/params",
|
|
|
|
Target: "default",
|
|
|
|
TrimSpace: false,
|
|
|
|
Files: map[string]string{
|
|
|
|
"hello.txt": "Hello\n",
|
|
|
|
"world.txt": "World\n",
|
|
|
|
"exclamation.txt": "!\n",
|
|
|
|
"dep1.txt": "Dependence1\n",
|
|
|
|
"dep2.txt": "Dependence2\n",
|
|
|
|
"spanish.txt": "¡Holla mundo!\n",
|
|
|
|
"spanish-dep.txt": "¡Holla dependencia!\n",
|
|
|
|
"portuguese.txt": "Olá, mundo!\n",
|
|
|
|
"portuguese2.txt": "Olá, mundo!\n",
|
|
|
|
"german.txt": "Welt!\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
|
|
|
|
2017-03-16 02:15:27 +02:00
|
|
|
func TestDeps(t *testing.T) {
|
|
|
|
const dir = "testdata/deps"
|
|
|
|
|
|
|
|
files := []string{
|
|
|
|
"d1.txt",
|
|
|
|
"d2.txt",
|
|
|
|
"d3.txt",
|
|
|
|
"d11.txt",
|
|
|
|
"d12.txt",
|
|
|
|
"d13.txt",
|
|
|
|
"d21.txt",
|
|
|
|
"d22.txt",
|
|
|
|
"d23.txt",
|
|
|
|
"d31.txt",
|
|
|
|
"d32.txt",
|
|
|
|
"d33.txt",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
2017-03-25 15:52:41 +02:00
|
|
|
_ = os.Remove(filepath.Join(dir, f))
|
2017-03-16 02:15:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 16:24:01 +02:00
|
|
|
e := &task.Executor{
|
2017-07-01 20:32:13 +02:00
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
2017-03-16 02:15:27 +02:00
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup())
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "default"}))
|
2017-03-16 02:15:27 +02:00
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
f = filepath.Join(dir, f)
|
|
|
|
if _, err := os.Stat(f); err != nil {
|
|
|
|
t.Errorf("File %s should exists", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 15:51:30 +02:00
|
|
|
|
2017-05-17 19:37:11 +02:00
|
|
|
func TestStatus(t *testing.T) {
|
|
|
|
const dir = "testdata/status"
|
|
|
|
var file = filepath.Join(dir, "foo.txt")
|
|
|
|
|
|
|
|
_ = os.Remove(file)
|
|
|
|
|
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Errorf("File should not exists: %v", err)
|
|
|
|
}
|
2017-07-01 20:32:13 +02:00
|
|
|
|
2018-02-17 20:12:41 +02:00
|
|
|
var buff bytes.Buffer
|
2017-07-01 20:32:13 +02:00
|
|
|
e := &task.Executor{
|
|
|
|
Dir: dir,
|
2018-02-17 20:12:41 +02:00
|
|
|
Stdout: &buff,
|
|
|
|
Stderr: &buff,
|
|
|
|
Silent: true,
|
2017-05-17 19:37:11 +02:00
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup())
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-foo"}))
|
2017-07-01 20:32:13 +02:00
|
|
|
|
2017-05-17 19:37:11 +02:00
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Errorf("File should exists: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:12:41 +02:00
|
|
|
e.Silent = false
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-foo"}))
|
2017-07-01 20:32:13 +02:00
|
|
|
|
2017-05-17 19:37:11 +02:00
|
|
|
if buff.String() != `task: Task "gen-foo" is up to date`+"\n" {
|
|
|
|
t.Errorf("Wrong output message: %s", buff.String())
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 20:38:46 +02:00
|
|
|
|
2017-07-12 11:30:28 +02:00
|
|
|
func TestGenerates(t *testing.T) {
|
2019-02-22 01:52:27 +02:00
|
|
|
const (
|
|
|
|
srcTask = "sub/src.txt"
|
|
|
|
relTask = "rel.txt"
|
|
|
|
absTask = "abs.txt"
|
|
|
|
fileWithSpaces = "my text file.txt"
|
|
|
|
)
|
2017-07-12 11:30:28 +02:00
|
|
|
|
|
|
|
// This test does not work with a relative dir.
|
|
|
|
dir, err := filepath.Abs("testdata/generates")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var srcFile = filepath.Join(dir, srcTask)
|
|
|
|
|
2019-02-22 01:52:27 +02:00
|
|
|
for _, task := range []string{srcTask, relTask, absTask, fileWithSpaces} {
|
2017-07-12 11:30:28 +02:00
|
|
|
path := filepath.Join(dir, task)
|
|
|
|
_ = os.Remove(path)
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
t.Errorf("File should not exists: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buff := bytes.NewBuffer(nil)
|
|
|
|
e := &task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: buff,
|
|
|
|
Stderr: buff,
|
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup())
|
2017-07-12 11:30:28 +02:00
|
|
|
|
2019-02-22 01:52:27 +02:00
|
|
|
for _, theTask := range []string{relTask, absTask, fileWithSpaces} {
|
2017-09-07 18:57:06 +02:00
|
|
|
var destFile = filepath.Join(dir, theTask)
|
2017-07-12 11:30:28 +02:00
|
|
|
var upToDate = fmt.Sprintf("task: Task \"%s\" is up to date\n", srcTask) +
|
2017-09-07 18:57:06 +02:00
|
|
|
fmt.Sprintf("task: Task \"%s\" is up to date\n", theTask)
|
2017-07-12 11:30:28 +02:00
|
|
|
|
|
|
|
// Run task for the first time.
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: theTask}))
|
2017-07-12 11:30:28 +02:00
|
|
|
|
|
|
|
if _, err := os.Stat(srcFile); err != nil {
|
|
|
|
t.Errorf("File should exists: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(destFile); err != nil {
|
|
|
|
t.Errorf("File should exists: %v", err)
|
|
|
|
}
|
|
|
|
// Ensure task was not incorrectly found to be up-to-date on first run.
|
|
|
|
if buff.String() == upToDate {
|
|
|
|
t.Errorf("Wrong output message: %s", buff.String())
|
|
|
|
}
|
|
|
|
buff.Reset()
|
|
|
|
|
|
|
|
// Re-run task to ensure it's now found to be up-to-date.
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: theTask}))
|
2017-07-12 11:30:28 +02:00
|
|
|
if buff.String() != upToDate {
|
|
|
|
t.Errorf("Wrong output message: %s", buff.String())
|
|
|
|
}
|
|
|
|
buff.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:44:13 +02:00
|
|
|
func TestStatusChecksum(t *testing.T) {
|
|
|
|
const dir = "testdata/checksum"
|
|
|
|
|
|
|
|
files := []string{
|
|
|
|
"generated.txt",
|
2017-11-02 14:37:02 +02:00
|
|
|
".task/checksum/build",
|
2017-09-16 16:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
_ = os.Remove(filepath.Join(dir, f))
|
|
|
|
|
|
|
|
_, err := os.Stat(filepath.Join(dir, f))
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: &buff,
|
|
|
|
Stderr: &buff,
|
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup())
|
2017-09-16 16:44:13 +02:00
|
|
|
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
|
2017-09-16 16:44:13 +02:00
|
|
|
for _, f := range files {
|
|
|
|
_, err := os.Stat(filepath.Join(dir, f))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buff.Reset()
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
|
2017-09-16 16:44:13 +02:00
|
|
|
assert.Equal(t, `task: Task "build" is up to date`+"\n", buff.String())
|
|
|
|
}
|
|
|
|
|
2017-05-17 20:38:46 +02:00
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
const dir = "testdata/init"
|
|
|
|
var file = filepath.Join(dir, "Taskfile.yml")
|
|
|
|
|
|
|
|
_ = os.Remove(file)
|
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Errorf("Taskfile.yml should not exists")
|
|
|
|
}
|
|
|
|
|
2017-07-31 00:29:49 +02:00
|
|
|
if err := task.InitTaskfile(ioutil.Discard, dir); err != nil {
|
2017-05-17 20:38:46 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-07-01 20:32:13 +02:00
|
|
|
|
2017-05-17 20:38:46 +02:00
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Errorf("Taskfile.yml should exists")
|
|
|
|
}
|
|
|
|
}
|
2017-07-02 20:30:50 +02:00
|
|
|
|
2017-07-08 18:33:55 +02:00
|
|
|
func TestCyclicDep(t *testing.T) {
|
|
|
|
const dir = "testdata/cyclic"
|
|
|
|
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup())
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.IsType(t, &task.MaximumTaskCallExceededError{}, e.Run(context.Background(), taskfile.Call{Task: "task-1"}))
|
2017-07-08 18:33:55 +02:00
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
|
|
|
|
func TestTaskVersion(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Dir string
|
2018-03-03 23:54:42 +02:00
|
|
|
Version string
|
2017-12-29 22:27:32 +02:00
|
|
|
}{
|
2018-03-03 23:54:42 +02:00
|
|
|
{"testdata/version/v1", "1"},
|
|
|
|
{"testdata/version/v2", "2"},
|
2017-12-29 22:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Dir, func(t *testing.T) {
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: test.Dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
}
|
2018-03-11 19:39:40 +02:00
|
|
|
assert.NoError(t, e.Setup())
|
2017-12-29 22:27:32 +02:00
|
|
|
assert.Equal(t, test.Version, e.Taskfile.Version)
|
|
|
|
assert.Equal(t, 2, len(e.Taskfile.Tasks))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 10:44:58 +02:00
|
|
|
|
|
|
|
func TestTaskIgnoreErrors(t *testing.T) {
|
|
|
|
const dir = "testdata/ignore_errors"
|
|
|
|
|
2018-08-05 17:56:55 +02:00
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.Setup())
|
|
|
|
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task-should-pass"}))
|
|
|
|
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "task-should-fail"}))
|
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "cmd-should-pass"}))
|
|
|
|
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "cmd-should-fail"}))
|
2018-07-10 10:44:58 +02:00
|
|
|
}
|
2018-08-01 10:47:25 +02:00
|
|
|
|
2018-07-15 20:37:20 +02:00
|
|
|
func TestExpand(t *testing.T) {
|
|
|
|
const dir = "testdata/expand"
|
|
|
|
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't get $HOME: %v", err)
|
|
|
|
}
|
|
|
|
var buff bytes.Buffer
|
|
|
|
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: &buff,
|
|
|
|
Stderr: &buff,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.Setup())
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "pwd"}))
|
2018-07-15 20:37:20 +02:00
|
|
|
assert.Equal(t, home, strings.TrimSpace(buff.String()))
|
|
|
|
}
|
2018-08-01 00:09:55 +02:00
|
|
|
|
2018-08-05 16:28:02 +02:00
|
|
|
func TestDry(t *testing.T) {
|
|
|
|
const dir = "testdata/dry"
|
2018-08-01 00:09:55 +02:00
|
|
|
|
|
|
|
file := filepath.Join(dir, "file.txt")
|
|
|
|
_ = os.Remove(file)
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: &buff,
|
|
|
|
Stderr: &buff,
|
2018-08-05 16:28:02 +02:00
|
|
|
Dry: true,
|
2018-08-01 00:09:55 +02:00
|
|
|
}
|
|
|
|
assert.NoError(t, e.Setup())
|
2019-02-09 14:16:13 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
|
2018-08-01 00:09:55 +02:00
|
|
|
|
2018-08-05 16:28:02 +02:00
|
|
|
assert.Equal(t, "touch file.txt", strings.TrimSpace(buff.String()))
|
2018-08-01 00:09:55 +02:00
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Errorf("File should not exist %s", file)
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 21:56:51 +02:00
|
|
|
|
2019-02-09 14:41:19 +02:00
|
|
|
// TestDryChecksum tests if the checksum file is not being written to disk
|
|
|
|
// if the dry mode is enabled.
|
|
|
|
func TestDryChecksum(t *testing.T) {
|
|
|
|
const dir = "testdata/dry_checksum"
|
|
|
|
|
|
|
|
checksumFile := filepath.Join(dir, ".task/checksum/default")
|
|
|
|
_ = os.Remove(checksumFile)
|
|
|
|
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
Dry: true,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.Setup())
|
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "default"}))
|
|
|
|
|
|
|
|
_, err := os.Stat(checksumFile)
|
|
|
|
assert.Error(t, err, "checksum file should not exist")
|
|
|
|
|
|
|
|
e.Dry = false
|
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "default"}))
|
|
|
|
_, err = os.Stat(checksumFile)
|
|
|
|
assert.NoError(t, err, "checksum file should exist")
|
|
|
|
}
|
|
|
|
|
2018-10-13 21:56:51 +02:00
|
|
|
func TestIncludes(t *testing.T) {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: "testdata/includes",
|
|
|
|
Target: "default",
|
|
|
|
TrimSpace: true,
|
|
|
|
Files: map[string]string{
|
2018-10-13 22:52:09 +02:00
|
|
|
"main.txt": "main",
|
|
|
|
"included_directory.txt": "included_directory",
|
|
|
|
"included_taskfile.txt": "included_taskfile",
|
2018-10-13 21:56:51 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
2018-12-02 18:17:32 +02:00
|
|
|
|
|
|
|
func TestIncludesEmptyMain(t *testing.T) {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: "testdata/includes_empty",
|
|
|
|
Target: "included:default",
|
|
|
|
TrimSpace: true,
|
|
|
|
Files: map[string]string{
|
|
|
|
"file.txt": "default",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
2018-12-09 19:54:58 +02:00
|
|
|
|
|
|
|
func TestIncludesDependencies(t *testing.T) {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: "testdata/includes_deps",
|
|
|
|
Target: "default",
|
|
|
|
TrimSpace: true,
|
|
|
|
Files: map[string]string{
|
|
|
|
"default.txt": "default",
|
|
|
|
"called_dep.txt": "called_dep",
|
|
|
|
"called_task.txt": "called_task",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
2019-02-03 01:12:57 +02:00
|
|
|
|
|
|
|
func TestIncludesCallingRoot(t *testing.T) {
|
|
|
|
tt := fileContentTest{
|
|
|
|
Dir: "testdata/includes_call_root_task",
|
|
|
|
Target: "included:call-root",
|
|
|
|
TrimSpace: true,
|
|
|
|
Files: map[string]string{
|
|
|
|
"root_task.txt": "root task",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Run(t)
|
|
|
|
}
|
2019-02-24 10:53:49 +02:00
|
|
|
|
2019-02-24 12:09:55 +02:00
|
|
|
func TestDetailsParsing(t *testing.T) {
|
|
|
|
const dir = "testdata/details"
|
|
|
|
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.Setup())
|
|
|
|
|
|
|
|
assert.Equal(t, e.Taskfile.Tasks["task-with-details"].Details, "This is a very long detailed description\nwith multiple lines\n")
|
2019-02-24 12:19:08 +02:00
|
|
|
assert.Equal(t, e.Taskfile.Tasks["other-task-with-details"].Details, "details of other-task-with-details")
|
2019-02-24 12:09:55 +02:00
|
|
|
assert.Equal(t, e.Taskfile.Tasks["task-without-details"].Details, "")
|
|
|
|
}
|
|
|
|
|
2019-02-24 10:53:49 +02:00
|
|
|
func TestDetails(t *testing.T) {
|
|
|
|
const dir = "testdata/details"
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: &buff,
|
|
|
|
Stderr: &buff,
|
|
|
|
Details: true,
|
2019-02-24 12:01:48 +02:00
|
|
|
Silent: true,
|
2019-02-24 10:53:49 +02:00
|
|
|
}
|
|
|
|
assert.NoError(t, e.Setup())
|
2019-02-24 12:01:48 +02:00
|
|
|
|
2019-02-24 12:09:55 +02:00
|
|
|
buff.Reset()
|
2019-02-24 12:01:48 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task-with-details"}))
|
2019-02-24 12:09:55 +02:00
|
|
|
assert.Contains(t, buff.String(), "This is a very long detailed description")
|
|
|
|
assert.Contains(t, buff.String(), "with multiple lines")
|
|
|
|
|
2019-02-24 12:01:48 +02:00
|
|
|
assert.NotContains(t, buff.String(), "task-with-details was executed")
|
|
|
|
assert.NotContains(t, buff.String(), "dependend-task was executed")
|
|
|
|
|
2019-02-24 12:09:55 +02:00
|
|
|
buff.Reset()
|
2019-02-24 12:01:48 +02:00
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task-without-details"}))
|
|
|
|
assert.NotContains(t, buff.String(), "task-without-details was executed")
|
|
|
|
assert.NotContains(t, buff.String(), "dependend-task was executed")
|
|
|
|
|
2019-02-24 12:09:55 +02:00
|
|
|
buff.Reset()
|
|
|
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "other-task-with-details"}, taskfile.Call{Task: "task-with-details"}))
|
|
|
|
assert.Contains(t, buff.String(), "details of other-task-with-details")
|
|
|
|
assert.NotContains(t, buff.String(), "This is a very long detailed description")
|
2019-02-24 10:53:49 +02:00
|
|
|
}
|