Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Deprecate --packages flag and add --package #370

Merged
merged 12 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
is collected.
* Add a `branchHits` field to `HitMap`.
* Add support for scraping the service URI from the new Dart VM service message.
* Replace `--packages` flag with `--package`, which takes the package's root
directory, instead of the .package file.
* Deprecate the packagesPath parameter and add packagePath instead, in
`HitMap.parseJson`, `HitMap.parseFiles`, `createHitmap`, and `parseCoverage`.

## 1.1.0 - 2022-1-18

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ all isolates are paused before collecting coverage.
#### Formatting coverage data

```
pub global run coverage:format_coverage --packages=app_package/.packages -i coverage.json
pub global run coverage:format_coverage --package=app_package -i coverage.json
```

or if the `pub global run` exectuables are on your PATH,

```
format_coverage --packages=app_package/.packages -i coverage.json
format_coverage --package=app_package -i coverage.json
```

where `app_package` is the path to the package whose coverage is being
collected. If `--sdk-root` is set, Dart SDK coverage will also be output.
collected (defaults to the current working directory). If `--sdk-root` is set,
Dart SDK coverage will also be output.

#### Ignore lines from coverage

Expand Down
23 changes: 12 additions & 11 deletions bin/format_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Environment {
required this.input,
required this.lcov,
required this.output,
required this.packagesPath,
required this.packagePath,
required this.prettyPrint,
required this.prettyPrintFunc,
required this.prettyPrintBranch,
Expand All @@ -35,7 +35,7 @@ class Environment {
String input;
bool lcov;
IOSink output;
String? packagesPath;
String? packagePath;
bool prettyPrint;
bool prettyPrintFunc;
bool prettyPrintBranch;
Expand All @@ -54,7 +54,7 @@ Future<void> main(List<String> arguments) async {
print(' # files: ${files.length}');
print(' # workers: ${env.workers}');
print(' sdk-root: ${env.sdkRoot}');
print(' package-spec: ${env.packagesPath}');
print(' package-path: ${env.packagePath}');
print(' report-on: ${env.reportOn}');
print(' check-ignore: ${env.checkIgnore}');
}
Expand All @@ -63,7 +63,7 @@ Future<void> main(List<String> arguments) async {
final hitmap = await HitMap.parseFiles(
files,
checkIgnoredLines: env.checkIgnore,
packagesPath: env.packagesPath,
packagePath: env.packagePath,
);

// All workers are done. Process the data.
Expand All @@ -74,7 +74,7 @@ Future<void> main(List<String> arguments) async {
String output;
final resolver = env.bazel
? BazelResolver(workspacePath: env.bazelWorkspace)
: Resolver(packagesPath: env.packagesPath, sdkRoot: env.sdkRoot);
: Resolver(packagePath: env.packagePath, sdkRoot: env.sdkRoot);
final loader = Loader();
if (env.prettyPrint) {
output = await hitmap.prettyPrint(resolver, loader,
Expand Down Expand Up @@ -116,7 +116,8 @@ Environment parseArgs(List<String> arguments) {
final parser = ArgParser();

parser.addOption('sdk-root', abbr: 's', help: 'path to the SDK root');
parser.addOption('packages', help: 'path to the package spec file');
parser.addOption('package',
help: 'root directory of the package', defaultsTo: '.');
parser.addOption('in', abbr: 'i', help: 'input(s): may be file or directory');
parser.addOption('out',
abbr: 'o', defaultsTo: 'stdout', help: 'output: may be file or stdout');
Expand Down Expand Up @@ -184,10 +185,10 @@ Environment parseArgs(List<String> arguments) {
}
}

final packagesPath = args['packages'] as String?;
if (packagesPath != null) {
if (!FileSystemEntity.isFileSync(packagesPath)) {
fail('Package spec "${args["packages"]}" not found, or not a file.');
final packagePath = args['package'] as String?;
if (packagePath != null) {
if (!FileSystemEntity.isDirectorySync(packagePath)) {
fail('Package spec "${args["package"]}" not found, or not a directory.');
}
}

Expand Down Expand Up @@ -253,7 +254,7 @@ Environment parseArgs(List<String> arguments) {
input: input,
lcov: lcov,
output: output,
packagesPath: packagesPath,
packagePath: packagePath,
prettyPrint: prettyPrint,
prettyPrintFunc: prettyPrintFunc,
prettyPrintBranch: prettyPrintBranch,
Expand Down
21 changes: 15 additions & 6 deletions lib/src/hitmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class HitMap {
static Future<Map<String, HitMap>> parseJson(
List<Map<String, dynamic>> jsonResult, {
bool checkIgnoredLines = false,
String? packagesPath,
@Deprecated('Use packagePath') String? packagesPath,
String? packagePath,
}) async {
final resolver = Resolver(packagesPath: packagesPath);
final resolver = await Resolver.create(
packagesPath: packagesPath, packagePath: packagePath);
final loader = Loader();

// Map of source file to map of line to hit count for that line.
Expand Down Expand Up @@ -159,7 +161,8 @@ class HitMap {
static Future<Map<String, HitMap>> parseFiles(
Iterable<File> files, {
bool checkIgnoredLines = false,
String? packagesPath,
@Deprecated('Use packagePath') String? packagesPath,
String? packagePath,
}) async {
final globalHitmap = <String, HitMap>{};
for (var file in files) {
Expand All @@ -171,6 +174,7 @@ class HitMap {
jsonResult.cast<Map<String, dynamic>>(),
checkIgnoredLines: checkIgnoredLines,
packagesPath: packagesPath,
packagePath: packagePath,
));
}
}
Expand Down Expand Up @@ -239,12 +243,14 @@ class _HitInfo {
Future<Map<String, Map<int, int>>> createHitmap(
List<Map<String, dynamic>> jsonResult, {
bool checkIgnoredLines = false,
String? packagesPath,
@Deprecated('Use packagePath') String? packagesPath,
String? packagePath,
}) async {
final result = await HitMap.parseJson(
jsonResult,
checkIgnoredLines: checkIgnoredLines,
packagesPath: packagesPath,
packagePath: packagePath,
);
return result.map((key, value) => MapEntry(key, value.lineHits));
}
Expand Down Expand Up @@ -276,10 +282,13 @@ Future<Map<String, Map<int, int>>> parseCoverage(
Iterable<File> files,
int _, {
bool checkIgnoredLines = false,
String? packagesPath,
@Deprecated('Use packagePath') String? packagesPath,
String? packagePath,
}) async {
final result = await HitMap.parseFiles(files,
checkIgnoredLines: checkIgnoredLines, packagesPath: packagesPath);
checkIgnoredLines: checkIgnoredLines,
packagesPath: packagesPath,
packagePath: packagePath);
return result.map((key, value) => MapEntry(key, value.lineHits));
}

Expand Down
32 changes: 30 additions & 2 deletions lib/src/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,30 @@ import 'package:path/path.dart' as p;

/// [Resolver] resolves imports with respect to a given environment.
class Resolver {
Resolver({this.packagesPath, this.sdkRoot})
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null;
Resolver(
{this.packagesPath,
this.packagePath,
this.sdkRoot,
Map<String, Uri>? packages})
: _packages = packages;

static Future<Resolver> create({
@Deprecated('Use packagePath') String? packagesPath,
String? packagePath,
String? sdkRoot,
}) async {
return Resolver(
packagesPath: packagesPath,
packagePath: packagePath,
sdkRoot: sdkRoot,
packages: packagesPath != null
? _parsePackages(packagesPath)
: (packagePath != null ? await _parsePackage(packagePath) : null),
);
}

final String? packagesPath;
final String? packagePath;
final String? sdkRoot;
final List<String> failed = [];
final Map<String, Uri>? _packages;
Expand Down Expand Up @@ -112,6 +132,14 @@ class Resolver {
return packageMap;
}
}

static Future<Map<String, Uri>?> _parsePackage(String packagePath) async {
final parsed = await findPackageConfig(Directory(packagePath));
if (parsed == null) return null;
return {
for (var package in parsed.packages) package.name: package.packageUriRoot
};
}
}

/// Bazel URI resolver.
Expand Down
3 changes: 1 addition & 2 deletions test/collect_coverage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ void main() {
await outputFile.writeAsString(coverageResults, flush: true);

final parsedResult = await HitMap.parseFiles([outputFile],
packagesPath: '.dart_tool/package_config.json',
checkIgnoredLines: true);
packagePath: '.', checkIgnoredLines: true);

// This file has ignore:coverage-file.
expect(parsedResult, isNot(contains(_sampleAppFileUri)));
Expand Down
22 changes: 11 additions & 11 deletions test/lcov_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void main() {
test('format()', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
// ignore: deprecated_member_use_from_same_package
final formatter = LcovFormatter(resolver);

Expand All @@ -94,7 +94,7 @@ void main() {
test('formatLcov()', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res = hitmap.formatLcov(resolver);

expect(res, contains(p.absolute(_sampleAppPath)));
Expand All @@ -105,7 +105,7 @@ void main() {
test('formatLcov() includes files in reportOn list', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res = hitmap.formatLcov(resolver, reportOn: ['lib/', 'test/']);

expect(res, contains(p.absolute(_sampleAppPath)));
Expand All @@ -116,7 +116,7 @@ void main() {
test('formatLcov() excludes files not in reportOn list', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res = hitmap.formatLcov(resolver, reportOn: ['lib/']);

expect(res, isNot(contains(p.absolute(_sampleAppPath))));
Expand All @@ -127,7 +127,7 @@ void main() {
test('formatLcov() uses paths relative to basePath', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res = hitmap.formatLcov(resolver, basePath: p.absolute('lib'));

expect(
Expand All @@ -140,7 +140,7 @@ void main() {
test('format()', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
// ignore: deprecated_member_use_from_same_package
final formatter = PrettyPrintFormatter(resolver, Loader());

Expand All @@ -167,7 +167,7 @@ void main() {
test('prettyPrint()', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res = await hitmap.prettyPrint(resolver, Loader());

expect(res, contains(p.absolute(_sampleAppPath)));
Expand All @@ -190,7 +190,7 @@ void main() {
test('prettyPrint() includes files in reportOn list', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res = await hitmap
.prettyPrint(resolver, Loader(), reportOn: ['lib/', 'test/']);

Expand All @@ -202,7 +202,7 @@ void main() {
test('prettyPrint() excludes files not in reportOn list', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res =
await hitmap.prettyPrint(resolver, Loader(), reportOn: ['lib/']);

Expand All @@ -214,7 +214,7 @@ void main() {
test('prettyPrint() functions', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res =
await hitmap.prettyPrint(resolver, Loader(), reportFuncs: true);

Expand All @@ -232,7 +232,7 @@ void main() {
test('prettyPrint() branches', () async {
final hitmap = await _getHitMap();

final resolver = Resolver(packagesPath: '.dart_tool/package_config.json');
final resolver = await Resolver.create(packagePath: '.');
final res =
await hitmap.prettyPrint(resolver, Loader(), reportBranches: true);

Expand Down
15 changes: 11 additions & 4 deletions test/resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,31 @@ foo:file:///${d.sandbox}/foo/lib
});

test('can be created from a package_config.json', () async {
final resolver = Resolver(
final resolver = await Resolver.create(
packagesPath:
p.join(d.sandbox, 'foo', '.dart_tool', 'package_config.json'));
expect(resolver.resolve('package:foo/foo.dart'),
'${d.sandbox}/foo/lib/foo.dart');
});

test('can be created from a .packages file', () async {
final resolver = await Resolver.create(
packagesPath: p.join(d.sandbox, 'foo', '.packages'));
expect(resolver.resolve('package:foo/foo.dart'),
'${d.sandbox}/foo/lib/foo.dart');
});

test('can be created from a package directory', () async {
final resolver =
Resolver(packagesPath: p.join(d.sandbox, 'foo', '.packages'));
await Resolver.create(packagePath: p.join(d.sandbox, 'foo'));
expect(resolver.resolve('package:foo/foo.dart'),
'${d.sandbox}/foo/lib/foo.dart');
});

test('errors if the packagesFile is an unknown format', () async {
expect(
() =>
Resolver(packagesPath: p.join(d.sandbox, 'foo', '.bad.packages')),
() => await Resolver.create(
packagesPath: p.join(d.sandbox, 'foo', '.bad.packages')),
throwsA(isA<FormatException>()));
});
});
Expand Down
1 change: 0 additions & 1 deletion tool/test_and_collect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ if [[ $(dart --version 2>&1 ) =~ '(dev)' ]]; then
--lcov \
--in=var/coverage.json \
--out=var/lcov.info \
--packages=.dart_tool/package_config.json \
--report-on=lib \
--check-ignore
fi