Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: pkg/analysis_server/test/context_manager_test.dart

Issue 1223413003: Hook for overriding the ContextManager and some code clean-up (with more to follow) (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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:analysis_server/src/source/optimizing_pub_package_map_provider.d art'; 10 import 'package:analysis_server/src/source/optimizing_pub_package_map_provider.d art';
11 import 'package:analysis_server/uri/resolver_provider.dart';
11 import 'package:analyzer/file_system/file_system.dart'; 12 import 'package:analyzer/file_system/file_system.dart';
12 import 'package:analyzer/file_system/memory_file_system.dart'; 13 import 'package:analyzer/file_system/memory_file_system.dart';
13 import 'package:analyzer/instrumentation/instrumentation.dart'; 14 import 'package:analyzer/instrumentation/instrumentation.dart';
14 import 'package:analyzer/source/package_map_resolver.dart'; 15 import 'package:analyzer/source/package_map_resolver.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 16 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/source.dart'; 17 import 'package:analyzer/src/generated/source.dart';
17 import 'package:analyzer/src/generated/source_io.dart'; 18 import 'package:analyzer/src/generated/source_io.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';
23 import 'package:analysis_server/uri/resolver_provider.dart';
24 24
25 main() { 25 main() {
26 groupSep = ' | '; 26 groupSep = ' | ';
27 defineReflectiveTests(ContextManagerTest); 27 defineReflectiveTests(AbstractContextManagerTest);
28 } 28 }
29 29
30 @reflectiveTest 30 @reflectiveTest
31 class ContextManagerTest { 31 class AbstractContextManagerTest {
32 /** 32 /**
33 * The name of the 'bin' directory. 33 * The name of the 'bin' directory.
34 */ 34 */
35 static const String BIN_NAME = 'bin'; 35 static const String BIN_NAME = 'bin';
36 36
37 /** 37 /**
38 * The name of the 'example' directory. 38 * The name of the 'example' directory.
39 */ 39 */
40 static const String EXAMPLE_NAME = 'example'; 40 static const String EXAMPLE_NAME = 'example';
41 41
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 return folderPath; 76 return folderPath;
77 } 77 }
78 78
79 UriResolver providePackageResolver(Folder folder) { 79 UriResolver providePackageResolver(Folder folder) {
80 return packageResolver; 80 return packageResolver;
81 } 81 }
82 82
83 void setUp() { 83 void setUp() {
84 resourceProvider = new MemoryResourceProvider(); 84 resourceProvider = new MemoryResourceProvider();
85 packageMapProvider = new MockPackageMapProvider(); 85 packageMapProvider = new MockPackageMapProvider();
86 manager = new TestContextManager(resourceProvider, providePackageResolver, p ackageMapProvider); 86 manager = new TestContextManager(
87 resourceProvider, providePackageResolver, packageMapProvider);
87 resourceProvider.newFolder(projPath); 88 resourceProvider.newFolder(projPath);
88 } 89 }
89 90
90 test_ignoreFilesInPackagesFolder() { 91 test_ignoreFilesInPackagesFolder() {
91 // create a context with a pubspec.yaml file 92 // create a context with a pubspec.yaml file
92 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); 93 String pubspecPath = posix.join(projPath, 'pubspec.yaml');
93 resourceProvider.newFile(pubspecPath, 'pubspec'); 94 resourceProvider.newFile(pubspecPath, 'pubspec');
94 // create a file in the "packages" folder 95 // create a file in the "packages" folder
95 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); 96 String filePath1 = posix.join(projPath, 'packages', 'file1.dart');
96 resourceProvider.newFile(filePath1, 'contents'); 97 resourceProvider.newFile(filePath1, 'contents');
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } 196 }
196 197
197 void test_setRoots_addFolderWithDartFile() { 198 void test_setRoots_addFolderWithDartFile() {
198 String filePath = posix.join(projPath, 'foo.dart'); 199 String filePath = posix.join(projPath, 'foo.dart');
199 resourceProvider.newFile(filePath, 'contents'); 200 resourceProvider.newFile(filePath, 'contents');
200 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 201 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
201 // verify 202 // verify
202 var filePaths = manager.currentContextFilePaths[projPath]; 203 var filePaths = manager.currentContextFilePaths[projPath];
203 expect(filePaths, hasLength(1)); 204 expect(filePaths, hasLength(1));
204 expect(filePaths, contains(filePath)); 205 expect(filePaths, contains(filePath));
205 List<AnalysisContext> contextsInAnalysisRoot = manager.contextsInAnalysisRoo t(resourceProvider.newFolder(projPath)); 206 List<AnalysisContext> contextsInAnalysisRoot =
207 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath));
206 expect(contextsInAnalysisRoot, hasLength(1)); 208 expect(contextsInAnalysisRoot, hasLength(1));
207 AnalysisContext context = contextsInAnalysisRoot[0]; 209 AnalysisContext context = contextsInAnalysisRoot[0];
208 expect(context, isNotNull); 210 expect(context, isNotNull);
209 Source result = context.sourceFactory.forUri('package:foo/foo.dart'); 211 Source result = context.sourceFactory.forUri('package:foo/foo.dart');
210 expect(result, isNotNull); 212 expect(result, isNotNull);
211 expect(result.exists(), isFalse); 213 expect(result.exists(), isFalse);
212 } 214 }
213 215
214 void test_setRoots_packageResolver() {
215 Uri uri = Uri.parse('package:foo/foo.dart');
216 Source source = new TestSource();
217 packageResolver = new TestUriResolver({uri : source});
218 String filePath = posix.join(projPath, 'foo.dart');
219 resourceProvider.newFile(filePath, 'contents');
220 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
221
222 List<AnalysisContext> contextsInAnalysisRoot = manager.contextsInAnalysisRoo t(resourceProvider.newFolder(projPath));
223 expect(contextsInAnalysisRoot, hasLength(1));
224 AnalysisContext context = contextsInAnalysisRoot[0];
225 expect(context, isNotNull);
226 Source result = context.sourceFactory.forUri2(uri);
227 expect(result, same(source));
228 }
229
230 void test_setRoots_addFolderWithDartFileInSubfolder() { 216 void test_setRoots_addFolderWithDartFileInSubfolder() {
231 String filePath = posix.join(projPath, 'foo', 'bar.dart'); 217 String filePath = posix.join(projPath, 'foo', 'bar.dart');
232 resourceProvider.newFile(filePath, 'contents'); 218 resourceProvider.newFile(filePath, 'contents');
233 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 219 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
234 // verify 220 // verify
235 var filePaths = manager.currentContextFilePaths[projPath]; 221 var filePaths = manager.currentContextFilePaths[projPath];
236 expect(filePaths, hasLength(1)); 222 expect(filePaths, hasLength(1));
237 expect(filePaths, contains(filePath)); 223 expect(filePaths, contains(filePath));
238 } 224 }
239 225
240 void test_setRoots_addFolderWithDummyLink() { 226 void test_setRoots_addFolderWithDummyLink() {
241 String filePath = posix.join(projPath, 'foo.dart'); 227 String filePath = posix.join(projPath, 'foo.dart');
242 resourceProvider.newDummyLink(filePath); 228 resourceProvider.newDummyLink(filePath);
243 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 229 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
244 // verify 230 // verify
245 var filePaths = manager.currentContextFilePaths[projPath]; 231 var filePaths = manager.currentContextFilePaths[projPath];
246 expect(filePaths, isEmpty); 232 expect(filePaths, isEmpty);
247 } 233 }
248 234
249 void test_setRoots_addFolderWithNestedPubspec() { 235 void test_setRoots_addFolderWithNestedPubspec() {
250 String examplePath = newFolder([projPath, EXAMPLE_NAME]); 236 String examplePath = newFolder([projPath, EXAMPLE_NAME]);
251 String libPath = newFolder([projPath, LIB_NAME]); 237 String libPath = newFolder([projPath, LIB_NAME]);
252 238
253 newFile([projPath, PUBSPEC_NAME]); 239 newFile([projPath, AbstractContextManager.PUBSPEC_NAME]);
254 newFile([libPath, 'main.dart']); 240 newFile([libPath, 'main.dart']);
255 newFile([examplePath, PUBSPEC_NAME]); 241 newFile([examplePath, AbstractContextManager.PUBSPEC_NAME]);
256 newFile([examplePath, 'example.dart']); 242 newFile([examplePath, 'example.dart']);
257 243
258 packageMapProvider.packageMap['proj'] = 244 packageMapProvider.packageMap['proj'] =
259 [resourceProvider.getResource(libPath)]; 245 [resourceProvider.getResource(libPath)];
260 246
261 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 247 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
262 248
263 expect(manager.currentContextPaths, hasLength(2)); 249 expect(manager.currentContextPaths, hasLength(2));
264 250
265 expect(manager.currentContextPaths, contains(projPath)); 251 expect(manager.currentContextPaths, contains(projPath));
(...skipping 26 matching lines...) Expand all
292 expect(manager.currentContextPaths, contains(projPath)); 278 expect(manager.currentContextPaths, contains(projPath));
293 expect(manager.currentContextFilePaths[projPath], hasLength(0)); 279 expect(manager.currentContextFilePaths[projPath], hasLength(0));
294 } 280 }
295 281
296 void test_setRoots_addFolderWithPubspecAndLib() { 282 void test_setRoots_addFolderWithPubspecAndLib() {
297 String binPath = newFolder([projPath, BIN_NAME]); 283 String binPath = newFolder([projPath, BIN_NAME]);
298 String libPath = newFolder([projPath, LIB_NAME]); 284 String libPath = newFolder([projPath, LIB_NAME]);
299 String srcPath = newFolder([libPath, SRC_NAME]); 285 String srcPath = newFolder([libPath, SRC_NAME]);
300 String testPath = newFolder([projPath, TEST_NAME]); 286 String testPath = newFolder([projPath, TEST_NAME]);
301 287
302 newFile([projPath, PUBSPEC_NAME]); 288 newFile([projPath, AbstractContextManager.PUBSPEC_NAME]);
303 String appPath = newFile([binPath, 'app.dart']); 289 String appPath = newFile([binPath, 'app.dart']);
304 newFile([libPath, 'main.dart']); 290 newFile([libPath, 'main.dart']);
305 newFile([srcPath, 'internal.dart']); 291 newFile([srcPath, 'internal.dart']);
306 String testFilePath = newFile([testPath, 'main_test.dart']); 292 String testFilePath = newFile([testPath, 'main_test.dart']);
307 293
308 packageMapProvider.packageMap['proj'] = 294 packageMapProvider.packageMap['proj'] =
309 [resourceProvider.getResource(libPath)]; 295 [resourceProvider.getResource(libPath)];
310 296
311 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 297 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
312 Set<Source> sources = manager.currentContextSources[projPath]; 298 Set<Source> sources = manager.currentContextSources[projPath];
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 } 505 }
520 506
521 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { 507 void test_setRoots_newlyAddedFoldersGetProperPackageMap() {
522 String packagePath = '/package/foo'; 508 String packagePath = '/package/foo';
523 Folder packageFolder = resourceProvider.newFolder(packagePath); 509 Folder packageFolder = resourceProvider.newFolder(packagePath);
524 packageMapProvider.packageMap = {'foo': [packageFolder]}; 510 packageMapProvider.packageMap = {'foo': [packageFolder]};
525 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 511 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
526 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); 512 _checkPackageMap(projPath, equals(packageMapProvider.packageMap));
527 } 513 }
528 514
515 void test_setRoots_packageResolver() {
516 Uri uri = Uri.parse('package:foo/foo.dart');
517 Source source = new TestSource();
518 packageResolver = new TestUriResolver({uri: source});
519 String filePath = posix.join(projPath, 'foo.dart');
520 resourceProvider.newFile(filePath, 'contents');
521 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
522
523 List<AnalysisContext> contextsInAnalysisRoot =
524 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath));
525 expect(contextsInAnalysisRoot, hasLength(1));
526 AnalysisContext context = contextsInAnalysisRoot[0];
527 expect(context, isNotNull);
528 Source result = context.sourceFactory.forUri2(uri);
529 expect(result, same(source));
530 }
531
529 void test_setRoots_removeFolderWithoutPubspec() { 532 void test_setRoots_removeFolderWithoutPubspec() {
530 packageMapProvider.packageMap = null; 533 packageMapProvider.packageMap = null;
531 // add one root - there is a context 534 // add one root - there is a context
532 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 535 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
533 expect(manager.currentContextPaths, hasLength(1)); 536 expect(manager.currentContextPaths, hasLength(1));
534 // set empty roots - no contexts 537 // set empty roots - no contexts
535 manager.setRoots(<String>[], <String>[], <String, String>{}); 538 manager.setRoots(<String>[], <String>[], <String, String>{});
536 expect(manager.currentContextPaths, hasLength(0)); 539 expect(manager.currentContextPaths, hasLength(0));
537 expect(manager.currentContextFilePaths, hasLength(0)); 540 expect(manager.currentContextFilePaths, hasLength(0));
538 } 541 }
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 * using a package root maching [expectation]. 971 * using a package root maching [expectation].
969 */ 972 */
970 void _checkPackageRoot(String path, expectation) { 973 void _checkPackageRoot(String path, expectation) {
971 UriResolver resolver = manager.currentContextPackageUriResolvers[path]; 974 UriResolver resolver = manager.currentContextPackageUriResolvers[path];
972 expect(resolver, new isInstanceOf<PackageUriResolver>()); 975 expect(resolver, new isInstanceOf<PackageUriResolver>());
973 PackageUriResolver packageUriResolver = resolver; 976 PackageUriResolver packageUriResolver = resolver;
974 expect(packageUriResolver.packagesDirectory_forTesting, expectation); 977 expect(packageUriResolver.packagesDirectory_forTesting, expectation);
975 } 978 }
976 } 979 }
977 980
978 class TestContextManager extends ContextManager { 981 class TestContextManager extends AbstractContextManager {
979 /** 982 /**
980 * Source of timestamps stored in [currentContextFilePaths]. 983 * Source of timestamps stored in [currentContextFilePaths].
981 */ 984 */
982 int now = 0; 985 int now = 0;
983 986
984 /** 987 /**
985 * The analysis context that was created. 988 * The analysis context that was created.
986 */ 989 */
987 AnalysisContext currentContext; 990 AnalysisContext currentContext;
988 991
(...skipping 15 matching lines...) Expand all
1004 final Map<String, Set<Source>> currentContextSources = <String, Set<Source>>{ 1007 final Map<String, Set<Source>> currentContextSources = <String, Set<Source>>{
1005 }; 1008 };
1006 1009
1007 /** 1010 /**
1008 * Map from context to package URI resolver. 1011 * Map from context to package URI resolver.
1009 */ 1012 */
1010 final Map<String, UriResolver> currentContextPackageUriResolvers = 1013 final Map<String, UriResolver> currentContextPackageUriResolvers =
1011 <String, UriResolver>{}; 1014 <String, UriResolver>{};
1012 1015
1013 TestContextManager(MemoryResourceProvider resourceProvider, 1016 TestContextManager(MemoryResourceProvider resourceProvider,
1014 ResolverProvider packageResolverProvider, 1017 ResolverProvider packageResolverProvider,
1015 OptimizingPubPackageMapProvider packageMapProvider) 1018 OptimizingPubPackageMapProvider packageMapProvider)
1016 : super(resourceProvider, packageResolverProvider, packageMapProvider, 1019 : super(resourceProvider, packageResolverProvider, packageMapProvider,
1017 InstrumentationService.NULL_SERVICE); 1020 InstrumentationService.NULL_SERVICE);
1018 1021
1019 /** 1022 /**
1020 * Iterable of the paths to contexts that currently exist. 1023 * Iterable of the paths to contexts that currently exist.
1021 */ 1024 */
1022 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; 1025 Iterable<String> get currentContextPaths => currentContextTimestamps.keys;
1023 1026
1024 @override 1027 @override
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 return file.exists; 1094 return file.exists;
1092 } 1095 }
1093 1096
1094 @override 1097 @override
1095 void updateContextPackageUriResolver( 1098 void updateContextPackageUriResolver(
1096 Folder contextFolder, UriResolver packageUriResolver) { 1099 Folder contextFolder, UriResolver packageUriResolver) {
1097 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; 1100 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver;
1098 } 1101 }
1099 } 1102 }
1100 1103
1104 /**
1105 * A [Source] that knows it's [fullName].
1106 */
1107 class TestSource implements Source {
1108 TestSource();
1109
1110 @override
1111 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1112 }
1113
1101 class TestUriResolver extends UriResolver { 1114 class TestUriResolver extends UriResolver {
1102 Map<Uri, Source> uriMap; 1115 Map<Uri, Source> uriMap;
1103 1116
1104 TestUriResolver(this.uriMap); 1117 TestUriResolver(this.uriMap);
1105 1118
1106 @override 1119 @override
1107 Source resolveAbsolute(Uri uri) { 1120 Source resolveAbsolute(Uri uri) {
1108 return uriMap[uri]; 1121 return uriMap[uri];
1109 } 1122 }
1110 } 1123 }
1111
1112 /**
1113 * A [Source] that knows it's [fullName].
1114 */
1115 class TestSource implements Source {
1116 TestSource();
1117
1118 @override
1119 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698