-
Notifications
You must be signed in to change notification settings - Fork 14
/
alfred_test.go
161 lines (140 loc) · 6.32 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
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"testing"
)
func TestAlfredComponents(t *testing.T) {
tt := map[string]TestComponent{
"|show.tasks": TestComponent{expectedOutput: []string{" summary | TESTING SUMMARY"}, failWithOutput: []string{"hidden.task"}},
"invalid.task": TestComponent{shouldFail: true},
"invalid.task|formatted": TestComponent{shouldFail: true, expectedOutput: []string{"0s"}},
"invalid.task|not.formatted": TestComponent{args: "--no-formatting", shouldFail: true, failWithOutput: []string{"0s"}},
"summary": TestComponent{expectedOutput: []string{"TESTING SUMMARY"}},
"hidden.task": TestComponent{expectedOutput: []string{"Testing a hidden task"}},
"command": TestComponent{expectedOutput: []string{"HELLO ALFRED", "HELLO NEWLINE"}},
"commands": TestComponent{shouldFail: true, expectedOutput: []string{"HELLO ALFRED"}, failWithOutput: []string{"THIS LINE NOT SHOWN"}},
"exit": TestComponent{shouldFail: true},
"arguments|without": TestComponent{shouldFail: true},
"arguments": TestComponent{params: "ARG1", expectedOutput: []string{"ARG::ARG1"}},
"default.arguments": TestComponent{expectedOutput: []string{"ARG::ARG1"}},
"required.arguments": TestComponent{shouldFail: true, failWithOutput: []string{"ARG::ARG2"}},
"required.arguments|with_args": TestComponent{params: "ARG1", expectedOutput: []string{"ARG::ARG1 ARG::ARG2"}},
"ok": TestComponent{expectedOutput: []string{"TEST::OK", "ARG::ARG1", "ARG::OKTEST"}},
"ok|witharg": TestComponent{params: "DEFAULTARG", expectedOutput: []string{"TEST::OK", "ARG::DEFAULTARG", "ARG::OKTEST"}},
"fail": TestComponent{expectedOutput: []string{"TEST::FAIL", "ARG::ARG1", "ARG::FAILTEST"}},
"tasks": TestComponent{expectedOutput: []string{"ARG::ARG1", "ARG::TASKTEST"}},
"multitask": TestComponent{expectedOutput: []string{"ARG::ARG1", "ARG::MULTITASKTEST"}},
"check|should_skip": TestComponent{params: "alfred.yml", expectedOutput: []string{"skipped"}, failWithOutput: []string{"TEST::CHECK"}},
"check|should_run": TestComponent{params: "doesnotexist", expectedOutput: []string{"TEST::CHECK"}},
"config|file": TestComponent{params: "config.yml", expectedOutput: []string{"FOO::BAR", "FIZZ::BUZZ"}},
"config|text": TestComponent{params: "'foo: BAR\nfizz: BUZZ'", expectedOutput: []string{"FOO::BAR", "FIZZ::BUZZ"}},
"dir": TestComponent{params: "/tmp/", expectedOutput: []string{"PWD::/private/tmp|PWD::/tmp"}},
"dir|with_alfred_folder": TestComponent{dir: "catalog2", failWithOutput: []string{"tests/catalog2/alfred"}},
"wait": TestComponent{expectedOutput: []string{"wait wait 2s"}},
"template|sprig": TestComponent{expectedOutput: []string{"HELLO!HELLO!HELLO!HELLO!HELLO!"}},
"for": TestComponent{expectedOutput: []string{"ARG::0", "ARG::1", "ARG::2", "ARG::3", "ARG::4"}, failWithOutput: []string{"ARG::5"}},
"include": TestComponent{expectedOutput: []string{"taska.alfred.yml", "taskb.alfred.yml"}},
"@catalog": TestComponent{expectedOutput: []string{"taska"}, failWithOutput: []string{"taskb"}},
"@catalog:taska": TestComponent{expectedOutput: []string{"taska.alfred.yml"}},
"env": TestComponent{expectedOutput: []string{"ARG::TESTENV"}},
"register": TestComponent{expectedOutput: []string{"REGISTER::var", "WHOAMI::(kcmerrill|root)"}},
// Tests that _should_ be working(read: bugs)
"arguments|empty_log": TestComponent{skip: true, shouldFail: true, args: "--log scratch/test_empty.log", filesExist: []string{"scratch/"}},
"arguments|log_written": TestComponent{skip: true, args: "--log scratch/test.log", filesExist: []string{"scratch/test.log"}},
}
/* SHOULD NOT HAVE TO CHANGE TOO MUCH BELOW. Add tests above ^^^^ */
// lets be in the correct directory
err := os.Chdir("tests/")
if err != nil {
t.Fatalf("Could not switch directories. Bailing ...")
}
// lets clean up our scratch directory
testCommand("rm -rf scratch/", ".")
// lets cycle through our test map and get to testing!
for task, tc := range tt {
if tc.skip {
// we should skip this test
continue
}
taskBits := strings.Split(task, "|")
taskName := taskBits[0]
output, ok := runAlfredCommand(taskName, tc)
if tc.shouldFail && tc.shouldFail != !ok {
fmt.Println(output)
t.Fatalf("[" + task + "] Expected task failure ...")
}
if !tc.shouldFail && !ok {
fmt.Println(output)
t.Fatalf("[" + task + "] Expected task success ...")
}
for _, expectedOutput := range tc.expectedOutput {
found := false
for _, actualOutput := range strings.Split(output, "\n") {
if matched, _ := regexp.MatchString(expectedOutput, actualOutput); matched {
found = true
break
}
}
if !found {
t.Fatalf("["+task+"]Output: Expected to find '%s' in: \n\n%s", expectedOutput, output)
}
}
for _, ignoreOutput := range tc.failWithOutput {
found := false
for _, actualOutput := range strings.Split(output, "\n") {
if strings.Contains(actualOutput, ignoreOutput) {
found = true
break
}
}
if found {
t.Fatalf("["+task+"]Output: Expected to _NOT_ find '%s' in: \n\n%s", ignoreOutput, output)
}
}
for _, file := range tc.filesExist {
if _, exists := os.Stat(file); exists != nil {
t.Fatalf("["+task+"]File: Expected the file '%s' to exist ...", file)
}
}
}
}
type TestComponent struct {
filesExist []string
fileContents map[string]string
tasksStarted []string
expectedOutput []string
failWithOutput []string
params string
args string
shouldFail bool
skip bool
dir string
}
func testCommand(command, dir string) (string, bool) {
cmd := exec.Command("bash", "-c", command)
cmd.Dir = dir
cmdOutput, error := cmd.CombinedOutput()
if error != nil {
return string(cmdOutput), false
}
return string(cmdOutput), true
}
func runAlfredCommand(task string, tc TestComponent) (string, bool) {
cmd := "alfred --no-colors"
if tc.args != "" {
cmd += " " + tc.args
}
cmd += " " + task
if tc.params != "" {
cmd += " " + tc.params
}
dir := "."
if tc.dir != "" {
dir = tc.dir
}
return testCommand(cmd, dir)
}