@@ -152,16 +152,25 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
152
152
153
153
Future <String > assertDecodeArgsFailure (List <String > args) async {
154
154
var cli = _createCli ();
155
- await cli.run (MigrationCli .createParser ().parse (args));
156
- var stderrText = assertErrorExit (cli);
155
+ var stderrText = await assertErrorExit (
156
+ cli, () => cli.run (MigrationCli .createParser ().parse (args)),
157
+ withUsage: true );
157
158
expect (stderrText, isNot (contains ('Exception' )));
158
159
return stderrText;
159
160
}
160
161
161
- String assertErrorExit (MigrationCli cli, {bool withUsage = true }) {
162
+ Future <String > assertErrorExit (
163
+ MigrationCli cli, Future <void > Function () callback,
164
+ {@required bool withUsage, dynamic expectedExitCode = anything}) async {
165
+ try {
166
+ await callback ();
167
+ fail ('Migration succeeded; expected it to abort with an error' );
168
+ } on MigrationExit catch (migrationExit) {
169
+ expect (migrationExit.exitCode, isNotNull);
170
+ expect (migrationExit.exitCode, isNot (0 ));
171
+ expect (migrationExit.exitCode, expectedExitCode);
172
+ }
162
173
expect (cli.isPreviewServerRunning, isFalse);
163
- expect (cli.exitCode, isNotNull);
164
- expect (cli.exitCode, isNot (0 ));
165
174
var stderrText = logger.stderrBuffer.toString ();
166
175
expect (stderrText, withUsage ? hasUsageText : isNot (hasUsageText));
167
176
expect (stderrText,
@@ -186,7 +195,6 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
186
195
187
196
void assertNormalExit (MigrationCli cli) {
188
197
expect (cli.isPreviewServerRunning, isFalse);
189
- expect (cli.exitCode, 0 );
190
198
}
191
199
192
200
Future <String > assertParseArgsFailure (List <String > args) async {
@@ -202,8 +210,9 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
202
210
CommandLineOptions assertParseArgsSuccess (List <String > args) {
203
211
var cli = _createCli ();
204
212
cli.decodeCommandLineArgs (MigrationCli .createParser ().parse (args));
205
- expect (cli.exitCode, isNull );
213
+ assertNormalExit (cli);
206
214
var options = cli.options;
215
+ expect (options, isNotNull);
207
216
return options;
208
217
}
209
218
@@ -253,6 +262,16 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
253
262
expect (success, isTrue);
254
263
}
255
264
265
+ Future <String > assertRunFailure (List <String > args,
266
+ {MigrationCli cli,
267
+ bool withUsage = false ,
268
+ dynamic expectedExitCode = anything}) async {
269
+ cli ?? = _createCli ();
270
+ return await assertErrorExit (
271
+ cli, () => cli.run (MigrationCli .createParser ().parse (args)),
272
+ withUsage: withUsage, expectedExitCode: expectedExitCode);
273
+ }
274
+
256
275
String createProjectDir (Map <String , String > contents,
257
276
{String posixPath = '/test_project' }) {
258
277
for (var entry in contents.entries) {
@@ -346,15 +365,14 @@ int${migrated ? '?' : ''} f() => null;
346
365
expect (newCoreLibText, isNot (oldCoreLibText));
347
366
coreLib.writeAsStringSync (newCoreLibText);
348
367
var projectDir = await createProjectDir (simpleProject ());
349
- await cli.run (MigrationCli .createParser ().parse ([projectDir]));
350
- assertErrorExit (cli, withUsage: false );
368
+ await assertRunFailure ([projectDir], cli: cli);
351
369
var output = logger.stdoutBuffer.toString ();
352
370
expect (output, contains (messages.sdkNnbdOff));
353
371
}
354
372
355
373
test_detect_old_sdk_environment_variable () async {
356
374
environmentVariables['SDK_PATH' ] = '/fake-old-sdk-path' ;
357
- var cli = _createCli ();
375
+ var cli = _createCli (); // Creates the mock SDK as a side effect
358
376
// Alter the mock SDK, changing the signature of Object.operator== to match
359
377
// the signature that was present prior to NNBD. (This is what the
360
378
// migration tool uses to detect an old SDK).
@@ -367,8 +385,7 @@ int${migrated ? '?' : ''} f() => null;
367
385
expect (newCoreLibText, isNot (oldCoreLibText));
368
386
coreLib.writeAsStringSync (newCoreLibText);
369
387
var projectDir = await createProjectDir (simpleProject ());
370
- await cli.run (MigrationCli .createParser ().parse ([projectDir]));
371
- assertErrorExit (cli, withUsage: false );
388
+ await assertRunFailure ([projectDir], cli: cli);
372
389
var output = logger.stdoutBuffer.toString ();
373
390
expect (output, contains (messages.sdkNnbdOff));
374
391
expect (output, contains (messages.sdkPathEnvironmentVariableSet));
@@ -522,8 +539,7 @@ linter:
522
539
var projectContents = simpleProject (sourceText: 'main() { print(0); }' );
523
540
var projectDir = await createProjectDir (projectContents);
524
541
var cli = _createCli (injectArtificialException: true );
525
- await cli.run (_parseArgs ([projectDir]));
526
- assertErrorExit (cli, withUsage: false );
542
+ await assertRunFailure ([projectDir], cli: cli);
527
543
var errorOutput = logger.stderrBuffer.toString ();
528
544
expect (errorOutput, contains ('Artificial exception triggered' ));
529
545
expect (
@@ -556,8 +572,7 @@ linter:
556
572
simpleProject (sourceText: 'main() { print(0); print(1); }' );
557
573
var projectDir = await createProjectDir (projectContents);
558
574
var cli = _createCli (injectArtificialException: true );
559
- await cli.run (_parseArgs ([projectDir]));
560
- assertErrorExit (cli, withUsage: false );
575
+ await assertRunFailure ([projectDir], cli: cli);
561
576
var errorOutput = logger.stderrBuffer.toString ();
562
577
expect (
563
578
'Artificial exception triggered' .allMatches (errorOutput), hasLength (1 ));
@@ -571,8 +586,7 @@ linter:
571
586
simpleProject (sourceText: 'main() { print(0); unresolved; }' );
572
587
var projectDir = await createProjectDir (projectContents);
573
588
var cli = _createCli (injectArtificialException: true );
574
- await cli.run (_parseArgs (['--ignore-errors' , projectDir]));
575
- assertErrorExit (cli, withUsage: false );
589
+ await assertRunFailure (['--ignore-errors' , projectDir], cli: cli);
576
590
var errorOutput = logger.stderrBuffer.toString ();
577
591
expect (errorOutput, contains ('Artificial exception triggered' ));
578
592
expect (errorOutput, contains ('try to fix errors in the source code' ));
@@ -584,9 +598,7 @@ linter:
584
598
int f() => null
585
599
''' );
586
600
var projectDir = await createProjectDir (projectContents);
587
- var cli = _createCli ();
588
- await cli.run (_parseArgs ([projectDir]));
589
- assertErrorExit (cli, withUsage: false );
601
+ await assertRunFailure ([projectDir]);
590
602
var output = logger.stdoutBuffer.toString ();
591
603
expect (output, contains ('1 analysis issue found' ));
592
604
var sep = resourceProvider.pathContext.separator;
@@ -1047,11 +1059,8 @@ int f() => null;
1047
1059
}
1048
1060
''' /* stdout */ ,
1049
1061
'' /* stderr */ );
1050
- var cli = _createCli ();
1051
- await cli.run (_parseArgs ([projectDir]));
1052
- var output = assertErrorExit (cli, withUsage: false );
1062
+ var output = await assertRunFailure ([projectDir], expectedExitCode: 1 );
1053
1063
expect (output, contains ('Warning: dependencies are outdated.' ));
1054
- expect (cli.exitCode, 1 );
1055
1064
}
1056
1065
1057
1066
test_lifecycle_skip_pub_outdated_enable () async {
@@ -1146,9 +1155,7 @@ import 'package:does_not/exist.dart';
1146
1155
int f() => null;
1147
1156
''' );
1148
1157
var projectDir = await createProjectDir (projectContents);
1149
- var cli = _createCli ();
1150
- await cli.run (_parseArgs ([projectDir]));
1151
- assertErrorExit (cli, withUsage: false );
1158
+ await assertRunFailure ([projectDir]);
1152
1159
var output = logger.stdoutBuffer.toString ();
1153
1160
expect (output, contains ('1 analysis issue found' ));
1154
1161
expect (output, contains ('uri_does_not_exist' ));
@@ -1199,9 +1206,7 @@ int f() => null;
1199
1206
}
1200
1207
1201
1208
test_migrate_path_two () async {
1202
- var cli = _createCli ();
1203
- await cli.run (_parseArgs (['foo' , 'bar' ]));
1204
- var stderrText = assertErrorExit (cli);
1209
+ var stderrText = await assertRunFailure (['foo' , 'bar' ], withUsage: true );
1205
1210
expect (stderrText, contains ('No more than one path may be specified' ));
1206
1211
}
1207
1212
@@ -1581,7 +1586,7 @@ name: test
1581
1586
var cli = _createCli ();
1582
1587
await cli.run (_parseArgs (
1583
1588
['--${CommandLineOptions .helpFlag }' , if (verbose) '--verbose' ]));
1584
- expect (cli.exitCode, 0 );
1589
+ assertNormalExit (cli);
1585
1590
var helpText = logger.stderrBuffer.toString ();
1586
1591
return helpText;
1587
1592
}
0 commit comments