fix: better handling usage exception on the CLI (#2351)

This commit is contained in:
Erick
2024-07-12 10:52:40 -03:00
committed by GitHub
parent 9335f0a6af
commit a8dfb44def
2 changed files with 21 additions and 11 deletions
@@ -202,6 +202,13 @@ Engine • revision ${shorebirdEnv.shorebirdEngineRevision}''');
exitCode = await super.runCommand(topLevelResults);
} on ProcessExit catch (error) {
exitCode = error.exitCode;
} on UsageException catch (e) {
logger
..err(e.message)
..info(e.usage);
// When on an usage exception we don't need to show the "if you aren't
// sure" message, so we do an early return here.
return ExitCode.usage.code;
} catch (error, stackTrace) {
logger
..detail('$error')
@@ -113,20 +113,23 @@ void main() {
});
test('handles UsageException', () async {
final exception = UsageException('oops!', 'exception usage');
var isFirstInvocation = true;
when(() => logger.info(any())).thenAnswer((_) {
if (isFirstInvocation) {
isFirstInvocation = false;
throw exception;
}
});
final result = await runWithOverrides(
() => commandRunner.run(['--version']),
// fly_to_the_moon is not a valid command.
() => commandRunner.run(['fly_to_the_moon']),
);
expect(result, equals(ExitCode.usage.code));
verify(() => logger.err(exception.message)).called(1);
verify(() => logger.info('exception usage')).called(1);
verify(
() => logger.err('Could not find a command named "fly_to_the_moon".'),
).called(1);
verify(
() => logger.info(
any(
that: contains(
'Usage: shorebird <command> [arguments]',
),
),
),
).called(1);
});
test('handles missing option error', () async {