@@ -152,16 +152,25 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
152152
153153 Future <String > assertDecodeArgsFailure (List <String > args) async {
154154 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 );
157158 expect (stderrText, isNot (contains ('Exception' )));
158159 return stderrText;
159160 }
160161
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+ }
162173 expect (cli.isPreviewServerRunning, isFalse);
163- expect (cli.exitCode, isNotNull);
164- expect (cli.exitCode, isNot (0 ));
165174 var stderrText = logger.stderrBuffer.toString ();
166175 expect (stderrText, withUsage ? hasUsageText : isNot (hasUsageText));
167176 expect (stderrText,
@@ -186,7 +195,6 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
186195
187196 void assertNormalExit (MigrationCli cli) {
188197 expect (cli.isPreviewServerRunning, isFalse);
189- expect (cli.exitCode, 0 );
190198 }
191199
192200 Future <String > assertParseArgsFailure (List <String > args) async {
@@ -202,8 +210,9 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
202210 CommandLineOptions assertParseArgsSuccess (List <String > args) {
203211 var cli = _createCli ();
204212 cli.decodeCommandLineArgs (MigrationCli .createParser ().parse (args));
205- expect (cli.exitCode, isNull );
213+ assertNormalExit (cli);
206214 var options = cli.options;
215+ expect (options, isNotNull);
207216 return options;
208217 }
209218
@@ -253,6 +262,16 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
253262 expect (success, isTrue);
254263 }
255264
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+
256275 String createProjectDir (Map <String , String > contents,
257276 {String posixPath = '/test_project' }) {
258277 for (var entry in contents.entries) {
@@ -346,15 +365,14 @@ int${migrated ? '?' : ''} f() => null;
346365 expect (newCoreLibText, isNot (oldCoreLibText));
347366 coreLib.writeAsStringSync (newCoreLibText);
348367 var projectDir = await createProjectDir (simpleProject ());
349- await cli.run (MigrationCli .createParser ().parse ([projectDir]));
350- assertErrorExit (cli, withUsage: false );
368+ await assertRunFailure ([projectDir], cli: cli);
351369 var output = logger.stdoutBuffer.toString ();
352370 expect (output, contains (messages.sdkNnbdOff));
353371 }
354372
355373 test_detect_old_sdk_environment_variable () async {
356374 environmentVariables['SDK_PATH' ] = '/fake-old-sdk-path' ;
357- var cli = _createCli ();
375+ var cli = _createCli (); // Creates the mock SDK as a side effect
358376 // Alter the mock SDK, changing the signature of Object.operator== to match
359377 // the signature that was present prior to NNBD. (This is what the
360378 // migration tool uses to detect an old SDK).
@@ -367,8 +385,7 @@ int${migrated ? '?' : ''} f() => null;
367385 expect (newCoreLibText, isNot (oldCoreLibText));
368386 coreLib.writeAsStringSync (newCoreLibText);
369387 var projectDir = await createProjectDir (simpleProject ());
370- await cli.run (MigrationCli .createParser ().parse ([projectDir]));
371- assertErrorExit (cli, withUsage: false );
388+ await assertRunFailure ([projectDir], cli: cli);
372389 var output = logger.stdoutBuffer.toString ();
373390 expect (output, contains (messages.sdkNnbdOff));
374391 expect (output, contains (messages.sdkPathEnvironmentVariableSet));
@@ -522,8 +539,7 @@ linter:
522539 var projectContents = simpleProject (sourceText: 'main() { print(0); }' );
523540 var projectDir = await createProjectDir (projectContents);
524541 var cli = _createCli (injectArtificialException: true );
525- await cli.run (_parseArgs ([projectDir]));
526- assertErrorExit (cli, withUsage: false );
542+ await assertRunFailure ([projectDir], cli: cli);
527543 var errorOutput = logger.stderrBuffer.toString ();
528544 expect (errorOutput, contains ('Artificial exception triggered' ));
529545 expect (
@@ -556,8 +572,7 @@ linter:
556572 simpleProject (sourceText: 'main() { print(0); print(1); }' );
557573 var projectDir = await createProjectDir (projectContents);
558574 var cli = _createCli (injectArtificialException: true );
559- await cli.run (_parseArgs ([projectDir]));
560- assertErrorExit (cli, withUsage: false );
575+ await assertRunFailure ([projectDir], cli: cli);
561576 var errorOutput = logger.stderrBuffer.toString ();
562577 expect (
563578 'Artificial exception triggered' .allMatches (errorOutput), hasLength (1 ));
@@ -571,8 +586,7 @@ linter:
571586 simpleProject (sourceText: 'main() { print(0); unresolved; }' );
572587 var projectDir = await createProjectDir (projectContents);
573588 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);
576590 var errorOutput = logger.stderrBuffer.toString ();
577591 expect (errorOutput, contains ('Artificial exception triggered' ));
578592 expect (errorOutput, contains ('try to fix errors in the source code' ));
@@ -584,9 +598,7 @@ linter:
584598int f() => null
585599''' );
586600 var projectDir = await createProjectDir (projectContents);
587- var cli = _createCli ();
588- await cli.run (_parseArgs ([projectDir]));
589- assertErrorExit (cli, withUsage: false );
601+ await assertRunFailure ([projectDir]);
590602 var output = logger.stdoutBuffer.toString ();
591603 expect (output, contains ('1 analysis issue found' ));
592604 var sep = resourceProvider.pathContext.separator;
@@ -1047,11 +1059,8 @@ int f() => null;
10471059}
10481060''' /* stdout */ ,
10491061 '' /* 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 );
10531063 expect (output, contains ('Warning: dependencies are outdated.' ));
1054- expect (cli.exitCode, 1 );
10551064 }
10561065
10571066 test_lifecycle_skip_pub_outdated_enable () async {
@@ -1146,9 +1155,7 @@ import 'package:does_not/exist.dart';
11461155int f() => null;
11471156''' );
11481157 var projectDir = await createProjectDir (projectContents);
1149- var cli = _createCli ();
1150- await cli.run (_parseArgs ([projectDir]));
1151- assertErrorExit (cli, withUsage: false );
1158+ await assertRunFailure ([projectDir]);
11521159 var output = logger.stdoutBuffer.toString ();
11531160 expect (output, contains ('1 analysis issue found' ));
11541161 expect (output, contains ('uri_does_not_exist' ));
@@ -1199,9 +1206,7 @@ int f() => null;
11991206 }
12001207
12011208 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 );
12051210 expect (stderrText, contains ('No more than one path may be specified' ));
12061211 }
12071212
@@ -1581,7 +1586,7 @@ name: test
15811586 var cli = _createCli ();
15821587 await cli.run (_parseArgs (
15831588 ['--${CommandLineOptions .helpFlag }' , if (verbose) '--verbose' ]));
1584- expect (cli.exitCode, 0 );
1589+ assertNormalExit (cli);
15851590 var helpText = logger.stderrBuffer.toString ();
15861591 return helpText;
15871592 }
0 commit comments