| 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 library test.analysis_server.src.single_context_manager; | 5 library test.analysis_server.src.single_context_manager; |
| 6 | 6 |
| 7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/single_context_manager.dart'; | 9 import 'package:analysis_server/src/single_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/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/java_io.dart'; | 13 import 'package:analyzer/src/generated/java_io.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart'; | 14 import 'package:analyzer/src/generated/sdk.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/generated/source_io.dart'; | 16 import 'package:analyzer/src/generated/source_io.dart'; |
| 17 import 'package:analyzer/src/util/glob.dart'; | 17 import 'package:analyzer/src/util/glob.dart'; |
| 18 import 'package:linter/src/plugin/linter_plugin.dart'; | 18 import 'package:linter/src/plugin/linter_plugin.dart'; |
| 19 import 'package:path/path.dart'; | 19 import 'package:path/path.dart'; |
| 20 import 'package:plugin/manager.dart'; | 20 import 'package:plugin/manager.dart'; |
| 21 import 'package:plugin/plugin.dart'; | 21 import 'package:plugin/plugin.dart'; |
| 22 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 22 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 23 import 'package:unittest/unittest.dart'; | 23 import 'package:unittest/unittest.dart'; |
| 24 | 24 |
| 25 import 'context_manager_test.dart' show TestContextManagerCallbacks; | 25 import 'context_manager_test.dart' show TestContextManagerCallbacks; |
| 26 import 'mock_sdk.dart'; | 26 import 'mock_sdk.dart'; |
| 27 import 'mocks.dart'; |
| 27 import 'utils.dart'; | 28 import 'utils.dart'; |
| 28 | 29 |
| 29 main() { | 30 main() { |
| 30 initializeTestEnvironment(); | 31 initializeTestEnvironment(); |
| 31 defineReflectiveTests(SingleContextManagerTest); | 32 defineReflectiveTests(SingleContextManagerTest); |
| 32 } | 33 } |
| 33 | 34 |
| 34 @reflectiveTest | 35 @reflectiveTest |
| 35 class SingleContextManagerTest { | 36 class SingleContextManagerTest { |
| 36 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); | 37 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 callbacks = new TestContextManagerCallbacks(resourceProvider); | 81 callbacks = new TestContextManagerCallbacks(resourceProvider); |
| 81 manager.callbacks = callbacks; | 82 manager.callbacks = callbacks; |
| 82 resourceProvider.newFolder(projPath); | 83 resourceProvider.newFolder(projPath); |
| 83 } | 84 } |
| 84 | 85 |
| 85 void test_setRoots_addFolderWithDartFile() { | 86 void test_setRoots_addFolderWithDartFile() { |
| 86 String filePath = posix.join(projPath, 'lib', 'foo.dart'); | 87 String filePath = posix.join(projPath, 'lib', 'foo.dart'); |
| 87 resourceProvider.newFile(filePath, 'contents'); | 88 resourceProvider.newFile(filePath, 'contents'); |
| 88 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 89 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 89 // verify | 90 // verify |
| 90 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | 91 callbacks.assertContextFiles(projPath, [filePath]); |
| 91 expect(filePaths, hasLength(1)); | |
| 92 expect(filePaths, contains(filePath)); | |
| 93 // There is an analysis context. | 92 // There is an analysis context. |
| 94 List<AnalysisContext> contextsInAnalysisRoot = | 93 List<AnalysisContext> contextsInAnalysisRoot = |
| 95 manager.contextsInAnalysisRoot(rootFolder); | 94 manager.contextsInAnalysisRoot(rootFolder); |
| 96 expect(contextsInAnalysisRoot, hasLength(1)); | 95 expect(contextsInAnalysisRoot, hasLength(1)); |
| 97 AnalysisContext context = contextsInAnalysisRoot[0]; | 96 AnalysisContext context = contextsInAnalysisRoot[0]; |
| 98 expect(context, isNotNull); | 97 expect(context, isNotNull); |
| 99 // Files in lib/ have package: URIs. | 98 // Files in lib/ have package: URIs. |
| 100 Source result = context.sourceFactory.forUri('package:foo/foo.dart'); | 99 Source result = context.sourceFactory.forUri('package:foo/foo.dart'); |
| 101 expect(result, isNotNull); | 100 expect(result, isNotNull); |
| 102 expect(result.exists(), isTrue); | 101 expect(result.exists(), isTrue); |
| 103 } | 102 } |
| 104 | 103 |
| 105 void test_setRoots_addFolderWithDartFileInSubfolder() { | 104 void test_setRoots_addFolderWithDartFileInSubfolder() { |
| 106 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | 105 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 107 resourceProvider.newFile(filePath, 'contents'); | 106 resourceProvider.newFile(filePath, 'contents'); |
| 108 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 107 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 109 // verify | 108 // verify |
| 110 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | 109 callbacks.assertContextFiles(projPath, [filePath]); |
| 111 expect(filePaths, hasLength(1)); | |
| 112 expect(filePaths, contains(filePath)); | |
| 113 } | 110 } |
| 114 | 111 |
| 115 void test_setRoots_addFolderWithDummyLink() { | 112 void test_setRoots_addFolderWithDummyLink() { |
| 116 String filePath = posix.join(projPath, 'foo.dart'); | 113 String filePath = posix.join(projPath, 'foo.dart'); |
| 117 resourceProvider.newDummyLink(filePath); | 114 resourceProvider.newDummyLink(filePath); |
| 118 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 115 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 119 // verify | 116 // verify |
| 120 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | 117 callbacks.assertContextFiles(projPath, []); |
| 121 expect(filePaths, isEmpty); | |
| 122 } | |
| 123 | |
| 124 void test_setRoots_addFolderWithNestedPackageSpec() { | |
| 125 newFile([projPath, 'aaa', 'pubspec.yaml']); | |
| 126 newFile([projPath, 'bbb', 'pubspec.yaml']); | |
| 127 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
| 128 // We don't care about pubspec.yaml files - still just one context. | |
| 129 callbacks.assertContextPaths([projPath]); | |
| 130 expect(manager.contextsInAnalysisRoot(rootFolder), hasLength(1)); | |
| 131 } | 118 } |
| 132 | 119 |
| 133 void test_setRoots_exclude_newRoot_withExcludedFile() { | 120 void test_setRoots_exclude_newRoot_withExcludedFile() { |
| 134 String project = '/project'; | 121 String project = '/project'; |
| 135 String file1 = '$project/file1.dart'; | 122 String file1 = '$project/file1.dart'; |
| 136 String file2 = '$project/file2.dart'; | 123 String file2 = '$project/file2.dart'; |
| 137 // create files | 124 // create files |
| 138 resourceProvider.newFile(file1, '// 1'); | 125 resourceProvider.newFile(file1, '// 1'); |
| 139 resourceProvider.newFile(file2, '// 2'); | 126 resourceProvider.newFile(file2, '// 2'); |
| 140 // set roots | 127 // set roots |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 // begins with '.', then that is not sufficient to cause any files in the | 313 // begins with '.', then that is not sufficient to cause any files in the |
| 327 // context to be ignored. | 314 // context to be ignored. |
| 328 String project = '/.pub/project'; | 315 String project = '/.pub/project'; |
| 329 String fileA = '$project/foo.dart'; | 316 String fileA = '$project/foo.dart'; |
| 330 resourceProvider.newFile(fileA, ''); | 317 resourceProvider.newFile(fileA, ''); |
| 331 manager.setRoots(<String>[project], <String>[], <String, String>{}); | 318 manager.setRoots(<String>[project], <String>[], <String, String>{}); |
| 332 callbacks.assertContextPaths([project]); | 319 callbacks.assertContextPaths([project]); |
| 333 callbacks.assertContextFiles(project, [fileA]); | 320 callbacks.assertContextFiles(project, [fileA]); |
| 334 } | 321 } |
| 335 | 322 |
| 323 test_watch_addFile() async { |
| 324 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 325 // empty folder initially |
| 326 callbacks.assertContextFiles(projPath, []); |
| 327 // add file |
| 328 String filePath = posix.join(projPath, 'foo.dart'); |
| 329 resourceProvider.newFile(filePath, 'contents'); |
| 330 // the file was added |
| 331 await pumpEventQueue(); |
| 332 callbacks.assertContextFiles(projPath, [filePath]); |
| 333 } |
| 334 |
| 335 test_watch_addFile_excluded() async { |
| 336 String folderA = '$projPath/aaa'; |
| 337 String folderB = '$projPath/bbb'; |
| 338 String fileA = '$folderA/a.dart'; |
| 339 String fileB = '$folderB/b.dart'; |
| 340 // create files |
| 341 resourceProvider.newFile(fileA, 'library a;'); |
| 342 // set roots |
| 343 manager.setRoots(<String>[projPath], <String>[folderB], <String, String>{}); |
| 344 callbacks.assertContextPaths([projPath]); |
| 345 callbacks.assertContextFiles(projPath, [fileA]); |
| 346 // add a file, ignored as excluded |
| 347 resourceProvider.newFile(fileB, 'library b;'); |
| 348 await pumpEventQueue(); |
| 349 callbacks.assertContextPaths([projPath]); |
| 350 callbacks.assertContextFiles(projPath, [fileA]); |
| 351 } |
| 352 |
| 353 test_watch_addFile_notInRoot() async { |
| 354 String contextPath = '/roots'; |
| 355 String root1 = '$contextPath/root1'; |
| 356 String root2 = '$contextPath/root2'; |
| 357 String root3 = '$contextPath/root3'; |
| 358 String file1 = '$root1/file1.dart'; |
| 359 String file2 = '$root2/file2.dart'; |
| 360 String file3 = '$root3/file3.dart'; |
| 361 // create files |
| 362 resourceProvider.newFile(file1, ''); |
| 363 resourceProvider.newFile(file2, ''); |
| 364 // set roots |
| 365 manager.setRoots(<String>[root1, root2], <String>[], <String, String>{}); |
| 366 callbacks.assertContextPaths([contextPath]); |
| 367 callbacks.assertContextFiles(contextPath, [file1, file2]); |
| 368 // add a file, not in a root - ignored |
| 369 resourceProvider.newFile(file3, ''); |
| 370 await pumpEventQueue(); |
| 371 callbacks.assertContextPaths([contextPath]); |
| 372 callbacks.assertContextFiles(contextPath, [file1, file2]); |
| 373 } |
| 374 |
| 375 test_watch_addFile_pathContainsDotFile() async { |
| 376 // If a file is added and the absolute path to it contains a folder whose |
| 377 // name begins with '.', then the file is ignored. |
| 378 String project = '/project'; |
| 379 String fileA = '$project/foo.dart'; |
| 380 String fileB = '$project/.pub/bar.dart'; |
| 381 resourceProvider.newFile(fileA, ''); |
| 382 manager.setRoots(<String>[project], <String>[], <String, String>{}); |
| 383 callbacks.assertContextPaths([project]); |
| 384 callbacks.assertContextFiles(project, [fileA]); |
| 385 resourceProvider.newFile(fileB, ''); |
| 386 await pumpEventQueue(); |
| 387 callbacks.assertContextPaths([project]); |
| 388 callbacks.assertContextFiles(project, [fileA]); |
| 389 } |
| 390 |
| 391 test_watch_addFileInSubfolder() async { |
| 392 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 393 // empty folder initially |
| 394 callbacks.assertContextFiles(projPath, []); |
| 395 // add file in subfolder |
| 396 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 397 resourceProvider.newFile(filePath, 'contents'); |
| 398 // the file was added |
| 399 await pumpEventQueue(); |
| 400 callbacks.assertContextFiles(projPath, [filePath]); |
| 401 } |
| 402 |
| 403 test_watch_deleteFile() async { |
| 404 String filePath = posix.join(projPath, 'foo.dart'); |
| 405 // add root with a file |
| 406 resourceProvider.newFile(filePath, 'contents'); |
| 407 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 408 // the file was added |
| 409 callbacks.assertContextFiles(projPath, [filePath]); |
| 410 // delete the file |
| 411 resourceProvider.deleteFile(filePath); |
| 412 await pumpEventQueue(); |
| 413 callbacks.assertContextFiles(projPath, []); |
| 414 } |
| 415 |
| 416 test_watch_deleteFolder() async { |
| 417 String filePath = posix.join(projPath, 'foo.dart'); |
| 418 // add root with a file |
| 419 resourceProvider.newFile(filePath, 'contents'); |
| 420 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 421 // the file was added |
| 422 callbacks.assertContextFiles(projPath, [filePath]); |
| 423 // delete the folder |
| 424 resourceProvider.deleteFolder(projPath); |
| 425 await pumpEventQueue(); |
| 426 callbacks.assertContextFiles(projPath, []); |
| 427 } |
| 428 |
| 429 test_watch_modifyFile() async { |
| 430 String filePath = posix.join(projPath, 'foo.dart'); |
| 431 // add root with a file |
| 432 resourceProvider.newFile(filePath, 'contents'); |
| 433 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 434 // the file was added |
| 435 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; |
| 436 expect(filePaths, hasLength(1)); |
| 437 expect(filePaths, contains(filePath)); |
| 438 expect(filePaths[filePath], equals(callbacks.now)); |
| 439 // update the file |
| 440 callbacks.now++; |
| 441 resourceProvider.modifyFile(filePath, 'new contents'); |
| 442 await pumpEventQueue(); |
| 443 return expect(filePaths[filePath], equals(callbacks.now)); |
| 444 } |
| 445 |
| 336 void _processRequiredPlugins() { | 446 void _processRequiredPlugins() { |
| 337 List<Plugin> plugins = <Plugin>[]; | 447 List<Plugin> plugins = <Plugin>[]; |
| 338 plugins.addAll(AnalysisEngine.instance.requiredPlugins); | 448 plugins.addAll(AnalysisEngine.instance.requiredPlugins); |
| 339 plugins.add(AnalysisEngine.instance.commandLinePlugin); | 449 plugins.add(AnalysisEngine.instance.commandLinePlugin); |
| 340 plugins.add(AnalysisEngine.instance.optionsPlugin); | 450 plugins.add(AnalysisEngine.instance.optionsPlugin); |
| 341 plugins.add(linterPlugin); | 451 plugins.add(linterPlugin); |
| 342 ExtensionManager manager = new ExtensionManager(); | 452 ExtensionManager manager = new ExtensionManager(); |
| 343 manager.processPlugins(plugins); | 453 manager.processPlugins(plugins); |
| 344 } | 454 } |
| 345 } | 455 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 359 String relPath = resourceProvider.pathContext.joinAll(relSegments); | 469 String relPath = resourceProvider.pathContext.joinAll(relSegments); |
| 360 Resource file = rootFolder.getChild(relPath); | 470 Resource file = rootFolder.getChild(relPath); |
| 361 if (file is File && file.exists) { | 471 if (file is File && file.exists) { |
| 362 return file.createSource(uri); | 472 return file.createSource(uri); |
| 363 } | 473 } |
| 364 } | 474 } |
| 365 } | 475 } |
| 366 return null; | 476 return null; |
| 367 } | 477 } |
| 368 } | 478 } |
| OLD | NEW |