OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:convert'; |
| 6 import 'dart:typed_data'; |
| 7 |
| 8 import 'package:analyzer/file_system/file_system.dart'; |
| 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 10 import 'package:analyzer/source/package_map_resolver.dart'; |
| 11 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 12 import 'package:analyzer/src/dart/analysis/driver.dart' show PerformanceLog; |
| 13 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart' |
| 15 show AnalysisOptions, AnalysisOptionsImpl; |
| 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/util/fast_uri.dart'; |
| 18 import 'package:convert/convert.dart'; |
| 19 import 'package:crypto/crypto.dart'; |
| 20 import 'package:test/test.dart'; |
| 21 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 22 |
| 23 import '../../context/mock_sdk.dart'; |
| 24 |
| 25 main() { |
| 26 defineReflectiveSuite(() { |
| 27 defineReflectiveTests(FileSystemStateTest); |
| 28 }); |
| 29 } |
| 30 |
| 31 @reflectiveTest |
| 32 class FileSystemStateTest { |
| 33 static final MockSdk sdk = new MockSdk(); |
| 34 |
| 35 final MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 36 final ByteStore byteStore = new MemoryByteStore(); |
| 37 final FileContentOverlay contentOverlay = new FileContentOverlay(); |
| 38 |
| 39 final StringBuffer logBuffer = new StringBuffer(); |
| 40 PerformanceLog logger; |
| 41 |
| 42 FileSystemState fileSystemState; |
| 43 |
| 44 void setUp() { |
| 45 logger = new PerformanceLog(logBuffer); |
| 46 SourceFactory sourceFactory = new SourceFactory([ |
| 47 new DartUriResolver(sdk), |
| 48 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 49 'aaa': [provider.getFolder(_p('/aaa/lib'))], |
| 50 'bbb': [provider.getFolder(_p('/bbb/lib'))], |
| 51 }), |
| 52 new ResourceUriResolver(provider) |
| 53 ], null, provider); |
| 54 AnalysisOptions analysisOptions = new AnalysisOptionsImpl() |
| 55 ..strongMode = true; |
| 56 fileSystemState = new FileSystemState(logger, byteStore, contentOverlay, |
| 57 provider, sourceFactory, analysisOptions, new Uint32List(0)); |
| 58 } |
| 59 |
| 60 test_getFileForPath_doesNotExist() { |
| 61 String path = _p('/aaa/lib/a.dart'); |
| 62 FileState file = fileSystemState.getFileForPath(path); |
| 63 expect(file.path, path); |
| 64 expect(file.uri, FastUri.parse('package:aaa/a.dart')); |
| 65 expect(file.content, ''); |
| 66 expect(file.contentHash, _md5('')); |
| 67 expect(file.importedFiles, isEmpty); |
| 68 expect(file.exportedFiles, isEmpty); |
| 69 expect(file.partedFiles, isEmpty); |
| 70 expect(file.dependencies, isEmpty); |
| 71 expect(file.isPart, isFalse); |
| 72 expect(file.library, isNull); |
| 73 expect(file.unlinked, isNotNull); |
| 74 expect(file.unlinked.classes, isEmpty); |
| 75 } |
| 76 |
| 77 test_getFileForPath_library() { |
| 78 String a1 = _p('/aaa/lib/a1.dart'); |
| 79 String a2 = _p('/aaa/lib/a2.dart'); |
| 80 String a3 = _p('/aaa/lib/a3.dart'); |
| 81 String a4 = _p('/aaa/lib/a4.dart'); |
| 82 String b1 = _p('/bbb/lib/b1.dart'); |
| 83 String b2 = _p('/bbb/lib/b2.dart'); |
| 84 String content_a1 = r''' |
| 85 import 'package:aaa/a2.dart'; |
| 86 import 'package:bbb/b1.dart'; |
| 87 export 'package:bbb/b2.dart'; |
| 88 export 'package:aaa/a3.dart'; |
| 89 part 'a4.dart'; |
| 90 |
| 91 class A1 {} |
| 92 '''; |
| 93 provider.newFile(a1, content_a1); |
| 94 |
| 95 FileState file = fileSystemState.getFileForPath(a1); |
| 96 expect(file.path, a1); |
| 97 expect(file.content, content_a1); |
| 98 expect(file.contentHash, _md5(content_a1)); |
| 99 |
| 100 expect(file.isPart, isFalse); |
| 101 expect(file.library, isNull); |
| 102 expect(file.unlinked, isNotNull); |
| 103 expect(file.unlinked.classes, hasLength(1)); |
| 104 expect(file.unlinked.classes[0].name, 'A1'); |
| 105 |
| 106 expect(file.importedFiles, hasLength(2)); |
| 107 expect(file.importedFiles[0].path, a2); |
| 108 expect(file.importedFiles[0].uri, FastUri.parse('package:aaa/a2.dart')); |
| 109 expect(file.importedFiles[0].source, isNotNull); |
| 110 expect(file.importedFiles[1].path, b1); |
| 111 expect(file.importedFiles[1].uri, FastUri.parse('package:bbb/b1.dart')); |
| 112 expect(file.importedFiles[1].source, isNotNull); |
| 113 |
| 114 expect(file.exportedFiles, hasLength(2)); |
| 115 expect(file.exportedFiles[0].path, b2); |
| 116 expect(file.exportedFiles[0].uri, FastUri.parse('package:bbb/b2.dart')); |
| 117 expect(file.exportedFiles[0].source, isNotNull); |
| 118 expect(file.exportedFiles[1].path, a3); |
| 119 expect(file.exportedFiles[1].uri, FastUri.parse('package:aaa/a3.dart')); |
| 120 expect(file.exportedFiles[1].source, isNotNull); |
| 121 |
| 122 expect(file.partedFiles, hasLength(1)); |
| 123 expect(file.partedFiles[0].path, a4); |
| 124 expect(file.partedFiles[0].uri, FastUri.parse('package:aaa/a4.dart')); |
| 125 |
| 126 expect(file.dependencies, hasLength(5)); |
| 127 |
| 128 expect(fileSystemState.getFilesForPath(a1), [file]); |
| 129 } |
| 130 |
| 131 test_getFileForPath_part() { |
| 132 String a1 = _p('/aaa/lib/a1.dart'); |
| 133 String a2 = _p('/aaa/lib/a2.dart'); |
| 134 provider.newFile( |
| 135 a1, |
| 136 r''' |
| 137 library a1; |
| 138 part 'a2.dart'; |
| 139 '''); |
| 140 provider.newFile( |
| 141 a2, |
| 142 r''' |
| 143 part of a1; |
| 144 class A2 {} |
| 145 '''); |
| 146 |
| 147 FileState file_a2 = fileSystemState.getFileForPath(a2); |
| 148 expect(file_a2.path, a2); |
| 149 expect(file_a2.uri, FastUri.parse('package:aaa/a2.dart')); |
| 150 |
| 151 expect(file_a2.unlinked, isNotNull); |
| 152 expect(file_a2.unlinked.classes, hasLength(1)); |
| 153 expect(file_a2.unlinked.classes[0].name, 'A2'); |
| 154 |
| 155 expect(file_a2.importedFiles, isEmpty); |
| 156 expect(file_a2.exportedFiles, isEmpty); |
| 157 expect(file_a2.partedFiles, isEmpty); |
| 158 expect(file_a2.dependencies, isEmpty); |
| 159 |
| 160 // The library is not known yet. |
| 161 expect(file_a2.isPart, isTrue); |
| 162 expect(file_a2.library, isNull); |
| 163 |
| 164 // Ask for the library. |
| 165 FileState file_a1 = fileSystemState.getFileForPath(a1); |
| 166 expect(file_a1.partedFiles, hasLength(1)); |
| 167 expect(file_a1.partedFiles[0], same(file_a2)); |
| 168 expect(file_a1.dependencies, unorderedEquals([file_a2])); |
| 169 |
| 170 // Now the part knows its library. |
| 171 expect(file_a2.library, same(file_a1)); |
| 172 |
| 173 // Now update the library, and refresh its file. |
| 174 // The 'a2.dart' is not referenced anymore. |
| 175 // So the part file does not have the library anymore. |
| 176 provider.newFile( |
| 177 a1, |
| 178 r''' |
| 179 library a1; |
| 180 part 'not-a2.dart'; |
| 181 '''); |
| 182 file_a1.refresh(); |
| 183 expect(file_a2.library, isNull); |
| 184 } |
| 185 |
| 186 test_getFileForPath_samePath() { |
| 187 String path = _p('/aaa/lib/a.dart'); |
| 188 FileState file1 = fileSystemState.getFileForPath(path); |
| 189 FileState file2 = fileSystemState.getFileForPath(path); |
| 190 expect(file2, same(file1)); |
| 191 } |
| 192 |
| 193 test_getFileForUri_packageVsFileUri() { |
| 194 String path = _p('/aaa/lib/a.dart'); |
| 195 var packageUri = FastUri.parse('package:aaa/a.dart'); |
| 196 var fileUri = provider.pathContext.toUri(path); |
| 197 |
| 198 // The files with `package:` and `file:` URIs are different. |
| 199 FileState filePackageUri = fileSystemState.getFileForUri(packageUri); |
| 200 FileState fileFileUri = fileSystemState.getFileForUri(fileUri); |
| 201 expect(filePackageUri, isNot(same(fileFileUri))); |
| 202 |
| 203 expect(filePackageUri.path, path); |
| 204 expect(filePackageUri.uri, packageUri); |
| 205 |
| 206 expect(fileFileUri.path, path); |
| 207 expect(fileFileUri.uri, fileUri); |
| 208 |
| 209 // The file with the `package:` style URI is canonical, and is the first. |
| 210 var files = fileSystemState.getFilesForPath(path); |
| 211 expect(files, [filePackageUri, fileFileUri]); |
| 212 } |
| 213 |
| 214 test_refresh_differentApiSignature() { |
| 215 String path = _p('/aaa/lib/a.dart'); |
| 216 provider.newFile( |
| 217 path, |
| 218 r''' |
| 219 class C { |
| 220 foo() {} |
| 221 } |
| 222 '''); |
| 223 FileState file = fileSystemState.getFileForPath(path); |
| 224 List<int> signature = file.apiSignature; |
| 225 |
| 226 // Update the resource and refresh the file state. |
| 227 provider.newFile( |
| 228 path, |
| 229 r''' |
| 230 class C { |
| 231 bar() {} |
| 232 } |
| 233 '''); |
| 234 bool apiSignatureChanged = file.refresh(); |
| 235 expect(apiSignatureChanged, isTrue); |
| 236 |
| 237 expect(file.apiSignature, isNot(signature)); |
| 238 } |
| 239 |
| 240 test_refresh_sameApiSignature() { |
| 241 String path = _p('/aaa/lib/a.dart'); |
| 242 provider.newFile( |
| 243 path, |
| 244 r''' |
| 245 class C { |
| 246 foo() { |
| 247 print(111); |
| 248 } |
| 249 } |
| 250 '''); |
| 251 FileState file = fileSystemState.getFileForPath(path); |
| 252 List<int> signature = file.apiSignature; |
| 253 |
| 254 // Update the resource and refresh the file state. |
| 255 provider.newFile( |
| 256 path, |
| 257 r''' |
| 258 class C { |
| 259 foo() { |
| 260 print(222); |
| 261 } |
| 262 } |
| 263 '''); |
| 264 bool apiSignatureChanged = file.refresh(); |
| 265 expect(apiSignatureChanged, isFalse); |
| 266 |
| 267 expect(file.apiSignature, signature); |
| 268 } |
| 269 |
| 270 String _p(String path) => provider.convertPath(path); |
| 271 |
| 272 static String _md5(String content) { |
| 273 return hex.encode(md5.convert(UTF8.encode(content)).bytes); |
| 274 } |
| 275 } |
OLD | NEW |