Skip to content

Commit bd769e9

Browse files
authored
fix listFiles and relative for precompiled package dependencies (#1588)
1 parent de5a2a5 commit bd769e9

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

lib/src/cached_package.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class CachedPackage extends Package {
4444
}
4545

4646
String relative(String path) {
47-
if (p.isWithin(path, _cacheDir)) return p.relative(path, from: _cacheDir);
47+
if (p.isWithin(_cacheDir, path)) {
48+
return p.relative(path, from: _cacheDir);
49+
}
4850
return super.relative(path);
4951
}
5052

@@ -56,14 +58,18 @@ class CachedPackage extends Package {
5658
return super.listFiles(recursive: recursive, useGitIgnore: useGitIgnore);
5759
}
5860

59-
if (_pathInCache(beneath)) return listDir(p.join(_cacheDir, beneath));
61+
if (_pathInCache(beneath)) {
62+
return listDir(p.join(_cacheDir, beneath),
63+
includeDirs: false, recursive: recursive);
64+
}
6065
return super.listFiles(
6166
beneath: beneath, recursive: recursive, useGitIgnore: useGitIgnore);
6267
}
6368

6469
/// Returns whether [relativePath], a path relative to the package's root,
6570
/// is in a cached directory.
66-
bool _pathInCache(String relativePath) => p.isWithin('lib', relativePath);
71+
bool _pathInCache(String relativePath) =>
72+
relativePath == 'lib' || p.isWithin('lib', relativePath);
6773
}
6874

6975
/// A pubspec wrapper that reports no transformers.

test/cached_package_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:path/path.dart' as p;
6+
import 'package:scheduled_test/scheduled_test.dart';
7+
8+
import 'package:pub/src/cached_package.dart';
9+
import 'package:pub/src/package.dart';
10+
import 'package:pub/src/pubspec.dart';
11+
12+
import 'descriptor.dart' as d;
13+
import 'test_pub.dart';
14+
15+
main() {
16+
// Regression test for https://github.com/dart-lang/pub/issues/1586.
17+
integration('Can list the cached lib dir and compute relative paths', () {
18+
d.dir('cache', [
19+
d.dir('app', [
20+
d.dir('lib', [
21+
d.file('cached.txt', 'hello'),
22+
d.file('original.txt', 'world'),
23+
]),
24+
]),
25+
]).create();
26+
d.dir('app', [
27+
d.dir('lib', [
28+
d.file('original.txt'),
29+
])
30+
]).create();
31+
32+
var cachedPackage = new CachedPackage(
33+
new Package(new Pubspec('a'), p.join(d.defaultRoot, 'app')),
34+
p.join(d.defaultRoot, 'cache', 'app'));
35+
36+
schedule(() {
37+
var paths = cachedPackage.listFiles(beneath: 'lib');
38+
expect(
39+
paths,
40+
unorderedMatches([
41+
endsWith('cached.txt'),
42+
endsWith('original.txt'),
43+
]));
44+
for (var path in paths) {
45+
expect(cachedPackage.relative(path), startsWith('lib'));
46+
}
47+
});
48+
});
49+
}

test/get/cache_transformed_dependency_test.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,44 @@ class HasInputTransformer extends Transformer {
5555
}
5656
""";
5757

58+
const COPY_TRANSFORMER = """
59+
import 'dart:async';
60+
61+
import 'package:barback/barback.dart';
62+
63+
class CopyTransformer extends Transformer {
64+
CopyTransformer.asPlugin();
65+
66+
bool isPrimary(AssetId id) => true;
67+
68+
Future apply(Transform transform) async {
69+
transform.addOutput(new Asset.fromString(
70+
transform.primaryInput.id.addExtension('.copy'),
71+
await transform.primaryInput.readAsString()));
72+
}
73+
}
74+
""";
75+
76+
const LIST_INPUTS_TRANSFORMER = """
77+
import 'dart:async';
78+
79+
import 'package:barback/barback.dart';
80+
81+
class ListInputsTransformer extends AggregateTransformer {
82+
ListInputsTransformer.asPlugin();
83+
84+
String classifyPrimary(AssetId id) => '';
85+
86+
Future apply(AggregateTransform transform) async {
87+
var inputs = await transform.primaryInputs.toList();
88+
var names = inputs.map((asset) => asset.id.toString()).toList();
89+
names.sort();
90+
transform.addOutput(new Asset.fromString(
91+
new AssetId(transform.package, 'lib/inputs.txt'), names.join('\\n')));
92+
}
93+
}
94+
""";
95+
5896
main() {
5997
integration("caches a transformed dependency", () {
6098
servePackages((builder) {
@@ -414,6 +452,48 @@ main() {
414452
d.dir(appPath, [d.nothing(".pub/deps/debug/foo")]).validate();
415453
});
416454

455+
// Regression test for https://github.com/dart-lang/pub/issues/1586.
456+
integration(
457+
"AggregateTransformers can read generated inputs from cached packages",
458+
() {
459+
servePackages((builder) {
460+
builder.serveRealPackage('barback');
461+
462+
builder.serve("foo", "1.2.3", deps: {
463+
'barback': 'any'
464+
}, pubspec: {
465+
'transformers': ['foo/copy_transformer', 'foo/list_transformer'],
466+
}, contents: [
467+
d.dir("lib", [
468+
d.file("hello.dart", "String get hello => 'hello';"),
469+
d.file("copy_transformer.dart", COPY_TRANSFORMER),
470+
d.file("list_transformer.dart", LIST_INPUTS_TRANSFORMER),
471+
])
472+
]);
473+
});
474+
475+
d.appDir({"foo": "1.2.3"}).create();
476+
477+
pubGet(output: contains("Precompiled foo"));
478+
479+
d.dir(appPath, [
480+
d.matcherFile(
481+
".pub/deps/debug/foo/lib/inputs.txt", contains('hello.dart.copy'))
482+
]).validate();
483+
484+
pubServe();
485+
requestShouldSucceed(
486+
"packages/foo/inputs.txt",
487+
"""
488+
foo|lib/copy_transformer.dart
489+
foo|lib/copy_transformer.dart.copy
490+
foo|lib/hello.dart
491+
foo|lib/hello.dart.copy
492+
foo|lib/list_transformer.dart
493+
foo|lib/list_transformer.dart.copy""");
494+
endPubServe();
495+
});
496+
417497
group("with --no-precompile", () {
418498
integration("doesn't cache a transformed dependency", () {
419499
servePackages((builder) {

0 commit comments

Comments
 (0)