Skip to content

Commit d6028e8

Browse files
authored
Deprecate --packages flag and add --package (dart-archive/coverage#370)
* Migrate tests to package_config.json * Swap --packages flag for --package * Fix analysis errors * Keep the --packages flag, but delete support for .package in resolver * Make separate private constructor for Resolver * Improve resolver test coverage * Switch readme example commands from `pub` to `dart pub` * 1.2.0 is published, so move changes to 1.3.0-dev
1 parent 594ca55 commit d6028e8

10 files changed

+161
-72
lines changed

pkgs/coverage/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.3.0-dev
2+
* Add a `--package` flag, which takes the package's root directory, instead of
3+
the .package file. Deprecate the `--packages` flag.
4+
* Deprecate the packagesPath parameter and add packagePath instead, in
5+
`HitMap.parseJson`, `HitMap.parseFiles`, `createHitmap`, and `parseCoverage`.
6+
17
## 1.2.0
28

39
* Support branch level coverage information, when running tests in the Dart VM.

pkgs/coverage/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ Tools
1616

1717
dart pub global activate coverage
1818

19-
Consider adding the `pub global run` executables directory to your path.
19+
Consider adding the `dart pub global run` executables directory to your path.
2020
See [Running a script from your PATH](https://dart.dev/tools/pub/cmd/pub-global#running-a-script-from-your-path)
2121
for more details.
2222

2323
#### Collecting coverage from the VM
2424

2525
```
2626
dart --pause-isolates-on-exit --disable-service-auth-codes --enable-vm-service=NNNN script.dart
27-
pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates
27+
dart pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates
2828
```
2929

30-
or if the `pub global run` executables are on your PATH,
30+
or if the `dart pub global run` executables are on your PATH,
3131

3232
```
3333
collect_coverage --uri=http://... -o coverage.json --resume-isolates
@@ -44,17 +44,18 @@ all isolates are paused before collecting coverage.
4444
#### Formatting coverage data
4545

4646
```
47-
pub global run coverage:format_coverage --packages=app_package/.packages -i coverage.json
47+
dart pub global run coverage:format_coverage --package=app_package -i coverage.json
4848
```
4949

50-
or if the `pub global run` exectuables are on your PATH,
50+
or if the `dart pub global run` exectuables are on your PATH,
5151

5252
```
53-
format_coverage --packages=app_package/.packages -i coverage.json
53+
format_coverage --package=app_package -i coverage.json
5454
```
5555

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

5960
#### Ignore lines from coverage
6061

@@ -69,15 +70,15 @@ collect_coverage:
6970

7071
```
7172
dart --pause-isolates-on-exit --disable-service-auth-codes --enable-vm-service=NNNN script.dart
72-
pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates --function-coverage
73+
dart pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates --function-coverage
7374
```
7475

7576
To gather branch level coverage information, pass `--branch-coverage` to *both*
7677
collect_coverage and the Dart command you're gathering coverage from:
7778

7879
```
7980
dart --pause-isolates-on-exit --disable-service-auth-codes --enable-vm-service=NNNN --branch-coverage script.dart
80-
pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates --branch-coverage
81+
dart pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates --branch-coverage
8182
```
8283

8384
Branch coverage requires Dart VM 2.17.0, with service API v3.56. Function,
@@ -86,5 +87,5 @@ those flags:
8687

8788
```
8889
dart --pause-isolates-on-exit --disable-service-auth-codes --enable-vm-service=NNNN --branch-coverage script.dart
89-
pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates --function-coverage --branch-coverage
90+
dart pub global run coverage:collect_coverage --uri=http://... -o coverage.json --resume-isolates --function-coverage --branch-coverage
9091
```

pkgs/coverage/bin/format_coverage.dart

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Environment {
1919
required this.lcov,
2020
required this.output,
2121
required this.packagesPath,
22+
required this.packagePath,
2223
required this.prettyPrint,
2324
required this.prettyPrintFunc,
2425
required this.prettyPrintBranch,
@@ -36,6 +37,7 @@ class Environment {
3637
bool lcov;
3738
IOSink output;
3839
String? packagesPath;
40+
String packagePath;
3941
bool prettyPrint;
4042
bool prettyPrintFunc;
4143
bool prettyPrintBranch;
@@ -54,7 +56,8 @@ Future<void> main(List<String> arguments) async {
5456
print(' # files: ${files.length}');
5557
print(' # workers: ${env.workers}');
5658
print(' sdk-root: ${env.sdkRoot}');
57-
print(' package-spec: ${env.packagesPath}');
59+
print(' package-path: ${env.packagePath}');
60+
print(' packages-path: ${env.packagesPath}');
5861
print(' report-on: ${env.reportOn}');
5962
print(' check-ignore: ${env.checkIgnore}');
6063
}
@@ -63,7 +66,9 @@ Future<void> main(List<String> arguments) async {
6366
final hitmap = await HitMap.parseFiles(
6467
files,
6568
checkIgnoredLines: env.checkIgnore,
69+
// ignore: deprecated_member_use_from_same_package
6670
packagesPath: env.packagesPath,
71+
packagePath: env.packagePath,
6772
);
6873

6974
// All workers are done. Process the data.
@@ -74,7 +79,11 @@ Future<void> main(List<String> arguments) async {
7479
String output;
7580
final resolver = env.bazel
7681
? BazelResolver(workspacePath: env.bazelWorkspace)
77-
: Resolver(packagesPath: env.packagesPath, sdkRoot: env.sdkRoot);
82+
: await Resolver.create(
83+
packagesPath: env.packagesPath,
84+
packagePath: env.packagePath,
85+
sdkRoot: env.sdkRoot,
86+
);
7887
final loader = Loader();
7988
if (env.prettyPrint) {
8089
output = await hitmap.prettyPrint(resolver, loader,
@@ -116,7 +125,10 @@ Environment parseArgs(List<String> arguments) {
116125
final parser = ArgParser();
117126

118127
parser.addOption('sdk-root', abbr: 's', help: 'path to the SDK root');
119-
parser.addOption('packages', help: 'path to the package spec file');
128+
parser.addOption('packages',
129+
help: '[DEPRECATED] path to the package spec file');
130+
parser.addOption('package',
131+
help: 'root directory of the package', defaultsTo: '.');
120132
parser.addOption('in', abbr: 'i', help: 'input(s): may be file or directory');
121133
parser.addOption('out',
122134
abbr: 'o', defaultsTo: 'stdout', help: 'output: may be file or stdout');
@@ -191,6 +203,11 @@ Environment parseArgs(List<String> arguments) {
191203
}
192204
}
193205

206+
final packagePath = args['package'] as String;
207+
if (!FileSystemEntity.isDirectorySync(packagePath)) {
208+
fail('Package spec "${args["package"]}" not found, or not a directory.');
209+
}
210+
194211
if (args['in'] == null) fail('No input files given.');
195212
final input = p.absolute(p.normalize(args['in'] as String));
196213
if (!FileSystemEntity.isDirectorySync(input) &&
@@ -254,6 +271,7 @@ Environment parseArgs(List<String> arguments) {
254271
lcov: lcov,
255272
output: output,
256273
packagesPath: packagesPath,
274+
packagePath: packagePath,
257275
prettyPrint: prettyPrint,
258276
prettyPrintFunc: prettyPrintFunc,
259277
prettyPrintBranch: prettyPrintBranch,

pkgs/coverage/lib/src/hitmap.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ class HitMap {
3939
static Future<Map<String, HitMap>> parseJson(
4040
List<Map<String, dynamic>> jsonResult, {
4141
bool checkIgnoredLines = false,
42-
String? packagesPath,
42+
@Deprecated('Use packagePath') String? packagesPath,
43+
String? packagePath,
4344
}) async {
44-
final resolver = Resolver(packagesPath: packagesPath);
45+
final resolver = await Resolver.create(
46+
packagesPath: packagesPath, packagePath: packagePath);
4547
final loader = Loader();
4648

4749
// Map of source file to map of line to hit count for that line.
@@ -159,7 +161,8 @@ class HitMap {
159161
static Future<Map<String, HitMap>> parseFiles(
160162
Iterable<File> files, {
161163
bool checkIgnoredLines = false,
162-
String? packagesPath,
164+
@Deprecated('Use packagePath') String? packagesPath,
165+
String? packagePath,
163166
}) async {
164167
final globalHitmap = <String, HitMap>{};
165168
for (var file in files) {
@@ -170,7 +173,9 @@ class HitMap {
170173
globalHitmap.merge(await HitMap.parseJson(
171174
jsonResult.cast<Map<String, dynamic>>(),
172175
checkIgnoredLines: checkIgnoredLines,
176+
// ignore: deprecated_member_use_from_same_package
173177
packagesPath: packagesPath,
178+
packagePath: packagePath,
174179
));
175180
}
176181
}
@@ -239,12 +244,14 @@ class _HitInfo {
239244
Future<Map<String, Map<int, int>>> createHitmap(
240245
List<Map<String, dynamic>> jsonResult, {
241246
bool checkIgnoredLines = false,
242-
String? packagesPath,
247+
@Deprecated('Use packagePath') String? packagesPath,
248+
String? packagePath,
243249
}) async {
244250
final result = await HitMap.parseJson(
245251
jsonResult,
246252
checkIgnoredLines: checkIgnoredLines,
247253
packagesPath: packagesPath,
254+
packagePath: packagePath,
248255
);
249256
return result.map((key, value) => MapEntry(key, value.lineHits));
250257
}
@@ -276,10 +283,13 @@ Future<Map<String, Map<int, int>>> parseCoverage(
276283
Iterable<File> files,
277284
int _, {
278285
bool checkIgnoredLines = false,
279-
String? packagesPath,
286+
@Deprecated('Use packagePath') String? packagesPath,
287+
String? packagePath,
280288
}) async {
281289
final result = await HitMap.parseFiles(files,
282-
checkIgnoredLines: checkIgnoredLines, packagesPath: packagesPath);
290+
checkIgnoredLines: checkIgnoredLines,
291+
packagesPath: packagesPath,
292+
packagePath: packagePath);
283293
return result.map((key, value) => MapEntry(key, value.lineHits));
284294
}
285295

pkgs/coverage/lib/src/resolver.dart

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,42 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:convert';
65
import 'dart:io';
76

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

1110
/// [Resolver] resolves imports with respect to a given environment.
1211
class Resolver {
12+
@Deprecated('Use Resolver.create')
1313
Resolver({this.packagesPath, this.sdkRoot})
14-
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null;
14+
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null,
15+
packagePath = null;
16+
17+
Resolver._(
18+
{this.packagesPath,
19+
this.packagePath,
20+
this.sdkRoot,
21+
Map<String, Uri>? packages})
22+
: _packages = packages;
23+
24+
static Future<Resolver> create({
25+
String? packagesPath,
26+
String? packagePath,
27+
String? sdkRoot,
28+
}) async {
29+
return Resolver._(
30+
packagesPath: packagesPath,
31+
packagePath: packagePath,
32+
sdkRoot: sdkRoot,
33+
packages: packagesPath != null
34+
? _parsePackages(packagesPath)
35+
: (packagePath != null ? await _parsePackage(packagePath) : null),
36+
);
37+
}
1538

1639
final String? packagesPath;
40+
final String? packagePath;
1741
final String? sdkRoot;
1842
final List<String> failed = [];
1943
final Map<String, Uri>? _packages;
@@ -86,32 +110,20 @@ class Resolver {
86110

87111
static Map<String, Uri> _parsePackages(String packagesPath) {
88112
final content = File(packagesPath).readAsStringSync();
89-
try {
90-
final packagesUri = p.toUri(packagesPath);
91-
final parsed =
92-
PackageConfig.parseString(content, Uri.base.resolveUri(packagesUri));
93-
return {
94-
for (var package in parsed.packages)
95-
package.name: package.packageUriRoot
96-
};
97-
} on FormatException catch (_) {
98-
// It was probably an old style .packages file
99-
final lines = LineSplitter.split(content);
100-
final packageMap = <String, Uri>{};
101-
for (var line in lines) {
102-
if (line.startsWith('#')) continue;
103-
final firstColon = line.indexOf(':');
104-
if (firstColon == -1) {
105-
throw FormatException(
106-
'Unexpected package config format, expected an old style '
107-
'.packages file or new style package_config.json file.',
108-
content);
109-
}
110-
packageMap[line.substring(0, firstColon)] =
111-
Uri.parse(line.substring(firstColon + 1, line.length));
112-
}
113-
return packageMap;
114-
}
113+
final packagesUri = p.toUri(packagesPath);
114+
final parsed =
115+
PackageConfig.parseString(content, Uri.base.resolveUri(packagesUri));
116+
return {
117+
for (var package in parsed.packages) package.name: package.packageUriRoot
118+
};
119+
}
120+
121+
static Future<Map<String, Uri>?> _parsePackage(String packagePath) async {
122+
final parsed = await findPackageConfig(Directory(packagePath));
123+
if (parsed == null) return null;
124+
return {
125+
for (var package in parsed.packages) package.name: package.packageUriRoot
126+
};
115127
}
116128
}
117129

pkgs/coverage/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: coverage
2-
version: 1.2.0
2+
version: 1.3.0-dev
33
description: Coverage data manipulation and formatting
44
homepage: https://github.com/dart-lang/coverage
55

pkgs/coverage/test/collect_coverage_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ void main() {
299299
await outputFile.writeAsString(coverageResults, flush: true);
300300

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

305304
// This file has ignore:coverage-file.
306305
expect(parsedResult, isNot(contains(_sampleAppFileUri)));

0 commit comments

Comments
 (0)