| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.resource; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:analysis_server/src/resource.dart'; | |
| 10 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | |
| 11 import 'package:analyzer/src/generated/source.dart'; | |
| 12 import 'package:path/path.dart'; | |
| 13 import 'package:unittest/unittest.dart'; | |
| 14 import 'package:watcher/watcher.dart'; | |
| 15 | |
| 16 import 'mocks.dart'; | |
| 17 | |
| 18 | |
| 19 var _isFile = new isInstanceOf<File>(); | |
| 20 var _isFolder = new isInstanceOf<Folder>(); | |
| 21 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); | |
| 22 | |
| 23 | |
| 24 main() { | |
| 25 groupSep = ' | '; | |
| 26 | |
| 27 group('MemoryResourceProvider', () { | |
| 28 MemoryResourceProvider provider; | |
| 29 | |
| 30 setUp(() { | |
| 31 provider = new MemoryResourceProvider(); | |
| 32 }); | |
| 33 | |
| 34 test('MemoryResourceException', () { | |
| 35 var exception = new MemoryResourceException('/my/path', 'my message'); | |
| 36 expect(exception.path, '/my/path'); | |
| 37 expect(exception.message, 'my message'); | |
| 38 expect(exception.toString(), | |
| 39 'MemoryResourceException(path=/my/path; message=my message)'); | |
| 40 }); | |
| 41 | |
| 42 group('Watch', () { | |
| 43 | |
| 44 Future delayed(computation()) { | |
| 45 return pumpEventQueue().then((_) => computation()); | |
| 46 } | |
| 47 | |
| 48 watchingFolder(String path, test(List<WatchEvent> changesReceived)) { | |
| 49 Folder folder = provider.getResource(path); | |
| 50 var changesReceived = <WatchEvent>[]; | |
| 51 folder.changes.listen(changesReceived.add); | |
| 52 return test(changesReceived); | |
| 53 } | |
| 54 | |
| 55 test('create file', () { | |
| 56 String rootPath = '/my/path'; | |
| 57 provider.newFolder(rootPath); | |
| 58 watchingFolder(rootPath, (changesReceived) { | |
| 59 expect(changesReceived, hasLength(0)); | |
| 60 String path = posix.join(rootPath, 'foo'); | |
| 61 provider.newFile(path, 'contents'); | |
| 62 return delayed(() { | |
| 63 expect(changesReceived, hasLength(1)); | |
| 64 expect(changesReceived[0].type, equals(ChangeType.ADD)); | |
| 65 expect(changesReceived[0].path, equals(path)); | |
| 66 }); | |
| 67 }); | |
| 68 }); | |
| 69 | |
| 70 test('modify file', () { | |
| 71 String rootPath = '/my/path'; | |
| 72 provider.newFolder(rootPath); | |
| 73 String path = posix.join(rootPath, 'foo'); | |
| 74 provider.newFile(path, 'contents 1'); | |
| 75 return watchingFolder(rootPath, (changesReceived) { | |
| 76 expect(changesReceived, hasLength(0)); | |
| 77 provider.modifyFile(path, 'contents 2'); | |
| 78 return delayed(() { | |
| 79 expect(changesReceived, hasLength(1)); | |
| 80 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); | |
| 81 expect(changesReceived[0].path, equals(path)); | |
| 82 }); | |
| 83 }); | |
| 84 }); | |
| 85 | |
| 86 test('modify file in subdir', () { | |
| 87 String rootPath = '/my/path'; | |
| 88 provider.newFolder(rootPath); | |
| 89 String subdirPath = posix.join(rootPath, 'foo'); | |
| 90 provider.newFolder(subdirPath); | |
| 91 String path = posix.join(rootPath, 'bar'); | |
| 92 provider.newFile(path, 'contents 1'); | |
| 93 return watchingFolder(rootPath, (changesReceived) { | |
| 94 expect(changesReceived, hasLength(0)); | |
| 95 provider.modifyFile(path, 'contents 2'); | |
| 96 return delayed(() { | |
| 97 expect(changesReceived, hasLength(1)); | |
| 98 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); | |
| 99 expect(changesReceived[0].path, equals(path)); | |
| 100 }); | |
| 101 }); | |
| 102 }); | |
| 103 | |
| 104 test('delete file', () { | |
| 105 String rootPath = '/my/path'; | |
| 106 provider.newFolder(rootPath); | |
| 107 String path = posix.join(rootPath, 'foo'); | |
| 108 provider.newFile(path, 'contents 1'); | |
| 109 return watchingFolder(rootPath, (changesReceived) { | |
| 110 expect(changesReceived, hasLength(0)); | |
| 111 provider.deleteFile(path); | |
| 112 return delayed(() { | |
| 113 expect(changesReceived, hasLength(1)); | |
| 114 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); | |
| 115 expect(changesReceived[0].path, equals(path)); | |
| 116 }); | |
| 117 }); | |
| 118 }); | |
| 119 }); | |
| 120 | |
| 121 group('newFolder', () { | |
| 122 test('empty path', () { | |
| 123 expect(() { | |
| 124 provider.newFolder(''); | |
| 125 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 126 }); | |
| 127 | |
| 128 test('not absolute', () { | |
| 129 expect(() { | |
| 130 provider.newFolder('not/absolute'); | |
| 131 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 132 }); | |
| 133 | |
| 134 group('already exists', () { | |
| 135 test('as folder', () { | |
| 136 Folder folder = provider.newFolder('/my/folder'); | |
| 137 Folder newFolder = provider.newFolder('/my/folder'); | |
| 138 expect(newFolder, folder); | |
| 139 }); | |
| 140 | |
| 141 test('as file', () { | |
| 142 File file = provider.newFile('/my/file', 'qwerty'); | |
| 143 expect(() { | |
| 144 provider.newFolder('/my/file'); | |
| 145 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 146 }); | |
| 147 }); | |
| 148 }); | |
| 149 | |
| 150 group('modifyFile', () { | |
| 151 test('nonexistent', () { | |
| 152 String path = '/my/file'; | |
| 153 expect(() { | |
| 154 provider.modifyFile(path, 'contents'); | |
| 155 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 156 Resource file = provider.getResource(path); | |
| 157 expect(file, isNotNull); | |
| 158 expect(file.exists, isFalse); | |
| 159 }); | |
| 160 | |
| 161 test('is folder', () { | |
| 162 String path = '/my/file'; | |
| 163 provider.newFolder(path); | |
| 164 expect(() { | |
| 165 provider.modifyFile(path, 'contents'); | |
| 166 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 167 expect(provider.getResource(path), new isInstanceOf<Folder>()); | |
| 168 }); | |
| 169 | |
| 170 test('successful', () { | |
| 171 String path = '/my/file'; | |
| 172 provider.newFile(path, 'contents 1'); | |
| 173 Resource file = provider.getResource(path); | |
| 174 expect(file, new isInstanceOf<File>()); | |
| 175 Source source = (file as File).createSource(UriKind.FILE_URI); | |
| 176 expect(source.contents.data, equals('contents 1')); | |
| 177 provider.modifyFile(path, 'contents 2'); | |
| 178 expect(source.contents.data, equals('contents 2')); | |
| 179 }); | |
| 180 }); | |
| 181 | |
| 182 group('deleteFile', () { | |
| 183 test('nonexistent', () { | |
| 184 String path = '/my/file'; | |
| 185 expect(() { | |
| 186 provider.deleteFile(path); | |
| 187 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 188 Resource file = provider.getResource(path); | |
| 189 expect(file, isNotNull); | |
| 190 expect(file.exists, isFalse); | |
| 191 }); | |
| 192 | |
| 193 test('is folder', () { | |
| 194 String path = '/my/file'; | |
| 195 provider.newFolder(path); | |
| 196 expect(() { | |
| 197 provider.deleteFile(path); | |
| 198 }, throwsA(new isInstanceOf<ArgumentError>())); | |
| 199 expect(provider.getResource(path), new isInstanceOf<Folder>()); | |
| 200 }); | |
| 201 | |
| 202 test('successful', () { | |
| 203 String path = '/my/file'; | |
| 204 provider.newFile(path, 'contents'); | |
| 205 Resource file = provider.getResource(path); | |
| 206 expect(file, new isInstanceOf<File>()); | |
| 207 expect(file.exists, isTrue); | |
| 208 provider.deleteFile(path); | |
| 209 expect(file.exists, isFalse); | |
| 210 }); | |
| 211 }); | |
| 212 | |
| 213 group('File', () { | |
| 214 group('==', () { | |
| 215 test('false', () { | |
| 216 File fileA = provider.getResource('/fileA.txt'); | |
| 217 File fileB = provider.getResource('/fileB.txt'); | |
| 218 expect(fileA == new Object(), isFalse); | |
| 219 expect(fileA == fileB, isFalse); | |
| 220 }); | |
| 221 | |
| 222 test('true', () { | |
| 223 File file = provider.getResource('/file.txt'); | |
| 224 expect(file == file, isTrue); | |
| 225 }); | |
| 226 | |
| 227 test('before and after creation', () { | |
| 228 String path = '/file.txt'; | |
| 229 File file1 = provider.getResource(path); | |
| 230 provider.newFile(path, 'contents'); | |
| 231 File file2 = provider.getResource(path); | |
| 232 expect(file1 == file2, isTrue); | |
| 233 }); | |
| 234 }); | |
| 235 | |
| 236 group('exists', () { | |
| 237 test('false', () { | |
| 238 File file = provider.getResource('/file.txt'); | |
| 239 expect(file, isNotNull); | |
| 240 expect(file.exists, isFalse); | |
| 241 }); | |
| 242 | |
| 243 test('true', () { | |
| 244 provider.newFile('/foo/file.txt', 'qwerty'); | |
| 245 File file = provider.getResource('/foo/file.txt'); | |
| 246 expect(file, isNotNull); | |
| 247 expect(file.exists, isTrue); | |
| 248 }); | |
| 249 }); | |
| 250 | |
| 251 test('fullName', () { | |
| 252 File file = provider.getResource('/foo/bar/file.txt'); | |
| 253 expect(file.path, '/foo/bar/file.txt'); | |
| 254 }); | |
| 255 | |
| 256 test('hashCode', () { | |
| 257 String path = '/foo/bar/file.txt'; | |
| 258 File file1 = provider.getResource(path); | |
| 259 provider.newFile(path, 'contents'); | |
| 260 File file2 = provider.getResource(path); | |
| 261 expect(file1.hashCode, equals(file2.hashCode)); | |
| 262 }); | |
| 263 | |
| 264 test('shortName', () { | |
| 265 File file = provider.getResource('/foo/bar/file.txt'); | |
| 266 expect(file.shortName, 'file.txt'); | |
| 267 }); | |
| 268 | |
| 269 test('toString', () { | |
| 270 File file = provider.getResource('/foo/bar/file.txt'); | |
| 271 expect(file.toString(), '/foo/bar/file.txt'); | |
| 272 }); | |
| 273 | |
| 274 test('parent', () { | |
| 275 provider.newFile('/foo/bar/file.txt', 'content'); | |
| 276 File file = provider.getResource('/foo/bar/file.txt'); | |
| 277 Resource parent = file.parent; | |
| 278 expect(parent, new isInstanceOf<Folder>()); | |
| 279 expect(parent.path, equals('/foo/bar')); | |
| 280 }); | |
| 281 }); | |
| 282 | |
| 283 group('Folder', () { | |
| 284 Folder folder; | |
| 285 const String path = '/foo/bar'; | |
| 286 | |
| 287 setUp(() { | |
| 288 folder = provider.newFolder(path); | |
| 289 }); | |
| 290 | |
| 291 test('hashCode', () { | |
| 292 Folder folder2 = provider.getResource(path); | |
| 293 expect(folder.hashCode, equals(folder2.hashCode)); | |
| 294 }); | |
| 295 | |
| 296 test('equality: same path', () { | |
| 297 Folder folder2 = provider.getResource(path); | |
| 298 expect(folder == folder2, isTrue); | |
| 299 }); | |
| 300 | |
| 301 test('equality: different paths', () { | |
| 302 String path2 = '/foo/baz'; | |
| 303 Folder folder2 = provider.newFolder(path2); | |
| 304 expect(folder == folder2, isFalse); | |
| 305 }); | |
| 306 | |
| 307 group('getChild', () { | |
| 308 test('does not exist', () { | |
| 309 File file = folder.getChild('file.txt'); | |
| 310 expect(file, isNotNull); | |
| 311 expect(file.exists, isFalse); | |
| 312 }); | |
| 313 | |
| 314 test('file', () { | |
| 315 provider.newFile('/foo/bar/file.txt', 'content'); | |
| 316 File child = folder.getChild('file.txt'); | |
| 317 expect(child, isNotNull); | |
| 318 expect(child.exists, isTrue); | |
| 319 }); | |
| 320 | |
| 321 test('folder', () { | |
| 322 provider.newFolder('/foo/bar/baz'); | |
| 323 Folder child = folder.getChild('baz'); | |
| 324 expect(child, isNotNull); | |
| 325 expect(child.exists, isTrue); | |
| 326 }); | |
| 327 }); | |
| 328 | |
| 329 test('getChildren', () { | |
| 330 provider.newFile('/foo/bar/a.txt', 'aaa'); | |
| 331 provider.newFolder('/foo/bar/bFolder'); | |
| 332 provider.newFile('/foo/bar/c.txt', 'ccc'); | |
| 333 // prepare 3 children | |
| 334 List<Resource> children = folder.getChildren(); | |
| 335 expect(children, hasLength(3)); | |
| 336 children.sort((a, b) => a.shortName.compareTo(b.shortName)); | |
| 337 // check that each child exists | |
| 338 children.forEach((child) { | |
| 339 expect(child.exists, true); | |
| 340 }); | |
| 341 // check names | |
| 342 expect(children[0].shortName, 'a.txt'); | |
| 343 expect(children[1].shortName, 'bFolder'); | |
| 344 expect(children[2].shortName, 'c.txt'); | |
| 345 // check types | |
| 346 expect(children[0], _isFile); | |
| 347 expect(children[1], _isFolder); | |
| 348 expect(children[2], _isFile); | |
| 349 }); | |
| 350 | |
| 351 test('parent', () { | |
| 352 Resource parent1 = folder.parent; | |
| 353 expect(parent1, new isInstanceOf<Folder>()); | |
| 354 expect(parent1.path, equals('/foo')); | |
| 355 Resource parent2 = parent1.parent; | |
| 356 expect(parent2, new isInstanceOf<Folder>()); | |
| 357 expect(parent2.path, equals('/')); | |
| 358 expect(parent2.parent, isNull); | |
| 359 }); | |
| 360 | |
| 361 test('canonicalizePath', () { | |
| 362 expect(folder.canonicalizePath('baz'), equals('/foo/bar/baz')); | |
| 363 expect(folder.canonicalizePath('/baz'), equals('/baz')); | |
| 364 expect(folder.canonicalizePath('../baz'), equals('/foo/baz')); | |
| 365 expect(folder.canonicalizePath('/a/b/../c'), equals('/a/c')); | |
| 366 expect(folder.canonicalizePath('./baz'), equals('/foo/bar/baz')); | |
| 367 expect(folder.canonicalizePath('/a/b/./c'), equals('/a/b/c')); | |
| 368 }); | |
| 369 }); | |
| 370 | |
| 371 group('_MemoryFileSource', () { | |
| 372 Source source; | |
| 373 | |
| 374 group('existent', () { | |
| 375 setUp(() { | |
| 376 File file = provider.newFile('/foo/test.dart', 'library test;'); | |
| 377 source = file.createSource(UriKind.FILE_URI); | |
| 378 }); | |
| 379 | |
| 380 group('==', () { | |
| 381 group('true', () { | |
| 382 test('self', () { | |
| 383 File file = provider.newFile('/foo/test.dart', ''); | |
| 384 Source source = file.createSource(UriKind.FILE_URI); | |
| 385 expect(source == source, isTrue); | |
| 386 }); | |
| 387 | |
| 388 test('same file', () { | |
| 389 File file = provider.newFile('/foo/test.dart', ''); | |
| 390 Source sourceA = file.createSource(UriKind.FILE_URI); | |
| 391 Source sourceB = file.createSource(UriKind.FILE_URI); | |
| 392 expect(sourceA == sourceB, isTrue); | |
| 393 }); | |
| 394 }); | |
| 395 | |
| 396 group('false', () { | |
| 397 test('not a memory Source', () { | |
| 398 File file = provider.newFile('/foo/test.dart', ''); | |
| 399 Source source = file.createSource(UriKind.FILE_URI); | |
| 400 expect(source == new Object(), isFalse); | |
| 401 }); | |
| 402 | |
| 403 test('different file', () { | |
| 404 File fileA = provider.newFile('/foo/a.dart', ''); | |
| 405 File fileB = provider.newFile('/foo/b.dart', ''); | |
| 406 Source sourceA = fileA.createSource(UriKind.FILE_URI); | |
| 407 Source sourceB = fileB.createSource(UriKind.FILE_URI); | |
| 408 expect(sourceA == sourceB, isFalse); | |
| 409 }); | |
| 410 }); | |
| 411 }); | |
| 412 | |
| 413 test('contents', () { | |
| 414 TimestampedData<String> contents = source.contents; | |
| 415 expect(contents.data, 'library test;'); | |
| 416 }); | |
| 417 | |
| 418 test('encoding', () { | |
| 419 expect(source.encoding, 'f/foo/test.dart'); | |
| 420 }); | |
| 421 | |
| 422 test('exists', () { | |
| 423 expect(source.exists(), isTrue); | |
| 424 }); | |
| 425 | |
| 426 test('fullName', () { | |
| 427 expect(source.fullName, '/foo/test.dart'); | |
| 428 }); | |
| 429 | |
| 430 test('hashCode', () { | |
| 431 source.hashCode; | |
| 432 }); | |
| 433 | |
| 434 test('shortName', () { | |
| 435 expect(source.shortName, 'test.dart'); | |
| 436 }); | |
| 437 | |
| 438 test('resolveRelative', () { | |
| 439 var relative = source.resolveRelative(new Uri.file('bar/baz.dart')); | |
| 440 expect(relative.fullName, '/foo/bar/baz.dart'); | |
| 441 }); | |
| 442 }); | |
| 443 | |
| 444 group('non-existent', () { | |
| 445 setUp(() { | |
| 446 File file = provider.getResource('/foo/test.dart'); | |
| 447 source = file.createSource(UriKind.FILE_URI); | |
| 448 }); | |
| 449 | |
| 450 test('contents', () { | |
| 451 expect(() { | |
| 452 source.contents; | |
| 453 }, throwsA(_isMemoryResourceException)); | |
| 454 }); | |
| 455 | |
| 456 test('encoding', () { | |
| 457 expect(source.encoding, 'f/foo/test.dart'); | |
| 458 }); | |
| 459 | |
| 460 test('exists', () { | |
| 461 expect(source.exists(), isFalse); | |
| 462 }); | |
| 463 | |
| 464 test('fullName', () { | |
| 465 expect(source.fullName, '/foo/test.dart'); | |
| 466 }); | |
| 467 | |
| 468 test('shortName', () { | |
| 469 expect(source.shortName, 'test.dart'); | |
| 470 }); | |
| 471 | |
| 472 test('resolveRelative', () { | |
| 473 var relative = source.resolveRelative(new Uri.file('bar/baz.dart')); | |
| 474 expect(relative.fullName, '/foo/bar/baz.dart'); | |
| 475 }); | |
| 476 }); | |
| 477 }); | |
| 478 }); | |
| 479 | |
| 480 group('ResourceUriResolver', testResourceResourceUriResolver); | |
| 481 } | |
| 482 | |
| 483 | |
| 484 void testResourceResourceUriResolver() { | |
| 485 MemoryResourceProvider provider; | |
| 486 ResourceUriResolver resolver; | |
| 487 | |
| 488 setUp(() { | |
| 489 provider = new MemoryResourceProvider(); | |
| 490 resolver = new ResourceUriResolver(provider); | |
| 491 provider.newFile('/test.dart', ''); | |
| 492 provider.newFolder('/folder'); | |
| 493 }); | |
| 494 | |
| 495 group('fromEncoding', () { | |
| 496 test('file', () { | |
| 497 var uri = new Uri(path: '/test.dart'); | |
| 498 Source source = resolver.fromEncoding(UriKind.FILE_URI, uri); | |
| 499 expect(source, isNotNull); | |
| 500 expect(source.exists(), isTrue); | |
| 501 expect(source.fullName, '/test.dart'); | |
| 502 }); | |
| 503 | |
| 504 test('not a UriKind.FILE_URI', () { | |
| 505 var uri = new Uri(path: '/test.dart'); | |
| 506 Source source = resolver.fromEncoding(UriKind.DART_URI, uri); | |
| 507 expect(source, isNull); | |
| 508 }); | |
| 509 }); | |
| 510 | |
| 511 group('resolveAbsolute', () { | |
| 512 test('file', () { | |
| 513 var uri = new Uri(scheme: 'file', path: '/test.dart'); | |
| 514 Source source = resolver.resolveAbsolute(uri); | |
| 515 expect(source, isNotNull); | |
| 516 expect(source.exists(), isTrue); | |
| 517 expect(source.fullName, '/test.dart'); | |
| 518 }); | |
| 519 | |
| 520 test('folder', () { | |
| 521 var uri = new Uri(scheme: 'file', path: '/folder'); | |
| 522 Source source = resolver.resolveAbsolute(uri); | |
| 523 expect(source, isNull); | |
| 524 }); | |
| 525 | |
| 526 test('not a file URI', () { | |
| 527 var uri = new Uri(scheme: 'https', path: '127.0.0.1/test.dart'); | |
| 528 Source source = resolver.resolveAbsolute(uri); | |
| 529 expect(source, isNull); | |
| 530 }); | |
| 531 }); | |
| 532 } | |
| OLD | NEW |