Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: tests/standalone/src/io/FileTest.dart

Issue 9630012: Error reporting on File in dart:io (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased and implemented on all platforms Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Dart test program for testing file I/O. 5 // Dart test program for testing file I/O.
6 6
7 #import("dart:io"); 7 #import("dart:io");
8 #import("dart:isolate"); 8 #import("dart:isolate");
9 9
10 class MyListOfOneElement implements List { 10 class MyListOfOneElement implements List {
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 Expect.isTrue(d.existsSync()); 527 Expect.isTrue(d.existsSync());
528 Expect.isTrue(d.path.endsWith(tempDir)); 528 Expect.isTrue(d.path.endsWith(tempDir));
529 file.deleteSync(); 529 file.deleteSync();
530 // Directories should throw exception. 530 // Directories should throw exception.
531 var file_dir = new File("."); 531 var file_dir = new File(".");
532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); 532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
533 file_dir = new File(tempDir); 533 file_dir = new File(tempDir);
534 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); 534 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
535 } 535 }
536 536
537 static void testFileError() {
538 bool checkOpenNonExistentFileException(e) {
539 Expect.isTrue(e is FileIOException);
540 Expect.isTrue(e.osError != null);
541 Expect.isTrue(e.toString().indexOf("Cannot open file") != -1);
542 Platform platform = new Platform();
543 if (platform.operatingSystem() == "linux") {
544 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
545 }
546 else if (platform.operatingSystem() == "macos") {
547 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
548 }
549 else if (platform.operatingSystem() == "windows") {
550 Expect.isTrue(
551 e.toString().indexOf(
552 "The system cannot find the file specified") != -1);
553 }
554 // File not not found has error code 2 on all supported platforms.
555 Expect.equals(2, e.osError.errorCode);
556
557 return true;
558 }
559
560 bool checkDeleteNonExistentFileException(e) {
561 Expect.isTrue(e is FileIOException);
562 Expect.isTrue(e.osError != null);
563 Expect.isTrue(e.toString().indexOf("Cannot delete file") != -1);
564 Platform platform = new Platform();
565 if (platform.operatingSystem() == "linux") {
566 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
567 }
568 else if (platform.operatingSystem() == "macos") {
569 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
570 }
571 else if (platform.operatingSystem() == "windows") {
572 Expect.isTrue(
573 e.toString().indexOf(
574 "The system cannot find the file specified") != -1);
575 }
576 // File not not found has error code 2 on all supported platforms.
577 Expect.equals(2, e.osError.errorCode);
578
579 return true;
580 }
581
582 bool checkCreateInNonExistentDirectoryException(e) {
583 Expect.isTrue(e is FileIOException);
584 Expect.isTrue(e.osError != null);
585 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1);
586 Platform platform = new Platform();
587 if (platform.operatingSystem() == "linux") {
588 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
589 Expect.equals(2, e.osError.errorCode);
590 }
591 else if (platform.operatingSystem() == "macos") {
592 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
593 Expect.equals(2, e.osError.errorCode);
594 }
595 else if (platform.operatingSystem() == "windows") {
596 Expect.isTrue(
597 e.toString().indexOf(
598 "The system cannot find the path specified") != -1);
599 Expect.equals(3, e.osError.errorCode);
600 }
601
602 return true;
603 }
604
605 bool checkFullPathOnNonExistentDirectoryException(e) {
606 Expect.isTrue(e is FileIOException);
607 Expect.isTrue(e.osError != null);
608 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1);
609 Platform platform = new Platform();
610 if (platform.operatingSystem() == "linux") {
611 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
612 }
613 else if (platform.operatingSystem() == "macos") {
614 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
615 }
616 else if (platform.operatingSystem() == "windows") {
617 Expect.isTrue(
618 e.toString().indexOf(
619 "The system cannot find the file specified") != -1);
620 }
621 // File not not found has error code 2 on all supported platforms.
622 Expect.equals(2, e.osError.errorCode);
623
624 return true;
625 }
626
627 bool checkDirectoryInNonExistentDirectoryException(e) {
628 Expect.isTrue(e is FileIOException);
629 Expect.isTrue(e.osError != null);
630 Expect.isTrue(
631 e.toString().indexOf("Cannot retrieve directory for file") != -1);
632 Platform platform = new Platform();
633 if (platform.operatingSystem() == "linux") {
634 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
635 }
636 else if (platform.operatingSystem() == "macos") {
637 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
638 }
639 else if (platform.operatingSystem() == "windows") {
640 Expect.isTrue(
641 e.toString().indexOf(
642 "The system cannot find the file specified") != -1);
643 }
644 // File not not found has error code 2 on all supported platforms.
645 Expect.equals(2, e.osError.errorCode);
646
647 return true;
648 }
649
650 var tempDir = tempDirectory.path;
651 {
Mads Ager (google) 2012/03/13 10:56:17 How about lifting the helpers to the top level and
Søren Gjesse 2012/03/13 12:39:49 Split into individual tests and moved to a separat
652 var file = new File("${tempDir}/nonExistentFile1");
653
654 // Non-existing file should throw exception.
655 Expect.throws(() => file.openSync(),
656 (e) => checkOpenNonExistentFileException(e));
657
658 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code"));
659 file.onError = (e) => checkOpenNonExistentFileException(e);
660 }
661
662 {
663 var file = new File("${tempDir}/nonExistentFile2");
664
665 // Non-existing file should throw exception.
666 Expect.throws(() => file.deleteSync(),
667 (e) => checkDeleteNonExistentFileException(e));
668
669 file.delete(() => Expect.fail("Unreachable code"));
670 file.onError = (e) => checkDeleteNonExistentFileException(e);
671 }
672
673 {
674 var file = new File("${tempDir}/nonExistentDirectory/newFile");
675
676 // Create in non-existent directory should throw exception.
677 Expect.throws(() => file.createSync(),
678 (e) => checkCreateInNonExistentDirectoryException(e));
679
680 file.create(() => Expect.fail("Unreachable code"));
681 file.onError = (e) => checkCreateInNonExistentDirectoryException(e);
682 }
683
684 {
685 var file = new File("${tempDir}/nonExistentDirectory");
686
687 // Create in non-existent directory should throw exception.
688 Expect.throws(() => file.fullPathSync(),
689 (e) => checkFullPathOnNonExistentDirectoryException(e));
690
691 file.fullPath((path) => Expect.fail("Unreachable code $path"));
692 file.onError = (e) => checkFullPathOnNonExistentDirectoryException(e);
693 }
694
695 {
696 var file = new File("${tempDir}/nonExistentDirectory/newFile");
697
698 // Create in non-existent directory should throw exception.
699 Expect.throws(() => file.directorySync(),
700 (e) => checkDirectoryInNonExistentDirectoryException(e));
701
702 file.directory((directory) => Expect.fail("Unreachable code"));
703 file.onError = (e) => checkDirectoryInNonExistentDirectoryException(e);
704 }
705
706 {
707 var file = new File("${tempDir}/nonExistentFile3");
708
709 // Non-existing file should throw exception.
710 Expect.throws(() => file.readAsBytesSync(),
711 (e) => checkOpenNonExistentFileException(e));
712
713 // TODO(sgjesse): Handle error for file.readAsBytes as well.
714 }
715
716 {
717 var file = new File("${tempDir}/nonExistentFile4");
718
719 // Non-existing file should throw exception.
720 Expect.throws(() => file.readAsTextSync(),
721 (e) => checkOpenNonExistentFileException(e));
722
723 // TODO(sgjesse): Handle error for file.readAsText as well.
724 }
725
726 {
727 var file = new File("${tempDir}/nonExistentFile5");
728
729 // Non-existing file should throw exception.
730 Expect.throws(() => file.readAsLinesSync(),
731 (e) => checkOpenNonExistentFileException(e));
732
733 // TODO(sgjesse): Handle error for file.readAsLines as well.
734 }
735 }
736
737
537 // Test for file length functionality. 738 // Test for file length functionality.
538 static void testLength() { 739 static void testLength() {
539 String filename = getFilename("tests/vm/data/fixed_length_file"); 740 String filename = getFilename("tests/vm/data/fixed_length_file");
540 RandomAccessFile input = (new File(filename)).openSync(); 741 RandomAccessFile input = (new File(filename)).openSync();
541 input.onError = (s) { 742 input.onError = (s) {
542 Expect.fail("No errors expected"); 743 Expect.fail("No errors expected");
543 }; 744 };
544 input.length((length) { 745 input.length((length) {
545 Expect.equals(42, length); 746 Expect.equals(42, length);
546 input.close(() => null); 747 input.close(() => null);
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 testCloseExceptionStream(); 1300 testCloseExceptionStream();
1100 testBufferOutOfBoundsException(); 1301 testBufferOutOfBoundsException();
1101 testAppend(); 1302 testAppend();
1102 testAppendSync(); 1303 testAppendSync();
1103 testWriteAppend(); 1304 testWriteAppend();
1104 testOutputStreamWriteAppend(); 1305 testOutputStreamWriteAppend();
1105 testOutputStreamWriteString(); 1306 testOutputStreamWriteString();
1106 testWriteVariousLists(); 1307 testWriteVariousLists();
1107 testDirectory(); 1308 testDirectory();
1108 testDirectorySync(); 1309 testDirectorySync();
1310 testFileError();
1109 }); 1311 });
1110 } 1312 }
1111 } 1313 }
1112 1314
1113 main() { 1315 main() {
1114 FileTest.testMain(); 1316 FileTest.testMain();
1115 } 1317 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698