forked from parse-community/parse-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_test.go
152 lines (130 loc) · 4.09 KB
/
default_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/ensure"
)
func newDefaultParseConfig(t testing.TB, h *parsecli.Harness) *parsecli.ParseConfig {
c, err := parsecli.ConfigFromDir(h.Env.Root)
ensure.Nil(t, err)
config, ok := c.(*parsecli.ParseConfig)
ensure.True(t, ok)
config.Applications = map[string]*parsecli.ParseAppConfig{
"first": {},
"second": {},
parsecli.DefaultKey: {Link: "first"},
}
config.ProjectConfig = &parsecli.ProjectConfig{
Type: parsecli.LegacyParseFormat,
Parse: &parsecli.ParseProjectConfig{},
}
ensure.Nil(t, parsecli.StoreConfig(h.Env, config))
return config
}
func newDefaultCmdHarness(t testing.TB) (*parsecli.Harness, *defaultCmd, *parsecli.ParseConfig) {
h := parsecli.NewHarness(t)
h.MakeEmptyRoot()
ensure.Nil(t, parsecli.CloneSampleCloudCode(h.Env, true))
config := newDefaultParseConfig(t, h)
h.Out.Reset()
d := &defaultCmd{}
return h, d, config
}
func TestPrintDefaultNotSet(t *testing.T) {
t.Parallel()
h, _, _ := newDefaultCmdHarness(t)
defer h.Stop()
ensure.Err(t, parsecli.PrintDefault(h.Env, ""), regexp.MustCompile("No app is set as default app"))
}
func TestPrintDefaultSet(t *testing.T) {
t.Parallel()
h, _, _ := newDefaultCmdHarness(t)
defer h.Stop()
parsecli.PrintDefault(h.Env, "first")
ensure.DeepEqual(t, h.Out.String(),
`Current default app is first
`)
}
func TestSetDefaultInvalid(t *testing.T) {
t.Parallel()
h, _, config := newDefaultCmdHarness(t)
defer h.Stop()
ensure.Err(
t,
parsecli.SetDefault(h.Env, "invalid", "", config),
regexp.MustCompile(`Invalid application name "invalid". Please select from the valid applications printed above.`),
)
ensure.DeepEqual(t, h.Out.String(),
`The following apps are associated with Cloud Code in the current directory:
* first
second
`)
}
func TestSetDefault(t *testing.T) {
t.Parallel()
h, _, config := newDefaultCmdHarness(t)
defer h.Stop()
ensure.Nil(t, parsecli.SetDefault(h.Env, "first", "", config))
ensure.DeepEqual(t, h.Out.String(), "Default app set to first.\n")
}
func TestSetDefaultWithApp(t *testing.T) {
t.Parallel()
h, _, config := newDefaultCmdHarness(t)
defer h.Stop()
ensure.Nil(t, parsecli.SetDefault(h.Env, "first", "first", config))
ensure.DeepEqual(t, h.Out.String(), "Default app set to first.\n")
}
func TestDefaultRunMoreArgs(t *testing.T) {
t.Parallel()
h, d, _ := newDefaultCmdHarness(t)
defer h.Stop()
ensure.Err(t, d.run(h.Env, []string{"one", "two"}), regexp.MustCompile(`unexpected arguments, only an optional app name is expected:`))
}
// NOTE: testing for legacy format
func newLegacyDefaultCmdHarness(t testing.TB) (*parsecli.Harness, *defaultCmd, *parsecli.ParseConfig) {
h := parsecli.NewHarness(t)
h.MakeEmptyRoot()
ensure.Nil(t, parsecli.CloneSampleCloudCode(h.Env, true))
ensure.Nil(t, os.MkdirAll(filepath.Join(h.Env.Root, parsecli.ConfigDir), 0755))
ensure.Nil(t, ioutil.WriteFile(filepath.Join(h.Env.Root, parsecli.LegacyConfigFile),
[]byte("{}"),
0600),
)
config := newDefaultParseConfig(t, h)
h.Out.Reset()
d := &defaultCmd{}
return h, d, config
}
func TestLegacySetDefaultInvalid(t *testing.T) {
t.Parallel()
h, _, config := newLegacyDefaultCmdHarness(t)
defer h.Stop()
ensure.Err(
t,
parsecli.SetDefault(h.Env, "invalid", "", config),
regexp.MustCompile(`Invalid application name "invalid". Please select from the valid applications printed above.`),
)
ensure.DeepEqual(t, h.Out.String(),
`The following apps are associated with Cloud Code in the current directory:
* first
second
`)
}
func TestLegacySetDefault(t *testing.T) {
t.Parallel()
h, _, config := newLegacyDefaultCmdHarness(t)
defer h.Stop()
ensure.Nil(t, parsecli.SetDefault(h.Env, "first", "", config))
ensure.DeepEqual(t, h.Out.String(), "Default app set to first.\n")
}
func TestLegacySetDefaultWithApp(t *testing.T) {
t.Parallel()
h, _, config := newLegacyDefaultCmdHarness(t)
defer h.Stop()
ensure.Nil(t, parsecli.SetDefault(h.Env, "first", "first", config))
ensure.DeepEqual(t, h.Out.String(), "Default app set to first.\n")
}