| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library test.source.optimizing_pub_package_map_provider; | |
| 6 | |
| 7 import 'dart:convert'; | |
| 8 import 'dart:io' as io; | |
| 9 | |
| 10 import 'package:analysis_server/src/source/optimizing_pub_package_map_provider.d
art'; | |
| 11 import 'package:analyzer/file_system/file_system.dart'; | |
| 12 import 'package:analyzer/file_system/memory_file_system.dart'; | |
| 13 import 'package:path/path.dart'; | |
| 14 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 15 import 'package:unittest/unittest.dart'; | |
| 16 | |
| 17 main() { | |
| 18 groupSep = ' | '; | |
| 19 defineReflectiveTests(OptimizingPubPackageMapProviderTest); | |
| 20 defineReflectiveTests(OptimizingPubPackageMapInfoTest); | |
| 21 } | |
| 22 | |
| 23 @reflectiveTest | |
| 24 class OptimizingPubPackageMapInfoTest { | |
| 25 MemoryResourceProvider resourceProvider; | |
| 26 | |
| 27 int createFile(String path) { | |
| 28 return resourceProvider.newFile(path, 'contents').modificationStamp; | |
| 29 } | |
| 30 | |
| 31 void createFolder(String path) { | |
| 32 resourceProvider.newFolder(path); | |
| 33 } | |
| 34 | |
| 35 void modifyFile(String path) { | |
| 36 resourceProvider.modifyFile(path, 'contents'); | |
| 37 } | |
| 38 | |
| 39 void setUp() { | |
| 40 resourceProvider = new MemoryResourceProvider(); | |
| 41 } | |
| 42 | |
| 43 test_isChangedDependency_fileNotPresent() { | |
| 44 String path = '/dep'; | |
| 45 int timestamp = 1; | |
| 46 OptimizingPubPackageMapInfo info = | |
| 47 new OptimizingPubPackageMapInfo({}, [path].toSet(), {path: timestamp}); | |
| 48 expect(info.isChangedDependency(path, resourceProvider), isTrue); | |
| 49 } | |
| 50 | |
| 51 test_isChangedDependency_matchingTimestamp() { | |
| 52 String path = '/dep'; | |
| 53 int timestamp = createFile(path); | |
| 54 OptimizingPubPackageMapInfo info = | |
| 55 new OptimizingPubPackageMapInfo({}, [path].toSet(), {path: timestamp}); | |
| 56 expect(info.isChangedDependency(path, resourceProvider), isFalse); | |
| 57 } | |
| 58 | |
| 59 test_isChangedDependency_mismatchedTimestamp() { | |
| 60 String path = '/dep'; | |
| 61 int timestamp = createFile(path); | |
| 62 OptimizingPubPackageMapInfo info = | |
| 63 new OptimizingPubPackageMapInfo({}, [path].toSet(), {path: timestamp}); | |
| 64 modifyFile(path); | |
| 65 expect(info.isChangedDependency(path, resourceProvider), isTrue); | |
| 66 } | |
| 67 | |
| 68 test_isChangedDependency_nonDependency() { | |
| 69 OptimizingPubPackageMapInfo info = | |
| 70 new OptimizingPubPackageMapInfo({}, ['/dep1'].toSet(), {}); | |
| 71 expect(info.isChangedDependency('/dep2', resourceProvider), isFalse); | |
| 72 } | |
| 73 | |
| 74 test_isChangedDependency_nonFile() { | |
| 75 String path = '/dep'; | |
| 76 int timestamp = 1; | |
| 77 createFolder(path); | |
| 78 OptimizingPubPackageMapInfo info = | |
| 79 new OptimizingPubPackageMapInfo({}, [path].toSet(), {path: timestamp}); | |
| 80 expect(info.isChangedDependency(path, resourceProvider), isTrue); | |
| 81 } | |
| 82 | |
| 83 test_isChangedDependency_noTimestampInfo() { | |
| 84 String path = '/dep'; | |
| 85 OptimizingPubPackageMapInfo info = | |
| 86 new OptimizingPubPackageMapInfo({}, [path].toSet(), {}); | |
| 87 expect(info.isChangedDependency(path, resourceProvider), isTrue); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 @reflectiveTest | |
| 92 class OptimizingPubPackageMapProviderTest { | |
| 93 MemoryResourceProvider resourceProvider; | |
| 94 OptimizingPubPackageMapProvider provider; | |
| 95 Folder projectFolder; | |
| 96 io.ProcessResult pubListResult; | |
| 97 | |
| 98 void setPubListError() { | |
| 99 pubListResult = new _MockProcessResult(0, 1, '', 'ERROR'); | |
| 100 } | |
| 101 | |
| 102 void setPubListResult({Map<String, String> packages: const {}, | |
| 103 List<String> input_files: const []}) { | |
| 104 pubListResult = new _MockProcessResult(0, 0, | |
| 105 JSON.encode({'packages': packages, 'input_files': input_files}), ''); | |
| 106 } | |
| 107 | |
| 108 void setUp() { | |
| 109 resourceProvider = new MemoryResourceProvider(); | |
| 110 provider = new OptimizingPubPackageMapProvider( | |
| 111 resourceProvider, null, _runPubList); | |
| 112 projectFolder = resourceProvider.newFolder('/my/proj'); | |
| 113 } | |
| 114 | |
| 115 test_computePackageMap_noPreviousInfo() { | |
| 116 String dep = posix.join(projectFolder.path, 'dep'); | |
| 117 String pkgName = 'foo'; | |
| 118 String pkgPath = '/pkg/foo'; | |
| 119 setPubListResult(packages: {pkgName: pkgPath}, input_files: [dep]); | |
| 120 OptimizingPubPackageMapInfo info = | |
| 121 provider.computePackageMap(projectFolder); | |
| 122 expect(info.dependencies, hasLength(1)); | |
| 123 expect(info.dependencies, contains(dep)); | |
| 124 expect(info.packageMap, hasLength(1)); | |
| 125 expect(info.packageMap, contains(pkgName)); | |
| 126 expect(info.packageMap[pkgName], hasLength(1)); | |
| 127 expect(info.packageMap[pkgName][0].path, pkgPath); | |
| 128 expect(info.modificationTimes, isEmpty); | |
| 129 } | |
| 130 | |
| 131 test_computePackageMap_noPreviousInfo_pubListError() { | |
| 132 String pubspecLock = posix.join(projectFolder.path, 'pubspec.lock'); | |
| 133 setPubListError(); | |
| 134 OptimizingPubPackageMapInfo info = | |
| 135 provider.computePackageMap(projectFolder); | |
| 136 expect(info.dependencies, hasLength(1)); | |
| 137 expect(info.dependencies, contains(pubspecLock)); | |
| 138 expect(info.packageMap, isNull); | |
| 139 expect(info.modificationTimes, isEmpty); | |
| 140 } | |
| 141 | |
| 142 test_computePackageMap_withPreviousInfo() { | |
| 143 String dep = posix.join(projectFolder.path, 'dep'); | |
| 144 int timestamp = resourceProvider.newFile(dep, 'contents').modificationStamp; | |
| 145 setPubListResult(input_files: [dep]); | |
| 146 OptimizingPubPackageMapInfo info1 = | |
| 147 provider.computePackageMap(projectFolder); | |
| 148 OptimizingPubPackageMapInfo info2 = | |
| 149 provider.computePackageMap(projectFolder, info1); | |
| 150 expect(info2.dependencies, hasLength(1)); | |
| 151 expect(info2.dependencies, contains(dep)); | |
| 152 expect(info2.modificationTimes, hasLength(1)); | |
| 153 expect(info2.modificationTimes, contains(dep)); | |
| 154 expect(info2.modificationTimes[dep], timestamp); | |
| 155 } | |
| 156 | |
| 157 test_computePackageMap_withPreviousInfo_newDependency() { | |
| 158 String dep = posix.join(projectFolder.path, 'dep'); | |
| 159 resourceProvider.newFile(dep, 'contents').modificationStamp; | |
| 160 setPubListResult(input_files: []); | |
| 161 OptimizingPubPackageMapInfo info1 = | |
| 162 provider.computePackageMap(projectFolder); | |
| 163 setPubListResult(input_files: [dep]); | |
| 164 OptimizingPubPackageMapInfo info2 = | |
| 165 provider.computePackageMap(projectFolder, info1); | |
| 166 expect(info2.modificationTimes, isEmpty); | |
| 167 } | |
| 168 | |
| 169 test_computePackageMap_withPreviousInfo_oldDependencyNoLongerAFile() { | |
| 170 String dep = posix.join(projectFolder.path, 'dep'); | |
| 171 resourceProvider.newFile(dep, 'contents').modificationStamp; | |
| 172 setPubListResult(input_files: [dep]); | |
| 173 OptimizingPubPackageMapInfo info1 = | |
| 174 provider.computePackageMap(projectFolder); | |
| 175 resourceProvider.deleteFile(dep); | |
| 176 resourceProvider.newFolder(dep); | |
| 177 OptimizingPubPackageMapInfo info2 = | |
| 178 provider.computePackageMap(projectFolder, info1); | |
| 179 expect(info2.modificationTimes, isEmpty); | |
| 180 } | |
| 181 | |
| 182 test_computePackageMap_withPreviousInfo_oldDependencyNoLongerPresent() { | |
| 183 String dep = posix.join(projectFolder.path, 'dep'); | |
| 184 resourceProvider.newFile(dep, 'contents').modificationStamp; | |
| 185 setPubListResult(input_files: [dep]); | |
| 186 OptimizingPubPackageMapInfo info1 = | |
| 187 provider.computePackageMap(projectFolder); | |
| 188 resourceProvider.deleteFile(dep); | |
| 189 OptimizingPubPackageMapInfo info2 = | |
| 190 provider.computePackageMap(projectFolder, info1); | |
| 191 expect(info2.modificationTimes, isEmpty); | |
| 192 } | |
| 193 | |
| 194 test_computePackageMap_withPreviousInfo_oldDependencyNoLongerRelevant() { | |
| 195 String dep = posix.join(projectFolder.path, 'dep'); | |
| 196 resourceProvider.newFile(dep, 'contents').modificationStamp; | |
| 197 setPubListResult(input_files: [dep]); | |
| 198 OptimizingPubPackageMapInfo info1 = | |
| 199 provider.computePackageMap(projectFolder); | |
| 200 setPubListResult(input_files: []); | |
| 201 OptimizingPubPackageMapInfo info2 = | |
| 202 provider.computePackageMap(projectFolder, info1); | |
| 203 expect(info2.modificationTimes, isEmpty); | |
| 204 } | |
| 205 | |
| 206 test_computePackageMap_withPreviousInfo_pubListError() { | |
| 207 String dep = posix.join(projectFolder.path, 'dep'); | |
| 208 String pubspecLock = posix.join(projectFolder.path, 'pubspec.lock'); | |
| 209 int timestamp = resourceProvider.newFile(dep, 'contents').modificationStamp; | |
| 210 setPubListResult(input_files: [dep]); | |
| 211 OptimizingPubPackageMapInfo info1 = | |
| 212 provider.computePackageMap(projectFolder); | |
| 213 setPubListError(); | |
| 214 OptimizingPubPackageMapInfo info2 = | |
| 215 provider.computePackageMap(projectFolder, info1); | |
| 216 expect(info2.dependencies, hasLength(2)); | |
| 217 expect(info2.dependencies, contains(dep)); | |
| 218 expect(info2.dependencies, contains(pubspecLock)); | |
| 219 expect(info2.modificationTimes, hasLength(1)); | |
| 220 expect(info2.modificationTimes, contains(dep)); | |
| 221 expect(info2.modificationTimes[dep], timestamp); | |
| 222 } | |
| 223 | |
| 224 io.ProcessResult _runPubList(Folder folder) { | |
| 225 expect(folder, projectFolder); | |
| 226 return pubListResult; | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 class _MockProcessResult implements io.ProcessResult { | |
| 231 @override | |
| 232 final int pid; | |
| 233 | |
| 234 @override | |
| 235 final int exitCode; | |
| 236 | |
| 237 @override | |
| 238 final stdout; | |
| 239 | |
| 240 @override | |
| 241 final stderr; | |
| 242 | |
| 243 _MockProcessResult(this.pid, this.exitCode, this.stdout, this.stderr); | |
| 244 } | |
| OLD | NEW |