OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS d.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 library pub_tests; | 1 library pub_tests; |
6 | |
7 import 'package:path/path.dart' as p; | 2 import 'package:path/path.dart' as p; |
8 import 'package:scheduled_test/scheduled_test.dart'; | 3 import 'package:scheduled_test/scheduled_test.dart'; |
9 | |
10 import '../../lib/src/barback/cycle_exception.dart'; | 4 import '../../lib/src/barback/cycle_exception.dart'; |
11 import '../../lib/src/barback/transformers_needed_by_transformers.dart'; | 5 import '../../lib/src/barback/transformers_needed_by_transformers.dart'; |
12 import '../../lib/src/entrypoint.dart'; | 6 import '../../lib/src/entrypoint.dart'; |
13 import '../../lib/src/io.dart'; | 7 import '../../lib/src/io.dart'; |
14 import '../../lib/src/package.dart'; | 8 import '../../lib/src/package.dart'; |
15 import '../../lib/src/package_graph.dart'; | 9 import '../../lib/src/package_graph.dart'; |
16 import '../../lib/src/source/path.dart'; | 10 import '../../lib/src/source/path.dart'; |
17 import '../../lib/src/system_cache.dart'; | 11 import '../../lib/src/system_cache.dart'; |
18 import '../../lib/src/utils.dart'; | 12 import '../../lib/src/utils.dart'; |
19 import '../test_pub.dart'; | 13 import '../test_pub.dart'; |
20 | |
21 /// Expects that [computeTransformersNeededByTransformers] will return a graph | |
22 /// matching [expected] when run on the package graph defined by packages in | |
23 /// the sandbox. | |
24 void expectDependencies(Map<String, Iterable<String>> expected) { | 14 void expectDependencies(Map<String, Iterable<String>> expected) { |
25 expected = mapMap(expected, value: (_, ids) => ids.toSet()); | 15 expected = mapMap(expected, value: (_, ids) => ids.toSet()); |
26 | |
27 schedule(() { | 16 schedule(() { |
28 var result = mapMap( | 17 var result = mapMap( |
29 computeTransformersNeededByTransformers(_loadPackageGraph()), | 18 computeTransformersNeededByTransformers(_loadPackageGraph()), |
30 key: (id, _) => id.toString(), | 19 key: (id, _) => id.toString(), |
31 value: (_, ids) => ids.map((id) => id.toString()).toSet()); | 20 value: (_, ids) => ids.map((id) => id.toString()).toSet()); |
32 expect(result, equals(expected)); | 21 expect(result, equals(expected)); |
33 }, "expect dependencies to match $expected"); | 22 }, "expect dependencies to match $expected"); |
34 } | 23 } |
35 | |
36 /// Expects that [computeTransformersNeededByTransformers] will throw an | |
37 /// exception matching [matcher] when run on the package graph defiend by | |
38 /// packages in the sandbox. | |
39 void expectException(matcher) { | 24 void expectException(matcher) { |
40 schedule(() { | 25 schedule(() { |
41 expect(() => computeTransformersNeededByTransformers(_loadPackageGraph()), | 26 expect( |
| 27 () => computeTransformersNeededByTransformers(_loadPackageGraph()), |
42 throwsA(matcher)); | 28 throwsA(matcher)); |
43 }, "expect an exception: $matcher"); | 29 }, "expect an exception: $matcher"); |
44 } | 30 } |
45 | |
46 /// Expects that [computeTransformersNeededByTransformers] will throw a | |
47 /// [CycleException] with the given [steps] when run on the package graph | |
48 /// defiend by packages in the sandbox. | |
49 void expectCycleException(Iterable<String> steps) { | 31 void expectCycleException(Iterable<String> steps) { |
50 expectException(predicate((error) { | 32 expectException(predicate((error) { |
51 expect(error, new isInstanceOf<CycleException>()); | 33 expect(error, new isInstanceOf<CycleException>()); |
52 expect(error.steps, equals(steps)); | 34 expect(error.steps, equals(steps)); |
53 return true; | 35 return true; |
54 }, "cycle exception:\n${steps.map((step) => " $step").join("\n")}")); | 36 }, "cycle exception:\n${steps.map((step) => " $step").join("\n")}")); |
55 } | 37 } |
56 | |
57 /// Loads a [PackageGraph] from the packages in the sandbox. | |
58 /// | |
59 /// This graph will also include barback and its transitive dependencies from | |
60 /// the repo. | |
61 PackageGraph _loadPackageGraph() { | 38 PackageGraph _loadPackageGraph() { |
62 // Load the sandbox packages. | |
63 var packages = {}; | 39 var packages = {}; |
64 | |
65 var systemCache = new SystemCache(p.join(sandboxDir, cachePath)); | 40 var systemCache = new SystemCache(p.join(sandboxDir, cachePath)); |
66 systemCache.sources | 41 systemCache.sources |
67 ..register(new PathSource()) | 42 ..register(new PathSource()) |
68 ..setDefault('path'); | 43 ..setDefault('path'); |
69 var entrypoint = new Entrypoint(p.join(sandboxDir, appPath), systemCache); | 44 var entrypoint = new Entrypoint(p.join(sandboxDir, appPath), systemCache); |
70 | |
71 for (var package in listDir(sandboxDir)) { | 45 for (var package in listDir(sandboxDir)) { |
72 if (!fileExists(p.join(package, 'pubspec.yaml'))) continue; | 46 if (!fileExists(p.join(package, 'pubspec.yaml'))) continue; |
73 var packageName = p.basename(package); | 47 var packageName = p.basename(package); |
74 packages[packageName] = new Package.load( | 48 packages[packageName] = |
75 packageName, package, systemCache.sources); | 49 new Package.load(packageName, package, systemCache.sources); |
76 } | 50 } |
77 | |
78 loadPackage(packageName) { | 51 loadPackage(packageName) { |
79 if (packages.containsKey(packageName)) return; | 52 if (packages.containsKey(packageName)) return; |
80 packages[packageName] = new Package.load( | 53 packages[packageName] = new Package.load( |
81 packageName, p.join(pkgPath, packageName), systemCache.sources); | 54 packageName, |
| 55 p.join(pkgPath, packageName), |
| 56 systemCache.sources); |
82 for (var dep in packages[packageName].dependencies) { | 57 for (var dep in packages[packageName].dependencies) { |
83 loadPackage(dep.name); | 58 loadPackage(dep.name); |
84 } | 59 } |
85 } | 60 } |
86 | |
87 loadPackage('barback'); | 61 loadPackage('barback'); |
88 | |
89 return new PackageGraph(entrypoint, null, packages); | 62 return new PackageGraph(entrypoint, null, packages); |
90 } | 63 } |
91 | |
92 /// Returns the contents of a no-op transformer that imports each URL in | |
93 /// [imports]. | |
94 String transformer([Iterable<String> imports]) { | 64 String transformer([Iterable<String> imports]) { |
95 if (imports == null) imports = []; | 65 if (imports == null) imports = []; |
96 | 66 var buffer = |
97 var buffer = new StringBuffer() | 67 new StringBuffer()..writeln('import "package:barback/barback.dart";'); |
98 ..writeln('import "package:barback/barback.dart";'); | |
99 for (var import in imports) { | 68 for (var import in imports) { |
100 buffer.writeln('import "$import";'); | 69 buffer.writeln('import "$import";'); |
101 } | 70 } |
102 | |
103 buffer.writeln(""" | 71 buffer.writeln(""" |
104 NoOpTransformer extends Transformer { | 72 NoOpTransformer extends Transformer { |
105 bool isPrimary(AssetId id) => true; | 73 bool isPrimary(AssetId id) => true; |
106 void apply(Transform transform) {} | 74 void apply(Transform transform) {} |
107 } | 75 } |
108 """); | 76 """); |
109 | |
110 return buffer.toString(); | 77 return buffer.toString(); |
111 } | 78 } |
OLD | NEW |