Skip to content

Commit

Permalink
feat(shorebird_cli): suggest running with verbose on command failure
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman committed Apr 2, 2024
1 parent 7be7f4e commit 150d18e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
19 changes: 17 additions & 2 deletions packages/shorebird_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ ${lightCyan.wrap('shorebird release android -- --no-pub lib/main.dart')}''';
}

// Run the command or show version
final int? exitCode;
int? exitCode;
if (topLevelResults['version'] == true) {
final flutterVersion = await _tryGetFlutterVersion();
final shorebirdFlutterPrefix = StringBuffer('Flutter');
Expand All @@ -205,7 +205,22 @@ Run ${lightCyan.wrap('shorebird upgrade')} to upgrade.''');
}
exitCode = ExitCode.success.code;
} else {
exitCode = await super.runCommand(topLevelResults);
try {
exitCode = await super.runCommand(topLevelResults);
} catch (error, stackTrace) {
logger
..err('$error')
..info('$stackTrace');
exitCode = ExitCode.software.code;
}
}

if (exitCode == ExitCode.software.code && logger.level != Level.verbose) {
logger.info(
'''
If you aren't sure why this command failed, re-run with the ${lightCyan.wrap('--verbose')} flag to see more information.''',
);
}

return exitCode;
Expand Down
34 changes: 30 additions & 4 deletions packages/shorebird_cli/test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:shorebird_cli/src/logger.dart' hide logger;
import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_flutter.dart';
import 'package:shorebird_cli/src/shorebird_process.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
import 'package:shorebird_cli/src/version.dart';
import 'package:test/test.dart';
Expand All @@ -26,7 +25,6 @@ void main() {
late ShorebirdEnv shorebirdEnv;
late ShorebirdFlutter shorebirdFlutter;
late ShorebirdVersion shorebirdVersion;
late ShorebirdProcessResult processResult;
late ShorebirdCliCommandRunner commandRunner;

R runWithOverrides<R>(R Function() body) {
Expand All @@ -48,8 +46,7 @@ void main() {
shorebirdEnv = MockShorebirdEnv();
shorebirdFlutter = MockShorebirdFlutter();
shorebirdVersion = MockShorebirdVersion();
processResult = MockProcessResult();
when(() => processResult.exitCode).thenReturn(ExitCode.success.code);
when(() => logger.level).thenReturn(Level.info);
when(
() => shorebirdEnv.shorebirdEngineRevision,
).thenReturn(shorebirdEngineRevision);
Expand Down Expand Up @@ -274,6 +271,35 @@ Run ${lightCyan.wrap('shorebird upgrade')} to upgrade.'''),
});
});

group('on command failure', () {
group('when running with --verbose', () {
setUp(() {
when(() => logger.level).thenReturn(Level.verbose);
});

test('does not suggest running with --verbose', () async {
// This will fail due to missing scoped overrides.
// Note: the --verbose flag is here for illustrative purposes only.
// Because logger is a mock, setting the log level in code does
// nothing.
await runWithOverrides(
() => commandRunner.run(['release', 'android', '--verbose']),
);
verifyNever(() => logger.info(any(that: contains('--verbose'))));
});
});

group('when running without --verbose', () {
test('suggests using --verbose flag', () async {
// This will fail due to missing scoped overrides.
await runWithOverrides(
() => commandRunner.run(['release', 'android']),
);
verify(() => logger.info(any(that: contains('--verbose')))).called(1);
});
});
});

group('completion', () {
test('fast tracks completion', () async {
final result = await runWithOverrides(
Expand Down

0 comments on commit 150d18e

Please sign in to comment.