Skip to content

Commit

Permalink
fix: report analytics before running command
Browse files Browse the repository at this point in the history
  • Loading branch information
hampuslavin committed Nov 18, 2024
1 parent fd8f906 commit b0825f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/src/better_command_runner/better_command_runner.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:cli_tools/src/better_command_runner/exit_exception.dart';
Expand Down Expand Up @@ -134,22 +136,29 @@ class BetterCommandRunner extends CommandRunner {
_onAnalyticsEvent = null;
}

await _onBeforeRunCommand?.call(this);

try {
await super.runCommand(topLevelResults);
unawaited(Future(() async {
var command = topLevelResults.command;
if (command == null) {
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help);
} else {
if (command != null) {
// Command name can only be null for top level results.
// But since we are taking the name of a command from the top level
// results there should always be a name specified.
assert(command.name != null, 'Command name should never be null.');
_onAnalyticsEvent?.call(
command.name ?? BetterCommandRunnerAnalyticsEvents.invalid,
);
return;
}

var noUnexpectedArgs = topLevelResults.rest.isEmpty;
if (noUnexpectedArgs) {
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help);
}
}));

await _onBeforeRunCommand?.call(this);

try {
await super.runCommand(topLevelResults);
} on UsageException catch (e) {
_logError?.call(e.toString());
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.invalid);
Expand Down
18 changes: 18 additions & 0 deletions test/better_command_runner/analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:args/command_runner.dart';
import 'package:cli_tools/better_command_runner.dart';
import 'package:test/test.dart';

import '../test_utils.dart';

class MockCommand extends Command {
static String commandName = 'mock-command';

Expand Down Expand Up @@ -79,6 +81,8 @@ void main() {
var args = ['--no-${BetterCommandRunnerFlags.analytics}'];
await runner.run(args);

await flushEventQueue();

expect(runner.analyticsEnabled(), isFalse);
});

Expand All @@ -92,6 +96,8 @@ void main() {
// Ignore any exception
}

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('invalid'));
});
Expand All @@ -107,6 +113,8 @@ void main() {
// Ignore any exception
}

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('invalid'));
});
Expand All @@ -115,6 +123,8 @@ void main() {
() async {
await runner.run([]);

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('help'));
});
Expand All @@ -124,6 +134,8 @@ void main() {
() async {
await runner.run(['--${BetterCommandRunnerFlags.analytics}']);

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('help'));
});
Expand All @@ -150,6 +162,8 @@ void main() {

await runner.run(args);

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals(MockCommand.commandName));
});
Expand All @@ -161,6 +175,8 @@ void main() {

await runner.run(args);

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals(MockCommand.commandName));
});
Expand All @@ -176,6 +192,8 @@ void main() {
// Ignore any exception
}

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('invalid'));
});
Expand Down
5 changes: 5 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Test helper to flush the event queue.
/// Useful for waiting for async events to complete before continuing the test.
Future<void> flushEventQueue() {
return Future.delayed(Duration.zero);
}

0 comments on commit b0825f2

Please sign in to comment.