| 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 'package:analysis_server/src/context_manager.dart'; | 7 import 'package:analysis_server/src/context_manager.dart'; |
| 8 import 'package:analysis_server/src/package_map_provider.dart'; | 8 import 'package:analysis_server/src/package_map_provider.dart'; |
| 9 import 'package:analysis_testing/reflective_tests.dart'; | 9 import 'package:analysis_testing/reflective_tests.dart'; |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 void test_setRoots_addFolderWithPubspec() { | 98 void test_setRoots_addFolderWithPubspec() { |
| 99 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 99 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 100 resourceProvider.newFile(pubspecPath, 'pubspec'); | 100 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 101 manager.setRoots(<String>[projPath], <String>[]); | 101 manager.setRoots(<String>[projPath], <String>[]); |
| 102 // verify | 102 // verify |
| 103 expect(manager.currentContextPaths, hasLength(1)); | 103 expect(manager.currentContextPaths, hasLength(1)); |
| 104 expect(manager.currentContextPaths, contains(projPath)); | 104 expect(manager.currentContextPaths, contains(projPath)); |
| 105 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 105 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void test_setRoots_addFolderWithPubspecFolders() { |
| 109 // prepare paths |
| 110 String root = '/root'; |
| 111 String rootFile = '$root/root.dart'; |
| 112 String subProjectA = '$root/sub/aaa'; |
| 113 String subProjectB = '$root/sub/sub2/bbb'; |
| 114 String subProjectA_file = '$subProjectA/bin/a.dart'; |
| 115 String subProjectB_file = '$subProjectB/bin/b.dart'; |
| 116 // create files |
| 117 resourceProvider.newFile('$subProjectA/pubspec.yaml', 'pubspec'); |
| 118 resourceProvider.newFile('$subProjectB/pubspec.yaml', 'pubspec'); |
| 119 resourceProvider.newFile(rootFile, 'library root;'); |
| 120 resourceProvider.newFile(subProjectA_file, 'library a;'); |
| 121 resourceProvider.newFile(subProjectB_file, 'library b;'); |
| 122 // configure package maps |
| 123 packageMapProvider.packageMaps = { |
| 124 subProjectA: { |
| 125 'foo': [resourceProvider.newFolder('/package/foo')] |
| 126 }, |
| 127 subProjectA: { |
| 128 'bar': [resourceProvider.newFolder('/package/bar')] |
| 129 }, |
| 130 }; |
| 131 // set roots |
| 132 manager.setRoots(<String>[root], <String>[]); |
| 133 manager.assertContextPaths([root, subProjectA, subProjectB]); |
| 134 // verify files |
| 135 manager.assertContextFiles(root, [rootFile]); |
| 136 manager.assertContextFiles(subProjectA, [subProjectA_file]); |
| 137 manager.assertContextFiles(subProjectB, [subProjectB_file]); |
| 138 // verify package maps |
| 139 expect( |
| 140 manager.currentContextPackageMaps[root], |
| 141 equals(packageMapProvider.packageMaps[root])); |
| 142 expect( |
| 143 manager.currentContextPackageMaps[subProjectA], |
| 144 equals(packageMapProvider.packageMaps[subProjectA])); |
| 145 expect( |
| 146 manager.currentContextPackageMaps[subProjectB], |
| 147 equals(packageMapProvider.packageMaps[subProjectB])); |
| 148 } |
| 149 |
| 108 void test_setRoots_addFolderWithoutPubspec() { | 150 void test_setRoots_addFolderWithoutPubspec() { |
| 109 packageMapProvider.packageMap = null; | 151 packageMapProvider.packageMap = null; |
| 110 manager.setRoots(<String>[projPath], <String>[]); | 152 manager.setRoots(<String>[projPath], <String>[]); |
| 111 // verify | 153 // verify |
| 112 expect(manager.currentContextPaths, hasLength(1)); | 154 expect(manager.currentContextPaths, hasLength(1)); |
| 113 expect(manager.currentContextPaths, contains(projPath)); | 155 expect(manager.currentContextPaths, contains(projPath)); |
| 114 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 156 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 115 } | 157 } |
| 116 | 158 |
| 117 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { | 159 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 132 resourceProvider.newFile(pubspecPath, 'pubspec'); | 174 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 133 // add one root - there is a context | 175 // add one root - there is a context |
| 134 manager.setRoots(<String>[projPath], <String>[]); | 176 manager.setRoots(<String>[projPath], <String>[]); |
| 135 expect(manager.currentContextPaths, hasLength(1)); | 177 expect(manager.currentContextPaths, hasLength(1)); |
| 136 // set empty roots - no contexts | 178 // set empty roots - no contexts |
| 137 manager.setRoots(<String>[], <String>[]); | 179 manager.setRoots(<String>[], <String>[]); |
| 138 expect(manager.currentContextPaths, hasLength(0)); | 180 expect(manager.currentContextPaths, hasLength(0)); |
| 139 expect(manager.currentContextFilePaths, hasLength(0)); | 181 expect(manager.currentContextFilePaths, hasLength(0)); |
| 140 } | 182 } |
| 141 | 183 |
| 184 void test_setRoots_removeFolderWithPubspecFolder() { |
| 185 // prepare paths |
| 186 String projectA = '/projectA'; |
| 187 String projectB = '/projectB'; |
| 188 String subProjectA = '$projectA/sub'; |
| 189 String subProjectB = '$projectB/sub'; |
| 190 String projectA_file = '$projectA/a.dart'; |
| 191 String projectB_file = '$projectB/a.dart'; |
| 192 String subProjectA_pubspec = '$subProjectA/pubspec.yaml'; |
| 193 String subProjectB_pubspec = '$subProjectB/pubspec.yaml'; |
| 194 String subProjectA_file = '$subProjectA/bin/sub_a.dart'; |
| 195 String subProjectB_file = '$subProjectB/bin/sub_b.dart'; |
| 196 // create files |
| 197 resourceProvider.newFile(projectA_file, '// a'); |
| 198 resourceProvider.newFile(projectB_file, '// b'); |
| 199 resourceProvider.newFile(subProjectA_pubspec, 'pubspec'); |
| 200 resourceProvider.newFile(subProjectB_pubspec, 'pubspec'); |
| 201 resourceProvider.newFile(subProjectA_file, '// sub-a'); |
| 202 resourceProvider.newFile(subProjectB_file, '// sub-b'); |
| 203 // set roots |
| 204 manager.setRoots(<String>[projectA, projectB], <String>[]); |
| 205 manager.assertContextPaths([projectA, subProjectA, projectB, subProjectB]); |
| 206 manager.assertContextFiles(projectA, [projectA_file]); |
| 207 manager.assertContextFiles(projectB, [projectB_file]); |
| 208 manager.assertContextFiles(subProjectA, [subProjectA_file]); |
| 209 manager.assertContextFiles(subProjectB, [subProjectB_file]); |
| 210 // remove "projectB" |
| 211 manager.setRoots(<String>[projectA], <String>[]); |
| 212 manager.assertContextPaths([projectA, subProjectA]); |
| 213 manager.assertContextFiles(projectA, [projectA_file]); |
| 214 manager.assertContextFiles(subProjectA, [subProjectA_file]); |
| 215 } |
| 216 |
| 142 void test_setRoots_removeFolderWithoutPubspec() { | 217 void test_setRoots_removeFolderWithoutPubspec() { |
| 143 packageMapProvider.packageMap = null; | 218 packageMapProvider.packageMap = null; |
| 144 // add one root - there is a context | 219 // add one root - there is a context |
| 145 manager.setRoots(<String>[projPath], <String>[]); | 220 manager.setRoots(<String>[projPath], <String>[]); |
| 146 expect(manager.currentContextPaths, hasLength(1)); | 221 expect(manager.currentContextPaths, hasLength(1)); |
| 147 // set empty roots - no contexts | 222 // set empty roots - no contexts |
| 148 manager.setRoots(<String>[], <String>[]); | 223 manager.setRoots(<String>[], <String>[]); |
| 149 expect(manager.currentContextPaths, hasLength(0)); | 224 expect(manager.currentContextPaths, hasLength(0)); |
| 150 expect(manager.currentContextFilePaths, hasLength(0)); | 225 expect(manager.currentContextFilePaths, hasLength(0)); |
| 151 } | 226 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 // add file in subfolder | 262 // add file in subfolder |
| 188 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | 263 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 189 resourceProvider.newFile(filePath, 'contents'); | 264 resourceProvider.newFile(filePath, 'contents'); |
| 190 // the file was added | 265 // the file was added |
| 191 return pumpEventQueue().then((_) { | 266 return pumpEventQueue().then((_) { |
| 192 expect(filePaths, hasLength(1)); | 267 expect(filePaths, hasLength(1)); |
| 193 expect(filePaths, contains(filePath)); | 268 expect(filePaths, contains(filePath)); |
| 194 }); | 269 }); |
| 195 } | 270 } |
| 196 | 271 |
| 272 test_watch_addPubspec() { |
| 273 // prepare paths |
| 274 String root = '/root'; |
| 275 String rootFile = '$root/root.dart'; |
| 276 String subProject = '$root/sub/aaa'; |
| 277 String subPubspec = '$subProject/pubspec.yaml'; |
| 278 String subFile = '$subProject/bin/a.dart'; |
| 279 // create files |
| 280 resourceProvider.newFile(rootFile, 'library root;'); |
| 281 resourceProvider.newFile(subFile, 'library a;'); |
| 282 // set roots |
| 283 manager.setRoots(<String>[root], <String>[]); |
| 284 manager.assertContextPaths([root]); |
| 285 // verify files |
| 286 manager.assertContextFiles(root, [rootFile, subFile]); |
| 287 // add pubspec |
| 288 resourceProvider.newFile(subPubspec, 'pubspec'); |
| 289 return pumpEventQueue().then((_) { |
| 290 manager.assertContextPaths([root, subProject]); |
| 291 manager.assertContextFiles(root, [rootFile]); |
| 292 manager.assertContextFiles(subProject, [subFile]); |
| 293 }); |
| 294 } |
| 295 |
| 197 test_watch_deleteFile() { | 296 test_watch_deleteFile() { |
| 198 String filePath = posix.join(projPath, 'foo.dart'); | 297 String filePath = posix.join(projPath, 'foo.dart'); |
| 199 // add root with a file | 298 // add root with a file |
| 200 resourceProvider.newFile(filePath, 'contents'); | 299 resourceProvider.newFile(filePath, 'contents'); |
| 201 manager.setRoots(<String>[projPath], <String>[]); | 300 manager.setRoots(<String>[projPath], <String>[]); |
| 202 // the file was added | 301 // the file was added |
| 203 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 302 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 204 expect(filePaths, hasLength(1)); | 303 expect(filePaths, hasLength(1)); |
| 205 expect(filePaths, contains(filePath)); | 304 expect(filePaths, contains(filePath)); |
| 206 // delete the file | 305 // delete the file |
| 207 resourceProvider.deleteFile(filePath); | 306 resourceProvider.deleteFile(filePath); |
| 208 return pumpEventQueue().then((_) { | 307 return pumpEventQueue().then((_) { |
| 209 return expect(filePaths, hasLength(0)); | 308 return expect(filePaths, hasLength(0)); |
| 210 }); | 309 }); |
| 211 } | 310 } |
| 212 | 311 |
| 312 test_watch_deletePubspec() { |
| 313 // prepare paths |
| 314 String root = '/root'; |
| 315 String rootFile = '$root/root.dart'; |
| 316 String subProject = '$root/sub/aaa'; |
| 317 String subPubspec = '$subProject/pubspec.yaml'; |
| 318 String subFile = '$subProject/bin/a.dart'; |
| 319 // create files |
| 320 resourceProvider.newFile(subPubspec, 'pubspec'); |
| 321 resourceProvider.newFile(rootFile, 'library root;'); |
| 322 resourceProvider.newFile(subFile, 'library a;'); |
| 323 // set roots |
| 324 manager.setRoots(<String>[root], <String>[]); |
| 325 manager.assertContextPaths([root, subProject]); |
| 326 // verify files |
| 327 manager.assertContextFiles(root, [rootFile]); |
| 328 manager.assertContextFiles(subProject, [subFile]); |
| 329 // delete the pubspec |
| 330 resourceProvider.deleteFile(subPubspec); |
| 331 return pumpEventQueue().then((_) { |
| 332 manager.assertContextPaths([root]); |
| 333 manager.assertContextFiles(root, [rootFile, subFile]); |
| 334 }); |
| 335 } |
| 336 |
| 213 test_watch_modifyFile() { | 337 test_watch_modifyFile() { |
| 214 String filePath = posix.join(projPath, 'foo.dart'); | 338 String filePath = posix.join(projPath, 'foo.dart'); |
| 215 // add root with a file | 339 // add root with a file |
| 216 resourceProvider.newFile(filePath, 'contents'); | 340 resourceProvider.newFile(filePath, 'contents'); |
| 217 manager.setRoots(<String>[projPath], <String>[]); | 341 manager.setRoots(<String>[projPath], <String>[]); |
| 218 // the file was added | 342 // the file was added |
| 219 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 343 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 220 expect(filePaths, hasLength(1)); | 344 expect(filePaths, hasLength(1)); |
| 221 expect(filePaths, contains(filePath)); | 345 expect(filePaths, contains(filePath)); |
| 222 expect(filePaths[filePath], equals(manager.now)); | 346 expect(filePaths[filePath], equals(manager.now)); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 for (Source source in changeSet.removedSources) { | 449 for (Source source in changeSet.removedSources) { |
| 326 expect(filePaths, contains(source.fullName)); | 450 expect(filePaths, contains(source.fullName)); |
| 327 filePaths.remove(source.fullName); | 451 filePaths.remove(source.fullName); |
| 328 } | 452 } |
| 329 for (Source source in changeSet.changedSources) { | 453 for (Source source in changeSet.changedSources) { |
| 330 expect(filePaths, contains(source.fullName)); | 454 expect(filePaths, contains(source.fullName)); |
| 331 filePaths[source.fullName] = now; | 455 filePaths[source.fullName] = now; |
| 332 } | 456 } |
| 333 } | 457 } |
| 334 | 458 |
| 459 void assertContextFiles(String contextPath, List<String> expectedFiles) { |
| 460 var actualFiles = currentContextFilePaths[contextPath].keys; |
| 461 expect(actualFiles, unorderedEquals(expectedFiles)); |
| 462 } |
| 463 |
| 464 void assertContextPaths(List<String> expected) { |
| 465 expect(currentContextPaths, unorderedEquals(expected)); |
| 466 } |
| 467 |
| 335 @override | 468 @override |
| 336 void removeContext(Folder folder) { | 469 void removeContext(Folder folder) { |
| 337 String path = folder.path; | 470 String path = folder.path; |
| 338 currentContextPaths.remove(path); | 471 currentContextPaths.remove(path); |
| 339 currentContextFilePaths.remove(path); | 472 currentContextFilePaths.remove(path); |
| 340 currentContextPackageMaps.remove(path); | 473 currentContextPackageMaps.remove(path); |
| 341 } | 474 } |
| 342 | 475 |
| 343 @override | 476 @override |
| 344 void updateContextPackageMap(Folder contextFolder, Map<String, | 477 void updateContextPackageMap(Folder contextFolder, Map<String, |
| 345 List<Folder>> packageMap) { | 478 List<Folder>> packageMap) { |
| 346 currentContextPackageMaps[contextFolder.path] = packageMap; | 479 currentContextPackageMaps[contextFolder.path] = packageMap; |
| 347 } | 480 } |
| 348 } | 481 } |
| OLD | NEW |