This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main_test.go
322 lines (270 loc) · 7.25 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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"testing"
"github.com/phrase/phraseapp-client/cli"
"github.com/phrase/phraseapp-go/phraseapp"
)
func getBaseLocales() []*phraseapp.Locale {
return []*phraseapp.Locale{
{
Code: "en",
ID: "en-locale-id",
Name: "english",
},
{
Code: "de",
ID: "de-locale-id",
Name: "german",
},
}
}
func sortByPath(files LocaleFiles) {
sort.Slice(files, func(i, j int) bool { return strings.Compare(files[i].Path, files[j].Path) < 0 })
}
func compareLocaleFiles(actualFiles LocaleFiles, expectedFiles LocaleFiles) error {
sortByPath(actualFiles)
sortByPath(expectedFiles)
for idx, actualFile := range actualFiles {
expected := expectedFiles[idx]
actual := actualFile
if expected.Path != actual.Path {
return fmt.Errorf("Expected Path %s should eql %s", expected.Path, actual.Path)
}
if expected.Name != actual.Name {
return fmt.Errorf("Expected Name %s should eql %s", expected.Name, actual.Name)
}
if expected.Code != actual.Code {
return fmt.Errorf("Expected Code %s should eql %s", expected.Code, actual.Code)
}
if expected.ID != actual.ID {
return fmt.Errorf("Expected ID %s should eql %s", expected.ID, actual.ID)
}
}
return nil
}
func captureStderr(f func() error) (string, error) {
old := cli.DefaultWriter
defer func() {
cli.DefaultWriter = old
}()
buf := &bytes.Buffer{}
cli.DefaultWriter = buf
err := f()
return buf.String(), err
}
func runWithCfg(cfg *phraseapp.Config, cmd string, additionalOpts ...string) (string, error) {
r, err := router(cfg)
if err != nil {
return "", err
}
stderr, err := ioutil.TempFile("", "phraseapp_cli_test")
if err != nil {
return "", err
}
defer os.Remove(stderr.Name())
opts := strings.Split(cmd, "/")
opts = append(opts, additionalOpts...)
opts = append(opts, "-h")
out, err := captureStderr(func() error {
return r.Run(opts...)
})
if err != cli.ErrorHelpRequested {
return "", err
}
return out, nil
}
func TestCLIHelp_NoDefaults(t *testing.T) {
cfg := new(phraseapp.Config)
out, err := runWithCfg(cfg, "locale/download")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
if len(defaults) > 0 {
t.Errorf("expected no defaults in configuration, found: %#v", defaults)
}
}
func collectDefaults(out string) (map[string]string, error) {
res := map[string]string{}
sc := bufio.NewScanner(bytes.NewBufferString(out))
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if !strings.Contains(line, "(default:") {
continue
}
fields := strings.Split(line, " ")
if name, ok := searchFieldWithPrefix(fields, hasPrefix("--")); ok {
if value, ok := searchFieldWithPredecessor(fields, "(default:"); ok {
res[name] = value
}
}
}
return res, sc.Err()
}
func searchFieldWithPredecessor(flds []string, pre string) (string, bool) {
i := findInStringList(flds, func(s string) bool {
return s == pre
})
switch i {
case -1:
return "", false
case len(flds) - 1:
return "", false
default:
return strings.Trim(flds[i+1], ")"), true
}
}
func searchFieldWithPrefix(flds []string, pred func(string) bool) (string, bool) {
switch i := findInStringList(flds, pred); i {
case -1:
return "", false
default:
return flds[i], true
}
}
func hasPrefix(p string) func(s string) bool {
return func(s string) bool {
return strings.HasPrefix(s, p)
}
}
func findInStringList(l []string, f func(string) bool) int {
for i := range l {
if f(l[i]) {
return i
}
}
return -1
}
func matchDefaultExpectations(t *testing.T, got, exp map[string]string) {
for k, v := range exp {
switch gv, found := got[k]; {
case found && gv != v:
t.Errorf("%s: expected value %q, got %q", k, v, gv)
case !found:
t.Errorf("%s: expected to be present, it wasn't", k)
}
delete(got, k)
}
for k := range got {
t.Errorf("%s: is present, but wasn't expected", k)
}
}
func TestCLIHelp_FileFormatDefault(t *testing.T) {
cfg := new(phraseapp.Config)
cfg.DefaultFileFormat = "FILE_FORMAT"
out, err := runWithCfg(cfg, "locale/download")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
matchDefaultExpectations(t, defaults, map[string]string{
"--file-format": "FILE_FORMAT",
})
}
func TestCLIHelp_FileFormatDefaultTwice(t *testing.T) {
cfg := new(phraseapp.Config)
cfg.DefaultFileFormat = "FILE_FORMAT"
cfg.Defaults = map[string]map[string]interface{}{}
cfg.Defaults["locale/download"] = map[string]interface{}{
"file_format": "OTHER_FILE_FORMAT",
}
out, err := runWithCfg(cfg, "locale/download")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
matchDefaultExpectations(t, defaults, map[string]string{
"--file-format": "OTHER_FILE_FORMAT",
})
}
func TestCLIHelp_FileFormatDefaultThrice(t *testing.T) {
cfg := new(phraseapp.Config)
cfg.DefaultFileFormat = "FILE_FORMAT"
cfg.Defaults = map[string]map[string]interface{}{}
cfg.Defaults["locale/download"] = map[string]interface{}{
"file_format": "OTHER_FILE_FORMAT",
}
out, err := runWithCfg(cfg, "locale/download", "--file-format", "YET_ANOTHER_FILE_FORMAT")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
matchDefaultExpectations(t, defaults, map[string]string{
"--file-format": "YET_ANOTHER_FILE_FORMAT",
})
}
func itop(i int) *int {
return &i
}
func TestCLIHelp_PerPageSettings(t *testing.T) {
cfg := new(phraseapp.Config)
cfg.Page = itop(2)
cfg.PerPage = itop(12)
out, err := runWithCfg(cfg, "locales/list")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
matchDefaultExpectations(t, defaults, map[string]string{
"--page": "2",
"--per-page": "12",
})
}
func TestCLIHelp_PerPageSettingsOverride(t *testing.T) {
cfg := new(phraseapp.Config)
cfg.Page = itop(2)
cfg.PerPage = itop(12)
out, err := runWithCfg(cfg, "locales/list", "--page", "3", "--per-page", "42")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
matchDefaultExpectations(t, defaults, map[string]string{
"--page": "3",
"--per-page": "42",
})
}
func TestCLIHelp_FormatOptions(t *testing.T) {
cfg := new(phraseapp.Config)
cfg.Defaults = map[string]map[string]interface{}{}
cfg.Defaults["locale/download"] = map[string]interface{}{
"format_options": map[interface{}]interface{}{
"convert_placeholders": "true",
},
}
out, err := runWithCfg(cfg, "locale/download")
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
defaults, err := collectDefaults(out)
if err != nil {
t.Fatalf("didn't expect an error, got: %s", err)
}
// FormatOptions are ignored with regard to defaults!
matchDefaultExpectations(t, defaults, map[string]string{})
}