Skip to content

Commit

Permalink
test: adds missing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hampuslavin committed Nov 18, 2024
1 parent 80fe405 commit 6fd60b1
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions test/better_command_runner/analytics_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:args/command_runner.dart';
import 'package:cli_tools/better_command_runner.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -25,6 +27,31 @@ class MockCommand extends Command {
}
}

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

@override
String get description => 'Mock command used to test when process hangs';

Completer<void> completer = Completer<void>();

@override
void run() async {
await completer.future;
}

@override
String get name => commandName;

CompletableMockCommand() {
argParser.addOption(
'name',
defaultsTo: 'serverpod',
allowed: <String>['serverpod'],
);
}
}

void main() {
late BetterCommandRunner runner;
group('Given runner with null onAnalyticsEvent callback', () {
Expand Down Expand Up @@ -139,6 +166,26 @@ void main() {
expect(events, hasLength(1));
expect(events.first, equals('help'));
});

test('when running with help command then "help" analytics event is sent.',
() async {
await runner.run(['help']);

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('help'));
});

test('when running with help flag then "help" analytics event is sent.',
() async {
await runner.run(['--help']);

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('help'));
});
});

group('Given runner with registered command and analytics enabled', () {
Expand Down Expand Up @@ -198,4 +245,69 @@ void main() {
expect(events.first, equals('invalid'));
});
});

group('Given runner with registered command and analytics enabled', () {
List<String> events = [];
late CompletableMockCommand command;
setUp(() {
command = CompletableMockCommand();
runner = BetterCommandRunner(
'test',
'this is a test cli',
onAnalyticsEvent: (event) => events.add(event),
)..addCommand(command);
assert(runner.analyticsEnabled());
});

tearDown(() {
events = [];
});

test(
'when running with registered command that hangs then command name is sent',
() async {
var args = [CompletableMockCommand.commandName];

unawaited(runner.run(args));

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals(CompletableMockCommand.commandName));

command.completer.complete();
});

test(
'when running with registered command that hangs and option then command name is sent,',
() async {
var args = [CompletableMockCommand.commandName, '--name', 'serverpod'];

unawaited(runner.run(args));

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals(CompletableMockCommand.commandName));

command.completer.complete();
});

test(
'when running with registered command that hangs but invalid option then "invalid" analytics event is sent,',
() async {
var args = [CompletableMockCommand.commandName, '--name', 'invalid'];

unawaited(runner.run(args).catchError((_) {
// Ignore parse error
}));

await flushEventQueue();

expect(events, hasLength(1));
expect(events.first, equals('invalid'));

command.completer.complete();
});
});
}

0 comments on commit 6fd60b1

Please sign in to comment.