2017-03-16 02:15:27 +02:00
|
|
|
package task_test
|
|
|
|
|
|
|
|
import (
|
2017-05-17 19:37:11 +02:00
|
|
|
"bytes"
|
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
|
|
|
|
|
|
|
"github.com/go-task/task"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
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
|
|
|
}
|
2017-06-16 16:24:01 +02:00
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
assert.NoError(t, e.Run("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
|
|
|
|
|
|
|
func TestVars(t *testing.T) {
|
|
|
|
const dir = "testdata/vars"
|
|
|
|
|
|
|
|
files := []struct {
|
|
|
|
file string
|
|
|
|
content string
|
|
|
|
}{
|
|
|
|
{"foo.txt", "foo"},
|
|
|
|
{"bar.txt", "bar"},
|
|
|
|
{"foo2.txt", "foo2"},
|
|
|
|
{"bar2.txt", "bar2"},
|
2017-03-25 16:01:44 +02:00
|
|
|
{"equal.txt", "foo=bar"},
|
2017-03-25 15:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
_ = os.Remove(filepath.Join(dir, f.file))
|
|
|
|
}
|
|
|
|
|
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-25 15:51:30 +02:00
|
|
|
}
|
2017-06-16 16:24:01 +02:00
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
assert.NoError(t, e.Run("default"))
|
2017-03-25 15:51:30 +02:00
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
d, err := ioutil.ReadFile(filepath.Join(dir, f.file))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading %s: %v", f.file, err)
|
|
|
|
}
|
|
|
|
s := string(d)
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
if s != f.content {
|
|
|
|
t.Errorf("File content should be %s but is %s", f.content, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 20:26:42 +02:00
|
|
|
|
|
|
|
func TestTaskCall(t *testing.T) {
|
|
|
|
const dir = "testdata/task_call"
|
|
|
|
|
|
|
|
files := []string{
|
|
|
|
"foo.txt",
|
|
|
|
"bar.txt",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
_ = os.Remove(filepath.Join(dir, f))
|
|
|
|
}
|
|
|
|
|
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-25 20:26:42 +02:00
|
|
|
}
|
2017-06-16 16:24:01 +02:00
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
assert.NoError(t, e.Run("default"))
|
2017-03-25 20:26:42 +02:00
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
e := &task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
2017-05-17 19:37:11 +02:00
|
|
|
}
|
2017-07-01 20:32:13 +02:00
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
assert.NoError(t, e.Run("gen-foo"))
|
|
|
|
|
2017-05-17 19:37:11 +02:00
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Errorf("File should exists: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buff := bytes.NewBuffer(nil)
|
2017-07-01 20:32:13 +02:00
|
|
|
e.Stdout, e.Stderr = buff, buff
|
|
|
|
assert.NoError(t, e.Run("gen-foo"))
|
|
|
|
|
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) {
|
|
|
|
var srcTask = "sub/src.txt"
|
|
|
|
var relTask = "rel.txt"
|
|
|
|
var absTask = "abs.txt"
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
for _, task := range []string{srcTask, relTask, absTask} {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
|
|
|
|
for _, task := range []string{relTask, absTask} {
|
|
|
|
var destFile = filepath.Join(dir, task)
|
|
|
|
var upToDate = fmt.Sprintf("task: Task \"%s\" is up to date\n", srcTask) +
|
|
|
|
fmt.Sprintf("task: Task \"%s\" is up to date\n", task)
|
|
|
|
|
|
|
|
// Run task for the first time.
|
|
|
|
assert.NoError(t, e.Run(task))
|
|
|
|
|
|
|
|
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.
|
|
|
|
assert.NoError(t, e.Run(task))
|
|
|
|
if buff.String() != upToDate {
|
|
|
|
t.Errorf("Wrong output message: %s", buff.String())
|
|
|
|
}
|
|
|
|
buff.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01 20:32:13 +02:00
|
|
|
if err := task.InitTaskfile(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
|
|
|
|
|
|
|
func TestParams(t *testing.T) {
|
|
|
|
const dir = "testdata/params"
|
|
|
|
var files = []struct {
|
|
|
|
file string
|
|
|
|
content string
|
|
|
|
}{
|
|
|
|
{"hello.txt", "Hello\n"},
|
|
|
|
{"world.txt", "World\n"},
|
|
|
|
{"exclamation.txt", "!\n"},
|
|
|
|
{"dep1.txt", "Dependence1\n"},
|
|
|
|
{"dep2.txt", "Dependence2\n"},
|
2017-07-06 18:40:21 +02:00
|
|
|
{"spanish.txt", "¡Holla mundo!\n"},
|
2017-07-09 23:37:50 +02:00
|
|
|
{"spanish-dep.txt", "¡Holla dependencia!\n"},
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
_ = os.Remove(filepath.Join(dir, f.file))
|
|
|
|
}
|
|
|
|
|
|
|
|
e := task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: ioutil.Discard,
|
|
|
|
Stderr: ioutil.Discard,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
assert.NoError(t, e.Run("default"))
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
content, err := ioutil.ReadFile(filepath.Join(dir, f.file))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, f.content, string(content))
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.ReadTaskfile())
|
|
|
|
assert.IsType(t, &task.MaximumTaskCallExceededError{}, e.Run("task-1"))
|
|
|
|
}
|