1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +02:00
task/task_test.go

147 lines
2.4 KiB
Go
Raw Normal View History

2017-03-16 02:15:27 +02:00
package task_test
import (
"bytes"
2017-03-25 15:51:30 +02:00
"io/ioutil"
2017-03-16 02:15:27 +02:00
"os"
"os/exec"
"path/filepath"
2017-03-25 15:51:30 +02:00
"strings"
2017-03-16 02:15:27 +02:00
"testing"
)
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
}
c := exec.Command("task")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
return
}
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"},
{"equal.txt", "foo=bar"},
2017-03-25 15:51:30 +02:00
}
for _, f := range files {
_ = os.Remove(filepath.Join(dir, f.file))
}
c := exec.Command("task")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
return
}
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))
}
c := exec.Command("task")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
return
}
for _, f := range files {
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
t.Error(err)
}
}
}
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)
}
c := exec.Command("task", "gen-foo")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
}
if _, err := os.Stat(file); err != nil {
t.Errorf("File should exists: %v", err)
}
buff := bytes.NewBuffer(nil)
c = exec.Command("task", "gen-foo")
c.Dir = dir
c.Stderr = buff
c.Stdout = buff
if err := c.Run(); err != nil {
t.Error(err)
}
if buff.String() != `task: Task "gen-foo" is up to date`+"\n" {
t.Errorf("Wrong output message: %s", buff.String())
}
}