| 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 'mocks.dart'; |
| 8 import 'package:analysis_server/src/context_directory_manager.dart'; | 8 import 'package:analysis_server/src/context_directory_manager.dart'; |
| 9 import 'package:analysis_server/src/package_map_provider.dart'; |
| 9 import 'package:analysis_server/src/resource.dart'; | 10 import 'package:analysis_server/src/resource.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:path/path.dart'; | 13 import 'package:path/path.dart'; |
| 11 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | |
| 13 import 'package:analyzer/src/generated/source.dart'; | |
| 14 | 15 |
| 15 class TestContextDirectoryManager extends ContextDirectoryManager { | 16 class TestContextDirectoryManager extends ContextDirectoryManager { |
| 16 TestContextDirectoryManager(MemoryResourceProvider provider) : super(provider)
; | 17 TestContextDirectoryManager( |
| 18 MemoryResourceProvider resourceProvider, PackageMapProvider packageMapProv
ider) |
| 19 : super(resourceProvider, packageMapProvider); |
| 17 | 20 |
| 18 /** | 21 /** |
| 19 * Source of timestamps stored in [currentContextFilePaths]. | 22 * Source of timestamps stored in [currentContextFilePaths]. |
| 20 */ | 23 */ |
| 21 int now = 0; | 24 int now = 0; |
| 22 | 25 |
| 23 final Set<String> currentContextPaths = new Set<String>(); | 26 final Set<String> currentContextPaths = new Set<String>(); |
| 24 | 27 |
| 25 /** | 28 /** |
| 26 * Map from context to (map from file path to timestamp of last event) | 29 * Map from context to (map from file path to timestamp of last event) |
| 27 */ | 30 */ |
| 28 final Map<String, Map<String, int>> currentContextFilePaths = <String, Map<Str
ing, int>>{}; | 31 final Map<String, Map<String, int>> currentContextFilePaths = <String, Map<Str
ing, int>>{}; |
| 29 | 32 |
| 33 /** |
| 34 * Map from context to package map |
| 35 */ |
| 36 final Map<String, Map<String, List<Folder>>> currentContextPackageMaps = |
| 37 <String, Map<String, List<Folder>>>{}; |
| 38 |
| 30 @override | 39 @override |
| 31 void addContext(Folder folder) { | 40 void addContext(Folder folder, Map<String, List<Folder>> packageMap) { |
| 32 String path = folder.path; | 41 String path = folder.path; |
| 33 currentContextPaths.add(path); | 42 currentContextPaths.add(path); |
| 34 currentContextFilePaths[path] = <String, int>{}; | 43 currentContextFilePaths[path] = <String, int>{}; |
| 44 currentContextPackageMaps[path] = packageMap; |
| 35 } | 45 } |
| 36 | 46 |
| 37 @override | 47 @override |
| 38 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { | 48 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { |
| 39 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; | 49 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; |
| 40 for (Source source in changeSet.addedSources) { | 50 for (Source source in changeSet.addedSources) { |
| 41 expect(filePaths, isNot(contains(source.fullName))); | 51 expect(filePaths, isNot(contains(source.fullName))); |
| 42 filePaths[source.fullName] = now; | 52 filePaths[source.fullName] = now; |
| 43 } | 53 } |
| 44 for (Source source in changeSet.removedSources) { | 54 for (Source source in changeSet.removedSources) { |
| 45 expect(filePaths, contains(source.fullName)); | 55 expect(filePaths, contains(source.fullName)); |
| 46 filePaths.remove(source.fullName); | 56 filePaths.remove(source.fullName); |
| 47 } | 57 } |
| 48 for (Source source in changeSet.changedSources) { | 58 for (Source source in changeSet.changedSources) { |
| 49 expect(filePaths, contains(source.fullName)); | 59 expect(filePaths, contains(source.fullName)); |
| 50 filePaths[source.fullName] = now; | 60 filePaths[source.fullName] = now; |
| 51 } | 61 } |
| 52 } | 62 } |
| 53 | 63 |
| 54 @override | 64 @override |
| 55 void removeContext(Folder folder) { | 65 void removeContext(Folder folder) { |
| 56 String path = folder.path; | 66 String path = folder.path; |
| 57 currentContextPaths.remove(path); | 67 currentContextPaths.remove(path); |
| 58 currentContextFilePaths.remove(path); | 68 currentContextFilePaths.remove(path); |
| 69 currentContextPackageMaps.remove(path); |
| 70 } |
| 71 |
| 72 @override |
| 73 void updateContextPackageMap(Folder contextFolder, |
| 74 Map<String, List<Folder>> packageMap) { |
| 75 currentContextPackageMaps[contextFolder.path]= packageMap; |
| 59 } | 76 } |
| 60 } | 77 } |
| 61 | 78 |
| 62 main() { | 79 main() { |
| 63 groupSep = ' | '; | 80 groupSep = ' | '; |
| 64 | 81 |
| 65 group('ContextDirectoryManager', () { | 82 group('ContextDirectoryManager', () { |
| 66 TestContextDirectoryManager manager; | 83 TestContextDirectoryManager manager; |
| 67 MemoryResourceProvider provider; | 84 MemoryResourceProvider resourceProvider; |
| 85 MockPackageMapProvider packageMapProvider; |
| 68 | 86 |
| 69 setUp(() { | 87 setUp(() { |
| 70 provider = new MemoryResourceProvider(); | 88 resourceProvider = new MemoryResourceProvider(); |
| 71 manager = new TestContextDirectoryManager(provider); | 89 packageMapProvider = new MockPackageMapProvider(); |
| 90 manager = new TestContextDirectoryManager(resourceProvider, packageMapProv
ider); |
| 72 }); | 91 }); |
| 73 | 92 |
| 74 test('add folder with pubspec', () { | 93 test('add folder with pubspec', () { |
| 75 String projPath = '/my/proj'; | 94 String projPath = '/my/proj'; |
| 76 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 95 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 77 provider.newFolder(projPath); | 96 resourceProvider.newFolder(projPath); |
| 78 provider.newFile(pubspecPath, 'pubspec'); | 97 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 79 manager.setRoots(<String>[projPath], <String>[]); | 98 manager.setRoots(<String>[projPath], <String>[]); |
| 80 expect(manager.currentContextPaths, hasLength(1)); | 99 expect(manager.currentContextPaths, hasLength(1)); |
| 81 expect(manager.currentContextPaths, contains(projPath)); | 100 expect(manager.currentContextPaths, contains(projPath)); |
| 82 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 101 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 83 }); | 102 }); |
| 84 | 103 |
| 104 test('newly added folders get proper package map', () { |
| 105 String projPath = '/my/proj'; |
| 106 String packagePath = '/package/foo'; |
| 107 resourceProvider.newFolder(projPath); |
| 108 Folder packageFolder = resourceProvider.newFolder(packagePath); |
| 109 packageMapProvider.packageMap = {'foo': [packageFolder]}; |
| 110 manager.setRoots(<String>[projPath], <String>[]); |
| 111 expect(manager.currentContextPackageMaps[projPath], |
| 112 equals(packageMapProvider.packageMap)); |
| 113 }); |
| 114 |
| 85 test('add folder without pubspec', () { | 115 test('add folder without pubspec', () { |
| 86 String projPath = '/my/proj'; | 116 String projPath = '/my/proj'; |
| 87 provider.newFolder(projPath); | 117 resourceProvider.newFolder(projPath); |
| 88 manager.setRoots(<String>[projPath], <String>[]); | 118 manager.setRoots(<String>[projPath], <String>[]); |
| 89 expect(manager.currentContextPaths, hasLength(1)); | 119 expect(manager.currentContextPaths, hasLength(1)); |
| 90 expect(manager.currentContextPaths, contains(projPath)); | 120 expect(manager.currentContextPaths, contains(projPath)); |
| 91 expect(manager.currentContextFilePaths[projPath], hasLength(0)); | 121 expect(manager.currentContextFilePaths[projPath], hasLength(0)); |
| 92 }); | 122 }); |
| 93 | 123 |
| 94 test('add folder with dart file', () { | 124 test('add folder with dart file', () { |
| 95 String projPath = '/my/proj'; | 125 String projPath = '/my/proj'; |
| 96 provider.newFolder(projPath); | 126 resourceProvider.newFolder(projPath); |
| 97 String filePath = posix.join(projPath, 'foo.dart'); | 127 String filePath = posix.join(projPath, 'foo.dart'); |
| 98 provider.newFile(filePath, 'contents'); | 128 resourceProvider.newFile(filePath, 'contents'); |
| 99 manager.setRoots(<String>[projPath], <String>[]); | 129 manager.setRoots(<String>[projPath], <String>[]); |
| 100 var filePaths = manager.currentContextFilePaths[projPath]; | 130 var filePaths = manager.currentContextFilePaths[projPath]; |
| 101 expect(filePaths, hasLength(1)); | 131 expect(filePaths, hasLength(1)); |
| 102 expect(filePaths, contains(filePath)); | 132 expect(filePaths, contains(filePath)); |
| 103 }); | 133 }); |
| 104 | 134 |
| 105 test('add folder with dart file in subdir', () { | 135 test('add folder with dart file in subdir', () { |
| 106 String projPath = '/my/proj'; | 136 String projPath = '/my/proj'; |
| 107 provider.newFolder(projPath); | 137 resourceProvider.newFolder(projPath); |
| 108 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | 138 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 109 provider.newFile(filePath, 'contents'); | 139 resourceProvider.newFile(filePath, 'contents'); |
| 110 manager.setRoots(<String>[projPath], <String>[]); | 140 manager.setRoots(<String>[projPath], <String>[]); |
| 111 var filePaths = manager.currentContextFilePaths[projPath]; | 141 var filePaths = manager.currentContextFilePaths[projPath]; |
| 112 expect(filePaths, hasLength(1)); | 142 expect(filePaths, hasLength(1)); |
| 113 expect(filePaths, contains(filePath)); | 143 expect(filePaths, contains(filePath)); |
| 114 }); | 144 }); |
| 115 | 145 |
| 116 test('remove folder with pubspec', () { | 146 test('remove folder with pubspec', () { |
| 117 String projPath = '/my/proj'; | 147 String projPath = '/my/proj'; |
| 118 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 148 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 119 provider.newFolder(projPath); | 149 resourceProvider.newFolder(projPath); |
| 120 provider.newFile(pubspecPath, 'pubspec'); | 150 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 121 manager.setRoots(<String>[projPath], <String>[]); | 151 manager.setRoots(<String>[projPath], <String>[]); |
| 122 manager.setRoots(<String>[], <String>[]); | 152 manager.setRoots(<String>[], <String>[]); |
| 123 expect(manager.currentContextPaths, hasLength(0)); | 153 expect(manager.currentContextPaths, hasLength(0)); |
| 124 expect(manager.currentContextFilePaths, hasLength(0)); | 154 expect(manager.currentContextFilePaths, hasLength(0)); |
| 125 }); | 155 }); |
| 126 | 156 |
| 127 test('remove folder without pubspec', () { | 157 test('remove folder without pubspec', () { |
| 128 String projPath = '/my/proj'; | 158 String projPath = '/my/proj'; |
| 129 provider.newFolder(projPath); | 159 resourceProvider.newFolder(projPath); |
| 130 manager.setRoots(<String>[projPath], <String>[]); | 160 manager.setRoots(<String>[projPath], <String>[]); |
| 131 manager.setRoots(<String>[], <String>[]); | 161 manager.setRoots(<String>[], <String>[]); |
| 132 expect(manager.currentContextPaths, hasLength(0)); | 162 expect(manager.currentContextPaths, hasLength(0)); |
| 133 expect(manager.currentContextFilePaths, hasLength(0)); | 163 expect(manager.currentContextFilePaths, hasLength(0)); |
| 134 }); | 164 }); |
| 135 | 165 |
| 136 test('ignore files in packages dir', () { | 166 test('ignore files in packages dir', () { |
| 137 String projPath = '/my/proj'; | 167 String projPath = '/my/proj'; |
| 138 provider.newFolder(projPath); | 168 resourceProvider.newFolder(projPath); |
| 139 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); | 169 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); |
| 140 provider.newFile(pubspecPath, 'pubspec'); | 170 resourceProvider.newFile(pubspecPath, 'pubspec'); |
| 141 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); | 171 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); |
| 142 provider.newFile(filePath1, 'contents'); | 172 resourceProvider.newFile(filePath1, 'contents'); |
| 143 manager.setRoots(<String>[projPath], <String>[]); | 173 manager.setRoots(<String>[projPath], <String>[]); |
| 144 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 174 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 145 expect(filePaths, hasLength(0)); | 175 expect(filePaths, hasLength(0)); |
| 146 String filePath2 = posix.join(projPath, 'packages', 'file2.dart'); | 176 String filePath2 = posix.join(projPath, 'packages', 'file2.dart'); |
| 147 provider.newFile(filePath2, 'contents'); | 177 resourceProvider.newFile(filePath2, 'contents'); |
| 148 return pumpEventQueue().then((_) { | 178 return pumpEventQueue().then((_) { |
| 149 expect(filePaths, hasLength(0)); | 179 expect(filePaths, hasLength(0)); |
| 150 }); | 180 }); |
| 151 }); | 181 }); |
| 152 | 182 |
| 153 group('detect context modifications', () { | 183 group('detect context modifications', () { |
| 154 String projPath; | 184 String projPath; |
| 155 | 185 |
| 156 setUp(() { | 186 setUp(() { |
| 157 projPath = '/my/proj'; | 187 projPath = '/my/proj'; |
| 158 provider.newFolder(projPath); | 188 resourceProvider.newFolder(projPath); |
| 159 }); | 189 }); |
| 160 | 190 |
| 161 test('Add file', () { | 191 test('Add file', () { |
| 162 manager.setRoots(<String>[projPath], <String>[]); | 192 manager.setRoots(<String>[projPath], <String>[]); |
| 163 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 193 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 164 expect(filePaths, hasLength(0)); | 194 expect(filePaths, hasLength(0)); |
| 165 String filePath = posix.join(projPath, 'foo.dart'); | 195 String filePath = posix.join(projPath, 'foo.dart'); |
| 166 provider.newFile(filePath, 'contents'); | 196 resourceProvider.newFile(filePath, 'contents'); |
| 167 return pumpEventQueue().then((_) { | 197 return pumpEventQueue().then((_) { |
| 168 expect(filePaths, hasLength(1)); | 198 expect(filePaths, hasLength(1)); |
| 169 expect(filePaths, contains(filePath)); | 199 expect(filePaths, contains(filePath)); |
| 170 }); | 200 }); |
| 171 }); | 201 }); |
| 172 | 202 |
| 173 test('Add file in subdirectory', () { | 203 test('Add file in subdirectory', () { |
| 174 manager.setRoots(<String>[projPath], <String>[]); | 204 manager.setRoots(<String>[projPath], <String>[]); |
| 175 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 205 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 176 expect(filePaths, hasLength(0)); | 206 expect(filePaths, hasLength(0)); |
| 177 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | 207 String filePath = posix.join(projPath, 'foo', 'bar.dart'); |
| 178 provider.newFile(filePath, 'contents'); | 208 resourceProvider.newFile(filePath, 'contents'); |
| 179 return pumpEventQueue().then((_) { | 209 return pumpEventQueue().then((_) { |
| 180 expect(filePaths, hasLength(1)); | 210 expect(filePaths, hasLength(1)); |
| 181 expect(filePaths, contains(filePath)); | 211 expect(filePaths, contains(filePath)); |
| 182 }); | 212 }); |
| 183 }); | 213 }); |
| 184 | 214 |
| 185 test('Delete file', () { | 215 test('Delete file', () { |
| 186 String filePath = posix.join(projPath, 'foo.dart'); | 216 String filePath = posix.join(projPath, 'foo.dart'); |
| 187 provider.newFile(filePath, 'contents'); | 217 resourceProvider.newFile(filePath, 'contents'); |
| 188 manager.setRoots(<String>[projPath], <String>[]); | 218 manager.setRoots(<String>[projPath], <String>[]); |
| 189 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 219 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 190 expect(filePaths, hasLength(1)); | 220 expect(filePaths, hasLength(1)); |
| 191 expect(filePaths, contains(filePath)); | 221 expect(filePaths, contains(filePath)); |
| 192 provider.deleteFile(filePath); | 222 resourceProvider.deleteFile(filePath); |
| 193 return pumpEventQueue().then((_) => expect(filePaths, hasLength(0))); | 223 return pumpEventQueue().then((_) => expect(filePaths, hasLength(0))); |
| 194 }); | 224 }); |
| 195 | 225 |
| 196 test('Modify file', () { | 226 test('Modify file', () { |
| 197 String filePath = posix.join(projPath, 'foo.dart'); | 227 String filePath = posix.join(projPath, 'foo.dart'); |
| 198 provider.newFile(filePath, 'contents'); | 228 resourceProvider.newFile(filePath, 'contents'); |
| 199 manager.setRoots(<String>[projPath], <String>[]); | 229 manager.setRoots(<String>[projPath], <String>[]); |
| 200 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; | 230 Map<String, int> filePaths = manager.currentContextFilePaths[projPath]; |
| 201 expect(filePaths, hasLength(1)); | 231 expect(filePaths, hasLength(1)); |
| 202 expect(filePaths, contains(filePath)); | 232 expect(filePaths, contains(filePath)); |
| 203 expect(filePaths[filePath], equals(manager.now)); | 233 expect(filePaths[filePath], equals(manager.now)); |
| 204 manager.now++; | 234 manager.now++; |
| 205 provider.modifyFile(filePath, 'new contents'); | 235 resourceProvider.modifyFile(filePath, 'new contents'); |
| 206 return pumpEventQueue().then((_) => expect(filePaths[filePath], equals( | 236 return pumpEventQueue().then((_) => expect(filePaths[filePath], equals( |
| 207 manager.now))); | 237 manager.now))); |
| 208 }); | 238 }); |
| 239 |
| 240 test('Modify package map dependency', () { |
| 241 String dependencyPath = posix.join(projPath, 'dep'); |
| 242 resourceProvider.newFile(dependencyPath, 'contents'); |
| 243 String dartFilePath = posix.join(projPath, 'main.dart'); |
| 244 resourceProvider.newFile(dartFilePath, 'contents'); |
| 245 packageMapProvider.dependencies.add(dependencyPath); |
| 246 manager.setRoots(<String>[projPath], <String>[]); |
| 247 expect(manager.currentContextPackageMaps[projPath], |
| 248 equals(packageMapProvider.packageMap)); |
| 249 String packagePath = '/package/foo'; |
| 250 resourceProvider.newFolder(projPath); |
| 251 packageMapProvider.packageMap = {'foo': projPath}; |
| 252 // Changing a .dart file in the project shouldn't cause a new |
| 253 // package map to be picked up. |
| 254 resourceProvider.modifyFile(dartFilePath, 'new contents'); |
| 255 return pumpEventQueue().then((_) { |
| 256 expect(manager.currentContextPackageMaps[projPath], isEmpty); |
| 257 // However, changing the package map dependency should. |
| 258 resourceProvider.modifyFile(dependencyPath, 'new contents'); |
| 259 return pumpEventQueue().then((_) { |
| 260 expect(manager.currentContextPackageMaps[projPath], |
| 261 equals(packageMapProvider.packageMap)); |
| 262 }); |
| 263 }); |
| 264 }); |
| 209 }); | 265 }); |
| 210 }); | 266 }); |
| 211 } | 267 } |
| OLD | NEW |