Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ Run "very_good help" to see global options.
Run tests in a Dart or Flutter project.

```sh
# Run tests in the current directory
# Run all tests
very_good test

# Run tests in ./some/other/directory
# Run only tests in ./some/other/directory
very_good test ./some/other/directory

# Run tests recursively
Expand Down
6 changes: 5 additions & 1 deletion lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Flutter {
bool recursive = false,
bool collectCoverage = false,
double? minCoverage,
List<String>? arguments,
void Function(String)? stdout,
void Function(String)? stderr,
}) async {
Expand All @@ -120,10 +121,11 @@ class Flutter {
await _runCommand(
cmd: (cwd) {
void noop(String? _) {}
stdout?.call('Running "flutter test" in $cwd...\n');
stdout?.call('Running "flutter test" in ${p.canonicalize(cwd)}...\n');
return _flutterTest(
cwd: cwd,
collectCoverage: collectCoverage,
arguments: arguments,
stdout: stdout ?? noop,
stderr: stderr ?? noop,
);
Expand Down Expand Up @@ -172,6 +174,7 @@ Future<void> _runCommand<T>({
Future<void> _flutterTest({
String cwd = '.',
bool collectCoverage = false,
List<String>? arguments,
required void Function(String) stdout,
required void Function(String) stderr,
}) {
Expand Down Expand Up @@ -208,6 +211,7 @@ Future<void> _flutterTest({
workingDirectory: cwd,
arguments: [
if (collectCoverage) '--coverage',
...?arguments,
],
runInShell: true,
).listen(
Expand Down
15 changes: 7 additions & 8 deletions lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ class TestCommand extends Command<int> {

@override
Future<int> run() async {
if (_argResults.rest.length > 1) {
throw UsageException('Too many arguments', usage);
}

final recursive = _argResults['recursive'] as bool;
final target = _argResults.rest.length == 1 ? _argResults.rest[0] : '.';
final targetPath = path.normalize(Directory(target).absolute.path);
final targetPath = path.normalize(Directory.current.absolute.path);
final collectCoverage = _argResults['coverage'] as bool;
final minCoverage = double.tryParse(
_argResults['min-coverage'] as String? ?? '',
Expand All @@ -61,15 +56,19 @@ class TestCommand extends Command<int> {
if (isFlutterInstalled) {
try {
await Flutter.test(
cwd: targetPath,
recursive: recursive,
stdout: _logger.write,
stderr: _logger.err,
collectCoverage: collectCoverage,
minCoverage: minCoverage,
arguments: argResults?.rest,
);
} on PubspecNotFound catch (_) {
_logger.err('Could not find a pubspec.yaml in $targetPath');
_logger.err(
'''
Could not find a pubspec.yaml in $targetPath.
This command should be run from the root of your Flutter project.''',
);
return ExitCode.noInput.code;
} on MinCoverageNotMet catch (e) {
_logger.err(
Expand Down
45 changes: 22 additions & 23 deletions test/src/commands/test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ dev_dependencies:

void main() {
group('test', () {
final cwd = Directory.current;

setUp(() {
Directory.current = cwd;
});

test(
'help',
withRunner((commandRunner, logger, printLogs) async {
Expand All @@ -57,23 +63,13 @@ void main() {
}),
);

test(
'throws usage exception '
'when too many arguments are provided',
withRunner((commandRunner, logger, printLogs) async {
final result = await commandRunner.run(
['test', 'arg1', 'arg2'],
);
expect(result, equals(ExitCode.usage.code));
}),
);

test(
'throws pubspec not found exception '
'when no pubspec.yaml exists',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
final result = await commandRunner.run(['test', directory.path]);
Directory.current = directory.path;
final result = await commandRunner.run(['test']);
expect(result, equals(ExitCode.noInput.code));
verify(() {
logger.err(any(that: contains('Could not find a pubspec.yaml in')));
Expand All @@ -86,7 +82,8 @@ void main() {
'when no pubspec.yaml exists (recursive)',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
final result = await commandRunner.run(['test', '-r', directory.path]);
Directory.current = directory.path;
final result = await commandRunner.run(['test', '-r']);
expect(result, equals(ExitCode.noInput.code));
verify(() {
logger.err(any(that: contains('Could not find a pubspec.yaml in')));
Expand All @@ -99,8 +96,9 @@ void main() {
withRunner(
(commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
Directory.current = directory.path;
File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync('');
final result = await commandRunner.run(['test', directory.path]);
final result = await commandRunner.run(['test']);
expect(result, equals(ExitCode.unavailable.code));
},
),
Expand All @@ -111,6 +109,7 @@ void main() {
'when pubspec.yaml and tests exist',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
Directory.current = directory.path;
final testDirectory = Directory(path.join(directory.path, 'test'))
..createSync();
File(
Expand All @@ -119,7 +118,7 @@ void main() {
File(
path.join(testDirectory.path, 'example_test.dart'),
).writeAsStringSync(testContent);
final result = await commandRunner.run(['test', directory.path]);
final result = await commandRunner.run(['test']);
expect(result, equals(ExitCode.success.code));
verify(() {
logger.write(
Expand All @@ -136,6 +135,7 @@ void main() {
'completes normally --coverage',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
Directory.current = directory.path;
final testDirectory = Directory(path.join(directory.path, 'test'))
..createSync();
File(
Expand All @@ -144,9 +144,7 @@ void main() {
File(
path.join(testDirectory.path, 'example_test.dart'),
).writeAsStringSync(testContent);
final result = await commandRunner.run(
['test', '--coverage', directory.path],
);
final result = await commandRunner.run(['test', '--coverage']);
expect(result, equals(ExitCode.success.code));
verify(() {
logger.write(
Expand All @@ -163,6 +161,7 @@ void main() {
'completes normally --coverage --min-coverage 0',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
Directory.current = directory.path;
final testDirectory = Directory(path.join(directory.path, 'test'))
..createSync();
File(
Expand All @@ -172,7 +171,7 @@ void main() {
path.join(testDirectory.path, 'example_test.dart'),
).writeAsStringSync(testContent);
final result = await commandRunner.run(
['test', '--coverage', '--min-coverage', '0', directory.path],
['test', '--coverage', '--min-coverage', '0'],
);
expect(result, equals(ExitCode.success.code));
verify(() {
Expand All @@ -190,6 +189,7 @@ void main() {
'fails when coverage not met --coverage --min-coverage 100',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
Directory.current = directory.path;
final testDirectory = Directory(path.join(directory.path, 'test'))
..createSync();
File(
Expand All @@ -199,7 +199,7 @@ void main() {
path.join(testDirectory.path, 'example_test.dart'),
).writeAsStringSync(testContent);
final result = await commandRunner.run(
['test', '--coverage', '--min-coverage', '100', directory.path],
['test', '--coverage', '--min-coverage', '100'],
);
expect(result, equals(ExitCode.unavailable.code));
verify(() {
Expand All @@ -221,6 +221,7 @@ void main() {
'when pubspec.yaml and tests exist (recursive)',
withRunner((commandRunner, logger, printLogs) async {
final directory = Directory.systemTemp.createTempSync();
Directory.current = directory.path;
final testDirectoryA = Directory(
path.join(directory.path, 'example_a', 'test'),
)..createSync(recursive: true);
Expand All @@ -240,9 +241,7 @@ void main() {
path.join(directory.path, 'example_b', 'pubspec.yaml'),
).writeAsStringSync(pubspecContent('example_b'));

final result = await commandRunner.run(
['test', '--recursive', directory.path],
);
final result = await commandRunner.run(['test', '--recursive']);
expect(result, equals(ExitCode.success.code));
verify(() {
logger.write(
Expand Down