forked from ahoy-cli/ahoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathahoy_test.go
249 lines (207 loc) · 5.98 KB
/
ahoy_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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"testing"
)
func TestOverrideExample(t *testing.T) {
expected := "Overrode you.\n"
actual, _ := appRun([]string{"ahoy", "-f", "testdata/override-base.ahoy.yml", "docker", "override-example"})
if expected != actual {
t.Errorf("ahoy docker override-example: expected - %s; actual - %s", string(expected), string(actual))
}
}
func TestGetCommands(t *testing.T) {
// Get Command with no sub Commands.
config := Config{
Usage: "Test getSubCommands Usage.",
AhoyAPI: "v2",
Commands: map[string]Command{
"test-command": Command{
Description: "Testing example Command.",
Usage: "test-command a",
Cmd: "echo a.ahoy.yml",
Hide: false,
},
},
}
commands := getCommands(config)
if len(commands) != 1 {
t.Error("Expect that getCommands can get one command if passed config with one command.")
}
}
func TestGetSubCommand(t *testing.T) {
// Since we're not running the app directly, sourcedir doesn't get reset, so
// we need to reset it ourselves. TODO: Remove these globals somehow.
AhoyConf.srcDir = ""
// When empty return empty list of commands.
actual := getSubCommands([]string{})
if len(actual) != 0 {
t.Error("Expect that getSubCommands([]string) returns []Command{}")
}
// List of bogus or empty strings returns empty list of commands.
actual = getSubCommands([]string{
"./testing/bogus1.ahoy.yml",
"./testing/private.ahoy.yml",
})
if len(actual) != 0 {
t.Error("Expect that getSubCommands([]string) returns []Command{}")
}
// Commands with same name are merged, last one wins.
err := os.MkdirAll("testing", 0755)
if err != nil {
t.Error("Something went wrong creating the 'testing' directory")
}
file1, err := os.Create("testing/a.ahoy.yml")
if err != nil {
t.Error("Something went wrong with the file creation - file1.")
}
file2, err := os.Create("testing/b.ahoy.yml")
if err != nil {
t.Error("Something went wrong with the file creation - file2.")
}
yamlConfigA := `
ahoyapi: v2
commands:
test-command:
description: Testing example Command.
usage: test-command a
cmd: echo "test"
hide: false
`
yamlConfigB := `
ahoyapi: v2
commands:
test-command:
description: Testing example Command.
usage: test-command b
cmd: echo "test"
hide: false
`
_, err = file1.Write([]byte(yamlConfigA))
if err != nil {
t.Error("Error writing to file1.")
}
_, err = file2.Write([]byte(yamlConfigB))
if err != nil {
t.Error("Error writing to file2.")
}
actual = getSubCommands([]string{
"./testing/a.ahoy.yml",
"./testing/b.ahoy.yml",
})
if len(actual) != 1 {
t.Error("Sourcedir:", AhoyConf.srcDir)
t.Error("Failed: expect that two commands with the same name get merged into one.", actual)
}
if len(actual) > 0 && actual[0].Usage != "test-command b" {
t.Error("Failed: expect that when multiple commands are merged, last one wins.", actual)
}
// Test commands with different names do not get merged.
file3, err := os.Create("testing/c.ahoy.yml")
if err != nil {
t.Error("Something went wrong with the file creation - file3.")
}
//logger("fatal", "test")
yamlConfigC := `
ahoyapi: v2
commands:
test-new-command:
description: Testing new example Command.
usage: test-new-command a
cmd: "echo new a.ahoy.yml"
hide: false
`
_, err = file3.Write([]byte(yamlConfigC))
if err != nil {
t.Error("Error writing to file3.")
}
actual = getSubCommands([]string{
"./testing/a.ahoy.yml",
"./testing/b.ahoy.yml",
"./testing/c.ahoy.yml",
})
if len(actual) != 2 {
fmt.Printf("x = %#v \n", actual)
t.Error("Failed: expect unique commands to be captured separately.", "commands found", actual)
}
file1.Close()
file2.Close()
file3.Close()
os.RemoveAll("testing")
}
func TestGetConfig(t *testing.T) {
testFile, err := os.Create("test_getConfig.yml")
if err != nil {
t.Error("Something went wrong creating the test file.")
}
expected := Config{
Usage: "Test example usage.",
AhoyAPI: "v2",
Commands: map[string]Command{
"test-command": Command{
Description: "Testing example Command.",
Usage: "test-command",
Cmd: "echo 'Hello World'",
Hide: false,
Imports: []string{
"./path/a",
"./path/b",
},
},
},
}
testYaml, err := yaml.Marshal(expected)
if err != nil {
t.Error("Something went wrong mashelling the test object.")
}
testFile.Write([]byte(testYaml))
config, err := getConfig("test_getConfig.yml")
if err != nil {
t.Error("Something went wrong trying to load the config file.")
}
if config.Usage != expected.Usage {
t.Errorf("Expected config.Usage to be %s, but actaul is %s", expected.Usage, config.Usage)
}
if config.Commands["test-command"].Cmd != expected.Commands["test-command"].Cmd {
t.Errorf("Expected config.Commands['test-command'].cmd to be %s, but actaul is %s", expected.Commands["test-command"].Cmd, config.Commands["test-command"].Cmd)
}
testFile.Close()
os.Remove("test_getConfig.yml")
}
func TestGetConfigPath(t *testing.T) {
// Passinng empty string.
pwd, _ := os.Getwd()
expected := pwd + "/.ahoy.yml"
actual, _ := getConfigPath("")
if expected != actual {
t.Errorf("ahoy docker override-example: expected - %s; actual - %s", string(expected), string(actual))
}
// Passing known path works as expected
expected = pwd + "/.ahoy.yml"
actual, _ = getConfigPath(expected)
if expected != actual {
t.Errorf("ahoy docker override-example: expected - %s; actual - %s", string(expected), string(actual))
}
// TODO: Passing directory should return default
}
func TestGetConfigPathErrorOnBogusPath(t *testing.T) {
_, err := getConfigPath("~/bogus/path")
if err == nil {
t.Error("getConfigPath did not fail when passed a bogus path.")
}
}
func appRun(args []string) (string, error) {
stdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
setupApp(args[1:])
app.Run(args)
w.Close()
//@aashil thinks this reads from the command line
out, _ := ioutil.ReadAll(r)
os.Stdout = stdout
return string(out), nil
}