forked from parse-community/parse-cli
/
main_test.go
377 lines (325 loc) · 9.09 KB
/
main_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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/facebookgo/clock"
"github.com/facebookgo/ensure"
"github.com/facebookgo/errgroup"
"github.com/facebookgo/parse"
"github.com/facebookgo/stackerr"
"github.com/facebookgo/testname"
"github.com/spf13/cobra"
)
func noOpCmd() *cobra.Command {
var c cobra.Command
c.SetOutput(ioutil.Discard)
return &c
}
type Harness struct {
T testing.TB
Out bytes.Buffer
Err bytes.Buffer
Clock *clock.Mock
env *env
remove []string
}
func (h *Harness) makeEmptyRoot() {
var err error
prefix := fmt.Sprintf("%s-", testname.Get("parse-cli-"))
h.env.Root, err = ioutil.TempDir("", prefix)
ensure.Nil(h.T, err)
h.remove = append(h.remove, h.env.Root)
}
func (h *Harness) makeWithConfig(global string) {
h.env.Root = makeDirWithConfig(h.T, global)
}
func (h *Harness) Stop() {
for _, p := range h.remove {
os.RemoveAll(p)
}
}
func newHarness(t testing.TB) *Harness {
te := Harness{
T: t,
Clock: clock.NewMock(),
}
te.env = &env{
Out: &te.Out,
Err: &te.Err,
Clock: te.Clock,
ParseAPIClient: &ParseAPIClient{apiClient: &parse.Client{}},
}
return &te
}
// makes a temp directory with the given global config.
func makeDirWithConfig(t testing.TB, global string) string {
dir, err := ioutil.TempDir("", testname.Get("parse-cli-"))
ensure.Nil(t, err)
ensure.Nil(t, os.Mkdir(filepath.Join(dir, "config"), 0755))
ensure.Nil(t, ioutil.WriteFile(
filepath.Join(dir, legacyConfigFile),
[]byte(global),
os.FileMode(0600),
))
return dir
}
type transportFunc func(r *http.Request) (*http.Response, error)
func (t transportFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return t(r)
}
func jsonStr(t testing.TB, v interface{}) string {
b, err := json.Marshal(v)
ensure.Nil(t, err)
return string(b)
}
func TestVersion(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
var c versionCmd
err := c.run(h.env)
ensure.Nil(t, err)
ensure.DeepEqual(t, h.Out.String(), version+"\n")
ensure.DeepEqual(t, h.Err.String(), "")
}
// this just exists to make code coverage less noisy.
func TestRootCommand(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
parseRootCmd(h.env)
}
func TestErrorStringWithoutStack(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
h.env.ErrorStack = false
const message = "hello world"
actual := errorString(h.env, stackerr.New(message))
ensure.StringContains(t, actual, message)
ensure.StringDoesNotContain(t, actual, ".go")
}
type exitCode int
func TestErrorStringWithStack(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
h.env.ErrorStack = true
const message = "hello world"
actual := errorString(h.env, stackerr.New(message))
ensure.StringContains(t, actual, message)
ensure.StringContains(t, actual, ".go")
}
func TestRunNoArgsWithArg(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
h.env.Exit = func(i int) { panic(exitCode(i)) }
func() {
defer ensure.PanicDeepEqual(t, exitCode(1))
r := runNoArgs(h.env, nil)
r(noOpCmd(), []string{"foo"})
}()
ensure.StringContains(t, h.Err.String(), "unexpected arguments")
}
func TestRunNoArgsFuncError(t *testing.T) {
t.Parallel()
const message = "hello world"
h := newHarness(t)
defer h.Stop()
h.env.Exit = func(i int) { panic(exitCode(i)) }
func() {
defer ensure.PanicDeepEqual(t, exitCode(1))
r := runNoArgs(h.env, func(*env) error { return errors.New(message) })
r(noOpCmd(), nil)
}()
ensure.StringContains(t, h.Err.String(), message)
}
func TestRunWithAppMultipleArgs(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
h.env.Exit = func(i int) { panic(exitCode(i)) }
func() {
defer ensure.PanicDeepEqual(t, exitCode(1))
r := runWithClient(h.env, nil)
r(noOpCmd(), []string{"foo", "bar"})
}()
ensure.StringContains(
t,
h.Err.String(),
"only an optional app name is expected",
)
}
func TestRunWithAppNonProjectDir(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
h.makeEmptyRoot()
h.env.Exit = func(i int) { panic(exitCode(i)) }
func() {
defer ensure.PanicDeepEqual(t, exitCode(1))
r := runWithClient(h.env, nil)
r(noOpCmd(), nil)
}()
ensure.StringContains(
t,
h.Err.String(),
"Command must be run inside a Parse project.",
)
}
func TestRunWithAppNotFound(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
c := legacyConfig{Applications: map[string]*parseAppConfig{"a": {}}}
h.makeWithConfig(jsonStr(t, c))
h.env.Exit = func(i int) { panic(exitCode(i)) }
func() {
defer ensure.PanicDeepEqual(t, exitCode(1))
r := runWithClient(h.env, nil)
r(noOpCmd(), []string{"b"})
}()
ensure.StringContains(t, h.Err.String(), `App "b" wasn't found`)
}
func TestRunWithAppNamed(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
const appName = "a"
c := &parseConfig{
Applications: map[string]*parseAppConfig{
appName: {ApplicationID: "id", MasterKey: "token"},
},
}
h.makeWithConfig(jsonStr(t, c))
r := runWithClient(h.env, func(e *env, c *context) error {
ensure.NotNil(t, c)
return nil
})
r(noOpCmd(), []string{"a"})
}
func TestRunWithDefault(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
const appName = "a"
c := &parseConfig{
Applications: map[string]*parseAppConfig{
appName: {ApplicationID: "id", MasterKey: "token"},
defaultKey: {Link: appName},
},
}
h.makeWithConfig(jsonStr(t, c))
r := runWithClient(h.env, func(e *env, c *context) error {
ensure.NotNil(t, c)
return nil
})
r(noOpCmd(), []string{"a"})
}
func TestRunWithAppError(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
const appName = "a"
c := &parseConfig{
Applications: map[string]*parseAppConfig{
appName: {ApplicationID: "id", MasterKey: "token"},
},
}
h.makeWithConfig(jsonStr(t, c))
h.env.Exit = func(i int) { panic(exitCode(i)) }
const message = "hello world"
func() {
defer ensure.PanicDeepEqual(t, exitCode(1))
r := runWithClient(h.env, func(e *env, c *context) error {
ensure.NotNil(t, c)
return errors.New(message)
})
r(noOpCmd(), []string{"a"})
}()
ensure.StringContains(t, h.Err.String(), message)
}
func TestNewClientInvalidServerURL(t *testing.T) {
t.Parallel()
c, err := newParseAPIClient(&env{Server: ":"})
ensure.True(t, c == nil)
ensure.Err(t, err, regexp.MustCompile("invalid server URL"))
}
func TestErrorString(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
apiErr := &parse.Error{Code: -1, Message: "Error\nMessage"}
ensure.DeepEqual(t, `Error
Message`,
errorString(h.env, apiErr),
)
}
func TestGetProjectRoot(t *testing.T) {
t.Parallel()
h := newHarness(t)
h.makeEmptyRoot()
defer h.Stop()
ensure.Nil(t, os.Mkdir(filepath.Join(h.env.Root, "parse"), 0755))
ensure.Nil(t, os.Mkdir(filepath.Join(h.env.Root, "parse", "config"), 0755))
f, err := os.Create(filepath.Join(h.env.Root, "parse", legacyConfigFile))
ensure.Nil(t, err)
defer f.Close()
ensure.Nil(t, os.Mkdir(filepath.Join(h.env.Root, "parse", "cloud"), 0755))
ensure.Nil(t, os.Mkdir(filepath.Join(h.env.Root, "parse", "public"), 0755))
ensure.Nil(t, os.MkdirAll(filepath.Join(h.env.Root, "parse", "cloud", "other", "config"), 0755))
ensure.DeepEqual(t, getLegacyProjectRoot(h.env, h.env.Root), h.env.Root)
ensure.DeepEqual(t, getLegacyProjectRoot(h.env, filepath.Join(h.env.Root, "parse", "config")), filepath.Join(h.env.Root, "parse"))
ensure.DeepEqual(t, getLegacyProjectRoot(h.env, filepath.Join(h.env.Root, "parse", "cloud")), filepath.Join(h.env.Root, "parse"))
ensure.DeepEqual(t, getLegacyProjectRoot(h.env, filepath.Join(h.env.Root, "parse", "public")), filepath.Join(h.env.Root, "parse"))
ensure.DeepEqual(t, getLegacyProjectRoot(h.env, filepath.Join(h.env.Root, "parse", "cloud", "other")), filepath.Join(h.env.Root, "parse"))
}
func TestStackErrorString(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
err := stackerr.New("error")
h.env.ErrorStack = false
errStr := errorString(h.env, err)
ensure.DeepEqual(t, errStr, "error")
h.env.ErrorStack = true
errStr = errorString(h.env, err)
ensure.StringContains(t, errStr, "error")
ensure.StringContains(t, errStr, ".go")
err = stackerr.Wrap(&parse.Error{Message: "message", Code: 1})
h.env.ErrorStack = false
errStr = errorString(h.env, err)
ensure.DeepEqual(t, errStr, "message")
h.env.ErrorStack = true
errStr = errorString(h.env, err)
ensure.StringContains(t, errStr, `parse: api error with code=1 and message="message`)
ensure.StringContains(t, errStr, ".go")
}
func TestMultiErrorString(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
err := errgroup.MultiError(
[]error{
stackerr.New("error"),
stackerr.Wrap(&parse.Error{Message: "message", Code: 1}),
},
)
h.env.ErrorStack = false
errStr := errorString(h.env, err)
ensure.DeepEqual(t, errStr, "multiple errors: error | message")
h.env.ErrorStack = true
errStr = errorString(h.env, err)
ensure.StringContains(t, errStr, "multiple errors")
ensure.StringContains(t, errStr, `parse: api error with code=1 and message="message"`)
ensure.StringContains(t, errStr, ".go")
}