Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shorebird_cli): suggest running with verbose on command failure #1849

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
felangel marked this conversation as resolved.
Show resolved Hide resolved
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.
felangel marked this conversation as resolved.
Show resolved Hide resolved
// 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.
felangel marked this conversation as resolved.
Show resolved Hide resolved
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
Loading