forked from kcmerrill/alfred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfred_test.go
346 lines (282 loc) · 7.89 KB
/
alfred_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)
/*
The test suite is really a functional test.
Once I get the functional suite working, then I'll add unit tests
but first I'd like to get a happy path.
I say all that to say this: This package requires alfred in your path,
and it also requires alfred to already be compiled
-kc
*/
func init() {
os.Chdir("./examples/demo-everything/")
}
func run(cmd string, t *testing.T) (string, error) {
if out, err := exec.Command("bash", "-c", cmd).CombinedOutput(); err == nil {
fmt.Println(string(out))
return strings.Trim(string(out), "\n"), nil
} else {
return strings.Trim(string(out), "\n"), err
}
}
func TestCurrentDirectory(t *testing.T) {
if cur, err := os.Getwd(); err == nil {
if !strings.Contains(cur, "examples/demo-everything") {
t.Logf("Unable to properly switch to examples/demo-everything")
t.Fail()
}
}
}
func TestListing(t *testing.T) {
sut, err := run("alfred", t)
/* Was it able to run? */
if err != nil {
t.Logf("Unable to run alfred. Is it compiled and in your path?")
t.Fail()
}
/* Check summary */
if !strings.Contains(sut, "[one] Displaying the task name") {
t.Logf("Task one was not in the alfred listing")
t.Fail()
}
/* Check promoted/alphabetic ordering ... */
if !strings.HasSuffix(sut, `[two] A simple echo
----
[twentysix] Notice the astrick? It means it's a "main" task. Useful for a long alfred file`) {
t.Logf("Alphabetical ordering is off, and so is promoted")
t.Fail()
}
/* Check Usage */
if !strings.Contains(sut, "- Usage: alfred fourteen foldername") {
t.Logf("Usage was not displayed ...")
t.Fail()
}
/* Check Aliases */
if !strings.Contains(sut, "- Alias: six six.one six.two") {
t.Logf("Aliases were not displayed ...")
t.Fail()
}
/* Check Private */
if strings.Contains(sut, "[eight]") {
t.Logf("Private task [eight] should not be found")
t.Fail()
}
}
func TestSimpleCommand(t *testing.T) {
sut, _ := run("alfred two", t)
fmt.Println("system", sut)
/* Check summary */
if !strings.Contains(sut, "[two] A simple echo") {
t.Logf("Summary was not displayed")
t.FailNow()
}
/* Check command was run */
if !strings.Contains(sut, "A simple echo command") {
t.Logf("Command [two] was not run succesfully")
t.FailNow()
}
}
func TestDirKey(t *testing.T) {
sut, _ := run("alfred three", t)
if !strings.Contains(sut, "/tmp") {
t.Logf("The directory should have changed to /tmp")
t.FailNow()
}
}
func TestAlias(t *testing.T) {
sut, _ := run("alfred six", t)
/* Verify the summary changed */
if !strings.Contains(sut, "[six] Step five, but aliased as step six too! Space seperated") {
t.Logf("The task [five] alias should actually be [six] now")
t.FailNow()
}
/* Make sure the ls command ran */
if !strings.Contains(sut, "emptydir") {
t.Logf("ls should contain emptydir")
t.FailNow()
}
/* Make sure the ls has emptydir */
if !strings.Contains(sut, "emptydir") {
t.Logf("ls should contain the emptydir as well")
t.FailNow()
}
}
func TestOkTask(t *testing.T) {
sut, _ := run("alfred seven", t)
/* Verify task seven was called */
if !strings.Contains(sut, "[seven]") {
t.Logf("Task [seven] should have been called")
t.FailNow()
}
/* Ok, [eight] should have been called too */
if !strings.Contains(sut, "[eight]") {
t.Logf("Task [eight] should have been called")
t.FailNow()
}
/* Failure should _not_ have been called */
if strings.Contains(sut, "[ten]") {
t.Logf("Task [ten] should not have been called")
t.FailNow()
}
}
func TestTaskFail(t *testing.T) {
sut, err := run("alfred nine", t)
if err != nil {
fmt.Println("FAIL---", err)
t.Logf("While the task failed, alfred should report 0 exit code")
t.FailNow()
}
/* Verify that task [nine] ran */
if !strings.Contains(sut, "[nine]") {
t.Logf("Task [nine] should have been called")
t.FailNow()
}
/* Make sure we failed, and we see the output */
if !strings.Contains(sut, "No such file or directory") {
t.Logf("ls /kcwashere should not have existed ...")
t.FailNow()
}
/* Verify step [ten] was run as it was the fail task*/
if !strings.Contains(sut, "[ten]") {
t.Logf("Because kcwashere doesn't exist, step 10 should have run")
t.FailNow()
}
/* Verify step [eight] was _not_ run, given it was the ok task */
if strings.Contains(sut, "[eight]") {
t.Logf("Step [eight] was run, and it should _not_ have")
t.FailNow()
}
}
func TestTaskGroup(t *testing.T) {
sut, _ := run("alfred eleven", t)
/* task eleven is a task group */
if !strings.Contains(sut, "[eleven]") {
t.Logf("Task [eleven] should've been called with a summary!")
t.FailNow()
}
/* four, five and six should've been called */
if !strings.Contains(sut, "[four]") {
t.Logf("Task [four] should've been called with a summary!")
t.FailNow()
}
if !strings.Contains(sut, "[five]") {
t.Logf("Task [four] should've been called with a summary!")
t.FailNow()
}
if !strings.Contains(sut, "[six]") {
t.Logf("Task [six] should've been called with a summary!")
t.FailNow()
}
}
func TestEvery(t *testing.T) {
cmd := exec.Command("alfred", "thirteen")
stdout, _ := cmd.StdoutPipe()
cmd.Start()
time.AfterFunc(4*time.Second, func() {
cmd.Process.Kill()
buf := new(bytes.Buffer)
buf.ReadFrom(stdout)
s := buf.String()
/* Now, count the number of times we see [thirteen] */
ran := strings.Split(s, "[thirteen]")
if len(ran) <= 2 {
t.Logf("The task [thirteen] did not run as many times as we had hoped")
t.FailNow()
}
})
<-time.After(5 * time.Second)
}
func TestArgumentsOkAndDefaults(t *testing.T) {
cmds := []string{"fourteen .", "fifteen"}
for _, cmd := range cmds {
sut, _ := run("alfred "+cmd, t)
/* Verify our task ran */
if !strings.Contains(sut, "[fourteen]") && !strings.Contains(sut, "[fifteen]") {
t.Logf("Task should have run succesfully")
t.FailNow()
}
/* Verfify our command had run succesfully */
if !strings.Contains(sut, "emptydir") {
t.Logf("Listing of the current directory should show an alfred.yml file")
t.FailNow()
}
}
}
func TestArgumentsFailure(t *testing.T) {
sut, err := run("alfred fourteen", t)
if err == nil {
t.Logf("Missing arguments should cause an invalid exit code")
t.FailNow()
}
/* Verfify our error message */
if !strings.Contains(sut, "[fourteen:error] Missing argument(s).") {
t.Logf("The error message is invalid")
t.FailNow()
}
}
func TestRetryLogic(t *testing.T) {
sut, _ := run("alfred twentyseven", t)
/* Verfify our retry logic via error message */
if strings.Count(sut, "/step27-idonotexist") != 3 {
t.Logf("Expected Retry logic 3 times")
t.FailNow()
}
}
func TestCommands(t *testing.T) {
run("alfred twentynine", t)
if _, err := os.Stat("/tmp/commands.txt"); err == nil {
t.Logf("/tmp/commands.txt should _NOT_ exist")
t.FailNow()
}
}
func TestTest(t *testing.T) {
run("alfred thirty", t)
if _, err := os.Stat("/tmp/test.txt"); err == nil {
t.Logf("/tmp/test.txt should _NOT_ exist")
t.FailNow()
}
}
func TestCleanedArgs(t *testing.T) {
sut, _ := run("alfred thirtyone", t)
if !strings.Contains(sut, "bingowashisnameo") {
t.Logf("Expecting bingowashisnameo as a cleanedarg!")
t.FailNow()
}
}
func TestTaskMultiArguments(t *testing.T) {
sut, _ := run("alfred thirty.eight", t)
if !strings.Contains(sut, "-kc-merrill-") {
t.Logf("Expecting -kc-merrill- in command output")
t.FailNow()
}
if !strings.Contains(sut, "-bruce-wayne-") {
t.Logf("Expecting -bruce-wayne- in command output")
t.FailNow()
}
if !strings.Contains(sut, "-clark-kent-") {
t.Logf("Expecting -clark-kent- in command output")
t.FailNow()
}
}
func TestRegisteringVariables(t *testing.T) {
sut, _ := run("alfred fourty.one", t)
if !strings.Contains(sut, "The variable is 5678") {
t.Logf("Expecting 5678 to be returned")
t.FailNow()
}
}
func TestExample(t *testing.T) {
sut, _ := run("alfred", t)
if len(sut) <= 0 {
t.Logf("Test example failed ... meaning no data :(")
t.FailNow()
}
}