| 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/source/package_map_provider.dart'; | 13 import 'package:analyzer/source/package_map_provider.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; | 14 import 'package:analyzer/source/package_map_resolver.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.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:path/path.dart'; | 18 import 'package:path/path.dart'; |
| 18 import 'package:unittest/unittest.dart'; | 19 import 'package:unittest/unittest.dart'; |
| 19 | 20 |
| 20 import 'mocks.dart'; | 21 import 'mocks.dart'; |
| 21 import 'reflective_tests.dart'; | 22 import 'reflective_tests.dart'; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 183 |
| 183 void test_setRoots_addFolderWithDummyLink() { | 184 void test_setRoots_addFolderWithDummyLink() { |
| 184 String filePath = posix.join(projPath, 'foo.dart'); | 185 String filePath = posix.join(projPath, 'foo.dart'); |
| 185 resourceProvider.newDummyLink(filePath); | 186 resourceProvider.newDummyLink(filePath); |
| 186 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 187 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 187 // verify | 188 // verify |
| 188 var filePaths = manager.currentContextFilePaths[projPath]; | 189 var filePaths = manager.currentContextFilePaths[projPath]; |
| 189 expect(filePaths, isEmpty); | 190 expect(filePaths, isEmpty); |
| 190 } | 191 } |
| 191 | 192 |
| 193 void test_setRoots_addFolderWithoutPubspec() { |
| 194 packageMapProvider.packageMap = null; |
| 195 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 196 // verify |
| 197 expect(manager.currentContextPaths, hasLength(1)); |
| 198 expect(manager.currentContextPaths, contains(projPath)); |
| 199 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 200 } |
| 201 |
| 192 void test_setRoots_addFolderWithPubspec() { | 202 void test_setRoots_addFolderWithPubspec() { |
| 193 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 203 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 194 resourceProvider.newFile(pubspecPath, 'pubspec'); | 204 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 195 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 205 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 196 // verify | 206 // verify |
| 197 expect(manager.currentContextPaths, hasLength(1)); | 207 expect(manager.currentContextPaths, hasLength(1)); |
| 198 expect(manager.currentContextPaths, contains(projPath)); | 208 expect(manager.currentContextPaths, contains(projPath)); |
| 199 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 209 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 200 } | 210 } |
| 201 | 211 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // verify package maps | 271 // verify package maps |
| 262 _checkPackageMap(root, isNull); | 272 _checkPackageMap(root, isNull); |
| 263 _checkPackageMap( | 273 _checkPackageMap( |
| 264 subProjectA, | 274 subProjectA, |
| 265 equals(packageMapProvider.packageMaps[subProjectA])); | 275 equals(packageMapProvider.packageMaps[subProjectA])); |
| 266 _checkPackageMap( | 276 _checkPackageMap( |
| 267 subProjectB, | 277 subProjectB, |
| 268 equals(packageMapProvider.packageMaps[subProjectB])); | 278 equals(packageMapProvider.packageMaps[subProjectB])); |
| 269 } | 279 } |
| 270 | 280 |
| 271 void test_setRoots_addFolderWithoutPubspec() { | |
| 272 packageMapProvider.packageMap = null; | |
| 273 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
| 274 // verify | |
| 275 expect(manager.currentContextPaths, hasLength(1)); | |
| 276 expect(manager.currentContextPaths, contains(projPath)); | |
| 277 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | |
| 278 } | |
| 279 | |
| 280 void test_setRoots_addPackageRoot() { | 281 void test_setRoots_addPackageRoot() { |
| 281 String packagePathFoo = '/package1/foo'; | 282 String packagePathFoo = '/package1/foo'; |
| 282 String packageRootPath = '/package2/foo'; | 283 String packageRootPath = '/package2/foo'; |
| 283 Folder packageFolder = resourceProvider.newFolder(packagePathFoo); | 284 Folder packageFolder = resourceProvider.newFolder(packagePathFoo); |
| 284 packageMapProvider.packageMap = { | 285 packageMapProvider.packageMap = { |
| 285 'foo': [packageFolder] | 286 'foo': [packageFolder] |
| 286 }; | 287 }; |
| 287 List<String> includedPaths = <String>[projPath]; | 288 List<String> includedPaths = <String>[projPath]; |
| 288 List<String> excludedPaths = <String>[]; | 289 List<String> excludedPaths = <String>[]; |
| 289 manager.setRoots(includedPaths, excludedPaths, <String, String>{}); | 290 manager.setRoots(includedPaths, excludedPaths, <String, String>{}); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { | 465 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { |
| 465 String packagePath = '/package/foo'; | 466 String packagePath = '/package/foo'; |
| 466 Folder packageFolder = resourceProvider.newFolder(packagePath); | 467 Folder packageFolder = resourceProvider.newFolder(packagePath); |
| 467 packageMapProvider.packageMap = { | 468 packageMapProvider.packageMap = { |
| 468 'foo': [packageFolder] | 469 'foo': [packageFolder] |
| 469 }; | 470 }; |
| 470 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 471 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 471 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); | 472 _checkPackageMap(projPath, equals(packageMapProvider.packageMap)); |
| 472 } | 473 } |
| 473 | 474 |
| 475 void test_setRoots_removeFolderWithoutPubspec() { |
| 476 packageMapProvider.packageMap = null; |
| 477 // add one root - there is a context |
| 478 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 479 expect(manager.currentContextPaths, hasLength(1)); |
| 480 // set empty roots - no contexts |
| 481 manager.setRoots(<String>[], <String>[], <String, String>{}); |
| 482 expect(manager.currentContextPaths, hasLength(0)); |
| 483 expect(manager.currentContextFilePaths, hasLength(0)); |
| 484 } |
| 485 |
| 474 void test_setRoots_removeFolderWithPubspec() { | 486 void test_setRoots_removeFolderWithPubspec() { |
| 475 // create a pubspec | 487 // create a pubspec |
| 476 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 488 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 477 resourceProvider.newFile(pubspecPath, 'pubspec'); | 489 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 478 // add one root - there is a context | 490 // add one root - there is a context |
| 479 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 491 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 480 expect(manager.currentContextPaths, hasLength(1)); | 492 expect(manager.currentContextPaths, hasLength(1)); |
| 481 // set empty roots - no contexts | 493 // set empty roots - no contexts |
| 482 manager.setRoots(<String>[], <String>[], <String, String>{}); | 494 manager.setRoots(<String>[], <String>[], <String, String>{}); |
| 483 expect(manager.currentContextPaths, hasLength(0)); | 495 expect(manager.currentContextPaths, hasLength(0)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 513 manager.assertContextFiles(projectB, [projectB_file]); | 525 manager.assertContextFiles(projectB, [projectB_file]); |
| 514 manager.assertContextFiles(subProjectA, [subProjectA_file]); | 526 manager.assertContextFiles(subProjectA, [subProjectA_file]); |
| 515 manager.assertContextFiles(subProjectB, [subProjectB_file]); | 527 manager.assertContextFiles(subProjectB, [subProjectB_file]); |
| 516 // remove "projectB" | 528 // remove "projectB" |
| 517 manager.setRoots(<String>[projectA], <String>[], <String, String>{}); | 529 manager.setRoots(<String>[projectA], <String>[], <String, String>{}); |
| 518 manager.assertContextPaths([projectA, subProjectA]); | 530 manager.assertContextPaths([projectA, subProjectA]); |
| 519 manager.assertContextFiles(projectA, [projectA_file]); | 531 manager.assertContextFiles(projectA, [projectA_file]); |
| 520 manager.assertContextFiles(subProjectA, [subProjectA_file]); | 532 manager.assertContextFiles(subProjectA, [subProjectA_file]); |
| 521 } | 533 } |
| 522 | 534 |
| 523 void test_setRoots_removeFolderWithoutPubspec() { | |
| 524 packageMapProvider.packageMap = null; | |
| 525 // add one root - there is a context | |
| 526 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
| 527 expect(manager.currentContextPaths, hasLength(1)); | |
| 528 // set empty roots - no contexts | |
| 529 manager.setRoots(<String>[], <String>[], <String, String>{}); | |
| 530 expect(manager.currentContextPaths, hasLength(0)); | |
| 531 expect(manager.currentContextFilePaths, hasLength(0)); | |
| 532 } | |
| 533 | |
| 534 void test_setRoots_removePackageRoot() { | 535 void test_setRoots_removePackageRoot() { |
| 535 String packagePathFoo = '/package1/foo'; | 536 String packagePathFoo = '/package1/foo'; |
| 536 String packageRootPath = '/package2/foo'; | 537 String packageRootPath = '/package2/foo'; |
| 537 Folder packageFolder = resourceProvider.newFolder(packagePathFoo); | 538 Folder packageFolder = resourceProvider.newFolder(packagePathFoo); |
| 538 packageMapProvider.packageMap = { | 539 packageMapProvider.packageMap = { |
| 539 'foo': [packageFolder] | 540 'foo': [packageFolder] |
| 540 }; | 541 }; |
| 541 List<String> includedPaths = <String>[projPath]; | 542 List<String> includedPaths = <String>[projPath]; |
| 542 List<String> excludedPaths = <String>[]; | 543 List<String> excludedPaths = <String>[]; |
| 543 manager.setRoots(includedPaths, excludedPaths, <String, String>{ | 544 manager.setRoots(includedPaths, excludedPaths, <String, String>{ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 570 // add file | 571 // add file |
| 571 String filePath = posix.join(projPath, 'foo.dart'); | 572 String filePath = posix.join(projPath, 'foo.dart'); |
| 572 resourceProvider.newFile(filePath, 'contents'); | 573 resourceProvider.newFile(filePath, 'contents'); |
| 573 // the file was added | 574 // the file was added |
| 574 return pumpEventQueue().then((_) { | 575 return pumpEventQueue().then((_) { |
| 575 expect(filePaths, hasLength(1)); | 576 expect(filePaths, hasLength(1)); |
| 576 expect(filePaths, contains(filePath)); | 577 expect(filePaths, contains(filePath)); |
| 577 }); | 578 }); |
| 578 } | 579 } |
| 579 | 580 |
| 580 test_watch_addFileInSubfolder() { | |
| 581 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
| 582 // empty folder initially | |
| 583 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 584 expect(filePaths, hasLength(0)); | |
| 585 // add file in subfolder | |
| 586 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | |
| 587 resourceProvider.newFile(filePath, 'contents'); | |
| 588 // the file was added | |
| 589 return pumpEventQueue().then((_) { | |
| 590 expect(filePaths, hasLength(1)); | |
| 591 expect(filePaths, contains(filePath)); | |
| 592 }); | |
| 593 } | |
| 594 | |
| 595 test_watch_addFile_excluded() { | 581 test_watch_addFile_excluded() { |
| 596 // prepare paths | 582 // prepare paths |
| 597 String project = '/project'; | 583 String project = '/project'; |
| 598 String folderA = '$project/aaa'; | 584 String folderA = '$project/aaa'; |
| 599 String folderB = '$project/bbb'; | 585 String folderB = '$project/bbb'; |
| 600 String fileA = '$folderA/a.dart'; | 586 String fileA = '$folderA/a.dart'; |
| 601 String fileB = '$folderB/b.dart'; | 587 String fileB = '$folderB/b.dart'; |
| 602 // create files | 588 // create files |
| 603 resourceProvider.newFile(fileA, 'library a;'); | 589 resourceProvider.newFile(fileA, 'library a;'); |
| 604 // set roots | 590 // set roots |
| 605 manager.setRoots(<String>[project], <String>[folderB], <String, String>{}); | 591 manager.setRoots(<String>[project], <String>[folderB], <String, String>{}); |
| 606 manager.assertContextPaths([project]); | 592 manager.assertContextPaths([project]); |
| 607 manager.assertContextFiles(project, [fileA]); | 593 manager.assertContextFiles(project, [fileA]); |
| 608 // add a file, ignored as excluded | 594 // add a file, ignored as excluded |
| 609 resourceProvider.newFile(fileB, 'library b;'); | 595 resourceProvider.newFile(fileB, 'library b;'); |
| 610 return pumpEventQueue().then((_) { | 596 return pumpEventQueue().then((_) { |
| 611 manager.assertContextPaths([project]); | 597 manager.assertContextPaths([project]); |
| 612 manager.assertContextFiles(project, [fileA]); | 598 manager.assertContextFiles(project, [fileA]); |
| 613 }); | 599 }); |
| 614 } | 600 } |
| 615 | 601 |
| 602 test_watch_addFileInSubfolder() { |
| 603 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
| 604 // empty folder initially |
| 605 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 606 expect(filePaths, hasLength(0)); |
| 607 // add file in subfolder |
| 608 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 609 resourceProvider.newFile(filePath, 'contents'); |
| 610 // the file was added |
| 611 return pumpEventQueue().then((_) { |
| 612 expect(filePaths, hasLength(1)); |
| 613 expect(filePaths, contains(filePath)); |
| 614 }); |
| 615 } |
| 616 |
| 616 test_watch_addPubspec_toRoot() { | 617 test_watch_addPubspec_toRoot() { |
| 617 // prepare paths | 618 // prepare paths |
| 618 String root = '/root'; | 619 String root = '/root'; |
| 619 String rootFile = '$root/root.dart'; | 620 String rootFile = '$root/root.dart'; |
| 620 String rootPubspec = '$root/pubspec.yaml'; | 621 String rootPubspec = '$root/pubspec.yaml'; |
| 621 // create files | 622 // create files |
| 622 resourceProvider.newFile(rootFile, 'library root;'); | 623 resourceProvider.newFile(rootFile, 'library root;'); |
| 623 // set roots | 624 // set roots |
| 624 manager.setRoots(<String>[root], <String>[], <String, String>{}); | 625 manager.setRoots(<String>[root], <String>[], <String, String>{}); |
| 625 manager.assertContextPaths([root]); | 626 manager.assertContextPaths([root]); |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 Set<Source>>{}; | 863 Set<Source>>{}; |
| 863 | 864 |
| 864 /** | 865 /** |
| 865 * Map from context to package URI resolver. | 866 * Map from context to package URI resolver. |
| 866 */ | 867 */ |
| 867 final Map<String, UriResolver> currentContextPackageUriResolvers = <String, | 868 final Map<String, UriResolver> currentContextPackageUriResolvers = <String, |
| 868 UriResolver>{}; | 869 UriResolver>{}; |
| 869 | 870 |
| 870 TestContextManager(MemoryResourceProvider resourceProvider, | 871 TestContextManager(MemoryResourceProvider resourceProvider, |
| 871 PackageMapProvider packageMapProvider) | 872 PackageMapProvider packageMapProvider) |
| 872 : super(resourceProvider, packageMapProvider); | 873 : super( |
| 874 resourceProvider, |
| 875 packageMapProvider, |
| 876 InstrumentationService.NULL_SERVICE); |
| 873 | 877 |
| 874 /** | 878 /** |
| 875 * Iterable of the paths to contexts that currently exist. | 879 * Iterable of the paths to contexts that currently exist. |
| 876 */ | 880 */ |
| 877 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; | 881 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; |
| 878 | 882 |
| 879 @override | 883 @override |
| 880 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver) { | 884 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver) { |
| 881 String path = folder.path; | 885 String path = folder.path; |
| 882 expect(currentContextPaths, isNot(contains(path))); | 886 expect(currentContextPaths, isNot(contains(path))); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 929 currentContextSources.remove(path); | 933 currentContextSources.remove(path); |
| 930 currentContextPackageUriResolvers.remove(path); | 934 currentContextPackageUriResolvers.remove(path); |
| 931 } | 935 } |
| 932 | 936 |
| 933 @override | 937 @override |
| 934 void updateContextPackageUriResolver(Folder contextFolder, | 938 void updateContextPackageUriResolver(Folder contextFolder, |
| 935 UriResolver packageUriResolver) { | 939 UriResolver packageUriResolver) { |
| 936 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; | 940 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; |
| 937 } | 941 } |
| 938 } | 942 } |
| OLD | NEW |