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