-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcmd_test.go
539 lines (522 loc) · 14.1 KB
/
cmd_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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package main
import (
"bytes"
"fmt"
"os"
"testing"
tu "github.com/bitnami/gonit/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type iniTestValue struct {
globalOpts *Options
section string
key string
value string
isBoolean bool
}
type iniSetTest struct {
name string
values []iniTestValue
initialText string
expectedText string
createIniFile bool
expectedErr interface{}
}
type iniGetTest struct {
name string
iniTestValue
initialText string
createIniFile bool
expectedErr interface{}
}
type iniDelTest struct {
name string
values []iniTestValue
initialText string
expectedText string
createIniFile bool
expectedErr interface{}
}
var delTests = []iniDelTest{
{
name: "Deletes key non existent",
values: []iniTestValue{
{
section: "general", key: "mykey",
},
},
createIniFile: true,
expectedText: ``,
},
{
name: "Deletes boolean value",
initialText: "[general]\nboolkey\nkey1=val1\n",
values: []iniTestValue{
{
section: "general", key: "boolkey",
},
},
expectedText: `\[general\]\nkey1=val1\n\s*$`,
},
{
name: "Deletes key in file containing semicolon values in regular mode",
values: []iniTestValue{
{
section: "general", key: "key2",
},
},
initialText: "[general]\nkey1=`my ; value`\nkey2=val2",
expectedText: "\\[general\\]\nkey1=`my ; value`\n\\s*$",
},
{
name: "Deletes key in file containing semicolon values in regular mode (2)",
values: []iniTestValue{
{
section: "general", key: "key2",
},
},
initialText: "[general]\nkey1=my ; my comment\nkey2=val2",
expectedText: "\\[general\\]\n; my comment\nkey1=my\n\\s*$",
},
{
name: "Deletes key in file containing semicolon values in ignore-inline-comments mode",
values: []iniTestValue{
{
section: "general", key: "key2", globalOpts: &Options{IgnoreInlineComments: true},
},
},
initialText: "[general]\nkey1=`my ; value`\nkey2=val2",
expectedText: "\\[general\\]\nkey1=my ; value\n\\s*$",
},
{
name: "Deletes key in file containing semicolon values in ignore-inline-comments mode (2)",
values: []iniTestValue{
{
section: "general", key: "key2", globalOpts: &Options{IgnoreInlineComments: true},
},
},
initialText: "[general]\nkey1=my ; my comment\nkey2=val2",
expectedText: "\\[general\\]\nkey1=my ; my comment\n\\s*$",
},
{
name: "Deletes regular value",
initialText: "[general]\nkey1=val1\nkey2=val2\n",
values: []iniTestValue{
{
section: "general", key: "key1",
},
},
expectedText: `\[general\]\nkey2=val2\n\s*$`,
},
{
name: "Fails if ini file does not exists",
values: []iniTestValue{{section: "general", key: "key1"}},
expectedErr: "no such file or directory",
},
{
name: "Preserve comments",
createIniFile: true,
initialText: "# this is a comment\n[general]\n# key 1 sample\nkey1=value1\n# mykey comment\nmykey=myvalue",
values: []iniTestValue{
{section: "general", key: "key1"},
},
expectedText: `^# this is a comment\n\[general\]\n# mykey comment\nmykey=myvalue\n\s*$`,
},
}
var getTests = []iniGetTest{
{
name: "Get non-existent",
createIniFile: true,
iniTestValue: iniTestValue{
section: "general", key: "mykey", value: "",
},
},
{
name: "Get regular key",
initialText: "[general]\nmykey=myvalue\n",
iniTestValue: iniTestValue{
section: "general", key: "mykey", value: "myvalue",
},
},
{
name: "Get value with semicolon in regular mode",
initialText: "[general]\nmykey=`my ; value`\n",
iniTestValue: iniTestValue{
section: "general", key: "mykey", value: "my ; value",
},
},
{
name: "Get value with semicolon in regular mode (2)",
initialText: "[general]\nmykey=my ; this is a comment\n",
iniTestValue: iniTestValue{
section: "general", key: "mykey", value: "my",
},
},
{
name: "Get value with semicolon in ignore-inline-comments mode",
initialText: "[general]\nmykey=`my ; value`\n",
iniTestValue: iniTestValue{
globalOpts: &Options{IgnoreInlineComments: true},
section: "general", key: "mykey", value: "my ; value",
},
},
{
name: "Get value with semicolon in ignore-inline-comments mode (2)",
initialText: "[general]\nmykey=my ; this is a comment\n",
iniTestValue: iniTestValue{
globalOpts: &Options{IgnoreInlineComments: true},
section: "general", key: "mykey", value: "my ; this is a comment",
},
},
{
name: "Get present boolean key",
initialText: "[general]\nmykey\n",
iniTestValue: iniTestValue{
section: "general", key: "mykey", value: "true",
},
},
{
name: "Fails if ini file does not exists",
iniTestValue: iniTestValue{section: "general", key: "key1"},
expectedErr: "no such file or directory",
},
{
name: "Get from malformed file",
createIniFile: true,
initialText: "this is not a\nINI\nfile\nmykey\n[general]\nmykey=myvalue",
iniTestValue: iniTestValue{
section: "general", key: "mykey", value: "myvalue",
},
},
}
var setTests = []iniSetTest{
{
name: "Sets regular key non existent",
values: []iniTestValue{
{
section: "general", key: "mykey", value: "myvalue",
},
},
expectedText: `mykey=myvalue\n`,
},
{
name: "Sets value with semicolon in regular mode",
values: []iniTestValue{
{
section: "general", key: "mykey", value: "my ; value",
},
},
expectedText: "mykey=`my ; value`\n",
},
{
name: "Sets value with semicolon in ignore-inline-comments mode",
values: []iniTestValue{
{
globalOpts: &Options{IgnoreInlineComments: true},
section: "general", key: "mykey", value: "my ; value",
},
},
expectedText: "mykey=my ; value\n",
},
{
name: "Sets boolean value",
values: []iniTestValue{
{
section: "testbool", key: "mykey", isBoolean: true,
},
},
expectedText: `\[testbool\]\nmykey\n\s*$`,
},
{
name: "Override with boolean value",
initialText: `\[testbool\]\nmykey=value1\n\s*$`,
values: []iniTestValue{
{
section: "testbool", key: "mykey", isBoolean: true,
},
},
expectedText: `\[testbool\]\nmykey\n\s*$`,
},
{
name: "Override boolean value with regular one",
initialText: `\[testbool\]\nmykey\n\s*$`,
values: []iniTestValue{
{
section: "testbool", key: "mykey", value: "myvalue",
},
},
expectedText: `\[testbool\]\nmykey=myvalue\n\s*$`,
},
{
name: "Set value with duplicate key",
initialText: `
[testduplicate]
mykey=value1
mykey=value2
`,
values: []iniTestValue{
{
section: "testduplicate", key: "mykey", value: "value3",
},
},
expectedText: `^\[testduplicate\]\nmykey=value1\nmykey=value2\nmykey=value3\n\s*$`,
},
{
name: "Set multiple keys",
values: []iniTestValue{
{section: "general", key: "key1", value: "value1"},
{section: "general", key: "key2", value: "value2"},
{section: "general", key: "key3", value: "value3"},
{section: "general", key: "key4", isBoolean: true},
},
expectedText: `^\[general\]\nkey1=value1\nkey2=value2\nkey3=value3\nkey4\n\s*$`,
},
{
name: "Sets regular keys in existing file",
values: []iniTestValue{
{section: "general", key: "mykey", value: "myvalue"},
{section: "general", key: "key2", value: "newvalue2"},
{section: "newsection", key: "key5", value: "value5"},
},
initialText: `
[general]
key1=value1
key2=value2
key3=value3
[newsection]
key4=value4
`,
expectedText: `^\[general\]\nkey1=value1\nkey2=newvalue2\nkey3=value3\nmykey=myvalue\n\s+` +
`\[newsection\]\nkey4=value4\nkey5=value5\n.*`,
},
{
name: "Preserve comments",
createIniFile: true,
initialText: "# this is a comment\n[general]\n# key 1 sample\nkey1=value1",
values: []iniTestValue{
{section: "general", key: "mykey", value: "myvalue"},
},
expectedText: `^# this is a comment\n\[general\]\n# key 1 sample\nkey1=value1\nmykey=myvalue\n\s*$`,
},
{
name: "Preserve arrays",
createIniFile: true,
initialText: "[general]\narray[]=value1\narray[]=value2",
values: []iniTestValue{
{section: "general", key: "mykey", value: "myvalue"},
},
expectedText: `^\[general\]\narray\[\]=value1\narray\[\]=value2\nmykey=myvalue\n\s*$`,
},
}
func testFile(t *testing.T, path string, fn func(t *testing.T, contents string) bool, msgAndArgs ...interface{}) bool {
if !assert.FileExists(t, path) {
return false
}
data, err := os.ReadFile(path)
require.NoError(t, err)
return fn(t, string(data))
}
func AssertFileContains(t *testing.T, path string, expected interface{}, msgAndArgs ...interface{}) bool {
return testFile(t, path, func(t *testing.T, contents string) bool {
return assert.Regexp(t, expected, contents, msgAndArgs...)
})
}
func AssertFileDoesNotContain(t *testing.T, path string, expected interface{}, msgAndArgs ...interface{}) bool {
return testFile(t, path, func(t *testing.T, contents string) bool {
return assert.NotRegexp(t, expected, contents, msgAndArgs...)
})
}
func TestINIFileSetCmd_Execute(t *testing.T) {
type testFn func(file, section, key, value string, isBoolean bool, opts *Options) error
var testViaCommand = func(file, section, key, value string, isBoolean bool, opts *Options) error {
cmd := NewINIFileSetCmd()
cmd.Section = section
cmd.Key = key
cmd.Value = value
cmd.Boolean = isBoolean
cmd.Args.File = file
if opts != nil {
globalOpts = opts
} else {
globalOpts = &Options{}
}
return cmd.Execute([]string{})
}
var testViaCli = func(file, section, key, value string, isBoolean bool, opts *Options) error {
args := []string{"set", "-k", key, "-s", section, "-v", value}
if isBoolean {
args = append(args, "-b")
}
args = append(args, file)
if opts != nil && opts.IgnoreInlineComments {
args = append([]string{"--ignore-inline-comments"}, args...)
}
res := RunTool(args...)
if !res.Success() {
return fmt.Errorf("%s", res.Stderr())
}
return nil
}
for _, tt := range setTests {
for id, fn := range map[string]testFn{
"command": testViaCommand,
"cli": testViaCli,
} {
var err error
file := ""
sb := tu.NewSandbox()
defer sb.Cleanup()
if tt.initialText != "" || tt.createIniFile {
file, err = sb.Write("my.ini", tt.initialText)
require.NoError(t, err)
} else {
file = sb.Normalize("my.ini")
}
testTitle := fmt.Sprintf("%s (via %s)", tt.name, id)
t.Run(testTitle, func(t *testing.T) {
for _, v := range tt.values {
err = fn(file, v.section, v.key, v.value, v.isBoolean, v.globalOpts)
if err != nil {
break
}
}
if tt.expectedErr != nil {
if err == nil {
t.Errorf("the command was expected to fail but succeeded")
} else {
assert.Regexp(t, tt.expectedErr, err)
}
} else {
require.NoError(t, err)
AssertFileContains(t, file, tt.expectedText)
}
})
}
}
}
func TestINIFileGetCmd_Execute(t *testing.T) {
type testFn func(file, section, key string, opts *Options) (string, error)
var testViaCommand = func(file, section, key string, opts *Options) (string, error) {
b := &bytes.Buffer{}
cmd := NewINIFileGetCmd()
cmd.Section = section
cmd.Key = key
cmd.Args.File = file
cmd.OutWriter = b
if opts != nil {
globalOpts = opts
} else {
globalOpts = &Options{}
}
err := cmd.Execute([]string{})
stdout := b.String()
return stdout, err
}
var testViaCli = func(file, section, key string, opts *Options) (string, error) {
args := []string{"get", "-k", key, "-s", section, file}
if opts != nil && opts.IgnoreInlineComments {
args = append([]string{"--ignore-inline-comments"}, args...)
}
res := RunTool(args...)
stdout := res.Stdout()
var err error
if !res.Success() {
err = fmt.Errorf("%s", res.Stderr())
}
return stdout, err
}
for _, tt := range getTests {
for id, fn := range map[string]testFn{
"command": testViaCommand,
"cli": testViaCli,
} {
var err error
file := ""
sb := tu.NewSandbox()
defer sb.Cleanup()
if tt.initialText != "" || tt.createIniFile {
file, err = sb.Write("my.ini", tt.initialText)
require.NoError(t, err)
} else {
file = sb.Normalize("my.ini")
}
testTitle := fmt.Sprintf("%s (via %s)", tt.name, id)
t.Run(testTitle, func(t *testing.T) {
stdout, err := fn(file, tt.section, tt.key, tt.globalOpts)
if tt.expectedErr != nil {
if err == nil {
t.Errorf("the command was expected to fail but succeeded")
} else {
assert.Regexp(t, tt.expectedErr, err)
}
} else {
require.NoError(t, err)
assert.Equal(t, tt.value, stdout)
}
})
}
}
}
func TestINIFileDelCmd_Execute(t *testing.T) {
type testFn func(file, section, key string, opts *Options) error
var testViaCommand = func(file, section, key string, opts *Options) error {
cmd := NewINIFileDelCmd()
cmd.Section = section
cmd.Key = key
cmd.Args.File = file
if opts != nil {
globalOpts = opts
} else {
globalOpts = &Options{}
}
return cmd.Execute([]string{})
}
var testViaCli = func(file, section, key string, opts *Options) error {
args := []string{"del", "-k", key, "-s", section, file}
if opts != nil && opts.IgnoreInlineComments {
args = append([]string{"--ignore-inline-comments"}, args...)
}
res := RunTool(args...)
if !res.Success() {
return fmt.Errorf("%s", res.Stderr())
}
return nil
}
for _, tt := range delTests {
for id, fn := range map[string]testFn{
"command": testViaCommand,
"cli": testViaCli,
} {
var err error
file := ""
sb := tu.NewSandbox()
defer sb.Cleanup()
if tt.initialText != "" || tt.createIniFile {
file, err = sb.Write("my.ini", tt.initialText)
require.NoError(t, err)
} else {
file = sb.Normalize("my.ini")
}
testTitle := fmt.Sprintf("%s (via %s)", tt.name, id)
t.Run(testTitle, func(t *testing.T) {
for _, v := range tt.values {
err = fn(file, v.section, v.key, v.globalOpts)
if err != nil {
break
}
}
if tt.expectedErr != nil {
if err == nil {
t.Errorf("the command was expected to fail but succeeded")
} else {
assert.Regexp(t, tt.expectedErr, err)
}
} else {
require.NoError(t, err)
AssertFileContains(t, file, tt.expectedText)
}
})
}
}
}