Skip to content

Commit

Permalink
feat: improve update prompt (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Mar 15, 2022
1 parent e542532 commit 97045bb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 54 deletions.
23 changes: 3 additions & 20 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,11 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
..info('')
..info(
'''
+-------------------------------------------------------------------------------------+
| |
| ${lightYellow.wrap('Update available!')} ${lightCyan.wrap(packageVersion)} \u2192 ${lightCyan.wrap(latestVersion)} |
| ${lightYellow.wrap('Changelog:')} ${lightCyan.wrap('https://github.com/verygoodopensource/very_good_cli/releases/tag/v$latestVersion')} |
| |
+-------------------------------------------------------------------------------------+
''',
${lightYellow.wrap('Update available!')} ${lightCyan.wrap(packageVersion)} \u2192 ${lightCyan.wrap(latestVersion)}
${lightYellow.wrap('Changelog:')} ${lightCyan.wrap('https://github.com/verygoodopensource/very_good_cli/releases/tag/v$latestVersion')}
Run ${lightCyan.wrap('dart pub global activate very_good_cli')} to update''',
);
final response = _logger.prompt('Would you like to update? (y/n) ');
if (response.isYes()) {
final done = _logger.progress('Updating to $latestVersion');
await _pubUpdater.update(packageName: packageName);
done('Updated to $latestVersion');
}
}
} catch (_) {}
}
}

extension on String {
bool isYes() {
final normalized = toLowerCase().trim();
return normalized == 'y' || normalized == 'yes';
}
}
38 changes: 4 additions & 34 deletions test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:mason/mason.dart' hide packageVersion;
import 'package:mocktail/mocktail.dart';
import 'package:pub_updater/pub_updater.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart';
import 'package:usage/usage_io.dart';
import 'package:very_good_cli/src/command_runner.dart';
import 'package:very_good_cli/src/version.dart';
Expand All @@ -17,8 +16,6 @@ class MockLogger extends Mock implements Logger {}

class MockPubUpdater extends Mock implements PubUpdater {}

class FakeProcessResult extends Fake implements ProcessResult {}

const expectedUsage = [
'🦄 A Very Good Command Line Interface\n'
'\n'
Expand Down Expand Up @@ -47,13 +44,9 @@ const responseBody =
const latestVersion = '0.0.0';

final updatePrompt = '''
+-------------------------------------------------------------------------------------+
| |
| ${lightYellow.wrap('Update available!')} ${lightCyan.wrap(packageVersion)} \u2192 ${lightCyan.wrap(latestVersion)} |
| ${lightYellow.wrap('Changelog:')} ${lightCyan.wrap('https://github.com/verygoodopensource/very_good_cli/releases/tag/v$latestVersion')} |
| |
+-------------------------------------------------------------------------------------+
''';
${lightYellow.wrap('Update available!')} ${lightCyan.wrap(packageVersion)} \u2192 ${lightCyan.wrap(latestVersion)}
${lightYellow.wrap('Changelog:')} ${lightCyan.wrap('https://github.com/verygoodopensource/very_good_cli/releases/tag/v$latestVersion')}
Run ${lightCyan.wrap('dart pub global activate very_good_cli')} to update''';

void main() {
group('VeryGoodCommandRunner', () {
Expand Down Expand Up @@ -85,11 +78,6 @@ void main() {
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => packageVersion);
when(
() => pubUpdater.update(
packageName: any(named: 'packageName'),
),
).thenAnswer((_) => Future.value(FakeProcessResult()));

logger = MockLogger();

Expand All @@ -107,19 +95,14 @@ void main() {
});

group('run', () {
test('prompts for update when newer version exists', () async {
test('shows update message when newer version exists', () async {
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => latestVersion);

when(() => logger.prompt(any())).thenReturn('n');

final result = await commandRunner.run(['--version']);
expect(result, equals(ExitCode.success.code));
verify(() => logger.info(updatePrompt)).called(1);
verify(
() => logger.prompt('Would you like to update? (y/n) '),
).called(1);
});

test('handles pub update errors gracefully', () async {
Expand All @@ -132,19 +115,6 @@ void main() {
verifyNever(() => logger.info(updatePrompt));
});

test('updates on "y" response when newer version exists', () async {
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => latestVersion);

when(() => logger.prompt(any())).thenReturn('y');
when(() => logger.progress(any())).thenReturn(([String? message]) {});

final result = await commandRunner.run(['--version']);
expect(result, equals(ExitCode.success.code));
verify(() => logger.progress('Updating to $latestVersion')).called(1);
});

test('prompts for analytics collection on first run (y)', () async {
when(() => analytics.firstRun).thenReturn(true);
when(() => logger.prompt(any())).thenReturn('y');
Expand Down

0 comments on commit 97045bb

Please sign in to comment.