Repository: VeryGoodOpenSource/very_good_cli (dart)

Issue:
CLI prompt for update, after you update
If the command performed is "update", don't check for updates again.

Pull request:
<!--
  Thanks for contributing!

  Provide a description of your changes below and a general summary in the title

  Please look at the following checklist to ensure that your PR can be accepted quickly:
-->

## Description

fix: update prompt on update

<!--- Describe your changes in detail -->

fix: https://github.com/VeryGoodOpenSource/very_good_cli/issues/520
fix: https://github.com/VeryGoodOpenSource/very_good_cli/issues/521

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ] ✨ New feature (non-breaking change which adds functionality)
- [x] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
- [ ] 🧹 Code refactor
- [ ] ✅ Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore

Patch:
diff --git a/lib/src/command_runner.dart b/lib/src/command_runner.dart
index e8e2cc46c..e302a48de 100644
--- a/lib/src/command_runner.dart
+++ b/lib/src/command_runner.dart
@@ -61,6 +61,9 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
   final Analytics _analytics;
   final PubUpdater _pubUpdater;
 
+  @override
+  void printUsage() => _logger.info(usage);
+
   @override
   Future<int> run(Iterable<String> args) async {
     try {
@@ -140,7 +143,9 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
     } else {
       exitCode = await super.runCommand(topLevelResults);
     }
-    await _checkForUpdates();
+    if (topLevelResults.command?.name != UpdateCommand.commandName) {
+      await _checkForUpdates();
+    }
     return exitCode;
   }
 
diff --git a/lib/src/commands/update.dart b/lib/src/commands/update.dart
index 6687d7870..40df0ecee 100644
--- a/lib/src/commands/update.dart
+++ b/lib/src/commands/update.dart
@@ -21,8 +21,11 @@ class UpdateCommand extends Command<int> {
   @override
   String get description => 'Update Very Good CLI.';
 
+  /// The [name] of the command. But static.
+  static const String commandName = 'update';
+
   @override
-  String get name => 'update';
+  String get name => commandName;
 
   @override
   Future<int> run() async {


Test patch:
diff --git a/test/src/command_runner_test.dart b/test/src/command_runner_test.dart
index b1eaab26f..5f169b0b8 100644
--- a/test/src/command_runner_test.dart
+++ b/test/src/command_runner_test.dart
@@ -1,5 +1,6 @@
 // ignore_for_file: no_adjacent_strings_in_list
 import 'dart:async';
+import 'dart:io';
 
 import 'package:args/command_runner.dart';
 import 'package:mason/mason.dart' hide packageVersion;
@@ -16,6 +17,10 @@ class MockLogger extends Mock implements Logger {}
 
 class MockPubUpdater extends Mock implements PubUpdater {}
 
+class MockProgress extends Mock implements Progress {}
+
+class FakeProcessResult extends Fake implements ProcessResult {}
+
 const expectedUsage = [
   '🦄 A Very Good Command-Line Interface\n'
       '\n'
@@ -53,26 +58,12 @@ Run ${lightCyan.wrap('very_good update')} to update''';
 
 void main() {
   group('VeryGoodCommandRunner', () {
-    late List<String> printLogs;
     late Analytics analytics;
     late PubUpdater pubUpdater;
     late Logger logger;
     late VeryGoodCommandRunner commandRunner;
 
-    void Function() overridePrint(void Function() fn) {
-      return () {
-        final spec = ZoneSpecification(
-          print: (_, __, ___, String msg) {
-            printLogs.add(msg);
-          },
-        );
-        return Zone.current.fork(specification: spec).run<void>(fn);
-      };
-    }
-
     setUp(() {
-      printLogs = [];
-
       analytics = MockAnalytics();
       pubUpdater = MockPubUpdater();
 
@@ -108,6 +99,36 @@ void main() {
         verify(() => logger.info(updatePrompt)).called(1);
       });
 
+      test(
+        'doesnt show update message when using the update command',
+        () async {
+          when(
+            () => pubUpdater.getLatestVersion(any()),
+          ).thenAnswer((_) async => latestVersion);
+          when(
+            () => pubUpdater.update(packageName: packageName),
+          ).thenAnswer((_) => Future.value(FakeProcessResult()));
+          when(
+            () => pubUpdater.isUpToDate(
+              packageName: any(named: 'packageName'),
+              currentVersion: any(named: 'currentVersion'),
+            ),
+          ).thenAnswer((_) => Future.value(true));
+
+          final progress = MockProgress();
+          final progressLogs = <String>[];
+          when(() => progress.complete(any())).thenAnswer((_) {
+            final message = _.positionalArguments.elementAt(0) as String?;
+            if (message != null) progressLogs.add(message);
+          });
+          when(() => logger.progress(any())).thenReturn(progress);
+
+          final result = await commandRunner.run(['update']);
+          expect(result, equals(ExitCode.success.code));
+          verifyNever(() => logger.info(updatePrompt));
+        },
+      );
+
       test('handles pub update errors gracefully', () async {
         when(
           () => pubUpdater.getLatestVersion(any()),
@@ -164,30 +185,22 @@ void main() {
         verify(() => logger.info('exception usage')).called(1);
       });
 
-      test(
-        'handles no command',
-        overridePrint(() async {
-          final result = await commandRunner.run([]);
-          expect(printLogs, equals(expectedUsage));
-          expect(result, equals(ExitCode.success.code));
-        }),
-      );
+      test('handles no command', () async {
+        final result = await commandRunner.run([]);
+        verify(() => logger.info(expectedUsage.join())).called(1);
+        expect(result, equals(ExitCode.success.code));
+      });
 
       group('--help', () {
-        test(
-          'outputs usage',
-          overridePrint(() async {
-            final result = await commandRunner.run(['--help']);
-            expect(printLogs, equals(expectedUsage));
-            expect(result, equals(ExitCode.success.code));
-
-            printLogs.clear();
-
-            final resultAbbr = await commandRunner.run(['-h']);
-            expect(printLogs, equals(expectedUsage));
-            expect(resultAbbr, equals(ExitCode.success.code));
-          }),
-        );
+        test('outputs usage', () async {
+          final result = await commandRunner.run(['--help']);
+          verify(() => logger.info(expectedUsage.join())).called(1);
+          expect(result, equals(ExitCode.success.code));
+
+          final resultAbbr = await commandRunner.run(['-h']);
+          verify(() => logger.info(expectedUsage.join())).called(1);
+          expect(resultAbbr, equals(ExitCode.success.code));
+        });
       });
 
       group('--analytics', () {