| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:front_end/compiler_options.dart'; | 7 import 'package:front_end/compiler_options.dart'; |
| 8 import 'package:front_end/dependency_grapher.dart'; | 8 import 'package:front_end/dependency_grapher.dart'; |
| 9 import 'package:front_end/memory_file_system.dart'; | 9 import 'package:front_end/memory_file_system.dart'; |
| 10 import 'package:path/path.dart' as pathos; | 10 import 'package:path/path.dart' as pathos; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 {List<String> dependencies: const [], List<String> parts: const []}) { | 23 {List<String> dependencies: const [], List<String> parts: const []}) { |
| 24 var library = cycle.libraries[Uri.parse(uri)]; | 24 var library = cycle.libraries[Uri.parse(uri)]; |
| 25 expect('${library.uri}', uri); | 25 expect('${library.uri}', uri); |
| 26 expect(library.dependencies.map((dep) => '${dep.uri}'), | 26 expect(library.dependencies.map((dep) => '${dep.uri}'), |
| 27 unorderedEquals(dependencies)); | 27 unorderedEquals(dependencies)); |
| 28 expect(library.parts.map((part) => '$part'), unorderedEquals(parts)); | 28 expect(library.parts.map((part) => '$part'), unorderedEquals(parts)); |
| 29 return library; | 29 return library; |
| 30 } | 30 } |
| 31 | 31 |
| 32 Future<List<LibraryCycleNode>> getCycles(Map<String, String> contents, | 32 Future<List<LibraryCycleNode>> getCycles(Map<String, String> contents, |
| 33 [List<String> startingPoints]) async { | 33 {List<String> startingPoints, String packagesFilePath = ''}) async { |
| 34 // If no starting points given, assume the first entry in [contents] is the | 34 // If no starting points given, assume the first entry in [contents] is the |
| 35 // single starting point. | 35 // single starting point. |
| 36 startingPoints ??= [contents.keys.first]; | 36 startingPoints ??= [contents.keys.first]; |
| 37 var fileSystem = new MemoryFileSystem(pathos.posix, '/'); | 37 var fileSystem = new MemoryFileSystem(pathos.posix, '/'); |
| 38 contents.forEach((path, text) { | 38 contents.forEach((path, text) { |
| 39 fileSystem.entityForPath(path).writeAsStringSync(text); | 39 fileSystem.entityForPath(path).writeAsStringSync(text); |
| 40 }); | 40 }); |
| 41 // TODO(paulberry): implement and test other option possibilities. | 41 // TODO(paulberry): implement and test other option possibilities. |
| 42 var options = new CompilerOptions() | 42 var options = new CompilerOptions() |
| 43 ..fileSystem = fileSystem | 43 ..fileSystem = fileSystem |
| 44 ..chaseDependencies = true; | 44 ..chaseDependencies = true |
| 45 ..packagesFilePath = packagesFilePath; |
| 45 var graph = await graphForProgram( | 46 var graph = await graphForProgram( |
| 46 startingPoints.map(pathos.posix.toUri).toList(), options); | 47 startingPoints.map(pathos.posix.toUri).toList(), options); |
| 47 return graph.topologicallySortedCycles; | 48 return graph.topologicallySortedCycles; |
| 48 } | 49 } |
| 49 | 50 |
| 50 /// Sort the given library cycles into a deterministic order based on their | 51 /// Sort the given library cycles into a deterministic order based on their |
| 51 /// URIs for easier unit testing. | 52 /// URIs for easier unit testing. |
| 52 List<LibraryCycleNode> sortCycles(Iterable<LibraryCycleNode> cycles) { | 53 List<LibraryCycleNode> sortCycles(Iterable<LibraryCycleNode> cycles) { |
| 53 var result = cycles.toList(); | 54 var result = cycles.toList(); |
| 54 String sortKey(LibraryCycleNode node) => node.libraries.keys.join(','); | 55 String sortKey(LibraryCycleNode node) => node.libraries.keys.join(','); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 76 expect(cycles[1].libraries, hasLength(1)); | 77 expect(cycles[1].libraries, hasLength(1)); |
| 77 checkLibrary(cycles[1], 'file:///foo.dart', | 78 checkLibrary(cycles[1], 'file:///foo.dart', |
| 78 dependencies: ['file:///bar.dart']); | 79 dependencies: ['file:///bar.dart']); |
| 79 } | 80 } |
| 80 | 81 |
| 81 test_multipleStartingPoints() async { | 82 test_multipleStartingPoints() async { |
| 82 var cycles = await getCycles({ | 83 var cycles = await getCycles({ |
| 83 '/a.dart': 'import "c.dart";', | 84 '/a.dart': 'import "c.dart";', |
| 84 '/b.dart': 'import "c.dart";', | 85 '/b.dart': 'import "c.dart";', |
| 85 '/c.dart': '' | 86 '/c.dart': '' |
| 86 }, [ | 87 }, startingPoints: [ |
| 87 '/a.dart', | 88 '/a.dart', |
| 88 '/b.dart' | 89 '/b.dart' |
| 89 ]); | 90 ]); |
| 90 expect(cycles, hasLength(3)); | 91 expect(cycles, hasLength(3)); |
| 91 expect(cycles[0].libraries, hasLength(1)); | 92 expect(cycles[0].libraries, hasLength(1)); |
| 92 checkLibrary(cycles[0], 'file:///c.dart'); | 93 checkLibrary(cycles[0], 'file:///c.dart'); |
| 93 // The other two cycles might be in any order, so sort them for | 94 // The other two cycles might be in any order, so sort them for |
| 94 // reproducibility. | 95 // reproducibility. |
| 95 List<LibraryCycleNode> otherCycles = sortCycles(cycles.sublist(1)); | 96 List<LibraryCycleNode> otherCycles = sortCycles(cycles.sublist(1)); |
| 96 checkLibrary(otherCycles[0], 'file:///a.dart', | 97 checkLibrary(otherCycles[0], 'file:///a.dart', |
| 97 dependencies: ['file:///c.dart']); | 98 dependencies: ['file:///c.dart']); |
| 98 checkLibrary(otherCycles[1], 'file:///b.dart', | 99 checkLibrary(otherCycles[1], 'file:///b.dart', |
| 99 dependencies: ['file:///c.dart']); | 100 dependencies: ['file:///c.dart']); |
| 100 } | 101 } |
| 101 | 102 |
| 103 test_packages() async { |
| 104 var cycles = await getCycles({ |
| 105 '/foo.dart': 'import "package:foo/bar.dart";', |
| 106 '/.packages': 'foo:pkg/foo/lib\nbar:pkg/bar/lib\n', |
| 107 '/pkg/foo/lib/bar.dart': 'import "package:bar/baz.dart";', |
| 108 '/pkg/bar/lib/baz.dart': '' |
| 109 }, packagesFilePath: '/.packages'); |
| 110 expect(cycles, hasLength(3)); |
| 111 expect(cycles[0].libraries, hasLength(1)); |
| 112 checkLibrary(cycles[0], 'package:bar/baz.dart'); |
| 113 expect(cycles[1].libraries, hasLength(1)); |
| 114 checkLibrary(cycles[1], 'package:foo/bar.dart', |
| 115 dependencies: ['package:bar/baz.dart']); |
| 116 expect(cycles[2].libraries, hasLength(1)); |
| 117 checkLibrary(cycles[2], 'file:///foo.dart', |
| 118 dependencies: ['package:foo/bar.dart']); |
| 119 } |
| 120 |
| 102 test_parts() async { | 121 test_parts() async { |
| 103 var cycles = await getCycles({ | 122 var cycles = await getCycles({ |
| 104 '/foo.dart': 'library foo; part "a.dart"; part "b.dart";', | 123 '/foo.dart': 'library foo; part "a.dart"; part "b.dart";', |
| 105 '/a.dart': 'part of foo;', | 124 '/a.dart': 'part of foo;', |
| 106 '/b.dart': 'part of foo;' | 125 '/b.dart': 'part of foo;' |
| 107 }); | 126 }); |
| 108 expect(cycles, hasLength(1)); | 127 expect(cycles, hasLength(1)); |
| 109 expect(cycles[0].libraries, hasLength(1)); | 128 expect(cycles[0].libraries, hasLength(1)); |
| 110 checkLibrary(cycles[0], 'file:///foo.dart', | 129 checkLibrary(cycles[0], 'file:///foo.dart', |
| 111 parts: ['file:///a.dart', 'file:///b.dart']); | 130 parts: ['file:///a.dart', 'file:///b.dart']); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 expect(bar.dependencies[0], same(foo)); | 164 expect(bar.dependencies[0], same(foo)); |
| 146 } | 165 } |
| 147 | 166 |
| 148 test_singleFile() async { | 167 test_singleFile() async { |
| 149 var cycles = await getCycles({'/foo.dart': ''}); | 168 var cycles = await getCycles({'/foo.dart': ''}); |
| 150 expect(cycles, hasLength(1)); | 169 expect(cycles, hasLength(1)); |
| 151 expect(cycles[0].libraries, hasLength(1)); | 170 expect(cycles[0].libraries, hasLength(1)); |
| 152 checkLibrary(cycles[0], 'file:///foo.dart'); | 171 checkLibrary(cycles[0], 'file:///foo.dart'); |
| 153 } | 172 } |
| 154 } | 173 } |
| OLD | NEW |