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 9 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 @@ -15,6 +15,10 @@
* Add a `branchHits` field to `HitMap`.
* Add support for scraping the service URI from the new Dart VM service message.
* Correctly parse package_config files on Windows when the root URI is relative.
* Add a `--package` flag, which takes the package's root directory, instead of
the .package file. Deprecate the `--packages` flag.
* 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
24 changes: 21 additions & 3 deletions bin/format_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Environment {
required this.lcov,
required this.output,
required this.packagesPath,
required this.packagePath,
required this.prettyPrint,
required this.prettyPrintFunc,
required this.prettyPrintBranch,
Expand All @@ -36,6 +37,7 @@ class Environment {
bool lcov;
IOSink output;
String? packagesPath;
String packagePath;
bool prettyPrint;
bool prettyPrintFunc;
bool prettyPrintBranch;
Expand All @@ -54,7 +56,8 @@ 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(' packages-path: ${env.packagesPath}');
print(' report-on: ${env.reportOn}');
print(' check-ignore: ${env.checkIgnore}');
}
Expand All @@ -63,7 +66,9 @@ Future<void> main(List<String> arguments) async {
final hitmap = await HitMap.parseFiles(
files,
checkIgnoredLines: env.checkIgnore,
// ignore: deprecated_member_use_from_same_package
packagesPath: env.packagesPath,
packagePath: env.packagePath,
);

// All workers are done. Process the data.
Expand All @@ -74,7 +79,11 @@ Future<void> main(List<String> arguments) async {
String output;
final resolver = env.bazel
? BazelResolver(workspacePath: env.bazelWorkspace)
: Resolver(packagesPath: env.packagesPath, sdkRoot: env.sdkRoot);
: await Resolver.create(
packagesPath: env.packagesPath,
packagePath: env.packagePath,
sdkRoot: env.sdkRoot,
);
final loader = Loader();
if (env.prettyPrint) {
output = await hitmap.prettyPrint(resolver, loader,
Expand Down Expand Up @@ -116,7 +125,10 @@ 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('packages',
help: '[DEPRECATED] 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 @@ -191,6 +203,11 @@ Environment parseArgs(List<String> arguments) {
}
}

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

if (args['in'] == null) fail('No input files given.');
final input = p.absolute(p.normalize(args['in'] as String));
if (!FileSystemEntity.isDirectorySync(input) &&
Expand Down Expand Up @@ -254,6 +271,7 @@ Environment parseArgs(List<String> arguments) {
lcov: lcov,
output: output,
packagesPath: packagesPath,
packagePath: packagePath,
prettyPrint: prettyPrint,
prettyPrintFunc: prettyPrintFunc,
prettyPrintBranch: prettyPrintBranch,
Expand Down
22 changes: 16 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 @@ -170,7 +173,9 @@ class HitMap {
globalHitmap.merge(await HitMap.parseJson(
jsonResult.cast<Map<String, dynamic>>(),
checkIgnoredLines: checkIgnoredLines,
// ignore: deprecated_member_use_from_same_package
packagesPath: packagesPath,
packagePath: packagePath,
));
}
}
Expand Down Expand Up @@ -239,12 +244,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 +283,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
68 changes: 40 additions & 28 deletions lib/src/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

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

/// [Resolver] resolves imports with respect to a given environment.
class Resolver {
@Deprecated('Use Resolver.create')
Resolver({this.packagesPath, this.sdkRoot})
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null;
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null,
packagePath = null;

Resolver._(
{this.packagesPath,
this.packagePath,
this.sdkRoot,
Map<String, Uri>? packages})
: _packages = packages;

static Future<Resolver> create({
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 @@ -86,32 +110,20 @@ class Resolver {

static Map<String, Uri> _parsePackages(String packagesPath) {
final content = File(packagesPath).readAsStringSync();
try {
final packagesUri = p.toUri(packagesPath);
final parsed =
PackageConfig.parseString(content, Uri.base.resolveUri(packagesUri));
return {
for (var package in parsed.packages)
package.name: package.packageUriRoot
};
} on FormatException catch (_) {
// It was probably an old style .packages file
final lines = LineSplitter.split(content);
final packageMap = <String, Uri>{};
for (var line in lines) {
if (line.startsWith('#')) continue;
final firstColon = line.indexOf(':');
if (firstColon == -1) {
throw FormatException(
'Unexpected package config format, expected an old style '
'.packages file or new style package_config.json file.',
content);
}
packageMap[line.substring(0, firstColon)] =
Uri.parse(line.substring(firstColon + 1, line.length));
}
return packageMap;
}
final packagesUri = p.toUri(packagesPath);
final parsed =
PackageConfig.parseString(content, Uri.base.resolveUri(packagesUri));
return {
for (var package in parsed.packages) package.name: package.packageUriRoot
};
}

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

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
Loading