/
config.t
1058 lines (910 loc) · 36.5 KB
/
config.t
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More tests => 315;
#use Test::More 'no_plan';
use File::Spec;
use Test::MockModule;
use Test::Exception;
use Test::NoWarnings;
use Path::Class;
use File::Path qw(remove_tree);
my $CLASS;
BEGIN {
$CLASS = 'App::Sqitch';
use_ok $CLASS or die;
}
ok my $sqitch = App::Sqitch->new, 'Load a sqitch object';
isa_ok my $cmd = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'config',
config => $sqitch->config,
}), 'App::Sqitch::Command::config', 'Config command';
isa_ok $cmd, 'App::Sqitch::Command', 'Config command';
can_ok $cmd, qw(file action context get get_all get_regex set add unset unset_all list edit);
is_deeply [$cmd->options], [qw(
file|config-file|f=s
local
user
system
int
bool
bool-or-int
num
get
get-all
get-regex
add
replace-all
unset
unset-all
rename-section
remove-section
list|l
edit|e
)], 'Options should be configured';
##############################################################################
# Test configure errors.
my $mock = Test::MockModule->new('App::Sqitch::Command::config');
my @usage;
$mock->mock(usage => sub { shift; @usage = @_; die 'USAGE' });
# Test for multiple config file specifications.
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
user => 1,
system => 1,
}) } qr/USAGE/, 'Construct with user and system';
is_deeply \@usage, ['Only one config file at a time.'],
'Should get error for multiple config files';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
user => 1,
local => 1,
}) } qr/USAGE/, 'Construct with user and local';
is_deeply \@usage, ['Only one config file at a time.'],
'Should get error for multiple config files';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
file => 't/sqitch.ini',
system => 1,
})} qr/USAGE/, 'Construct with file and system';
is_deeply \@usage, ['Only one config file at a time.'],
'Should get another error for multiple config files';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
file => 't/sqitch.ini',
user => 1,
})} qr/USAGE/, 'Construct with file and user';
is_deeply \@usage, ['Only one config file at a time.'],
'Should get a third error for multiple config files';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
file => 't/sqitch.ini',
user => 1,
system => 1,
})} qr/USAGE/, 'Construct with file, system, and user';
is_deeply \@usage, ['Only one config file at a time.'],
'Should get one last error for multiple config files';
# Test for multiple type specifications.
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
bool => 1,
num => 1,
}) } qr/USAGE/, 'Construct with bool and num';
is_deeply \@usage, ['Only one type at a time.'],
'Should get error for multiple types';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
sqitch => $sqitch,
int => 1,
num => 1,
})} qr/USAGE/, 'Construct with int and num';
is_deeply \@usage, ['Only one type at a time.'],
'Should get another error for multiple types';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
int => 1,
bool => 1,
})} qr/USAGE/, 'Construct with int and bool';
is_deeply \@usage, ['Only one type at a time.'],
'Should get a third error for multiple types';
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
int => 1,
bool => 1,
num => 1,
})} qr/USAGE/, 'Construct with int, num, and bool';
is_deeply \@usage, ['Only one type at a time.'],
'Should get one last error for multiple types';
# Test for multiple action specifications.
for my $spec (
[qw(get unset)],
[qw(get unset edit)],
[qw(get unset edit list)],
[qw(unset edit)],
[qw(unset edit list)],
[qw(edit list)],
[qw(edit add list)],
[qw(edit add list get_all)],
[qw(edit add list get_regex)],
[qw(edit add list unset_all)],
[qw(edit add list get_all unset_all)],
[qw(edit list remove_section)],
[qw(edit list remove_section rename_section)],
) {
throws_ok { App::Sqitch::Command::config->configure( $sqitch->config, {
map { $_ => 1 } @{ $spec }
})} qr/USAGE/, 'Construct with ' . join ' & ' => @{ $spec };
is_deeply \@usage, ['Only one action at a time.'],
'Should get error for multiple actions';
}
##############################################################################
# Test context.
is $cmd->file, $sqitch->config->dir_file,
'Default context should be local context';
is $cmd->action, undef, 'Default action should be undef';
is $cmd->context, undef, 'Default context should be undef';
# Test local file name.
is_deeply App::Sqitch::Command::config->configure( $sqitch->config, {
local => 1,
}), {
context => 'local',
}, 'Local context should be local';
# Test user file name.
is_deeply App::Sqitch::Command::config->configure( $sqitch->config, {
user => 1,
}), {
context => 'user',
}, 'User context should be user';
# Test system file name.
is_deeply App::Sqitch::Command::config->configure( $sqitch->config, {
system => 1,
}), {
context => 'system',
}, 'System context should be system';
##############################################################################
# Test execute().
my @fail;
$mock->mock(fail => sub { shift; @fail = @_; die "FAIL @_" });
my @unfound;
$mock->mock(unfound => sub { shift; @unfound = @_; die "UNFOUND @_" });
my @set;
$mock->mock(set => sub { shift; @set = @_; return 1 });
my @get;
$mock->mock(get => sub { shift; @get = @_; return 1 });
my @get_all;
$mock->mock(get_all => sub { shift; @get_all = @_; return 1 });
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'system',
}), 'Create config set command';
ok $cmd->execute(qw(foo bar)), 'Execute the set command';
is_deeply \@set, [qw(foo bar)], 'The set method should have been called';
ok $cmd->execute(qw(foo)), 'Execute the get command';
is_deeply \@get, [qw(foo)], 'The get method should have been called';
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-all',
}), 'Create config get-all command';
$cmd->execute('boy.howdy');
is_deeply \@get_all, ['boy.howdy'],
'An action with a dash should have triggered a method with an underscore';
$mock->unmock(qw(set get get_all));
##############################################################################
# Test get().
chdir 't';
$ENV{SQITCH_USER_CONFIG} = 'user.conf';
$sqitch->config->load;
my @emit;
$mock->mock(emit => sub { shift; push @emit => [@_] });
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get',
}), 'Create config get command';
ok $cmd->execute('core.engine'), 'Get core.engine';
is_deeply \@emit, [['pg']], 'Should have emitted the merged core.engine';
@emit = ();
ok $cmd->execute('core.pg.host'), 'Get core.pg.host';
is_deeply \@emit, [['localhost']], 'Should have emitted the merged core.pg.host';
@emit = ();
ok $cmd->execute('core.pg.client'), 'Get core.pg.client';
is_deeply \@emit, [['/usr/local/pgsql/bin/psql']],
'Should have emitted the merged core.pg.client';
@emit = ();
# Make sure the key is required.
throws_ok { $cmd->get } qr/USAGE/, 'Should get usage for missing get key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing get key should trigger a usage message';
throws_ok { $cmd->get('') } qr/USAGE/, 'Should get usage for invalid get key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid get key should trigger a usage message';
# Make sure int data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get',
type => 'int',
}), 'Create config get int command';
ok $cmd->execute('revert.count'), 'Get revert.count as int';
is_deeply \@emit, [[2]],
'Should have emitted the revert count';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as int';
is_deeply \@emit, [[1]],
'Should have emitted the revert revision as an int';
@emit = ();
throws_ok { $cmd->execute('bundle.tags_only') } qr/FAIL/,
'Get bundle.tags_only as an int should fail';
# Make sure num data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get',
type => 'num',
}), 'Create config get num command';
ok $cmd->execute('revert.count'), 'Get revert.count as num';
is_deeply \@emit, [[2]],
'Should have emitted the revert count';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as num';
is_deeply \@emit, [[1.1]],
'Should have emitted the revert revision as an num';
@emit = ();
throws_ok { $cmd->execute('bundle.tags_only') } qr/FAIL/,
'Get bundle.tags_only as an num should fail';
# Make sure bool data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get',
type => 'bool',
}), 'Create config get bool command';
throws_ok { $cmd->execute('revert.count') } qr/FAIL/,
'Should get failure for invalid bool int';
throws_ok { $cmd->execute('revert.revision') } qr/FAIL/,
'Should get failure for invalid bool num';
ok $cmd->execute('bundle.tags_only'), 'Get bundle.tags_only as bool';
is_deeply \@emit, [['true']],
'Should have emitted bundle.tags_only as a bool';
@emit = ();
# Make sure bool-or-int data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get',
type => 'bool-or-int',
}), 'Create config get bool-or-int command';
ok $cmd->execute('revert.count'), 'Get revert.count as bool-or-int';
is_deeply \@emit, [[2]],
'Should have emitted the revert count as an int';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as bool-or-int';
is_deeply \@emit, [[1]],
'Should have emitted the revert revision as an int';
@emit = ();
ok $cmd->execute('bundle.tags_only'), 'Get bundle.tags_only as bool-or-int';
is_deeply \@emit, [['true']],
'Should have emitted bundle.tags_only as a bool';
@emit = ();
chdir File::Spec->updir;
CONTEXT: {
local $ENV{SQITCH_SYSTEM_CONFIG} = file qw(t sqitch.conf);
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'system',
action => 'get',
}), 'Create system config get command';
ok $cmd->execute('core.engine'), 'Get system core.engine';
is_deeply \@emit, [['pg']], 'Should have emitted the system core.engine';
@emit = ();
ok $cmd->execute('core.pg.client'), 'Get system core.pg.client';
is_deeply \@emit, [['/usr/local/pgsql/bin/psql']],
'Should have emitted the system core.pg.client';
@emit = @fail = @unfound = ();
throws_ok { $cmd->execute('core.pg.host') } qr/UNFOUND/,
'Attempt to get core.pg.host should fail';
is_deeply \@emit, [], 'Nothing should have been emitted';
is_deeply \@unfound, [], 'Nothing should have been output on failure';
local $ENV{SQITCH_USER_CONFIG} = file qw(t user.conf);
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'user',
action => 'get',
}), 'Create user config get command';
@emit = ();
ok $cmd->execute('core.pg.host'), 'Get user core.pg.host';
is_deeply \@emit, [['localhost']], 'Should have emitted the user core.pg.host';
@emit = ();
ok $cmd->execute('core.pg.client'), 'Get user core.pg.client';
is_deeply \@emit, [['/opt/local/pgsql/bin/psql']],
'Should have emitted the user core.pg.client';
@emit = ();
local $ENV{SQITCH_CONFIG} = file qw(t local.conf);
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'local',
action => 'get',
}), 'Create local config get command';
@emit = ();
ok $cmd->execute('core.pg.db_name'), 'Get local core.pg.db_name';
is_deeply \@emit, [['widgets']], 'Should have emitted the local core.pg.db_name';
@emit = ();
ok $cmd->execute('core.engine'), 'Get local core.engine';
is_deeply \@emit, [['pg']], 'Should have emitted the local core.engine';
@emit = ();
}
CONTEXT: {
# What happens when there is no config file?
local $ENV{SQITCH_SYSTEM_CONFIG} = 'NONEXISTENT';
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'system',
action => 'get',
}), 'Create another system config get command';
ok !-f $cmd->file, 'There should be no system config file';
throws_ok { $cmd->execute('core.engine') } qr/UNFOUND/,
'Should fail when no system config file';
is_deeply \@unfound, [], 'Nothing should have been emitted';
local $ENV{SQITCH_USER_CONFIG} = 'NONEXISTENT';
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'user',
action => 'get',
}), 'Create another user config get command';
ok !-f $cmd->file, 'There should be no user config file';
throws_ok { $cmd->execute('core.engine') } qr/UNFOUND/,
'Should fail when no user config file';
is_deeply \@unfound, [], 'Nothing should have been emitted';
local $ENV{SQITCH_CONFIG} = 'NONEXISTENT';
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'local',
action => 'get',
}), 'Create another local config get command';
ok !-f $cmd->file, 'There should be no local config file';
throws_ok { $cmd->execute('core.engine') } qr/UNFOUND/,
'Should fail when no local config file';
is_deeply \@unfound, [], 'Nothing should have been emitted';
}
##############################################################################
# Test list().
local $ENV{SQITCH_SYSTEM_CONFIG} = file qw(t sqitch.conf);
local $ENV{SQITCH_USER_CONFIG} = file qw(t user.conf);
local $ENV{SQITCH_CONFIG} = file qw(t local.conf);
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'list',
}), 'Create config list command';
ok $cmd->execute, 'Execute the list action';
is_deeply \@emit, [[
"bundle.dest_dir=_build/sql
bundle.from=gamma
bundle.tags_only=true
core.engine=pg
core.extension=ddl
core.mysql.client=/opt/local/mysql/bin/mysql
core.mysql.sqitch_prefix=meta
core.mysql.username=root
core.pg.client=/opt/local/pgsql/bin/psql
core.pg.db_name=widgets
core.pg.host=localhost
core.pg.sqitch_schema=meta
core.pg.username=postgres
core.sql_dir=migrations
core.sqlite.client=/opt/local/bin/sqlite3
core.sqlite.sqitch_prefix=meta
revert.count=2
revert.revision=1.1
revert.to=gamma
"
]], 'Should have emitted the merged config';
@emit = ();
CONTEXT: {
local $ENV{SQITCH_SYSTEM_CONFIG} = file qw(t sqitch.conf);
local $ENV{SQITCH_USER_CONFIG} = undef;
local $ENV{SQITCH_CONFIG} = undef;
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'system',
action => 'list',
}), 'Create system config list command';
ok $cmd->execute, 'List the system config';
is_deeply \@emit, [[
"bundle.dest_dir=_build/sql
bundle.from=gamma
bundle.tags_only=true
core.engine=pg
core.extension=ddl
core.pg.client=/usr/local/pgsql/bin/psql
core.pg.username=theory
core.sql_dir=migrations
revert.count=2
revert.revision=1.1
revert.to=gamma
"
]], 'Should have emitted the system config list';
@emit = ();
$ENV{SQITCH_USER_CONFIG} = file qw(t user.conf);
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'user',
action => 'list',
}), 'Create user config list command';
ok $cmd->execute, 'List the user config';
is_deeply \@emit, [[
"core.mysql.client=/opt/local/mysql/bin/mysql
core.mysql.sqitch_prefix=meta
core.mysql.username=root
core.pg.client=/opt/local/pgsql/bin/psql
core.pg.host=localhost
core.pg.sqitch_schema=meta
core.pg.username=postgres
core.sqlite.client=/opt/local/bin/sqlite3
core.sqlite.sqitch_prefix=meta
"
]], 'Should only have emitted the user config list';
@emit = ();
$ENV{SQITCH_CONFIG} = file qw(t local.conf);
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'local',
action => 'list',
}), 'Create local config list command';
ok $cmd->execute, 'List the local config';
is_deeply \@emit, [[
"core.engine=pg
core.pg.db_name=widgets
"
]], 'Should only have emitted the local config list';
@emit = ();
}
CONTEXT: {
# What happens when there is no config file?
local $ENV{SQITCH_SYSTEM_CONFIG} = 'NONEXISTENT';
local $ENV{SQITCH_USER_CONFIG} = undef;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'system',
action => 'list',
}), 'Create system config list command with no file';
ok $cmd->execute, 'List the system config';
is_deeply \@emit, [], 'Nothing should have been emitted';
$ENV{SQITCH_USER_CONFIG} = 'NONEXISTENT';
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
context => 'user',
action => 'list',
}), 'Create user config list command with no file';
ok $cmd->execute, 'List the user config';
is_deeply \@emit, [], 'Nothing should have been emitted';
}
##############################################################################
# Test set().
my $file = 'testconfig.conf';
$mock->mock(file => $file);
END { unlink $file }
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
}), 'Create system config set command';
ok $cmd->execute('core.foo' => 'bar'), 'Write core.foo';
is_deeply read_config($cmd->file), {'core.foo' => 'bar' },
'The property should have been written';
# Write another property.
ok $cmd->execute('core.engine' => 'funky'), 'Write core.engine';
is_deeply read_config($cmd->file), {'core.foo' => 'bar', 'core.engine' => 'funky' },
'Both settings should be saved';
# Write a sub-propery.
ok $cmd->execute('core.pg.user' => 'theory'), 'Write core.pg.user';
is_deeply read_config($cmd->file), {
'core.foo' => 'bar',
'core.engine' => 'funky',
'core.pg.user' => 'theory',
}, 'Both sections should be saved';
# Make sure the key is required.
throws_ok { $cmd->set } qr/USAGE/, 'Should set usage for missing set key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing set key should trigger a usage message';
throws_ok { $cmd->set('') } qr/USAGE/, 'Should set usage for invalid set key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid set key should trigger a usage message';
# Make sure the value is required.
throws_ok { $cmd->set('foo.bar') } qr/USAGE/, 'Should set usage for missing set value';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing set value should trigger a usage message';
##############################################################################
# Test add().
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'add',
}), 'Create system config add command';
ok $cmd->execute('core.foo' => 'baz'), 'Add to core.foo';
is_deeply read_config($cmd->file), {
'core.foo' => ['bar', 'baz'],
'core.engine' => 'funky',
'core.pg.user' => 'theory',
}, 'The value should have been added to the property';
# Make sure the key is required.
throws_ok { $cmd->add } qr/USAGE/, 'Should add usage for missing add key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing add key should trigger a usage message';
throws_ok { $cmd->add('') } qr/USAGE/, 'Should add usage for invalid add key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid add key should trigger a usage message';
# Make sure the value is required.
throws_ok { $cmd->add('foo.bar') } qr/USAGE/, 'Should add usage for missing add value';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing add value should trigger a usage message';
##############################################################################
# Test get with regex.
delete $ENV{SQITCH_CONFIG};
$ENV{SQITCH_USER_CONFIG} = $file;
$sqitch->config->load;
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get',
}), 'Create system config add command';
ok $cmd->execute('core.engine', 'funk'), 'Get core.engine with regex';
is_deeply \@emit, [['funky']], 'Should have emitted value';
@emit = ();
ok $cmd->execute('core.foo', 'z$'), 'Get core.foo with regex';
is_deeply \@emit, [['baz']], 'Should have emitted value';
@emit = ();
throws_ok { $cmd->execute('core.foo', 'x$') } qr/UNFOUND/,
'Attempt to get core.foo with non-matching regex should fail';
is_deeply \@emit, [], 'Nothing should have been emitted';
is_deeply \@unfound, [], 'Nothing should have been output on failure';
##############################################################################
# Test get_all().
@emit = ();
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-all',
}), 'Create system config get_all command';
ok $cmd->execute('core.engine'), 'Call get_all on core.engine';
is_deeply \@emit, [['funky']], 'The engine should have been emitted';
@emit = ();
ok $cmd->execute('core.engine', 'funk'), 'Get all core.engine with regex';
is_deeply \@emit, [['funky']], 'Should have emitted value';
@emit = ();
ok $cmd->execute('core.foo'), 'Call get_all on core.foo';
is_deeply \@emit, [["bar\nbaz"]], 'Both foos should have been emitted';
@emit = ();
ok $cmd->execute('core.foo', '^ba'), 'Call get_all on core.foo with regex';
is_deeply \@emit, [["bar\nbaz"]], 'Both foos should have been emitted';
@emit = ();
ok $cmd->execute('core.foo', 'z$'), 'Call get_all on core.foo with limiting regex';
is_deeply \@emit, [["baz"]], 'Only the one foo should have been emitted';
@emit = ();
throws_ok { $cmd->execute('core.foo', 'x$') } qr/UNFOUND/,
'Attempt to get_all core.foo with non-matching regex should fail';
is_deeply \@emit, [], 'Nothing should have been emitted';
is_deeply \@unfound, [], 'Nothing should have been output on failure';
# Make sure the key is required.
throws_ok { $cmd->get_all } qr/USAGE/, 'Should get_all usage for missing get_all key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing get_all key should trigger a usage message';
throws_ok { $cmd->get_all('') } qr/USAGE/, 'Should get_all usage for invalid get_all key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid get_all key should trigger a usage message';
# Make sure int data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-all',
type => 'int',
}), 'Create config get_all int command';
ok $cmd->execute('revert.count'), 'Get revert.count as int';
is_deeply \@emit, [[2]],
'Should have emitted the revert count';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as int';
is_deeply \@emit, [[1]],
'Should have emitted the revert revision as an int';
@emit = ();
throws_ok { $cmd->execute('bundle.tags_only') } qr/FAIL/,
'Get bundle.tags_only as an int should fail';
# Make sure num data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-all',
type => 'num',
}), 'Create config get_all num command';
ok $cmd->execute('revert.count'), 'Get revert.count as num';
is_deeply \@emit, [[2]],
'Should have emitted the revert count';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as num';
is_deeply \@emit, [[1.1]],
'Should have emitted the revert revision as an num';
@emit = ();
throws_ok { $cmd->execute('bundle.tags_only') } qr/FAIL/,
'Get bundle.tags_only as an num should fail';
# Make sure bool data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-all',
type => 'bool',
}), 'Create config get_all bool command';
throws_ok { $cmd->execute('revert.count') } qr/FAIL/,
'Should get failure for invalid bool int';
throws_ok { $cmd->execute('revert.revision') } qr/FAIL/,
'Should get failure for invalid bool num';
ok $cmd->execute('bundle.tags_only'), 'Get bundle.tags_only as bool';
is_deeply \@emit, [[$Config::GitLike::VERSION > 1.08 ? 'true' : 1]],
'Should have emitted bundle.tags_only as a bool';
@emit = ();
# Make sure bool-or-int data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-all',
type => 'bool-or-int',
}), 'Create config get_all bool-or-int command';
ok $cmd->execute('revert.count'), 'Get revert.count as bool-or-int';
is_deeply \@emit, [[2]],
'Should have emitted the revert count as an int';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as bool-or-int';
is_deeply \@emit, [[1]],
'Should have emitted the revert revision as an int';
@emit = ();
ok $cmd->execute('bundle.tags_only'), 'Get bundle.tags_only as bool-or-int';
is_deeply \@emit, [[$Config::GitLike::VERSION > 1.08 ? 'true' : 1]],
'Should have emitted bundle.tags_only as a bool';
@emit = ();
##############################################################################
# Test get_regex().
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-regex',
}), 'Create system config get_regex command';
ok $cmd->execute('core\\..+'), 'Call get_regex on core\\..+';
is_deeply \@emit, [[q{core.engine=funky
core.extension=ddl
core.foo=[bar, baz]
core.pg.client=/usr/local/pgsql/bin/psql
core.pg.user=theory
core.pg.username=theory
core.sql_dir=migrations}
]], 'Should match all core options';
@emit = ();
ok $cmd->execute('core\\.pg\\..+'), 'Call get_regex on core\\.pg\\..+';
is_deeply \@emit, [[q{core.pg.client=/usr/local/pgsql/bin/psql
core.pg.user=theory
core.pg.username=theory}
]], 'Should match all core.pg options';
@emit = ();
ok $cmd->execute('core\\.pg\\..+', 'theory$'),
'Call get_regex on core\\.pg\\..+ and value regex';
is_deeply \@emit, [[q{core.pg.user=theory
core.pg.username=theory}
]], 'Should match all core.pg options that match';
@emit = ();
throws_ok { $cmd->execute('core\\.pg\\..+', 'x$') } qr/UNFOUND/,
'Attempt to get_regex core.foo with non-matching regex should fail';
is_deeply \@emit, [], 'Nothing should have been emitted';
is_deeply \@unfound, [], 'Nothing should have been output on failure';
# Make sure the key is required.
throws_ok { $cmd->get_regex } qr/USAGE/, 'Should get_regex usage for missing get_regex key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing get_regex key should trigger a usage message';
throws_ok { $cmd->get_regex('') } qr/USAGE/, 'Should get_regex usage for invalid get_regex key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid get_regex key should trigger a usage message';
# Make sure int data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-regex',
type => 'int',
}), 'Create config get_regex int command';
ok $cmd->execute('revert.count'), 'Get revert.count as int';
is_deeply \@emit, [['revert.count=2']],
'Should have emitted the revert count';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as int';
is_deeply \@emit, [['revert.revision=1']],
'Should have emitted the revert revision as an int';
@emit = ();
throws_ok { $cmd->execute('bundle.tags_only') } qr/FAIL/,
'Get bundle.tags_only as an int should fail';
# Make sure num data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-regex',
type => 'num',
}), 'Create config get_regex num command';
ok $cmd->execute('revert.count'), 'Get revert.count as num';
is_deeply \@emit, [['revert.count=2']],
'Should have emitted the revert count';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as num';
is_deeply \@emit, [['revert.revision=1.1']],
'Should have emitted the revert revision as an num';
@emit = ();
throws_ok { $cmd->execute('bundle.tags_only') } qr/FAIL/,
'Get bundle.tags_only as an num should fail';
# Make sure bool data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-regex',
type => 'bool',
}), 'Create config get_regex bool command';
throws_ok { $cmd->execute('revert.count') } qr/FAIL/,
'Should get failure for invalid bool int';
throws_ok { $cmd->execute('revert.revision') } qr/FAIL/,
'Should get failure for invalid bool num';
ok $cmd->execute('bundle.tags_only'), 'Get bundle.tags_only as bool';
is_deeply \@emit, [['bundle.tags_only=' . ($Config::GitLike::VERSION > 1.08 ? 'true' : 1)]],
'Should have emitted bundle.tags_only as a bool';
@emit = ();
# Make sure int data type works.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'get-regex',
type => 'bool-or-int',
}), 'Create config get_regex bool-or-int command';
ok $cmd->execute('revert.count'), 'Get revert.count as bool-or-int';
is_deeply \@emit, [['revert.count=2']],
'Should have emitted the revert count as an int';
@emit = ();
ok $cmd->execute('revert.revision'), 'Get revert.revision as bool-or-int';
is_deeply \@emit, [['revert.revision=1']],
'Should have emitted the revert revision as an int';
@emit = ();
ok $cmd->execute('bundle.tags_only'), 'Get bundle.tags_only as bool-or-int';
is_deeply \@emit, [['bundle.tags_only=' . ($Config::GitLike::VERSION > 1.08 ? 'true' : 1)]],
'Should have emitted bundle.tags_only as a bool';
@emit = ();
##############################################################################
# Test unset().
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'unset',
}), 'Create system config unset command';
ok $cmd->execute('core.pg.user'), 'Unset core.pg.user';
is_deeply read_config($cmd->file), {
'core.foo' => ['bar', 'baz'],
'core.engine' => 'funky',
}, 'core.pg.user should be gone';
ok $cmd->execute('core.engine'), 'Unset core.engine';
is_deeply read_config($cmd->file), {
'core.foo' => ['bar', 'baz'],
}, 'core.engine should have been removed';
throws_ok { $cmd->execute('core.foo') } qr/FAIL/,
'Should get failure trying to delete multivalue key';
is_deeply \@fail, ['Cannot unset key with multiple values'],
'And it should have show the proper error message';
ok $cmd->execute('core.foo', 'z$'), 'Unset core.foo with a regex';
is_deeply read_config($cmd->file), {
'core.foo' => 'bar',
}, 'The core.foo "baz" value should have been removed';
# Make sure the key is required.
throws_ok { $cmd->unset } qr/USAGE/, 'Should unset usage for missing unset key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing unset key should trigger a usage message';
throws_ok { $cmd->unset('') } qr/USAGE/, 'Should unset usage for invalid unset key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid unset key should trigger a usage message';
##############################################################################
# Test unset_all().
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'unset-all',
}), 'Create system config unset-all command';
$cmd->add('core.foo', 'baz');
ok $cmd->execute('core.foo'), 'Unset-all core.foo';
is_deeply read_config($cmd->file), {}, 'core.foo should have been removed';
# Test handling of multiple value.
$cmd->add('core.foo', 'bar');
$cmd->add('core.foo', 'baz');
$cmd->add('core.foo', 'yo');
ok $cmd->execute('core.foo', '^ba'), 'Unset-all core.foo with regex';
is_deeply read_config($cmd->file), {
'core.foo' => 'yo',
}, 'core.foo should have one value left';
# Make sure the key is required.
throws_ok { $cmd->unset_all } qr/USAGE/, 'Should unset_all usage for missing unset_all key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the missing unset_all key should trigger a usage message';
throws_ok { $cmd->unset_all('') } qr/USAGE/, 'Should unset_all usage for invalid unset_all key';
is_deeply \@usage, ['Wrong number of arguments.'],
'And the invalid unset_all key should trigger a usage message';
##############################################################################
# Test replace-all.
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'replace-all',
}), 'Create system config replace-all command';
$cmd->add('core.bar', 'bar');
$cmd->add('core.bar', 'baz');
$cmd->add('core.bar', 'yo');
ok $cmd->execute('core.bar', 'hi'), 'Replace all core.bar';
is_deeply read_config($cmd->file), {
'core.bar' => 'hi',
'core.foo' => 'yo',
}, 'core.bar should have all its values with one value';
$cmd->add('core.foo', 'bar');
$cmd->add('core.foo', 'baz');
ok $cmd->execute('core.foo', 'ba', '^ba'), 'Replace all core.bar matching /^ba/';
is_deeply read_config($cmd->file), {
'core.bar' => 'hi',
'core.foo' => ['yo', 'ba'],
}, 'core.foo should have had the matching values replaced';
# Clean up.
$cmd->unset_all('core.bar');
$cmd->unset('core.foo', 'ba');
##############################################################################
# Test rename_section().
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'rename-section',
}), 'Create system config rename-section command';
ok $cmd->execute('core', 'funk'), 'Rename "core" to "funk"';
is_deeply read_config($cmd->file), {
'funk.foo' => 'yo',
}, 'core.foo should have become funk.foo';
throws_ok { $cmd->execute('foo') } qr/USAGE/, 'Should fail with no new name';
is_deeply \@usage, ['Wrong number of arguments.'],
'Message should be in the usage call';
throws_ok { $cmd->execute('', 'bar') } qr/USAGE/, 'Should fail with bad old name';
is_deeply \@usage, ['Wrong number of arguments.'],
'Message should be in the usage call';
throws_ok { $cmd->execute('baz', '') } qr/USAGE/, 'Should fail with bad new name';
is_deeply \@usage, ['Wrong number of arguments.'],
'Message should be in the usage call';
throws_ok { $cmd->execute('foo', 'bar') } qr/FAIL/, 'Should fail with invalid section';
is_deeply \@fail, ['No such section!'],
'Message should be in the fail call';
##############################################################################
# Test remove_section().
ok $cmd = App::Sqitch::Command::config->new({
sqitch => $sqitch,
action => 'remove-section',
}), 'Create system config remove-section command';
ok $cmd->execute('funk'), 'Remove "func" section';
is_deeply read_config($cmd->file), {},
'The "funk" section should be gone';
throws_ok { $cmd->execute() } qr/USAGE/, 'Should fail with no name';