| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library test.context.directory.manager; | 5 library test.context.directory.manager; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/context_manager.dart'; | 9 import 'package:analysis_server/src/context_manager.dart'; |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| 11 import 'package:analyzer/file_system/memory_file_system.dart'; | 11 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 12 import 'package:analyzer/instrumentation/instrumentation.dart'; | 12 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 13 import 'package:analyzer/source/embedder.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/error.dart'; | 15 import 'package:analyzer/src/generated/error.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/generated/source_io.dart'; | 17 import 'package:analyzer/src/generated/source_io.dart'; |
| 17 import 'package:package_config/packages.dart'; | 18 import 'package:package_config/packages.dart'; |
| 18 import 'package:path/path.dart'; | 19 import 'package:path/path.dart'; |
| 19 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 20 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 20 import 'package:unittest/unittest.dart'; | 21 import 'package:unittest/unittest.dart'; |
| 21 | 22 |
| 22 import 'mocks.dart'; | 23 import 'mocks.dart'; |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 callbacks.now++; | 561 callbacks.now++; |
| 561 manager.refresh([resourceProvider.getResource(proj2Path)]); | 562 manager.refresh([resourceProvider.getResource(proj2Path)]); |
| 562 return pumpEventQueue().then((_) { | 563 return pumpEventQueue().then((_) { |
| 563 expect(callbacks.currentContextPaths.toList(), unorderedEquals(roots)); | 564 expect(callbacks.currentContextPaths.toList(), unorderedEquals(roots)); |
| 564 expect(callbacks.currentContextTimestamps[projPath], then); | 565 expect(callbacks.currentContextTimestamps[projPath], then); |
| 565 expect(callbacks.currentContextTimestamps[proj2Path], callbacks.now); | 566 expect(callbacks.currentContextTimestamps[proj2Path], callbacks.now); |
| 566 }); | 567 }); |
| 567 }); | 568 }); |
| 568 } | 569 } |
| 569 | 570 |
| 571 test_embedder_packagespec() async { |
| 572 // Create files. |
| 573 String libPath = newFolder([projPath, LIB_NAME]); |
| 574 newFile([libPath, 'main.dart']); |
| 575 newFile([libPath, 'nope.dart']); |
| 576 String sdkExtPath = newFolder([projPath, 'sdk_ext']); |
| 577 newFile([sdkExtPath, 'entry.dart']); |
| 578 String sdkExtSrcPath = newFolder([projPath, 'sdk_ext', 'src']); |
| 579 newFile([sdkExtSrcPath, 'part.dart']); |
| 580 // Setup _embedder.yaml. |
| 581 newFile( |
| 582 [libPath, '_embedder.yaml'], |
| 583 r''' |
| 584 embedder_libs: |
| 585 "dart:foobar": "../sdk_ext/entry.dart" |
| 586 "dart:typed_data": "../sdk_ext/src/part" |
| 587 '''); |
| 588 // Setup .packages file |
| 589 newFile( |
| 590 [projPath, '.packages'], |
| 591 r''' |
| 592 test_pack:lib/'''); |
| 593 // Setup context. |
| 594 |
| 595 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 596 await pumpEventQueue(); |
| 597 // Confirm that one context was created. |
| 598 var contexts = |
| 599 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath)); |
| 600 expect(contexts, isNotNull); |
| 601 expect(contexts.length, equals(1)); |
| 602 var context = contexts[0]; |
| 603 var source = context.sourceFactory.forUri('dart:foobar'); |
| 604 expect(source, isNotNull); |
| 605 expect(source.fullName, equals('/my/proj/sdk_ext/entry.dart')); |
| 606 // We can't find dart:core because we didn't list it in our |
| 607 // embedder_libs map. |
| 608 expect(context.sourceFactory.forUri('dart:core'), isNull); |
| 609 // We can find dart:typed_data because we listed it in our |
| 610 // embedder_libs map. |
| 611 expect(context.sourceFactory.forUri('dart:typed_data'), isNotNull); |
| 612 } |
| 613 |
| 570 test_sdk_ext_packagespec() async { | 614 test_sdk_ext_packagespec() async { |
| 571 // Create files. | 615 // Create files. |
| 572 String libPath = newFolder([projPath, LIB_NAME]); | 616 String libPath = newFolder([projPath, LIB_NAME]); |
| 573 newFile([libPath, 'main.dart']); | 617 newFile([libPath, 'main.dart']); |
| 574 newFile([libPath, 'nope.dart']); | 618 newFile([libPath, 'nope.dart']); |
| 575 String sdkExtPath = newFolder([projPath, 'sdk_ext']); | 619 String sdkExtPath = newFolder([projPath, 'sdk_ext']); |
| 576 newFile([sdkExtPath, 'entry.dart']); | 620 newFile([sdkExtPath, 'entry.dart']); |
| 577 String sdkExtSrcPath = newFolder([projPath, 'sdk_ext', 'src']); | 621 String sdkExtSrcPath = newFolder([projPath, 'sdk_ext', 'src']); |
| 578 newFile([sdkExtSrcPath, 'part.dart']); | 622 newFile([sdkExtSrcPath, 'part.dart']); |
| 579 // Setup sdk extension mapping. | 623 // Setup sdk extension mapping. |
| 580 newFile( | 624 newFile( |
| 581 [libPath, '_sdkext'], | 625 [libPath, '_sdkext'], |
| 582 r''' | 626 r''' |
| 583 { | 627 { |
| 584 "dart:foobar": "../sdk_ext/entry.dart" | 628 "dart:foobar": "../sdk_ext/entry.dart" |
| 585 } | 629 } |
| 586 '''); | 630 '''); |
| 587 // Setup .packages file | 631 // Setup .packages file |
| 588 newFile( | 632 newFile( |
| 589 [projPath, '.packages'], | 633 [projPath, '.packages'], |
| 590 r''' | 634 r''' |
| 591 test_pack:lib/ | 635 test_pack:lib/'''); |
| 592 '''); | |
| 593 // Setup context. | 636 // Setup context. |
| 594 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 637 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 595 // Confirm that one context was created. | 638 // Confirm that one context was created. |
| 596 var contexts = | 639 var contexts = |
| 597 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath)); | 640 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath)); |
| 598 expect(contexts, isNotNull); | 641 expect(contexts, isNotNull); |
| 599 expect(contexts.length, equals(1)); | 642 expect(contexts.length, equals(1)); |
| 600 var context = contexts[0]; | 643 var context = contexts[0]; |
| 601 var source = context.sourceFactory.forUri('dart:foobar'); | 644 var source = context.sourceFactory.forUri('dart:foobar'); |
| 602 expect(source.fullName, equals('/my/proj/sdk_ext/entry.dart')); | 645 expect(source.fullName, equals('/my/proj/sdk_ext/entry.dart')); |
| (...skipping 1288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1891 */ | 1934 */ |
| 1892 final ResourceProvider resourceProvider; | 1935 final ResourceProvider resourceProvider; |
| 1893 | 1936 |
| 1894 TestContextManagerCallbacks(this.resourceProvider); | 1937 TestContextManagerCallbacks(this.resourceProvider); |
| 1895 | 1938 |
| 1896 /** | 1939 /** |
| 1897 * Iterable of the paths to contexts that currently exist. | 1940 * Iterable of the paths to contexts that currently exist. |
| 1898 */ | 1941 */ |
| 1899 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; | 1942 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; |
| 1900 | 1943 |
| 1944 /// If [disposition] has a package map, attempt to locate `_embedder.yaml` |
| 1945 /// files. |
| 1946 void _locateEmbedderYamls(AnalysisContext context, |
| 1947 FolderDisposition disposition) { |
| 1948 Map<String, List<Folder>> packageMap; |
| 1949 if (disposition is PackageMapDisposition) { |
| 1950 packageMap = disposition.packageMap; |
| 1951 } else if (disposition is PackagesFileDisposition) { |
| 1952 packageMap = disposition.buildPackageMap(resourceProvider); |
| 1953 } |
| 1954 context.embedderYamlLocator.refresh(packageMap); |
| 1955 } |
| 1956 |
| 1901 @override | 1957 @override |
| 1902 AnalysisContext addContext(Folder folder, FolderDisposition disposition) { | 1958 AnalysisContext addContext(Folder folder, FolderDisposition disposition) { |
| 1903 String path = folder.path; | 1959 String path = folder.path; |
| 1904 expect(currentContextPaths, isNot(contains(path))); | 1960 expect(currentContextPaths, isNot(contains(path))); |
| 1905 currentContextTimestamps[path] = now; | 1961 currentContextTimestamps[path] = now; |
| 1906 currentContextFilePaths[path] = <String, int>{}; | 1962 currentContextFilePaths[path] = <String, int>{}; |
| 1907 currentContextSources[path] = new HashSet<Source>(); | 1963 currentContextSources[path] = new HashSet<Source>(); |
| 1908 currentContextDispositions[path] = disposition; | 1964 currentContextDispositions[path] = disposition; |
| 1909 currentContext = AnalysisEngine.instance.createAnalysisContext(); | 1965 currentContext = AnalysisEngine.instance.createAnalysisContext(); |
| 1966 _locateEmbedderYamls(currentContext, disposition); |
| 1910 List<UriResolver> resolvers = []; | 1967 List<UriResolver> resolvers = []; |
| 1968 EmbedderYamlLocator embedderYamlLocator = currentContext.embedderYamlLocator
; |
| 1969 EmbedderUriResolver embedderUriResolver = |
| 1970 new EmbedderUriResolver(embedderYamlLocator.embedderYamls); |
| 1971 if (embedderUriResolver.length > 0) { |
| 1972 // We have some embedder dart: uri mappings, add the resolver |
| 1973 // to the list. |
| 1974 resolvers.add(embedderUriResolver); |
| 1975 } |
| 1911 resolvers.addAll(disposition.createPackageUriResolvers(resourceProvider)); | 1976 resolvers.addAll(disposition.createPackageUriResolvers(resourceProvider)); |
| 1912 resolvers.add(new FileUriResolver()); | 1977 resolvers.add(new FileUriResolver()); |
| 1913 currentContext.sourceFactory = | 1978 currentContext.sourceFactory = |
| 1914 new SourceFactory(resolvers, disposition.packages); | 1979 new SourceFactory(resolvers, disposition.packages); |
| 1915 return currentContext; | 1980 return currentContext; |
| 1916 } | 1981 } |
| 1917 | 1982 |
| 1918 @override | 1983 @override |
| 1919 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { | 1984 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { |
| 1920 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; | 1985 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1991 class TestUriResolver extends UriResolver { | 2056 class TestUriResolver extends UriResolver { |
| 1992 Map<Uri, Source> uriMap; | 2057 Map<Uri, Source> uriMap; |
| 1993 | 2058 |
| 1994 TestUriResolver(this.uriMap); | 2059 TestUriResolver(this.uriMap); |
| 1995 | 2060 |
| 1996 @override | 2061 @override |
| 1997 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 2062 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 1998 return uriMap[uri]; | 2063 return uriMap[uri]; |
| 1999 } | 2064 } |
| 2000 } | 2065 } |
| OLD | NEW |