Skip to content

Commit

Permalink
Revert "yemp remove test"
Browse files Browse the repository at this point in the history
This reverts commit f712981.
  • Loading branch information
dickermoshe committed Dec 26, 2024
1 parent 229081e commit 1ce3ccb
Show file tree
Hide file tree
Showing 3 changed files with 945 additions and 0 deletions.
158 changes: 158 additions & 0 deletions test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:cli_completion/cli_completion.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pub_updater/pub_updater.dart';
import 'package:test/test.dart';
import 'package:widget_wrapper/src/command_runner.dart';
import 'package:widget_wrapper/src/version.dart';

class _MockLogger extends Mock implements Logger {}

class _MockProgress extends Mock implements Progress {}

class _MockPubUpdater extends Mock implements PubUpdater {}

const latestVersion = '0.0.0';

final updatePrompt = '''
${lightYellow.wrap('Update available!')} ${lightCyan.wrap(packageVersion)} \u2192 ${lightCyan.wrap(latestVersion)}
Run ${lightCyan.wrap('$executableName update')} to update''';

void main() {
group('WidgetWrapperCommandRunner', () {
late PubUpdater pubUpdater;
late Logger logger;
late WidgetWrapperCommandRunner commandRunner;

setUp(() {
pubUpdater = _MockPubUpdater();

when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => packageVersion);

logger = _MockLogger();

commandRunner = WidgetWrapperCommandRunner(
logger: logger,
pubUpdater: pubUpdater,
);
});

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

final result = await commandRunner.run(['--version']);
expect(result, equals(ExitCode.success.code));
verify(() => logger.info(updatePrompt)).called(1);
});

test(
'Does not show update message when the shell calls the '
'completion command',
() async {
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => latestVersion);

final result = await commandRunner.run(['completion']);
expect(result, equals(ExitCode.success.code));
verifyNever(() => logger.info(updatePrompt));
},
);

test('does not show update message when using update command', () async {
when(
() => pubUpdater.getLatestVersion(any()),
).thenAnswer((_) async => latestVersion);
when(
() => pubUpdater.update(
packageName: packageName,
versionConstraint: any(named: 'versionConstraint'),
),
).thenAnswer(
(_) async => ProcessResult(0, ExitCode.success.code, null, null),
);
when(
() => pubUpdater.isUpToDate(
packageName: any(named: 'packageName'),
currentVersion: any(named: 'currentVersion'),
),
).thenAnswer((_) async => true);

final progress = _MockProgress();
final progressLogs = <String>[];
when(() => progress.complete(any())).thenAnswer((answer) {
final message = answer.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('can be instantiated without an explicit analytics/logger instance',
() {
final commandRunner = WidgetWrapperCommandRunner();
expect(commandRunner, isNotNull);
expect(commandRunner, isA<CompletionCommandRunner<int>>());
});

test('handles FormatException', () async {
const exception = FormatException('oops!');
var isFirstInvocation = true;
when(() => logger.info(any())).thenAnswer((_) {
if (isFirstInvocation) {
isFirstInvocation = false;
throw exception;
}
});
final result = await commandRunner.run(['--version']);
expect(result, equals(ExitCode.usage.code));
verify(() => logger.err(exception.message)).called(1);
verify(() => logger.info(commandRunner.usage)).called(1);
});

test('handles UsageException', () async {
final exception = UsageException('oops!', 'exception usage');
var isFirstInvocation = true;
when(() => logger.info(any())).thenAnswer((_) {
if (isFirstInvocation) {
isFirstInvocation = false;
throw exception;
}
});
final result = await commandRunner.run(['--version']);
expect(result, equals(ExitCode.usage.code));
verify(() => logger.err(exception.message)).called(1);
verify(() => logger.info('exception usage')).called(1);
});

group('--version', () {
test('outputs current version', () async {
final result = await commandRunner.run(['--version']);
expect(result, equals(ExitCode.success.code));
verify(() => logger.info(packageVersion)).called(1);
});
});

group('--verbose', () {
test('enables verbose logging', () async {
final result = await commandRunner.run(['--verbose']);
expect(result, equals(ExitCode.success.code));

verify(() => logger.detail('Argument information:')).called(1);
verify(() => logger.detail(' Top level options:')).called(1);
verify(() => logger.detail(' - verbose: true')).called(1);
verifyNever(() => logger.detail(' Command options:'));
});
});
});
}
Loading

0 comments on commit 1ce3ccb

Please sign in to comment.