Index: lib/deferred_library_check.dart |
diff --git a/bin/deferred_library_check.dart b/lib/deferred_library_check.dart |
similarity index 61% |
copy from bin/deferred_library_check.dart |
copy to lib/deferred_library_check.dart |
index 0c092c5c5a2f537a50e82f29ee4174ec9dd36e6c..d472eae345809f2ad894f68a587478a8ec759cb2 100644 |
--- a/bin/deferred_library_check.dart |
+++ b/lib/deferred_library_check.dart |
@@ -2,8 +2,6 @@ |
// 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. |
-/// A command-line tool that verifies that deferred libraries split the code as |
-/// expected. |
/// This tool checks that the output from dart2js meets a given specification, |
/// given in a YAML file. The format of the YAML file is: |
/// |
@@ -33,33 +31,24 @@ |
/// The names for parts given in the specification YAML file (besides "main") |
/// are arbitrary and just used for reporting when the output does not meet the |
/// specification. |
-library dart2js_info.bin.deferred_library_check; |
+library dart2js_info.deferred_library_check; |
-import 'dart:async'; |
-import 'dart:convert'; |
-import 'dart:io'; |
- |
-import 'package:dart2js_info/info.dart'; |
+import 'info.dart'; |
import 'package:quiver/collection.dart'; |
-import 'package:yaml/yaml.dart'; |
- |
-Future main(List<String> args) async { |
- if (args.length < 2) { |
- usage(); |
- exit(1); |
- } |
- var info = await infoFromFile(args[0]); |
- var manifest = await manifestFromFile(args[1]); |
+List<ManifestComplianceFailure> checkDeferredLibraryManifest( |
+ AllInfo info, Map manifest) { |
// For each part in the manifest, record the expected "packages" for that |
// part. |
var packages = <String, String>{}; |
for (var part in manifest.keys) { |
for (var package in manifest[part]['packages']) { |
if (packages.containsKey(package)) { |
- print('You cannot specify that package "$package" maps to both parts ' |
+ throw new ArgumentError.value( |
+ manifest, |
+ 'manifest', |
+ 'You cannot specify that package "$package" maps to both parts ' |
'"$part" and "${packages[package]}".'); |
- exit(1); |
} |
packages[package] = part; |
} |
@@ -68,12 +57,12 @@ Future main(List<String> args) async { |
var guessedPartMapping = new BiMap<String, String>(); |
guessedPartMapping['main'] = 'main'; |
- bool anyFailed = false; |
+ var failures = <ManifestComplianceFailure>[]; |
checkInfo(BasicInfo info) { |
- var lib = getLibraryOf(info); |
- if (lib != null && isPackageUri(lib.uri)) { |
- var packageName = getPackageName(lib.uri); |
+ var lib = _getLibraryOf(info); |
+ if (lib != null && _isPackageUri(lib.uri)) { |
+ var packageName = _getPackageName(lib.uri); |
var outputUnitName = info.outputUnit.name; |
var expectedPart; |
if (packages.containsKey(packageName)) { |
@@ -88,14 +77,12 @@ Future main(List<String> args) async { |
if (expectedOutputUnit != outputUnitName) { |
// TODO(het): add options for how to treat unspecified packages |
if (!packages.containsKey(packageName)) { |
- print('"${info.name}" from package "$packageName" was not declared ' |
- 'to be in an explicit part but was not in the main part'); |
+ failures.add(new ManifestComplianceFailure(info.name, packageName)); |
} else { |
var actualPart = guessedPartMapping.inverse[outputUnitName]; |
- print('"${info.name}" from package "$packageName" was specified to ' |
- 'be in part $expectedPart but is in part $actualPart'); |
+ failures.add(new ManifestComplianceFailure( |
+ info.name, packageName, expectedPart, actualPart)); |
} |
- anyFailed = true; |
} |
} |
} |
@@ -103,14 +90,11 @@ Future main(List<String> args) async { |
info.functions.forEach(checkInfo); |
info.fields.forEach(checkInfo); |
- if (anyFailed) { |
- print('The dart2js output did not meet the specification.'); |
- } else { |
- print('The dart2js output meets the specification'); |
- } |
+ |
+ return failures; |
} |
-LibraryInfo getLibraryOf(Info info) { |
+LibraryInfo _getLibraryOf(Info info) { |
var current = info; |
while (current is! LibraryInfo) { |
if (current == null) { |
@@ -121,24 +105,29 @@ LibraryInfo getLibraryOf(Info info) { |
return current; |
} |
-bool isPackageUri(Uri uri) => uri.scheme == 'package'; |
+bool _isPackageUri(Uri uri) => uri.scheme == 'package'; |
-String getPackageName(Uri uri) { |
- assert(isPackageUri(uri)); |
+String _getPackageName(Uri uri) { |
+ assert(_isPackageUri(uri)); |
return uri.pathSegments.first; |
} |
-Future<AllInfo> infoFromFile(String fileName) async { |
- var file = await new File(fileName).readAsString(); |
- return new AllInfoJsonCodec().decode(JSON.decode(file)); |
-} |
+class ManifestComplianceFailure { |
+ final String infoName; |
+ final String packageName; |
+ final String expectedPart; |
+ final String actualPart; |
-Future manifestFromFile(String fileName) async { |
- var file = await new File(fileName).readAsString(); |
- return loadYaml(file); |
-} |
+ const ManifestComplianceFailure(this.infoName, this.packageName, |
+ [this.expectedPart, this.actualPart]); |
-void usage() { |
- print(''' |
-usage: dart2js_info_deferred_library_check dump.info.json manifest.yaml'''); |
+ String toString() { |
+ if (expectedPart == null && actualPart == null) { |
+ return '"$infoName" from package "$packageName" was not declared ' |
+ 'to be in an explicit part but was not in the main part'; |
+ } else { |
+ return '"$infoName" from package "$packageName" was specified to ' |
+ 'be in part $expectedPart but is in part $actualPart'; |
+ } |
+ } |
} |