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:io"); | 7 #import("dart:io"); |
8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
9 | 9 |
10 class DirectoryTest { | 10 class DirectoryTest { |
11 static void testListing() { | 11 static void testListing() { |
12 bool listedDir = false; | 12 bool listedDir = false; |
13 bool listedFile = false; | 13 bool listedFile = false; |
14 | 14 |
15 Directory directory = new Directory("").createTempSync(); | 15 Directory directory = new Directory("").createTempSync(); |
16 Directory subDirectory = new Directory("${directory.path}/subdir"); | 16 Directory subDirectory = new Directory("${directory.path}/subdir"); |
17 Expect.isFalse(subDirectory.existsSync()); | 17 Expect.isFalse(subDirectory.existsSync()); |
18 subDirectory.createSync(); | 18 subDirectory.createSync(); |
| 19 Expect.isTrue(subDirectory.existsSync()); |
19 File f = new File('${subDirectory.path}/file.txt'); | 20 File f = new File('${subDirectory.path}/file.txt'); |
20 Expect.isFalse(f.existsSync()); | 21 Expect.isFalse(f.existsSync()); |
21 f.createSync(); | 22 f.createSync(); |
22 | 23 |
23 var lister = directory.list(recursive: true); | 24 var lister = directory.list(recursive: true); |
24 | 25 |
25 lister.onDir = (dir) { | 26 lister.onDir = (dir) { |
26 listedDir = true; | 27 listedDir = true; |
27 Expect.isTrue(dir.contains(directory.path)); | 28 Expect.isTrue(dir.contains(directory.path)); |
28 Expect.isTrue(dir.contains('subdir')); | 29 Expect.isTrue(dir.contains('subdir')); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 testCreateTempError() { | 453 testCreateTempError() { |
453 var location = illegalTempDirectoryLocation(); | 454 var location = illegalTempDirectoryLocation(); |
454 if (location == null) return; | 455 if (location == null) return; |
455 | 456 |
456 var port = new ReceivePort(); | 457 var port = new ReceivePort(); |
457 var future = new Directory(location).createTemp(); | 458 var future = new Directory(location).createTemp(); |
458 future.handleException((e) => port.close()); | 459 future.handleException((e) => port.close()); |
459 } | 460 } |
460 | 461 |
461 | 462 |
| 463 testCreateExistingSync() { |
| 464 // Test that creating an existing directory succeeds. |
| 465 var d = new Directory(''); |
| 466 var temp = d.createTempSync(); |
| 467 var subDir = new Directory('${temp.path}/flaf'); |
| 468 Expect.isFalse(subDir.existsSync()); |
| 469 subDir.createSync(); |
| 470 Expect.isTrue(subDir.existsSync()); |
| 471 subDir.createSync(); |
| 472 Expect.isTrue(subDir.existsSync()); |
| 473 temp.deleteRecursivelySync(); |
| 474 } |
| 475 |
| 476 |
| 477 testCreateExisting() { |
| 478 // Test that creating an existing directory succeeds. |
| 479 var port = new ReceivePort(); |
| 480 var d = new Directory(''); |
| 481 d.createTemp().then((temp) { |
| 482 var subDir = new Directory('${temp.path}/flaf'); |
| 483 subDir.exists().then((dirExists) { |
| 484 Expect.isFalse(dirExists); |
| 485 subDir.create().then((_) { |
| 486 subDir.exists().then((dirExists) { |
| 487 Expect.isTrue(dirExists); |
| 488 subDir.create().then((_) { |
| 489 subDir.exists().then((dirExists) { |
| 490 Expect.isTrue(dirExists); |
| 491 temp.deleteRecursively().then((_) { |
| 492 port.close(); |
| 493 }); |
| 494 }); |
| 495 }); |
| 496 }); |
| 497 }); |
| 498 }); |
| 499 }); |
| 500 } |
| 501 |
| 502 |
| 503 testCreateDirExistingFileSync() { |
| 504 // Test that creating an existing directory succeeds. |
| 505 var d = new Directory(''); |
| 506 var temp = d.createTempSync(); |
| 507 var path = '${temp.path}/flaf'; |
| 508 var file = new File(path); |
| 509 file.createSync(); |
| 510 Expect.isTrue(file.existsSync()); |
| 511 Expect.throws(new Directory(path).createSync, |
| 512 (e) => e is DirectoryIOException); |
| 513 temp.deleteRecursivelySync(); |
| 514 } |
| 515 |
| 516 |
| 517 testCreateDirExistingFile() { |
| 518 // Test that creating an existing directory succeeds. |
| 519 var port = new ReceivePort(); |
| 520 var d = new Directory(''); |
| 521 d.createTemp().then((temp) { |
| 522 var path = '${temp.path}/flaf'; |
| 523 var file = new File(path); |
| 524 var subDir = new Directory(path); |
| 525 file.create().then((_) { |
| 526 subDir.create() |
| 527 ..then((_) { Expect.fail("dir create should fail on existing file"); }) |
| 528 ..handleException((e) { |
| 529 Expect.isTrue(e is DirectoryIOException); |
| 530 temp.deleteRecursively().then((_) { |
| 531 port.close(); |
| 532 }); |
| 533 return true; |
| 534 }); |
| 535 }); |
| 536 }); |
| 537 } |
| 538 |
| 539 |
462 testRename() { | 540 testRename() { |
463 var d = new Directory(''); | 541 var d = new Directory(''); |
464 var temp1 = d.createTempSync(); | 542 var temp1 = d.createTempSync(); |
465 var temp2 = d.createTempSync(); | 543 var temp2 = d.createTempSync(); |
466 var temp3 = temp1.renameSync(temp2.path); | 544 var temp3 = temp1.renameSync(temp2.path); |
467 Expect.isFalse(temp1.existsSync()); | 545 Expect.isFalse(temp1.existsSync()); |
468 Expect.isTrue(temp2.existsSync()); | 546 Expect.isTrue(temp2.existsSync()); |
469 Expect.equals(temp3.path, temp2.path); | 547 Expect.equals(temp3.path, temp2.path); |
470 | 548 |
471 temp2.rename(temp1.path).then((temp4) { | 549 temp2.rename(temp1.path).then((temp4) { |
472 Expect.isFalse(temp3.existsSync()); | 550 Expect.isFalse(temp3.existsSync()); |
473 Expect.isFalse(temp2.existsSync()); | 551 Expect.isFalse(temp2.existsSync()); |
474 Expect.isTrue(temp1.existsSync()); | 552 Expect.isTrue(temp1.existsSync()); |
475 Expect.isTrue(temp4.existsSync()); | 553 Expect.isTrue(temp4.existsSync()); |
476 Expect.equals(temp1.path, temp4.path); | 554 Expect.equals(temp1.path, temp4.path); |
477 temp1.deleteRecursivelySync(); | 555 temp1.deleteRecursivelySync(); |
478 }); | 556 }); |
479 } | 557 } |
480 | 558 |
481 main() { | 559 main() { |
482 DirectoryTest.testMain(); | 560 DirectoryTest.testMain(); |
483 NestedTempDirectoryTest.testMain(); | 561 NestedTempDirectoryTest.testMain(); |
484 testCreateTempErrorSync(); | 562 testCreateTempErrorSync(); |
485 testCreateTempError(); | 563 testCreateTempError(); |
| 564 testCreateExistingSync(); |
| 565 testCreateExisting(); |
| 566 testCreateDirExistingFileSync(); |
| 567 testCreateDirExistingFile(); |
486 testRename(); | 568 testRename(); |
487 } | 569 } |
OLD | NEW |