| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Directory listing test. | 5 // Directory listing test. |
| 6 | 6 |
| 7 import "dart:async"; | 7 import "dart:async"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 import "dart:isolate"; | 9 import "dart:isolate"; |
| 10 | 10 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 Expect.equals(listedFile, recursive); | 43 Expect.equals(listedFile, recursive); |
| 44 Expect.isTrue(listedDir); | 44 Expect.isTrue(listedDir); |
| 45 listedFile = false; | 45 listedFile = false; |
| 46 listedDir = false; | 46 listedDir = false; |
| 47 } | 47 } |
| 48 | 48 |
| 49 testSyncListing(true); | 49 testSyncListing(true); |
| 50 testSyncListing(false); | 50 testSyncListing(false); |
| 51 Expect.equals(f.fullPathSync(), fLong.fullPathSync()); | 51 Expect.equals(f.fullPathSync(), fLong.fullPathSync()); |
| 52 | 52 |
| 53 var lister = directory.list(recursive: true); | 53 var listingDonePort = new ReceivePort(); |
| 54 | 54 directory.list(recursive: true).listen( |
| 55 lister.onDir = (dir) { | 55 (FileSystemEntity entity) { |
| 56 listedDir = true; | 56 if (entity is File) { |
| 57 Expect.isTrue(dir.contains(directory.path)); | 57 var path = entity.name; |
| 58 Expect.isTrue(dir.contains('subdir')); | 58 listedFile = true; |
| 59 }; | 59 Expect.isTrue(path.contains(directory.path)); |
| 60 | 60 Expect.isTrue(path.contains('subdir')); |
| 61 lister.onFile = (f) { | 61 Expect.isTrue(path.contains('file.txt')); |
| 62 listedFile = true; | 62 } else { |
| 63 Expect.isTrue(f.contains(directory.path)); | 63 var path = entity.path; |
| 64 Expect.isTrue(f.contains('subdir')); | 64 Expect.isTrue(entity is Directory); |
| 65 Expect.isTrue(f.contains('file.txt')); | 65 listedDir = true; |
| 66 }; | 66 Expect.isTrue(path.contains(directory.path)); |
| 67 | 67 Expect.isTrue(path.contains('subdir')); |
| 68 lister.onDone = (completed) { | 68 } |
| 69 Expect.isTrue(completed, "directory listing did not complete"); | 69 }, |
| 70 Expect.isTrue(listedDir, "directory not found"); | 70 onDone: () { |
| 71 Expect.isTrue(listedFile, "file not found"); | 71 Expect.isTrue(listedDir, "directory not found"); |
| 72 directory.delete(recursive: true).then((ignore) { | 72 Expect.isTrue(listedFile, "file not found"); |
| 73 f.exists().then((exists) => Expect.isFalse(exists)); | 73 directory.delete(recursive: true).then((ignore) { |
| 74 directory.exists().then((exists) => Expect.isFalse(exists)); | 74 f.exists().then((exists) => Expect.isFalse(exists)); |
| 75 subDirectory.exists().then((exists) => Expect.isFalse(exists)); | 75 directory.exists().then((exists) => Expect.isFalse(exists)); |
| 76 }); | 76 subDirectory.exists().then((exists) => Expect.isFalse(exists)); |
| 77 }; | 77 listingDonePort.close(); |
| 78 }); |
| 79 }); |
| 78 | 80 |
| 79 // Listing is asynchronous, so nothing should be listed at this | 81 // Listing is asynchronous, so nothing should be listed at this |
| 80 // point. | 82 // point. |
| 81 Expect.isFalse(listedDir); | 83 Expect.isFalse(listedDir); |
| 82 Expect.isFalse(listedFile); | 84 Expect.isFalse(listedFile); |
| 83 } | 85 } |
| 84 | 86 |
| 85 static void testListNonExistent() { | 87 static void testListNonExistent() { |
| 86 setupListerHandlers(DirectoryLister lister) { | 88 setupListerHandlers(Stream<FileSystemEntity> stream) { |
| 87 // Test that listing a non-existing directory fails. | 89 stream.listen( |
| 88 lister.onError = (e) { | 90 (_) => Expect.fail("Listing of non-existing directory should fail"), |
| 89 Expect.isTrue(e is DirectoryIOException); | 91 onError: (e) { |
| 90 }; | 92 Expect.isTrue(e is AsyncError); |
| 91 lister.onFile = (file) { | 93 Expect.isTrue(e.error is DirectoryIOException); |
| 92 Expect.fail("Listing of non-existing directory should fail"); | 94 }); |
| 93 }; | |
| 94 lister.onDir = (dir) { | |
| 95 Expect.fail("Listing of non-existing directory should fail"); | |
| 96 }; | |
| 97 lister.onDone = (success) { | |
| 98 Expect.isFalse(success); | |
| 99 }; | |
| 100 } | 95 } |
| 101 new Directory("").createTemp().then((d) { | 96 new Directory("").createTemp().then((d) { |
| 102 d.delete().then((ignore) { | 97 d.delete().then((ignore) { |
| 103 setupListerHandlers(d.list()); | 98 setupListerHandlers(d.list()); |
| 104 setupListerHandlers(d.list(recursive: true)); | 99 setupListerHandlers(d.list(recursive: true)); |
| 105 }); | 100 }); |
| 106 }); | 101 }); |
| 107 } | 102 } |
| 108 | 103 |
| 109 static void testListTooLongName() { | 104 static void testListTooLongName() { |
| 110 new Directory("").createTemp().then((d) { | 105 new Directory("").createTemp().then((d) { |
| 111 var errors = 0; | 106 var errors = 0; |
| 112 setupListHandlers(DirectoryLister lister) { | 107 var port = new ReceivePort(); |
| 113 lister.onError = (e) { | 108 setupListHandlers(Stream<FileSystemEntity> stream) { |
| 114 Expect.isTrue(e is DirectoryIOException); | 109 stream.listen( |
| 115 if (++errors == 2) { | 110 (_) => Expect.fail("Listing of non-existing directory should fail"), |
| 116 d.delete(recursive: true); | 111 onError: (e) { |
| 117 } | 112 Expect.isTrue(e is AsyncError); |
| 118 }; | 113 Expect.isTrue(e.error is DirectoryIOException); |
| 119 lister.onFile = (file) { | 114 if (++errors == 2) { |
| 120 Expect.fail("Listing of non-existing directory should fail"); | 115 d.delete(recursive: true).then((_) { |
| 121 }; | 116 port.close(); |
| 122 lister.onDir = (dir) { | 117 }); |
| 123 Expect.fail("Listing of non-existing directory should fail"); | 118 } |
| 124 }; | 119 }); |
| 125 lister.onDone = (success) { | |
| 126 Expect.isFalse(success); | |
| 127 }; | |
| 128 } | 120 } |
| 129 var subDirName = 'subdir'; | 121 var subDirName = 'subdir'; |
| 130 var subDir = new Directory("${d.path}/$subDirName"); | 122 var subDir = new Directory("${d.path}/$subDirName"); |
| 131 subDir.create().then((ignore) { | 123 subDir.create().then((ignore) { |
| 132 // Construct a long string of the form | 124 // Construct a long string of the form |
| 133 // 'tempdir/subdir/../subdir/../subdir'. | 125 // 'tempdir/subdir/../subdir/../subdir'. |
| 134 var buffer = new StringBuffer(); | 126 var buffer = new StringBuffer(); |
| 135 buffer.add(subDir.path); | 127 buffer.add(subDir.path); |
| 136 for (var i = 0; i < 1000; i++) { | 128 for (var i = 0; i < 1000; i++) { |
| 137 buffer.add("/../${subDirName}"); | 129 buffer.add("/../${subDirName}"); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 buffer.add(subDir.path); | 199 buffer.add(subDir.path); |
| 208 for (var i = 0; i < 1000; i++) { | 200 for (var i = 0; i < 1000; i++) { |
| 209 buffer.add("/../${subDirName}"); | 201 buffer.add("/../${subDirName}"); |
| 210 } | 202 } |
| 211 var long = new Directory("${buffer.toString()}"); | 203 var long = new Directory("${buffer.toString()}"); |
| 212 Expect.throws(long.deleteSync); | 204 Expect.throws(long.deleteSync); |
| 213 Expect.throws(() => long.deleteSync(recursive: true)); | 205 Expect.throws(() => long.deleteSync(recursive: true)); |
| 214 d.deleteSync(recursive: true); | 206 d.deleteSync(recursive: true); |
| 215 } | 207 } |
| 216 | 208 |
| 217 static void testDeleteSymlink() { | |
| 218 // temp/ | |
| 219 // a/ | |
| 220 // file.txt | |
| 221 // b/ | |
| 222 // a_link -> a | |
| 223 var d = new Directory("").createTempSync(); | |
| 224 var a = new Directory("${d.path}/a"); | |
| 225 a.createSync(); | |
| 226 | |
| 227 var b = new Directory("${d.path}/b"); | |
| 228 b.createSync(); | |
| 229 | |
| 230 var f = new File("${d.path}/a/file.txt"); | |
| 231 f.createSync(); | |
| 232 Expect.isTrue(f.existsSync()); | |
| 233 | |
| 234 // Create a symlink (or junction on Windows) from | |
| 235 // temp/b/a_link to temp/a. | |
| 236 var cmd = "ln"; | |
| 237 var args = ['-s', "${d.path}/b/a_link", "${d.path}/a"]; | |
| 238 | |
| 239 if (Platform.operatingSystem == "windows") { | |
| 240 cmd = "cmd"; | |
| 241 args = ["/c", "mklink", "/j", "${d.path}\\b\\a_link", "${d.path}\\a"]; | |
| 242 } | |
| 243 | |
| 244 Process.run(cmd, args).then((_) { | |
| 245 // Delete the directory containing the junction. | |
| 246 b.deleteSync(recursive: true); | |
| 247 | |
| 248 // We should not have recursed through a_link into a. | |
| 249 Expect.isTrue(f.existsSync()); | |
| 250 | |
| 251 // Clean up after ourselves. | |
| 252 d.deleteSync(recursive: true); | |
| 253 }); | |
| 254 } | |
| 255 | |
| 256 static void testExistsCreateDelete() { | 209 static void testExistsCreateDelete() { |
| 257 new Directory("").createTemp().then((d) { | 210 new Directory("").createTemp().then((d) { |
| 258 d.exists().then((bool exists) { | 211 d.exists().then((bool exists) { |
| 259 Expect.isTrue(exists); | 212 Expect.isTrue(exists); |
| 260 Directory created = new Directory("${d.path}/subdir"); | 213 Directory created = new Directory("${d.path}/subdir"); |
| 261 created.create().then((ignore) { | 214 created.create().then((ignore) { |
| 262 created.exists().then((bool exists) { | 215 created.exists().then((bool exists) { |
| 263 Expect.isTrue(exists); | 216 Expect.isTrue(exists); |
| 264 created.delete().then((ignore) { | 217 created.delete().then((ignore) { |
| 265 created.exists().then((bool exists) { | 218 created.exists().then((bool exists) { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 } | 305 } |
| 353 | 306 |
| 354 static void testMain() { | 307 static void testMain() { |
| 355 testListing(); | 308 testListing(); |
| 356 testListNonExistent(); | 309 testListNonExistent(); |
| 357 testListTooLongName(); | 310 testListTooLongName(); |
| 358 testDeleteNonExistent(); | 311 testDeleteNonExistent(); |
| 359 testDeleteTooLongName(); | 312 testDeleteTooLongName(); |
| 360 testDeleteNonExistentSync(); | 313 testDeleteNonExistentSync(); |
| 361 testDeleteTooLongNameSync(); | 314 testDeleteTooLongNameSync(); |
| 362 testDeleteSymlink(); | |
| 363 testExistsCreateDelete(); | 315 testExistsCreateDelete(); |
| 364 testExistsCreateDeleteSync(); | 316 testExistsCreateDeleteSync(); |
| 365 testCreateTemp(); | 317 testCreateTemp(); |
| 366 testCreateDeleteTemp(); | 318 testCreateDeleteTemp(); |
| 367 testCurrent(); | 319 testCurrent(); |
| 368 testFromPath(); | 320 testFromPath(); |
| 369 } | 321 } |
| 370 } | 322 } |
| 371 | 323 |
| 372 | 324 |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 testCreateTempErrorSync(); | 519 testCreateTempErrorSync(); |
| 568 testCreateTempError(); | 520 testCreateTempError(); |
| 569 testCreateExistingSync(); | 521 testCreateExistingSync(); |
| 570 testCreateExisting(); | 522 testCreateExisting(); |
| 571 testCreateDirExistingFileSync(); | 523 testCreateDirExistingFileSync(); |
| 572 testCreateDirExistingFile(); | 524 testCreateDirExistingFile(); |
| 573 testCreateRecursive(); | 525 testCreateRecursive(); |
| 574 testCreateRecursiveSync(); | 526 testCreateRecursiveSync(); |
| 575 testRename(); | 527 testRename(); |
| 576 } | 528 } |
| OLD | NEW |