Skip to content
Open
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
71 changes: 70 additions & 1 deletion pkgs/test/test/runner/coverage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ end_of_record
''');
});

test('gathers coverage for tests in multiple pacakges', () async {
test('gathers coverage for tests in multiple packages', () async {
final clientPkgDir = p.join(d.sandbox, 'fake_client');
await (await runPub([
'get',
Expand Down Expand Up @@ -214,6 +214,75 @@ end_of_record
''');
});

test('gathers coverage for package in a workspace', () async {
await d.dir(d.sandbox, [
d.dir('fake_workspace', [
d.file('pubspec.yaml', '''
name: fake_workspace
version: 1.0.0
environment:
sdk: ^3.5.0
workspace:
- workspace_package
'''),
d.dir('workspace_package', [
d.file('pubspec.yaml', '''
name: workspace_package
version: 1.0.0
environment:
sdk: ^3.5.0
resolution: workspace
dev_dependencies:
test: ^1.26.2
'''),
d.dir('lib', [
d.file('calculate.dart', '''
int calculate(int x) {
if (x % 2 == 0) {
return x * 2;
} else {
return x * 3;
}
}
'''),
]),
d.dir('test', [
d.file('test.dart', '''
import 'package:workspace_package/calculate.dart';
import 'package:test/test.dart';

void main() {
test('test 1', () {
expect(calculate(6), 12);
});
}
'''),
]),
]),
]),
]).create();

final pkgDir = p.join(d.sandbox, 'fake_workspace', 'workspace_package');
await (await runPub(['get'], workingDirectory: pkgDir)).shouldExit(0);
final lcovFile = p.join(coverageDirectory.path, 'lcov.info');
var test = await runTest(
['--coverage-path', lcovFile, 'test/test.dart'],
packageConfig: p.join(pkgDir, '../.dart_tool/package_config.json'),
workingDirectory: pkgDir,
);
await validateTest(test);
expect(File(lcovFile).readAsStringSync(), '''
SF:${p.join(pkgDir, 'lib', 'calculate.dart')}
DA:1,1
DA:2,2
DA:3,1
DA:5,0
LF:4
LH:3
end_of_record
''');
});

test('gathers coverage for Chrome tests', () async {
await (await runPub(['get'], workingDirectory: pkgDir)).shouldExit(0);
var test = await runTest(
Expand Down
2 changes: 2 additions & 0 deletions pkgs/test_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Add `--coverage-package` flag, which filters the coverage report to specific
packages using RegExps.
* Fix default coverage filter when running in a workspace package. Default
filter now includes all the workspace's package.

## 0.6.14

Expand Down
2 changes: 1 addition & 1 deletion pkgs/test_core/lib/src/runner/vm/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ Future<Set<String>> _filterCoveragePackages(
List<RegExp>? coveragePackages,
) async {
if (coveragePackages == null || coveragePackages.isEmpty) {
return {(await currentPackage).name};
return workspacePackageNames(await currentPackage);
} else {
return (await currentPackageConfig).packages
.map((package) => package.name)
Expand Down
22 changes: 22 additions & 0 deletions pkgs/test_core/lib/src/util/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:isolate';

import 'package:package_config/package_config.dart';
import 'package:path/path.dart' as p;
import 'package:pubspec_parse/pubspec_parse.dart';

/// The [PackageConfig] parsed from the current isolates package config file.
final Future<PackageConfig> currentPackageConfig = () async {
Expand Down Expand Up @@ -42,3 +43,24 @@ Future<Uri> absoluteUri(String path) async {
final Future<Package> currentPackage = () async {
return (await currentPackageConfig).packageOf(await packageConfigUri)!;
}();

/// Returns the names of all the packages in the workspace.
///
/// The returned set includes the package's own name (if the package is not a
/// workspace, that will be the only name in the set).
Set<String> workspacePackageNames(Package package) =>
_getAllWorkspaceNames(package.root, <String>{});

Set<String> _getAllWorkspaceNames(Uri packageRoot, Set<String> results) {
final pubspecUri = packageRoot.resolve('pubspec.yaml');
final pubspecFile = File(pubspecUri.toFilePath());
if (pubspecFile.existsSync()) {
final yaml = pubspecFile.readAsStringSync();
final pubspec = Pubspec.parse(yaml, sourceUrl: pubspecUri);
results.add(pubspec.name);
for (final workspace in pubspec.workspace ?? <String>[]) {
_getAllWorkspaceNames(packageRoot.resolve('$workspace/'), results);
}
}
return results;
}
1 change: 1 addition & 0 deletions pkgs/test_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
package_config: ^2.0.0
path: ^1.8.0
pool: ^1.5.0
pubspec_parse: ^1.5.0
source_map_stack_trace: ^2.1.0
source_maps: ^0.10.10
source_span: ^1.8.0
Expand Down