OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library analyzer.test.generated.source_factory; |
| 6 |
| 7 import 'dart:convert'; |
| 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 11 import 'package:analyzer/source/package_map_resolver.dart'; |
| 12 import 'package:analyzer/src/generated/java_core.dart'; |
| 13 import 'package:analyzer/src/generated/java_engine_io.dart'; |
| 14 import 'package:analyzer/src/generated/java_io.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/generated/source_io.dart'; |
| 17 import 'package:analyzer/src/generated/utilities_dart.dart' as utils; |
| 18 import 'package:package_config/packages.dart'; |
| 19 import 'package:package_config/packages_file.dart' as pkgfile show parse; |
| 20 import 'package:package_config/src/packages_impl.dart'; |
| 21 import 'package:path/path.dart'; |
| 22 import 'package:unittest/unittest.dart'; |
| 23 |
| 24 import '../reflective_tests.dart'; |
| 25 import '../utils.dart'; |
| 26 import 'test_support.dart'; |
| 27 |
| 28 main() { |
| 29 initializeTestEnvironment(); |
| 30 runReflectiveTests(SourceFactoryTest); |
| 31 runPackageMapTests(); |
| 32 } |
| 33 |
| 34 Source createSource({String path, String uri}) => |
| 35 //TODO(pquitslund): find some way to pass an actual URI into source creation |
| 36 new MemoryResourceProvider() |
| 37 .getFile(path) |
| 38 .createSource(uri != null ? Uri.parse(uri) : null); |
| 39 |
| 40 void runPackageMapTests() { |
| 41 final Uri baseUri = new Uri.file('test/base'); |
| 42 final List<UriResolver> testResolvers = [new FileUriResolver()]; |
| 43 |
| 44 Packages createPackageMap(Uri base, String configFileContents) { |
| 45 List<int> bytes = UTF8.encode(configFileContents); |
| 46 Map<String, Uri> map = pkgfile.parse(bytes, base); |
| 47 return new MapPackages(map); |
| 48 } |
| 49 |
| 50 Map<String, List<Folder>> getPackageMap(String config) { |
| 51 Packages packages = createPackageMap(baseUri, config); |
| 52 SourceFactory factory = new SourceFactory(testResolvers, packages); |
| 53 return factory.packageMap; |
| 54 } |
| 55 |
| 56 String resolvePackageUri( |
| 57 {String uri, |
| 58 String config, |
| 59 Source containingSource, |
| 60 UriResolver customResolver}) { |
| 61 Packages packages = createPackageMap(baseUri, config); |
| 62 List<UriResolver> resolvers = testResolvers.toList(); |
| 63 if (customResolver != null) { |
| 64 resolvers.add(customResolver); |
| 65 } |
| 66 SourceFactory factory = new SourceFactory(resolvers, packages); |
| 67 Source source = factory.resolveUri(containingSource, uri); |
| 68 return source != null ? source.fullName : null; |
| 69 } |
| 70 |
| 71 Uri restorePackageUri( |
| 72 {Source source, String config, UriResolver customResolver}) { |
| 73 Packages packages = createPackageMap(baseUri, config); |
| 74 List<UriResolver> resolvers = testResolvers.toList(); |
| 75 if (customResolver != null) { |
| 76 resolvers.add(customResolver); |
| 77 } |
| 78 SourceFactory factory = new SourceFactory(resolvers, packages); |
| 79 return factory.restoreUri(source); |
| 80 } |
| 81 |
| 82 group('SourceFactoryTest', () { |
| 83 group('package mapping', () { |
| 84 group('resolveUri', () { |
| 85 test('URI in mapping', () { |
| 86 String uri = resolvePackageUri( |
| 87 config: ''' |
| 88 unittest:file:///home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| 89 async:file:///home/somebody/.pub/cache/async-1.1.0/lib/ |
| 90 quiver:file:///home/somebody/.pub/cache/quiver-1.2.1/lib |
| 91 ''', |
| 92 uri: 'package:unittest/unittest.dart'); |
| 93 expect( |
| 94 uri, |
| 95 equals( |
| 96 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart'))
; |
| 97 }); |
| 98 test('URI in mapping (no scheme)', () { |
| 99 String uri = resolvePackageUri( |
| 100 config: ''' |
| 101 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| 102 async:/home/somebody/.pub/cache/async-1.1.0/lib/ |
| 103 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| 104 ''', |
| 105 uri: 'package:unittest/unittest.dart'); |
| 106 expect( |
| 107 uri, |
| 108 equals( |
| 109 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart'))
; |
| 110 }); |
| 111 test('URI not in mapping', () { |
| 112 String uri = resolvePackageUri( |
| 113 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', |
| 114 uri: 'package:foo/foo.dart'); |
| 115 expect(uri, isNull); |
| 116 }); |
| 117 test('Non-package URI', () { |
| 118 var testResolver = new CustomUriResolver(uriPath: 'test_uri'); |
| 119 String uri = resolvePackageUri( |
| 120 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', |
| 121 uri: 'custom:custom.dart', |
| 122 customResolver: testResolver); |
| 123 expect(uri, testResolver.uriPath); |
| 124 }); |
| 125 test('Invalid URI', () { |
| 126 // TODO(pquitslund): fix clients to handle errors appropriately |
| 127 // CLI: print message 'invalid package file format' |
| 128 // SERVER: best case tell user somehow and recover... |
| 129 expect( |
| 130 () => resolvePackageUri( |
| 131 config: 'foo:<:&%>', uri: 'package:foo/bar.dart'), |
| 132 throwsA(new isInstanceOf('FormatException'))); |
| 133 }); |
| 134 test('Valid URI that cannot be further resolved', () { |
| 135 String uri = resolvePackageUri( |
| 136 config: 'foo:http://www.google.com', uri: 'package:foo/bar.dart'); |
| 137 expect(uri, isNull); |
| 138 }); |
| 139 test('Relative URIs', () { |
| 140 Source containingSource = createSource( |
| 141 path: '/foo/bar/baz/foo.dart', uri: 'package:foo/foo.dart'); |
| 142 String uri = resolvePackageUri( |
| 143 config: 'foo:/foo/bar/baz', |
| 144 uri: 'bar.dart', |
| 145 containingSource: containingSource); |
| 146 expect(uri, isNotNull); |
| 147 expect(uri, equals('/foo/bar/baz/bar.dart')); |
| 148 }); |
| 149 }); |
| 150 group('restoreUri', () { |
| 151 test('URI in mapping', () { |
| 152 Uri uri = restorePackageUri( |
| 153 config: ''' |
| 154 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| 155 async:/home/somebody/.pub/cache/async-1.1.0/lib/ |
| 156 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| 157 ''', |
| 158 source: new FileBasedSource(FileUtilities2.createFile( |
| 159 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart'))
); |
| 160 expect(uri, isNotNull); |
| 161 expect(uri.toString(), equals('package:unittest/unittest.dart')); |
| 162 }); |
| 163 }); |
| 164 group('packageMap', () { |
| 165 test('non-file URIs filtered', () { |
| 166 Map<String, List<Folder>> map = getPackageMap(''' |
| 167 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| 168 foo:http://www.google.com |
| 169 '''); |
| 170 expect(map.keys, unorderedEquals(['quiver'])); |
| 171 }); |
| 172 }); |
| 173 }); |
| 174 }); |
| 175 |
| 176 group('URI utils', () { |
| 177 group('URI', () { |
| 178 test('startsWith', () { |
| 179 expect(utils.startsWith(Uri.parse('/foo/bar/'), Uri.parse('/foo/')), |
| 180 isTrue); |
| 181 expect(utils.startsWith(Uri.parse('/foo/bar/'), Uri.parse('/foo/bar/')), |
| 182 isTrue); |
| 183 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')), |
| 184 isFalse); |
| 185 // Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126) |
| 186 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse); |
| 187 expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse); |
| 188 }); |
| 189 }); |
| 190 }); |
| 191 } |
| 192 |
| 193 class CustomUriResolver extends UriResolver { |
| 194 String uriPath; |
| 195 CustomUriResolver({this.uriPath}); |
| 196 |
| 197 @override |
| 198 Source resolveAbsolute(Uri uri, [Uri actualUri]) => |
| 199 createSource(path: uriPath); |
| 200 } |
| 201 |
| 202 @reflectiveTest |
| 203 class SourceFactoryTest { |
| 204 void test_creation() { |
| 205 expect(new SourceFactory([]), isNotNull); |
| 206 } |
| 207 |
| 208 void test_fromEncoding_invalidUri() { |
| 209 SourceFactory factory = new SourceFactory([]); |
| 210 expect(() => factory.fromEncoding("<:&%>"), |
| 211 throwsA(new isInstanceOf<IllegalArgumentException>())); |
| 212 } |
| 213 |
| 214 void test_fromEncoding_noResolver() { |
| 215 SourceFactory factory = new SourceFactory([]); |
| 216 expect(() => factory.fromEncoding("foo:/does/not/exist.dart"), |
| 217 throwsA(new isInstanceOf<IllegalArgumentException>())); |
| 218 } |
| 219 |
| 220 void test_fromEncoding_valid() { |
| 221 String encoding = "file:///does/not/exist.dart"; |
| 222 SourceFactory factory = new SourceFactory( |
| 223 [new UriResolver_SourceFactoryTest_test_fromEncoding_valid(encoding)]); |
| 224 expect(factory.fromEncoding(encoding), isNotNull); |
| 225 } |
| 226 |
| 227 void test_resolveUri_absolute() { |
| 228 UriResolver_absolute resolver = new UriResolver_absolute(); |
| 229 SourceFactory factory = new SourceFactory([resolver]); |
| 230 factory.resolveUri(null, "dart:core"); |
| 231 expect(resolver.invoked, isTrue); |
| 232 } |
| 233 |
| 234 void test_resolveUri_nonAbsolute_absolute() { |
| 235 SourceFactory factory = |
| 236 new SourceFactory([new UriResolver_nonAbsolute_absolute()]); |
| 237 String absolutePath = "/does/not/matter.dart"; |
| 238 Source containingSource = |
| 239 new FileBasedSource(FileUtilities2.createFile("/does/not/exist.dart")); |
| 240 Source result = factory.resolveUri(containingSource, absolutePath); |
| 241 expect(result.fullName, |
| 242 FileUtilities2.createFile(absolutePath).getAbsolutePath()); |
| 243 } |
| 244 |
| 245 void test_resolveUri_nonAbsolute_relative() { |
| 246 SourceFactory factory = |
| 247 new SourceFactory([new UriResolver_nonAbsolute_relative()]); |
| 248 Source containingSource = |
| 249 new FileBasedSource(FileUtilities2.createFile("/does/not/have.dart")); |
| 250 Source result = factory.resolveUri(containingSource, "exist.dart"); |
| 251 expect(result.fullName, |
| 252 FileUtilities2.createFile("/does/not/exist.dart").getAbsolutePath()); |
| 253 } |
| 254 |
| 255 void test_resolveUri_nonAbsolute_relative_package() { |
| 256 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 257 Context context = provider.pathContext; |
| 258 String packagePath = |
| 259 context.joinAll([context.separator, 'path', 'to', 'package']); |
| 260 String libPath = context.joinAll([packagePath, 'lib']); |
| 261 String dirPath = context.joinAll([libPath, 'dir']); |
| 262 String firstPath = context.joinAll([dirPath, 'first.dart']); |
| 263 String secondPath = context.joinAll([dirPath, 'second.dart']); |
| 264 |
| 265 provider.newFolder(packagePath); |
| 266 Folder libFolder = provider.newFolder(libPath); |
| 267 provider.newFolder(dirPath); |
| 268 File firstFile = provider.newFile(firstPath, ''); |
| 269 provider.newFile(secondPath, ''); |
| 270 |
| 271 PackageMapUriResolver resolver = new PackageMapUriResolver(provider, { |
| 272 'package': [libFolder] |
| 273 }); |
| 274 SourceFactory factory = new SourceFactory([resolver]); |
| 275 Source librarySource = |
| 276 firstFile.createSource(Uri.parse('package:package/dir/first.dart')); |
| 277 |
| 278 Source result = factory.resolveUri(librarySource, 'second.dart'); |
| 279 expect(result, isNotNull); |
| 280 expect(result.fullName, secondPath); |
| 281 expect(result.uri.toString(), 'package:package/dir/second.dart'); |
| 282 } |
| 283 |
| 284 void test_restoreUri() { |
| 285 JavaFile file1 = FileUtilities2.createFile("/some/file1.dart"); |
| 286 JavaFile file2 = FileUtilities2.createFile("/some/file2.dart"); |
| 287 Source source1 = new FileBasedSource(file1); |
| 288 Source source2 = new FileBasedSource(file2); |
| 289 Uri expected1 = parseUriWithException("file:///my_file.dart"); |
| 290 SourceFactory factory = |
| 291 new SourceFactory([new UriResolver_restoreUri(source1, expected1)]); |
| 292 expect(factory.restoreUri(source1), same(expected1)); |
| 293 expect(factory.restoreUri(source2), same(null)); |
| 294 } |
| 295 } |
| 296 |
| 297 class UriResolver_absolute extends UriResolver { |
| 298 bool invoked = false; |
| 299 |
| 300 UriResolver_absolute(); |
| 301 |
| 302 @override |
| 303 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 304 invoked = true; |
| 305 return null; |
| 306 } |
| 307 } |
| 308 |
| 309 class UriResolver_nonAbsolute_absolute extends UriResolver { |
| 310 @override |
| 311 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 312 return new FileBasedSource(new JavaFile.fromUri(uri), actualUri); |
| 313 } |
| 314 } |
| 315 |
| 316 class UriResolver_nonAbsolute_relative extends UriResolver { |
| 317 @override |
| 318 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 319 return new FileBasedSource(new JavaFile.fromUri(uri), actualUri); |
| 320 } |
| 321 } |
| 322 |
| 323 class UriResolver_restoreUri extends UriResolver { |
| 324 Source source1; |
| 325 Uri expected1; |
| 326 UriResolver_restoreUri(this.source1, this.expected1); |
| 327 |
| 328 @override |
| 329 Source resolveAbsolute(Uri uri, [Uri actualUri]) => null; |
| 330 |
| 331 @override |
| 332 Uri restoreAbsolute(Source source) { |
| 333 if (identical(source, source1)) { |
| 334 return expected1; |
| 335 } |
| 336 return null; |
| 337 } |
| 338 } |
| 339 |
| 340 class UriResolver_SourceFactoryTest_test_fromEncoding_valid |
| 341 extends UriResolver { |
| 342 String encoding; |
| 343 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); |
| 344 |
| 345 @override |
| 346 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 347 if (uri.toString() == encoding) { |
| 348 return new TestSource(); |
| 349 } |
| 350 return null; |
| 351 } |
| 352 } |
OLD | NEW |