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: 3 additions & 1 deletion .github/workflows/very_good_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ jobs:
run: flutter pub run test --run-skipped -t pull-request-only

- name: Run Tests
run: flutter test -x pull-request-only -x e2e --no-pub --coverage --test-randomize-ordering-seed random
run: |
flutter pub global activate coverage
flutter pub run test -j 1 -x pull-request-only -x e2e --coverage=coverage --test-randomize-ordering-seed random && dart run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --packages=.packages --report-on=lib

- name: Check Code Coverage
uses: VeryGoodOpenSource/very_good_coverage@v1.2.0
Expand Down
1 change: 1 addition & 0 deletions lib/src/cli/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:lcov_parser/lcov_parser.dart';
import 'package:mason/mason.dart';
import 'package:path/path.dart' as p;
import 'package:universal_io/io.dart';
import 'package:very_good_cli/src/commands/test/templates/test_runner_bundle.dart';
import 'package:very_good_test_runner/very_good_test_runner.dart';

part 'dart_cli.dart';
Expand Down
36 changes: 33 additions & 3 deletions lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ class Flutter {
String cwd = '.',
bool recursive = false,
bool collectCoverage = false,
bool optimizePerformance = false,
double? minCoverage,
String? excludeFromCoverage,
List<String>? arguments,
void Function([String?]) Function(String message)? progress,
void Function(String)? stdout,
void Function(String)? stderr,
}) async {
Expand All @@ -129,13 +131,41 @@ class Flutter {
}

await _runCommand(
cmd: (cwd) {
cmd: (cwd) async {
void noop(String? _) {}
stdout?.call('Running "flutter test" in ${p.canonicalize(cwd)}...\n');
final target = DirectoryGeneratorTarget(Directory(p.normalize(cwd)));
final workingDirectory = target.dir.absolute.path;
stdout?.call(
'Running "flutter test" in ${p.dirname(workingDirectory)}...\n',
);

if (optimizePerformance) {
final optimizationDone = progress?.call('Optimizing tests');
try {
final generator = await MasonGenerator.fromBundle(testRunnerBundle);
var vars = <String, dynamic>{'package-root': workingDirectory};
await generator.hooks.preGen(
vars: vars,
onVarsChanged: (v) => vars = v,
workingDirectory: workingDirectory,
);
await generator.generate(
target,
vars: vars,
fileConflictResolution: FileConflictResolution.overwrite,
);
} finally {
optimizationDone?.call();
}
}

return _flutterTest(
cwd: cwd,
collectCoverage: collectCoverage,
arguments: arguments,
arguments: [
...?arguments,
if (optimizePerformance) p.join('test', '.test_runner.dart')
],
stdout: stdout ?? noop,
stderr: stderr ?? noop,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/commands/commands.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export 'create/create.dart';
export 'packages.dart';
export 'test.dart';
export 'test/test.dart';
2 changes: 1 addition & 1 deletion lib/src/commands/create/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CreateCommand extends Command<int> {

final Analytics _analytics;
final Logger _logger;
final Future<MasonGenerator> Function(MasonBundle) _generator;
final GeneratorBuilder _generator;

@override
String get description =>
Expand Down
58 changes: 58 additions & 0 deletions lib/src/commands/test/templates/test_runner_bundle.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 42 additions & 12 deletions lib/src/commands/test.dart → lib/src/commands/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@ import 'package:path/path.dart' as path;
import 'package:universal_io/io.dart';
import 'package:very_good_cli/src/cli/cli.dart';

/// Signature for the [Flutter.installed] method.
typedef FlutterInstalledCommand = Future<bool> Function();

/// Signature for the [Flutter.test] method.
typedef FlutterTestCommand = Future<void> Function({
String cwd,
bool recursive,
bool collectCoverage,
bool optimizePerformance,
double? minCoverage,
String? excludeFromCoverage,
List<String>? arguments,
void Function([String?]) Function(String message)? progress,
void Function(String)? stdout,
void Function(String)? stderr,
});

/// {@template test_command}
/// `very_good test` command for running tests.
/// {@endtemplate}
class TestCommand extends Command<int> {
/// {@macro test_command}
TestCommand({Logger? logger}) : _logger = logger ?? Logger() {
TestCommand({
Logger? logger,
FlutterInstalledCommand? flutterInstalled,
FlutterTestCommand? flutterTest,
}) : _logger = logger ?? Logger(),
_flutterInstalled = flutterInstalled ?? Flutter.installed,
_flutterTest = flutterTest ?? Flutter.test {
argParser
..addFlag(
'recursive',
Expand Down Expand Up @@ -41,6 +64,8 @@ class TestCommand extends Command<int> {
}

final Logger _logger;
final FlutterInstalledCommand _flutterInstalled;
final FlutterTestCommand _flutterTest;

@override
String get description => 'Run tests in a Dart or Flutter project.';
Expand All @@ -56,21 +81,33 @@ class TestCommand extends Command<int> {

@override
Future<int> run() async {
final recursive = _argResults['recursive'] as bool;
final targetPath = path.normalize(Directory.current.absolute.path);
final pubspec = File(path.join(targetPath, 'pubspec.yaml'));

if (!pubspec.existsSync()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I m curious why moving this up and not using the exception anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to catch this error before running the pre gen hook since it would fail if a pubspec didn't exist.

_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;
}

final recursive = _argResults['recursive'] as bool;
final collectCoverage = _argResults['coverage'] as bool;
final minCoverage = double.tryParse(
_argResults['min-coverage'] as String? ?? '',
);
final excludeTags = _argResults['exclude-tags'] as String?;
final isFlutterInstalled = await Flutter.installed();

final isFlutterInstalled = await _flutterInstalled();
final excludeFromCoverage = _argResults['exclude-coverage'] as String?;

if (isFlutterInstalled) {
try {
await Flutter.test(
await _flutterTest(
optimizePerformance: _argResults.rest.isEmpty,
recursive: recursive,
progress: _logger.progress,
stdout: _logger.write,
stderr: _logger.err,
collectCoverage: collectCoverage,
Expand All @@ -81,13 +118,6 @@ class TestCommand extends Command<int> {
..._argResults.rest,
],
);
} on PubspecNotFound catch (_) {
_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(
'''Expected coverage >= ${minCoverage!.toStringAsFixed(2)}% but actual is ${e.coverage.toStringAsFixed(2)}%.''',
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
args: ^2.1.0
glob: ^2.0.2
lcov_parser: ^0.1.2
mason: ">=0.1.0-dev.9 <0.1.0-dev.10"
mason: ">=0.1.0-dev.12 <0.1.0-dev.13"
mason_logger: ^0.1.0-dev.6
meta: ^1.3.0
path: ^1.8.0
Expand Down
Loading