/
make_test.dart
1352 lines (1245 loc) · 45.8 KB
/
make_test.dart
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
// ignore_for_file: no_adjacent_strings_in_list
import 'dart:convert';
import 'dart:io';
import 'package:mason/mason.dart' hide packageVersion;
import 'package:mason/mason.dart' as mason show packageVersion;
import 'package:mason_cli/src/command_runner.dart';
import 'package:mason_cli/src/version.dart';
import 'package:mocktail/mocktail.dart';
import 'package:path/path.dart' as path;
import 'package:pub_updater/pub_updater.dart';
import 'package:test/test.dart';
import '../helpers/helpers.dart';
class MockLogger extends Mock implements Logger {}
class MockPubUpdater extends Mock implements PubUpdater {}
class MockProgress extends Mock implements Progress {}
void main() {
final cwd = Directory.current;
group('mason make', () {
late Logger logger;
late PubUpdater pubUpdater;
late MasonCommandRunner commandRunner;
setUpAll(() async {
registerFallbackValue(Object());
logger = MockLogger();
pubUpdater = MockPubUpdater();
when(
() => logger.prompt(any(), defaultValue: any(named: 'defaultValue')),
).thenReturn('');
when(() => logger.progress(any())).thenReturn(MockProgress());
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => packageVersion);
await MasonCommandRunner(logger: logger, pubUpdater: pubUpdater).run(
['cache', 'clear'],
);
});
setUp(() {
setUpTestingEnvironment(cwd, suffix: '.make');
File(path.join(Directory.current.path, 'mason.yaml')).writeAsStringSync(
'''
bricks:
app_icon:
path: ../../../../../bricks/app_icon
bio:
path: ../../../../../bricks/bio
documentation:
path: ../../../../../bricks/documentation
favorite_color:
path: ../../../../../bricks/favorite_color
flavors:
path: ../../../../../bricks/flavors
greeting:
path: ../../../../../bricks/greeting
legacy:
path: ../../../../../bricks/legacy
hello_world:
path: ../../../../../bricks/hello_world
hooks:
path: ../../../../../bricks/hooks
plugin:
path: ../../../../../bricks/plugin
random_color:
path: ../../../../../bricks/random_color
simple:
path: ../../../../../bricks/simple
todos:
path: ../../../../../bricks/todos
widget:
path: ../../../../../bricks/widget
''',
);
final bricksPath = path.join('..', '..', '..', '..', '..', 'bricks');
final appIconPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'app_icon'),
);
final bioPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'bio'),
);
final docPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'documentation'),
);
final favoriteColorPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'favorite_color'),
);
final flavorsPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'flavors'),
);
final greetingPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'greeting'),
);
final legacyPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'legacy'),
);
final helloWorldPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'hello_world'),
);
final hooksPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'hooks'),
);
final pluginPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'plugin'),
);
final randomColorPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'random_color'),
);
final simplePath = canonicalize(
path.join(Directory.current.path, bricksPath, 'simple'),
);
final todosPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'todos'),
);
final widgetPath = canonicalize(
path.join(Directory.current.path, bricksPath, 'widget'),
);
File(path.join(Directory.current.path, '.mason', 'bricks.json'))
..createSync(recursive: true)
..writeAsStringSync(
json.encode({
'app_icon': appIconPath,
'bio': bioPath,
'documentation': docPath,
'favorite_color': favoriteColorPath,
'flavors': flavorsPath,
'hello_world': helloWorldPath,
'hooks': hooksPath,
'greeting': greetingPath,
'legacy': legacyPath,
'plugin': pluginPath,
'random_color': randomColorPath,
'simple': simplePath,
'todos': todosPath,
'widget': widgetPath,
}),
);
printLogs = [];
logger = MockLogger();
pubUpdater = MockPubUpdater();
when(
() => logger.prompt(any(), defaultValue: any(named: 'defaultValue')),
).thenReturn('');
when(() => logger.progress(any())).thenReturn(MockProgress());
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => packageVersion);
commandRunner = MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
);
});
tearDown(() {
Directory.current = cwd;
});
test(
'--help shows correct help information',
overridePrint(() async {
const expectedPrintLogs = <String>[
'Generate code using an existing brick template.\n'
'\n'
'Usage: mason make <subcommand> [arguments]\n'
'-h, --help Print this usage information.\n'
' --no-hooks skips running hooks\n'
''' --set-exit-if-changed Return exit code 70 if there are files modified.\n'''
'''-c, --config-path Path to config json file containing variables.\n'''
'''-o, --output-dir Directory where to output the generated code.\n'''
' (defaults to ".")\n'
''' --on-conflict File conflict resolution strategy.\n'''
'\n'
''' [append] Always append conflicting files.\n'''
''' [overwrite] Always overwrite conflicting files.\n'''
''' [prompt] (default) Always prompt the user for each file conflict.\n'''
' [skip] Always skip conflicting files.\n'
'\n'
'Available subcommands:\n'
' app_icon Create an app icon file from a URL\n'
' bio A Bio Template\n'
' documentation Create Documentation Markdown Files\n'
' favorite_color A new brick created with the Mason CLI.\n'
' flavors A new brick created with the Mason CLI.\n'
' greeting A Simple Greeting Template\n'
' hello_world A Simple Hello World Template\n'
' hooks A Hooks Example Template\n'
' legacy A Legacy Greeting Template\n'
' plugin An example plugin template\n'
' random_color A Random Color Generator\n'
' simple A Simple Static Template\n'
' todos A Todos Template\n'
' widget Create a Simple Flutter Widget\n'
'\n'
'Run "mason help" to see global options.'
];
final result = await commandRunner.run(['make', '-h']);
expect(result, equals(ExitCode.success.code));
expect(printLogs, equals(expectedPrintLogs));
}),
);
test(
'<subcommand> --help shows correct help information (greeting)',
overridePrint(() async {
const expectedPrintLogs = <String>[
'A Simple Greeting Template\n'
'\n'
'Usage: mason make greeting [arguments]\n'
'-h, --help Print this usage information.\n'
' --no-hooks skips running hooks\n'
''' --set-exit-if-changed Return exit code 70 if there are files modified.\n'''
'''-c, --config-path Path to config json file containing variables.\n'''
'''-o, --output-dir Directory where to output the generated code.\n'''
' (defaults to ".")\n'
''' --on-conflict File conflict resolution strategy.\n'''
'\n'
''' [append] Always append conflicting files.\n'''
''' [overwrite] Always overwrite conflicting files.\n'''
''' [prompt] (default) Always prompt the user for each file conflict.\n'''
' [skip] Always skip conflicting files.\n'
'\n'
'''-------------------------------------------------------------------------------\n'''
'\n'
' --name Your name <string>\n'
' (defaults to "Dash")\n'
'\n'
'Run "mason help" to see global options.'
];
final result = await commandRunner.run(['make', 'greeting', '--help']);
expect(result, equals(ExitCode.success.code));
expect(printLogs, equals(expectedPrintLogs));
}),
);
test(
'<subcommand> --help shows correct help information (legacy)',
overridePrint(() async {
const expectedPrintLogs = <String>[
'A Legacy Greeting Template\n'
'\n'
'Usage: mason make legacy [arguments]\n'
'-h, --help Print this usage information.\n'
' --no-hooks skips running hooks\n'
''' --set-exit-if-changed Return exit code 70 if there are files modified.\n'''
'''-c, --config-path Path to config json file containing variables.\n'''
'''-o, --output-dir Directory where to output the generated code.\n'''
' (defaults to ".")\n'
''' --on-conflict File conflict resolution strategy.\n'''
'\n'
''' [append] Always append conflicting files.\n'''
''' [overwrite] Always overwrite conflicting files.\n'''
''' [prompt] (default) Always prompt the user for each file conflict.\n'''
' [skip] Always skip conflicting files.\n'
'\n'
'''-------------------------------------------------------------------------------\n'''
'\n'
' --name <string>\n'
'\n'
'Run "mason help" to see global options.'
];
final result = await commandRunner.run(['make', 'legacy', '--help']);
expect(result, equals(ExitCode.success.code));
expect(printLogs, equals(expectedPrintLogs));
}),
);
test(
'<subcommand> --help shows correct help information (bio)',
overridePrint(() async {
const expectedPrintLogs = <String>[
'A Bio Template\n'
'\n'
'Usage: mason make bio [arguments]\n'
'-h, --help Print this usage information.\n'
' --no-hooks skips running hooks\n'
''' --set-exit-if-changed Return exit code 70 if there are files modified.\n'''
'''-c, --config-path Path to config json file containing variables.\n'''
'''-o, --output-dir Directory where to output the generated code.\n'''
' (defaults to ".")\n'
''' --on-conflict File conflict resolution strategy.\n'''
'\n'
''' [append] Always append conflicting files.\n'''
''' [overwrite] Always overwrite conflicting files.\n'''
''' [prompt] (default) Always prompt the user for each file conflict.\n'''
' [skip] Always skip conflicting files.\n'
'\n'
'''-------------------------------------------------------------------------------\n'''
'\n'
''' --name Name of the current user <string>\n'''
' (defaults to "Dash")\n'
''' --age Age of the current user <number>\n'''
' (defaults to 42)\n'
''' --isDeveloper If the current user is a developer <boolean>\n'''
' (defaults to false)\n'
'\n'
'Run "mason help" to see global options.'
];
final result = await commandRunner.run(['make', 'bio', '--help']);
expect(result, equals(ExitCode.success.code));
expect(printLogs, equals(expectedPrintLogs));
}),
);
test('exits with code 64 when brick does not exist', () async {
final result = await commandRunner.run(['make', 'garbage']);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err(
'Could not find a subcommand named "garbage" for "mason make".',
),
).called(1);
});
test('exits with code 64 when missing subcommand', () async {
final result = await commandRunner.run(['make']);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err('Missing subcommand for "mason make".'),
).called(1);
});
test('exits with code 64 when local mason.yaml does not exist', () async {
try {
File(path.join(Directory.current.path, 'mason.yaml'))
.deleteSync(recursive: true);
} catch (_) {}
commandRunner = MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
);
final result = await commandRunner.run(['make', 'garbage']);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err(
'Could not find a subcommand named "garbage" for "mason make".',
),
).called(1);
});
test('exits with code 70 when mason version constraint cannot be resolved',
() async {
await commandRunner.run(['new', 'example']);
File(path.join(Directory.current.path, 'mason.yaml')).writeAsStringSync(
'''
example:
path: ./example
''',
mode: FileMode.append,
);
await commandRunner.run(['get']);
final brickYaml = File(path.join('example', 'brick.yaml'));
brickYaml.writeAsStringSync(
brickYaml.readAsStringSync().replaceFirst(
'mason: ">=0.1.0-dev.26 <0.1.0"',
'mason: ">=99.99.99 <100.0.0"',
),
);
commandRunner = MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
);
final result = await commandRunner.run(['make', 'example']);
expect(result, equals(ExitCode.software.code));
verify(
() => logger.err(
'''The current mason version is ${mason.packageVersion}.\nBecause example requires mason version >=99.99.99 <100.0.0, version solving failed.''',
),
).called(1);
});
test('exits with code 64 when json decode fails', () async {
final testDir = Directory(
path.join(Directory.current.path, 'todos'),
)..createSync(recursive: true);
Directory.current = testDir.path;
File(path.join(testDir.path, 'todos.json'))
.writeAsStringSync('''{"todos": [}''');
final result = await commandRunner.run([
'make',
'todos',
'--config-path',
'todos.json',
]);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err(
'''
FormatException: Unexpected character (at character 12)
{"todos": [}
^
in todos.json''',
),
).called(1);
});
test('exits with code 64 when config does not exist', () async {
final testDir = Directory(
path.join(Directory.current.path, 'todos'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'todos',
'--config-path',
'todos.json',
]);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err(
any(
that: contains(
"FileSystemException: Cannot open file, path = 'todos.json",
),
),
),
).called(1);
});
test('exits with code 64 when mason.yaml contains mismatch', () async {
File(path.join(Directory.current.path, 'mason.yaml')).writeAsStringSync(
'''
bricks:
app_icon1:
path: ../../../../../bricks/app_icon
''',
);
File(
path.join(Directory.current.path, '.mason', 'bricks.json'),
).writeAsStringSync(
json.encode({'app_icon1': '../../../../../bricks/app_icon'}),
);
commandRunner = MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
);
final makeResult = await commandRunner.run(['make', 'app_icon1']);
expect(makeResult, equals(ExitCode.usage.code));
const expectedErrorMessage =
'''Could not find a subcommand named "app_icon1" for "mason make".''';
verify(
() => logger.err(any(that: contains(expectedErrorMessage))),
).called(1);
});
test('exits with code 64 when bricks.json contains bad path', () async {
File(path.join(Directory.current.path, '.mason', 'bricks.json'))
.writeAsStringSync('''{"greeting1":"bricks/greeting"}''');
commandRunner = MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
);
final makeResult = await commandRunner.run(['make', 'greeting']);
expect(makeResult, equals(ExitCode.usage.code));
const expectedErrorMessage = 'Could not find brick at bricks/greeting';
verify(() => logger.err(expectedErrorMessage)).called(1);
});
test('exits with code 64 when variable input has type mismatch', () async {
when(
() => logger.prompt(
any(that: contains('What is your name?')),
defaultValue: any(named: 'defaultValue'),
),
).thenReturn('Dash');
when(
() => logger.prompt(
any(that: contains('How old are you?')),
defaultValue: any(named: 'defaultValue'),
),
).thenReturn('abc');
final result = await commandRunner.run(['make', 'bio']);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err('Invalid age.\n"abc" is not a number.'),
).called(1);
});
test('exits with code 70 when exception occurs while generating', () async {
const url =
'https://cdn.dribbble.com/users/163325/screenshots/6214023/app_icon.jpg';
when(
() => logger.prompt(any(), defaultValue: any(named: 'defaultValue')),
).thenReturn(url);
final progress = MockProgress();
when(() => progress.complete(any())).thenAnswer((invocation) {
final update = invocation.positionalArguments[0] as String?;
if (update == 'Made brick app_icon') throw Exception('oops');
});
when(() => logger.progress(any())).thenReturn(progress);
final result = await commandRunner.run(['make', 'app_icon']);
expect(result, equals(ExitCode.software.code));
verify(() => logger.err('Exception: oops')).called(1);
});
test('exits with code 70 when exception occurs post generation', () async {
when(() => logger.info(any(that: contains('Generated'))))
.thenThrow(Exception('oops'));
final result = await commandRunner.run(
['make', 'greeting', '--name', 'test-name'],
);
expect(result, equals(ExitCode.software.code));
verify(() => logger.err('Exception: oops')).called(1);
});
test('generates app_icon (from args)', () async {
final testDir = Directory(
path.join(Directory.current.path, 'app_icon'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'app_icon',
'--url',
'https://cdn.dribbble.com/users/163325/screenshots/6214023/app_icon.jpg'
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'app_icon'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'app_icon'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates app_icon (from prompt)', () async {
const url =
'https://cdn.dribbble.com/users/163325/screenshots/6214023/app_icon.jpg';
when(() => logger.prompt(any())).thenReturn(url);
final testDir = Directory(
path.join(Directory.current.path, 'app_icon'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(['make', 'app_icon']);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'app_icon'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'app_icon'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates bio (from prompt)', () async {
when(
() => logger.prompt(
any(that: contains('What is your name?')),
defaultValue: any(named: 'defaultValue'),
),
).thenReturn('Dash');
when(
() => logger.prompt(
any(that: contains('How old are you?')),
defaultValue: any(named: 'defaultValue'),
),
).thenReturn('42');
when(
() => logger.confirm(any(), defaultValue: any(named: 'defaultValue')),
).thenReturn(false);
final testDir = Directory(
path.join(Directory.current.path, 'bio'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(['make', 'bio']);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'bio'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'bio'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates documentation', () async {
final testDir = Directory(
path.join(Directory.current.path, 'documentation'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'documentation',
'--name',
'test-name',
'--description',
'test-description',
'--author',
'test-author'
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'documentation'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'documentation'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates favorite_color', () async {
final testDir = Directory(
path.join(Directory.current.path, 'favorite_color'),
)..createSync(recursive: true);
Directory.current = testDir.path;
when(
() => logger.chooseOne<String>(
any(),
choices: any(named: 'choices'),
defaultValue: any<String>(named: 'defaultValue'),
),
).thenReturn('blue');
final result = await commandRunner.run(['make', 'favorite_color']);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'favorite_color'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'favorite_color'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('throws FormatException when enum values is empty', () async {
final testDir = Directory(
path.join(Directory.current.path, 'enum_no_choices'),
)..createSync(recursive: true);
Directory.current = testDir.path;
await commandRunner.run([
'add',
'enum_no_choices',
'--path',
canonicalize(
path.join(
Directory.current.path,
'..',
'..',
'..',
'bricks',
'enum_no_choices',
),
)
]);
final result = await MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
).run(['make', 'enum_no_choices']);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err(
'Invalid color.\n"Enums must have at least one value.',
),
).called(1);
});
test('generates flavors', () async {
final testDir = Directory(
path.join(Directory.current.path, 'flavors'),
)..createSync(recursive: true);
Directory.current = testDir.path;
when(
() => logger.chooseAny<String>(
any(),
choices: any(named: 'choices'),
defaultValues: any(named: 'defaultValues'),
),
).thenReturn(['development', 'production']);
final result = await commandRunner.run(['make', 'flavors']);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'flavors'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'flavors'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('throws FormatException when array values is empty', () async {
final testDir = Directory(
path.join(Directory.current.path, 'array_no_choices'),
)..createSync(recursive: true);
Directory.current = testDir.path;
await commandRunner.run([
'add',
'array_no_choices',
'--path',
canonicalize(
path.join(
Directory.current.path,
'..',
'..',
'..',
'bricks',
'array_no_choices',
),
)
]);
final result = await MasonCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
).run(['make', 'array_no_choices']);
expect(result, equals(ExitCode.usage.code));
verify(
() => logger.err(
'Invalid colors.\n"Arrays must have at least one value.',
),
).called(1);
});
test('generates greeting', () async {
final testDir = Directory(
path.join(Directory.current.path, 'greeting'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'greeting',
'--name',
'test-name',
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'greeting'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'greeting'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates hello_world', () async {
final testDir = Directory(
path.join(Directory.current.path, 'hello_world'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'hello_world',
'--name',
'dash',
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'hello_world'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'hello_world'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates hooks', () async {
final testDir = Directory(
path.join(Directory.current.path, 'hooks', 'basic'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'hooks',
'--name',
'dash',
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'hooks', 'basic'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'hooks', 'basic'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates hooks (--no-hooks)', () async {
final testDir = Directory(
path.join(Directory.current.path, 'hooks', 'no_hooks'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'hooks',
'--name',
'dash',
'--no-hooks',
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'hooks', 'no_hooks'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'hooks', 'no_hooks'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates random_color', () async {
final testDir = Directory(
path.join(Directory.current.path, 'random_color'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'random_color',
'--name',
'dash',
]);
expect(result, equals(ExitCode.success.code));
final file = File(
path.join(
testFixturesPath(cwd, suffix: '.make'),
'random_color',
'color.md',
),
);
expect(file.existsSync(), isTrue);
final contents = file.readAsStringSync();
expect(contents, contains('Hi dash!'));
expect(contents, contains('Your favorite color is'));
});
test('generates plugin (empty)', () async {
final testDir = Directory(
path.join(Directory.current.path, 'plugin', 'empty'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(
['make', 'plugin', '--ios', 'false', '--android', 'false'],
);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'plugin', 'empty'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'plugin', 'empty'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates plugin (android)', () async {
final testDir = Directory(
path.join(Directory.current.path, 'plugin', 'android'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(
['make', 'plugin', '--ios', 'false', '--android', 'true'],
);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'plugin', 'android'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'plugin', 'android'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates plugin (ios)', () async {
final testDir = Directory(
path.join(Directory.current.path, 'plugin', 'ios'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(
['make', 'plugin', '--ios', 'true', '--android', 'false'],
);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'plugin', 'ios'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'plugin', 'ios'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates plugin (android + ios)', () async {
final testDir = Directory(
path.join(Directory.current.path, 'plugin', 'android_ios'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(
['make', 'plugin', '--ios', 'true', '--android', 'true'],
);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(
testFixturesPath(cwd, suffix: '.make'),
'plugin',
'android_ios',
),
);
final expected = Directory(
path.join(
testFixturesPath(cwd, suffix: 'make'),
'plugin',
'android_ios',
),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates simple', () async {
final testDir = Directory(
path.join(Directory.current.path, 'simple'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run(['make', 'simple']);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'simple'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'simple'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates todos', () async {
final testDir = Directory(
path.join(Directory.current.path, 'todos'),
)..createSync(recursive: true);
Directory.current = testDir.path;
File(path.join(testDir.path, 'todos.json')).writeAsStringSync(
'''
{
"todos": [
{ "todo": "Eat", "done": true },
{ "todo": "Code", "done": true },
{ "todo": "Sleep", "done": false }
],
"developers": [{ "name": "Alex" }, { "name": "Sam" }, { "name": "Jen" }]
}
''',
);
final result = await commandRunner.run([
'make',
'todos',
'-c',
'todos.json',
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'todos'),
);
final expected = Directory(
path.join(testFixturesPath(cwd, suffix: 'make'), 'todos'),
);
expect(directoriesDeepEqual(actual, expected), isTrue);
});
test('generates widget', () async {
final testDir = Directory(
path.join(Directory.current.path, 'widget'),
)..createSync(recursive: true);
Directory.current = testDir.path;
final result = await commandRunner.run([
'make',
'widget',
'--name',
'my_widget',
]);
expect(result, equals(ExitCode.success.code));
final actual = Directory(
path.join(testFixturesPath(cwd, suffix: '.make'), 'widget'),