Skip to content

Commit 0a2c07a

Browse files
committed
fix(coverage): changelog entry, more root tests, revert lint form
Adds a changelog entry for the file: containment fix. Adds tests covering the other roots _isWithinKnownRoots checks: sdkRoot, a known package root from package_config.json, and the current directory. Only packagePath had a positive test before. Reverts the null-aware ?packagePath/?sdkRoot elements back from the if (x != null) x form requested in an earlier review comment. CI runs dart analyze --fatal-infos, and this package's own lints recommend the null-aware form (use_null_aware_elements), so the if form was failing analyze in CI even though it type-checks fine locally without the flag.
1 parent 64d1382 commit 0a2c07a

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎pkgs/coverage/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
completing and the resume request reaching the VM service.
77
- Also ignore `SentinelException` when resuming the main isolate after
88
collection, matching the existing `RPCError` handling there.
9+
- `Resolver.resolve` now rejects `file:` URIs that fall outside the known
10+
package/SDK roots or the current directory, instead of resolving them
11+
unconditionally. A `source` entry in coverage data pointing at an arbitrary
12+
file (for example `file:///etc/passwd`) previously had its contents read
13+
and printed by `format_coverage --pretty-print`.
914

1015
## 1.15.1
1116

‎pkgs/coverage/lib/src/resolver.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class Resolver {
119119

120120
bool _isWithinKnownRoots(String path) {
121121
final roots = _knownRoots ??= [
122-
if (packagePath != null) packagePath!,
123-
if (sdkRoot != null) sdkRoot!,
122+
?packagePath,
123+
?sdkRoot,
124124
...?_packages?.values.map(p.fromUri),
125125
Directory.current.path,
126126
].map(p.normalize).toList();

‎pkgs/coverage/test/resolver_test.dart‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ void main() {
142142
expect(resolver.resolve(p.toUri(fooDartPath).toString()), fooDartPath);
143143
});
144144

145+
test('resolves file URIs within sdkRoot', () async {
146+
final resolver = await Resolver.create(
147+
packagePath: p.join(d.sandbox, 'foo'),
148+
sdkRoot: p.join(d.sandbox, 'sdk'),
149+
);
150+
final ioDartPath = p.join(d.sandbox, 'sdk', 'io', 'io.dart');
151+
expect(resolver.resolve(p.toUri(ioDartPath).toString()), ioDartPath);
152+
});
153+
154+
test('resolves file URIs within a known package root', () async {
155+
final resolver = await Resolver.create(
156+
packagesPath: p.join(
157+
d.sandbox,
158+
'foo',
159+
'.dart_tool',
160+
'package_config.json',
161+
),
162+
);
163+
final barDartPath = p.join(d.sandbox, 'bar', 'lib', 'bar.dart');
164+
expect(resolver.resolve(p.toUri(barDartPath).toString()), barDartPath);
165+
});
166+
167+
test('resolves file URIs within the current directory', () async {
168+
final resolver = await Resolver.create();
169+
final currentDirFile = File(
170+
p.join(Directory.current.path, 'resolver_test_current_dir_probe.txt'),
171+
)..writeAsStringSync('inside cwd');
172+
addTearDown(currentDirFile.deleteSync);
173+
174+
expect(
175+
resolver.resolve(p.toUri(currentDirFile.path).toString()),
176+
currentDirFile.path,
177+
);
178+
});
179+
145180
test('does not resolve file URIs outside every known root', () async {
146181
final resolver = await Resolver.create(
147182
packagePath: p.join(d.sandbox, 'foo'),

0 commit comments

Comments
 (0)