| 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 'mocks.dart'; | 7 import 'package:analysis_server/src/context_directory_manager.dart'; |
| 8 import 'package:analysis_server/src/package_map_provider.dart'; |
| 9 import 'package:analysis_testing/reflective_tests.dart'; |
| 8 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| 9 import 'package:analyzer/file_system/memory_file_system.dart'; | 11 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 10 import 'package:analysis_server/src/context_directory_manager.dart'; | |
| 11 import 'package:analysis_server/src/package_map_provider.dart'; | |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:path/path.dart'; | 14 import 'package:path/path.dart'; |
| 15 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 16 | 16 |
| 17 import 'mocks.dart'; |
| 18 |
| 19 |
| 20 main() { |
| 21 groupSep = ' | '; |
| 22 runReflectiveTests(ContextDirectoryManagerTest); |
| 23 } |
| 24 |
| 25 |
| 26 @ReflectiveTestCase() |
| 27 class ContextDirectoryManagerTest { |
| 28 TestContextDirectoryManager manager; |
| 29 MemoryResourceProvider resourceProvider; |
| 30 MockPackageMapProvider packageMapProvider; |
| 31 |
| 32 String projPath = '/my/proj'; |
| 33 |
| 34 void setUp() { |
| 35 resourceProvider = new MemoryResourceProvider(); |
| 36 packageMapProvider = new MockPackageMapProvider(); |
| 37 manager = new TestContextDirectoryManager( |
| 38 resourceProvider, |
| 39 packageMapProvider); |
| 40 resourceProvider.newFolder(projPath); |
| 41 } |
| 42 |
| 43 test_ignoreFilesInPackagesFolder() { |
| 44 // create a context with a pubspec.yaml file |
| 45 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 46 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 47 // create a file in the "packages" folder |
| 48 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); |
| 49 resourceProvider.newFile(filePath1, 'contents'); |
| 50 // "packages" files are ignored initially |
| 51 manager.setRoots(<String>[projPath], <String>[]); |
| 52 expect(manager.currentContextFilePaths[projPath], isEmpty); |
| 53 // "packages" files are ignored during watch |
| 54 String filePath2 = posix.join(projPath, 'packages', 'file2.dart'); |
| 55 resourceProvider.newFile(filePath2, 'contents'); |
| 56 return pumpEventQueue().then((_) { |
| 57 expect(manager.currentContextFilePaths[projPath], isEmpty); |
| 58 }); |
| 59 } |
| 60 |
| 61 void test_isInAnalysisRoot_inRoot() { |
| 62 manager.setRoots(<String>[projPath], <String>[]); |
| 63 expect(manager.isInAnalysisRoot('$projPath/test.dart'), isTrue); |
| 64 } |
| 65 |
| 66 void test_isInAnalysisRoot_notInRoot() { |
| 67 manager.setRoots(<String>[projPath], <String>[]); |
| 68 expect(manager.isInAnalysisRoot('/test.dart'), isFalse); |
| 69 } |
| 70 |
| 71 void test_setRoots_addFolderWithDartFile() { |
| 72 String filePath = posix.join(projPath, 'foo.dart'); |
| 73 resourceProvider.newFile(filePath, 'contents'); |
| 74 manager.setRoots(<String>[projPath], <String>[]); |
| 75 // verify |
| 76 var filePaths = manager.currentContextFilePaths[projPath]; |
| 77 expect(filePaths, hasLength(1)); |
| 78 expect(filePaths, contains(filePath)); |
| 79 } |
| 80 |
| 81 void test_setRoots_addFolderWithDartFileInSubfolder() { |
| 82 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 83 resourceProvider.newFile(filePath, 'contents'); |
| 84 manager.setRoots(<String>[projPath], <String>[]); |
| 85 // verify |
| 86 var filePaths = manager.currentContextFilePaths[projPath]; |
| 87 expect(filePaths, hasLength(1)); |
| 88 expect(filePaths, contains(filePath)); |
| 89 } |
| 90 |
| 91 void test_setRoots_addFolderWithDummyLink() { |
| 92 String filePath = posix.join(projPath, 'foo.dart'); |
| 93 resourceProvider.newDummyLink(filePath); |
| 94 manager.setRoots(<String>[projPath], <String>[]); |
| 95 // verify |
| 96 var filePaths = manager.currentContextFilePaths[projPath]; |
| 97 expect(filePaths, isEmpty); |
| 98 } |
| 99 |
| 100 void test_setRoots_addFolderWithPubspec() { |
| 101 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 102 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 103 manager.setRoots(<String>[projPath], <String>[]); |
| 104 // verify |
| 105 expect(manager.currentContextPaths, hasLength(1)); |
| 106 expect(manager.currentContextPaths, contains(projPath)); |
| 107 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 108 } |
| 109 |
| 110 void test_setRoots_addFolderWithoutPubspec() { |
| 111 packageMapProvider.packageMap = null; |
| 112 manager.setRoots(<String>[projPath], <String>[]); |
| 113 // verify |
| 114 expect(manager.currentContextPaths, hasLength(1)); |
| 115 expect(manager.currentContextPaths, contains(projPath)); |
| 116 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 117 } |
| 118 |
| 119 void test_setRoots_newlyAddedFoldersGetProperPackageMap() { |
| 120 String packagePath = '/package/foo'; |
| 121 Folder packageFolder = resourceProvider.newFolder(packagePath); |
| 122 packageMapProvider.packageMap = { |
| 123 'foo': [packageFolder] |
| 124 }; |
| 125 manager.setRoots(<String>[projPath], <String>[]); |
| 126 expect( |
| 127 manager.currentContextPackageMaps[projPath], |
| 128 equals(packageMapProvider.packageMap)); |
| 129 } |
| 130 |
| 131 void test_setRoots_removeFolderWithPubspec() { |
| 132 // create a pubspec |
| 133 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 134 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 135 // add one root - there is a context |
| 136 manager.setRoots(<String>[projPath], <String>[]); |
| 137 expect(manager.currentContextPaths, hasLength(1)); |
| 138 // set empty roots - no contexts |
| 139 manager.setRoots(<String>[], <String>[]); |
| 140 expect(manager.currentContextPaths, hasLength(0)); |
| 141 expect(manager.currentContextFilePaths, hasLength(0)); |
| 142 } |
| 143 |
| 144 void test_setRoots_removeFolderWithoutPubspec() { |
| 145 packageMapProvider.packageMap = null; |
| 146 // add one root - there is a context |
| 147 manager.setRoots(<String>[projPath], <String>[]); |
| 148 expect(manager.currentContextPaths, hasLength(1)); |
| 149 // set empty roots - no contexts |
| 150 manager.setRoots(<String>[], <String>[]); |
| 151 expect(manager.currentContextPaths, hasLength(0)); |
| 152 expect(manager.currentContextFilePaths, hasLength(0)); |
| 153 } |
| 154 |
| 155 test_watch_addDummyLink() { |
| 156 manager.setRoots(<String>[projPath], <String>[]); |
| 157 // empty folder initially |
| 158 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 159 expect(filePaths, isEmpty); |
| 160 // add link |
| 161 String filePath = posix.join(projPath, 'foo.dart'); |
| 162 resourceProvider.newDummyLink(filePath); |
| 163 // the link was ignored |
| 164 return pumpEventQueue().then((_) { |
| 165 expect(filePaths, isEmpty); |
| 166 }); |
| 167 } |
| 168 |
| 169 test_watch_addFile() { |
| 170 manager.setRoots(<String>[projPath], <String>[]); |
| 171 // empty folder initially |
| 172 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 173 expect(filePaths, hasLength(0)); |
| 174 // add file |
| 175 String filePath = posix.join(projPath, 'foo.dart'); |
| 176 resourceProvider.newFile(filePath, 'contents'); |
| 177 // the file was added |
| 178 return pumpEventQueue().then((_) { |
| 179 expect(filePaths, hasLength(1)); |
| 180 expect(filePaths, contains(filePath)); |
| 181 }); |
| 182 } |
| 183 |
| 184 test_watch_addFileInSubfolder() { |
| 185 manager.setRoots(<String>[projPath], <String>[]); |
| 186 // empty folder initially |
| 187 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 188 expect(filePaths, hasLength(0)); |
| 189 // add file in subfolder |
| 190 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 191 resourceProvider.newFile(filePath, 'contents'); |
| 192 // the file was added |
| 193 return pumpEventQueue().then((_) { |
| 194 expect(filePaths, hasLength(1)); |
| 195 expect(filePaths, contains(filePath)); |
| 196 }); |
| 197 } |
| 198 |
| 199 test_watch_deleteFile() { |
| 200 String filePath = posix.join(projPath, 'foo.dart'); |
| 201 // add root with a file |
| 202 resourceProvider.newFile(filePath, 'contents'); |
| 203 manager.setRoots(<String>[projPath], <String>[]); |
| 204 // the file was added |
| 205 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 206 expect(filePaths, hasLength(1)); |
| 207 expect(filePaths, contains(filePath)); |
| 208 // delete the file |
| 209 resourceProvider.deleteFile(filePath); |
| 210 return pumpEventQueue().then((_) { |
| 211 return expect(filePaths, hasLength(0)); |
| 212 }); |
| 213 } |
| 214 |
| 215 test_watch_modifyFile() { |
| 216 String filePath = posix.join(projPath, 'foo.dart'); |
| 217 // add root with a file |
| 218 resourceProvider.newFile(filePath, 'contents'); |
| 219 manager.setRoots(<String>[projPath], <String>[]); |
| 220 // the file was added |
| 221 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 222 expect(filePaths, hasLength(1)); |
| 223 expect(filePaths, contains(filePath)); |
| 224 expect(filePaths[filePath], equals(manager.now)); |
| 225 // update the file |
| 226 manager.now++; |
| 227 resourceProvider.modifyFile(filePath, 'new contents'); |
| 228 return pumpEventQueue().then((_) { |
| 229 return expect(filePaths[filePath], equals(manager.now)); |
| 230 }); |
| 231 } |
| 232 |
| 233 test_watch_modifyPackageMapDependency() { |
| 234 // create a dependency file |
| 235 String dependencyPath = posix.join(projPath, 'dep'); |
| 236 resourceProvider.newFile(dependencyPath, 'contents'); |
| 237 packageMapProvider.dependencies.add(dependencyPath); |
| 238 // create a Dart file |
| 239 String dartFilePath = posix.join(projPath, 'main.dart'); |
| 240 resourceProvider.newFile(dartFilePath, 'contents'); |
| 241 // the created context has the expected empty package map |
| 242 manager.setRoots(<String>[projPath], <String>[]); |
| 243 expect(manager.currentContextPackageMaps[projPath], isEmpty); |
| 244 // configure package map |
| 245 String packagePath = '/package/foo'; |
| 246 resourceProvider.newFolder(packagePath); |
| 247 packageMapProvider.packageMap = { |
| 248 'foo': projPath |
| 249 }; |
| 250 // Changing a .dart file in the project shouldn't cause a new |
| 251 // package map to be picked up. |
| 252 resourceProvider.modifyFile(dartFilePath, 'new contents'); |
| 253 return pumpEventQueue().then((_) { |
| 254 expect(manager.currentContextPackageMaps[projPath], isEmpty); |
| 255 // However, changing the package map dependency should. |
| 256 resourceProvider.modifyFile(dependencyPath, 'new contents'); |
| 257 return pumpEventQueue().then((_) { |
| 258 expect( |
| 259 manager.currentContextPackageMaps[projPath], |
| 260 equals(packageMapProvider.packageMap)); |
| 261 }); |
| 262 }); |
| 263 } |
| 264 |
| 265 test_watch_modifyPackageMapDependency_fail() { |
| 266 // create a dependency file |
| 267 String dependencyPath = posix.join(projPath, 'dep'); |
| 268 resourceProvider.newFile(dependencyPath, 'contents'); |
| 269 packageMapProvider.dependencies.add(dependencyPath); |
| 270 // create a Dart file |
| 271 String dartFilePath = posix.join(projPath, 'main.dart'); |
| 272 resourceProvider.newFile(dartFilePath, 'contents'); |
| 273 // the created context has the expected empty package map |
| 274 manager.setRoots(<String>[projPath], <String>[]); |
| 275 expect(manager.currentContextPackageMaps[projPath], isEmpty); |
| 276 // Change the package map dependency so that the packageMapProvider is |
| 277 // re-run, and arrange for it to return null from computePackageMap(). |
| 278 packageMapProvider.packageMap = null; |
| 279 resourceProvider.modifyFile(dependencyPath, 'new contents'); |
| 280 return pumpEventQueue().then((_) { |
| 281 // The package map should have been changed to null. |
| 282 expect(manager.currentContextPackageMaps[projPath], isNull); |
| 283 }); |
| 284 } |
| 285 } |
| 286 |
| 287 |
| 17 class TestContextDirectoryManager extends ContextDirectoryManager { | 288 class TestContextDirectoryManager extends ContextDirectoryManager { |
| 18 TestContextDirectoryManager( | |
| 19 MemoryResourceProvider resourceProvider, PackageMapProvider packageMapProv
ider) | |
| 20 : super(resourceProvider, packageMapProvider); | |
| 21 | |
| 22 /** | 289 /** |
| 23 * Source of timestamps stored in [currentContextFilePaths]. | 290 * Source of timestamps stored in [currentContextFilePaths]. |
| 24 */ | 291 */ |
| 25 int now = 0; | 292 int now = 0; |
| 26 | 293 |
| 27 final Set<String> currentContextPaths = new Set<String>(); | 294 final Set<String> currentContextPaths = new Set<String>(); |
| 28 | 295 |
| 29 /** | 296 /** |
| 30 * Map from context to (map from file path to timestamp of last event) | 297 * Map from context to (map from file path to timestamp of last event). |
| 31 */ | 298 */ |
| 32 final Map<String, Map<String, int>> currentContextFilePaths = <String, Map<Str
ing, int>>{}; | 299 final Map<String, Map<String, int>> currentContextFilePaths = <String, |
| 300 Map<String, int>>{}; |
| 33 | 301 |
| 34 /** | 302 /** |
| 35 * Map from context to package map | 303 * Map from context to package map. |
| 36 */ | 304 */ |
| 37 final Map<String, Map<String, List<Folder>>> currentContextPackageMaps = | 305 final Map<String, Map<String, List<Folder>>> currentContextPackageMaps = |
| 38 <String, Map<String, List<Folder>>>{}; | 306 <String, Map<String, List<Folder>>>{}; |
| 39 | 307 |
| 308 TestContextDirectoryManager(MemoryResourceProvider resourceProvider, |
| 309 PackageMapProvider packageMapProvider) |
| 310 : super(resourceProvider, packageMapProvider); |
| 311 |
| 40 @override | 312 @override |
| 41 void addContext(Folder folder, Map<String, List<Folder>> packageMap) { | 313 void addContext(Folder folder, Map<String, List<Folder>> packageMap) { |
| 42 String path = folder.path; | 314 String path = folder.path; |
| 43 currentContextPaths.add(path); | 315 currentContextPaths.add(path); |
| 44 currentContextFilePaths[path] = <String, int>{}; | 316 currentContextFilePaths[path] = <String, int>{}; |
| 45 currentContextPackageMaps[path] = packageMap; | 317 currentContextPackageMaps[path] = packageMap; |
| 46 } | 318 } |
| 47 | 319 |
| 48 @override | 320 @override |
| 49 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { | 321 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 64 | 336 |
| 65 @override | 337 @override |
| 66 void removeContext(Folder folder) { | 338 void removeContext(Folder folder) { |
| 67 String path = folder.path; | 339 String path = folder.path; |
| 68 currentContextPaths.remove(path); | 340 currentContextPaths.remove(path); |
| 69 currentContextFilePaths.remove(path); | 341 currentContextFilePaths.remove(path); |
| 70 currentContextPackageMaps.remove(path); | 342 currentContextPackageMaps.remove(path); |
| 71 } | 343 } |
| 72 | 344 |
| 73 @override | 345 @override |
| 74 void updateContextPackageMap(Folder contextFolder, | 346 void updateContextPackageMap(Folder contextFolder, Map<String, |
| 75 Map<String, List<Folder>> packageMap) { | 347 List<Folder>> packageMap) { |
| 76 currentContextPackageMaps[contextFolder.path]= packageMap; | 348 currentContextPackageMaps[contextFolder.path] = packageMap; |
| 77 } | 349 } |
| 78 } | 350 } |
| 79 | |
| 80 main() { | |
| 81 groupSep = ' | '; | |
| 82 | |
| 83 group('ContextDirectoryManager', () { | |
| 84 TestContextDirectoryManager manager; | |
| 85 MemoryResourceProvider resourceProvider; | |
| 86 MockPackageMapProvider packageMapProvider; | |
| 87 | |
| 88 setUp(() { | |
| 89 resourceProvider = new MemoryResourceProvider(); | |
| 90 packageMapProvider = new MockPackageMapProvider(); | |
| 91 manager = new TestContextDirectoryManager(resourceProvider, packageMapProv
ider); | |
| 92 }); | |
| 93 | |
| 94 test('add folder with pubspec', () { | |
| 95 String projPath = '/my/proj'; | |
| 96 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | |
| 97 resourceProvider.newFolder(projPath); | |
| 98 resourceProvider.newFile(pubspecPath, 'pubspec'); | |
| 99 manager.setRoots(<String>[projPath], <String>[]); | |
| 100 expect(manager.currentContextPaths, hasLength(1)); | |
| 101 expect(manager.currentContextPaths, contains(projPath)); | |
| 102 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | |
| 103 }); | |
| 104 | |
| 105 test('newly added folders get proper package map', () { | |
| 106 String projPath = '/my/proj'; | |
| 107 String packagePath = '/package/foo'; | |
| 108 resourceProvider.newFolder(projPath); | |
| 109 Folder packageFolder = resourceProvider.newFolder(packagePath); | |
| 110 packageMapProvider.packageMap = {'foo': [packageFolder]}; | |
| 111 manager.setRoots(<String>[projPath], <String>[]); | |
| 112 expect(manager.currentContextPackageMaps[projPath], | |
| 113 equals(packageMapProvider.packageMap)); | |
| 114 }); | |
| 115 | |
| 116 test('add folder without pubspec', () { | |
| 117 String projPath = '/my/proj'; | |
| 118 resourceProvider.newFolder(projPath); | |
| 119 packageMapProvider.packageMap = null; | |
| 120 manager.setRoots(<String>[projPath], <String>[]); | |
| 121 expect(manager.currentContextPaths, hasLength(1)); | |
| 122 expect(manager.currentContextPaths, contains(projPath)); | |
| 123 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | |
| 124 }); | |
| 125 | |
| 126 test('add folder with dart file', () { | |
| 127 String projPath = '/my/proj'; | |
| 128 resourceProvider.newFolder(projPath); | |
| 129 String filePath = posix.join(projPath, 'foo.dart'); | |
| 130 resourceProvider.newFile(filePath, 'contents'); | |
| 131 manager.setRoots(<String>[projPath], <String>[]); | |
| 132 var filePaths = manager.currentContextFilePaths[projPath]; | |
| 133 expect(filePaths, hasLength(1)); | |
| 134 expect(filePaths, contains(filePath)); | |
| 135 }); | |
| 136 | |
| 137 test('add folder with dummy link', () { | |
| 138 String projPath = '/my/proj'; | |
| 139 resourceProvider.newFolder(projPath); | |
| 140 String filePath = posix.join(projPath, 'foo.dart'); | |
| 141 resourceProvider.newDummyLink(filePath); | |
| 142 manager.setRoots(<String>[projPath], <String>[]); | |
| 143 var filePaths = manager.currentContextFilePaths[projPath]; | |
| 144 expect(filePaths, isEmpty); | |
| 145 }); | |
| 146 | |
| 147 test('add folder with dart file in subdir', () { | |
| 148 String projPath = '/my/proj'; | |
| 149 resourceProvider.newFolder(projPath); | |
| 150 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | |
| 151 resourceProvider.newFile(filePath, 'contents'); | |
| 152 manager.setRoots(<String>[projPath], <String>[]); | |
| 153 var filePaths = manager.currentContextFilePaths[projPath]; | |
| 154 expect(filePaths, hasLength(1)); | |
| 155 expect(filePaths, contains(filePath)); | |
| 156 }); | |
| 157 | |
| 158 test('remove folder with pubspec', () { | |
| 159 String projPath = '/my/proj'; | |
| 160 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | |
| 161 resourceProvider.newFolder(projPath); | |
| 162 resourceProvider.newFile(pubspecPath, 'pubspec'); | |
| 163 manager.setRoots(<String>[projPath], <String>[]); | |
| 164 manager.setRoots(<String>[], <String>[]); | |
| 165 expect(manager.currentContextPaths, hasLength(0)); | |
| 166 expect(manager.currentContextFilePaths, hasLength(0)); | |
| 167 }); | |
| 168 | |
| 169 test('remove folder without pubspec', () { | |
| 170 String projPath = '/my/proj'; | |
| 171 resourceProvider.newFolder(projPath); | |
| 172 packageMapProvider.packageMap = null; | |
| 173 manager.setRoots(<String>[projPath], <String>[]); | |
| 174 manager.setRoots(<String>[], <String>[]); | |
| 175 expect(manager.currentContextPaths, hasLength(0)); | |
| 176 expect(manager.currentContextFilePaths, hasLength(0)); | |
| 177 }); | |
| 178 | |
| 179 test('ignore files in packages dir', () { | |
| 180 String projPath = '/my/proj'; | |
| 181 resourceProvider.newFolder(projPath); | |
| 182 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | |
| 183 resourceProvider.newFile(pubspecPath, 'pubspec'); | |
| 184 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); | |
| 185 resourceProvider.newFile(filePath1, 'contents'); | |
| 186 manager.setRoots(<String>[projPath], <String>[]); | |
| 187 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 188 expect(filePaths, hasLength(0)); | |
| 189 String filePath2 = posix.join(projPath, 'packages', 'file2.dart'); | |
| 190 resourceProvider.newFile(filePath2, 'contents'); | |
| 191 return pumpEventQueue().then((_) { | |
| 192 expect(filePaths, hasLength(0)); | |
| 193 }); | |
| 194 }); | |
| 195 | |
| 196 group('isInAnalysisRoot', () { | |
| 197 test('in root', () { | |
| 198 String projPath = '/project'; | |
| 199 resourceProvider.newFolder(projPath); | |
| 200 manager.setRoots(<String>[projPath], <String>[]); | |
| 201 expect(manager.isInAnalysisRoot('/project/test.dart'), isTrue); | |
| 202 }); | |
| 203 | |
| 204 test('not in root', () { | |
| 205 String projPath = '/project'; | |
| 206 resourceProvider.newFolder(projPath); | |
| 207 manager.setRoots(<String>[projPath], <String>[]); | |
| 208 expect(manager.isInAnalysisRoot('/test.dart'), isFalse); | |
| 209 }); | |
| 210 }); | |
| 211 | |
| 212 group('detect context modifications', () { | |
| 213 String projPath; | |
| 214 | |
| 215 setUp(() { | |
| 216 projPath = '/my/proj'; | |
| 217 resourceProvider.newFolder(projPath); | |
| 218 }); | |
| 219 | |
| 220 test('Add dummy link', () { | |
| 221 manager.setRoots(<String>[projPath], <String>[]); | |
| 222 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 223 expect(filePaths, isEmpty); | |
| 224 String filePath = posix.join(projPath, 'foo.dart'); | |
| 225 resourceProvider.newDummyLink(filePath); | |
| 226 return pumpEventQueue().then((_) { | |
| 227 expect(filePaths, isEmpty); | |
| 228 }); | |
| 229 }); | |
| 230 | |
| 231 test('Add file', () { | |
| 232 manager.setRoots(<String>[projPath], <String>[]); | |
| 233 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 234 expect(filePaths, hasLength(0)); | |
| 235 String filePath = posix.join(projPath, 'foo.dart'); | |
| 236 resourceProvider.newFile(filePath, 'contents'); | |
| 237 return pumpEventQueue().then((_) { | |
| 238 expect(filePaths, hasLength(1)); | |
| 239 expect(filePaths, contains(filePath)); | |
| 240 }); | |
| 241 }); | |
| 242 | |
| 243 test('Add file in subdirectory', () { | |
| 244 manager.setRoots(<String>[projPath], <String>[]); | |
| 245 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 246 expect(filePaths, hasLength(0)); | |
| 247 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | |
| 248 resourceProvider.newFile(filePath, 'contents'); | |
| 249 return pumpEventQueue().then((_) { | |
| 250 expect(filePaths, hasLength(1)); | |
| 251 expect(filePaths, contains(filePath)); | |
| 252 }); | |
| 253 }); | |
| 254 | |
| 255 test('Delete file', () { | |
| 256 String filePath = posix.join(projPath, 'foo.dart'); | |
| 257 resourceProvider.newFile(filePath, 'contents'); | |
| 258 manager.setRoots(<String>[projPath], <String>[]); | |
| 259 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 260 expect(filePaths, hasLength(1)); | |
| 261 expect(filePaths, contains(filePath)); | |
| 262 resourceProvider.deleteFile(filePath); | |
| 263 return pumpEventQueue().then((_) => expect(filePaths, hasLength(0))); | |
| 264 }); | |
| 265 | |
| 266 test('Modify file', () { | |
| 267 String filePath = posix.join(projPath, 'foo.dart'); | |
| 268 resourceProvider.newFile(filePath, 'contents'); | |
| 269 manager.setRoots(<String>[projPath], <String>[]); | |
| 270 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | |
| 271 expect(filePaths, hasLength(1)); | |
| 272 expect(filePaths, contains(filePath)); | |
| 273 expect(filePaths[filePath], equals(manager.now)); | |
| 274 manager.now++; | |
| 275 resourceProvider.modifyFile(filePath, 'new contents'); | |
| 276 return pumpEventQueue().then((_) => expect(filePaths[filePath], equals( | |
| 277 manager.now))); | |
| 278 }); | |
| 279 | |
| 280 test('Modify package map dependency', () { | |
| 281 String dependencyPath = posix.join(projPath, 'dep'); | |
| 282 resourceProvider.newFile(dependencyPath, 'contents'); | |
| 283 String dartFilePath = posix.join(projPath, 'main.dart'); | |
| 284 resourceProvider.newFile(dartFilePath, 'contents'); | |
| 285 packageMapProvider.dependencies.add(dependencyPath); | |
| 286 manager.setRoots(<String>[projPath], <String>[]); | |
| 287 expect(manager.currentContextPackageMaps[projPath], | |
| 288 equals(packageMapProvider.packageMap)); | |
| 289 String packagePath = '/package/foo'; | |
| 290 resourceProvider.newFolder(packagePath); | |
| 291 packageMapProvider.packageMap = {'foo': projPath}; | |
| 292 // Changing a .dart file in the project shouldn't cause a new | |
| 293 // package map to be picked up. | |
| 294 resourceProvider.modifyFile(dartFilePath, 'new contents'); | |
| 295 return pumpEventQueue().then((_) { | |
| 296 expect(manager.currentContextPackageMaps[projPath], isEmpty); | |
| 297 // However, changing the package map dependency should. | |
| 298 resourceProvider.modifyFile(dependencyPath, 'new contents'); | |
| 299 return pumpEventQueue().then((_) { | |
| 300 expect(manager.currentContextPackageMaps[projPath], | |
| 301 equals(packageMapProvider.packageMap)); | |
| 302 }); | |
| 303 }); | |
| 304 }); | |
| 305 | |
| 306 test('Modify package map dependency - packageMapProvider failure', () { | |
| 307 String dependencyPath = posix.join(projPath, 'dep'); | |
| 308 resourceProvider.newFile(dependencyPath, 'contents'); | |
| 309 String dartFilePath = posix.join(projPath, 'main.dart'); | |
| 310 resourceProvider.newFile(dartFilePath, 'contents'); | |
| 311 packageMapProvider.dependencies.add(dependencyPath); | |
| 312 manager.setRoots(<String>[projPath], <String>[]); | |
| 313 expect(manager.currentContextPackageMaps[projPath], | |
| 314 equals(packageMapProvider.packageMap)); | |
| 315 // Change the package map dependency so that the packageMapProvider is | |
| 316 // re-run, and arrange for it to return null from computePackageMap(). | |
| 317 packageMapProvider.packageMap = null; | |
| 318 resourceProvider.modifyFile(dependencyPath, 'new contents'); | |
| 319 return pumpEventQueue().then((_) { | |
| 320 // The package map should have been changed to null. | |
| 321 expect(manager.currentContextPackageMaps[projPath], isNull); | |
| 322 }); | |
| 323 }); | |
| 324 }); | |
| 325 }); | |
| 326 } | |
| OLD | NEW |