-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
945 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:')); | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.