| 1 |
package godo |
| 2 |
|
| 3 |
import ( |
| 4 |
"sort" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| •8 |
"github.com/stretchr/testify/assert" |
| 9 |
|
| 10 |
"github.com/mgutz/str" |
| 11 |
) |
| 12 |
|
| 13 |
func TestSimpleTask(t *testing.T) { |
| 14 |
result := "" |
| 15 |
tasks := func(p *Project) { |
| 16 |
p.Task1("foo", func(c *Context) { |
| 17 |
result = "A" |
| 18 |
}) |
| 19 |
} |
| 20 |
|
| 21 |
runTask(tasks, "foo") |
| 22 |
if result != "A" { |
| 23 |
t.Error("should have run simple task") |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestErrorReturn(t *testing.T) { |
| 28 |
result := "" |
| 29 |
tasks := func(p *Project) { |
| 30 |
p.Task1("err", func(*Context) { |
| 31 |
Halt("error caught") |
| 32 |
|
| 33 |
result += "ERR" |
| 34 |
}) |
| 35 |
|
| 36 |
p.Task("foo", S{"err"}, func(*Context) { |
| 37 |
result = "A" |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
_, err := runTask(tasks, "foo") |
| 42 |
if result == "A" { |
| 43 |
t.Error("parent task should not run on error") |
| 44 |
} |
| 45 |
if err.Error() != `"foo>err": error caught` { |
| 46 |
t.Error("dependency errors should stop parent") |
| 47 |
} |
| 48 |
|
| 49 |
_, err = runTask(tasks, "err") |
| 50 |
if err.Error() != `"err": error caught` { |
| 51 |
t.Error("error was not handle properly") |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestTaskArgs(t *testing.T) { |
| 56 |
assert := assert.New(t) |
| 57 |
result := "" |
| 58 |
tasks := func(p *Project) { |
| 59 |
p.Task1("foo", func(c *Context) { |
| 60 |
name := c.Args.MustString("name") |
| 61 |
result = name |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
execCLI(tasks, []string{"foo", "--", "--name=gopher"}, nil) |
| 66 |
assert.Equal("gopher", result) |
| 67 |
assert.Panics(func() { |
| 68 |
runTask(tasks, "foo") |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
func TestDependency(t *testing.T) { |
| 73 |
result := "" |
| 74 |
tasks := func(p *Project) { |
| 75 |
p.Task1("foo", func(c *Context) { |
| 76 |
result = "A" |
| 77 |
}) |
| 78 |
|
| 79 |
p.Task("bar", S{"foo"}, nil) |
| 80 |
} |
| 81 |
runTask(tasks, "bar") |
| 82 |
if result != "A" { |
| 83 |
t.Error("should have run task's dependency") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestShouldExpandGlobs(t *testing.T) { |
| 88 |
result := "" |
| 89 |
tasks := func(p *Project) { |
| 90 |
p.Task("foo", nil, func(c *Context) { |
| 91 |
result = "A" |
| 92 |
}).Src("test/**/*.txt") |
| 93 |
|
| 94 |
p.Task("bar", S{"foo"}, nil).Src("test/**/*.html") |
| 95 |
} |
| 96 |
proj, err := runTask(tasks, "bar") |
| 97 |
assert.NoError(t, err) |
| 98 |
if len(proj.Tasks["bar"].SrcFiles) != 2 { |
| 99 |
t.Error("bar should have 2 HTML file") |
| 100 |
} |
| 101 |
if len(proj.Tasks["foo"].SrcFiles) != 7 { |
| 102 |
t.Error("foo should have 7 txt files, one is hidden, got", |
| 103 |
len(proj.Tasks["foo"].SrcFiles)) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestCalculateWatchPaths(t *testing.T) { |
| 108 |
|
| 109 |
paths := []string{ |
| 110 |
"example/views/**/*.go.html", |
| 111 |
"example.html", |
| 112 |
} |
| 113 |
paths = calculateWatchPaths(paths) |
| 114 |
if len(paths) != 1 { |
| 115 |
t.Error("Expected exact elements") |
| 116 |
} |
| 117 |
sort.Strings(paths) |
| 118 |
if paths[0] != "." { |
| 119 |
t.Error("Expected exact file paths got", paths[0]) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
paths = []string{ |
| 124 |
"**/*.go.html", |
| 125 |
"example.html", |
| 126 |
} |
| 127 |
paths = calculateWatchPaths(paths) |
| 128 |
|
| 129 |
if len(paths) != 1 { |
| 130 |
t.Error("Expected exact elements") |
| 131 |
} |
| 132 |
if paths[0] != "." { |
| 133 |
t.Error("Expected . got", paths[0]) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestLegacyIn(t *testing.T) { |
| 138 |
|
| 139 |
var cat = "cat" |
| 140 |
if isWindows { |
| 141 |
cat = "cmd /c type" |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
out, err := RunOutput(cat+" foo.txt", M{"$in": "test"}) |
| 148 |
assert.NoError(t, err) |
| 149 |
assert.Equal(t, "foo", str.Clean(out)) |
| 150 |
|
| 151 |
if isWindows { |
| 152 |
return |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
out, err = BashOutput("cat foo.txt", M{"$in": "test"}) |
| 159 |
assert.NoError(t, err) |
| 160 |
assert.Equal(t, "foo", str.Clean(out)) |
| 161 |
} |
| 162 |
|
| 163 |
func TestInvalidTask(t *testing.T) { |
| 164 |
tasks := func(p *Project) { |
| 165 |
} |
| 166 |
|
| 167 |
assert.Panics(t, func() { |
| 168 |
runTask(tasks, "dummy") |
| 169 |
}) |
| 170 |
} |
| 171 |
|
| 172 |
func TestParallel(t *testing.T) { |
| 173 |
var result string |
| 174 |
tasks := func(p *Project) { |
| 175 |
p.Task1("A", func(*Context) { |
| 176 |
result += "A" |
| 177 |
}) |
| 178 |
p.Task1("B", func(*Context) { |
| 179 |
time.Sleep(10 * time.Millisecond) |
| 180 |
result += "B" |
| 181 |
}) |
| 182 |
p.Task("C", nil, func(*Context) { |
| 183 |
result += "C" |
| 184 |
}) |
| 185 |
p.Task("D", nil, func(*Context) { |
| 186 |
result += "D" |
| 187 |
}) |
| 188 |
p.Task("default", P{"A", "B", "C", "D"}, nil) |
| 189 |
} |
| 190 |
|
| 191 |
argv := []string{} |
| 192 |
ch := make(chan int) |
| 193 |
go func() { |
| 194 |
execCLI(tasks, argv, func(code int) { |
| 195 |
assert.Equal(t, code, 0) |
| 196 |
assert.True(t, len(result) == 4) |
| 197 |
ch <- code |
| 198 |
close(ch) |
| 199 |
}) |
| 200 |
}() |
| 201 |
<-ch |
| 202 |
} |
| 203 |
|
| 204 |
func TestTaskD(t *testing.T) { |
| 205 |
trace := "" |
| 206 |
tasks := func(p *Project) { |
| 207 |
p.Task1("A", func(*Context) { |
| 208 |
trace += "A" |
| 209 |
}) |
| 210 |
|
| 211 |
p.TaskD("default", S{"A"}) |
| 212 |
} |
| 213 |
runTask(tasks, "default") |
| 214 |
assert.Equal(t, "A", trace) |
| 215 |
} |
| 216 |
|
| 217 |
func TestRunOnce(t *testing.T) { |
| 218 |
trace := "" |
| 219 |
tasks := func(p *Project) { |
| 220 |
p.Task("once?", nil, func(*Context) { |
| 221 |
trace += "1" |
| 222 |
}) |
| 223 |
|
| 224 |
p.Task("A", S{"once"}, func(*Context) { |
| 225 |
trace += "A" |
| 226 |
}) |
| 227 |
|
| 228 |
p.Task("B", S{"once"}, func(*Context) { |
| 229 |
trace += "B" |
| 230 |
}) |
| 231 |
} |
| 232 |
|
| 233 |
execCLI(tasks, []string{"A", "B"}, nil) |
| 234 |
assert.Equal(t, "1AB", trace) |
| 235 |
} |