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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: tests/standalone/src/io/FileTest.dart
diff --git a/tests/standalone/src/io/FileTest.dart b/tests/standalone/src/io/FileTest.dart
index df49cf83a652ff6bcee8c9d3731753df7bfd58c3..085a755eaaa7c5c13c537b18fc324a95a7767277 100644
--- a/tests/standalone/src/io/FileTest.dart
+++ b/tests/standalone/src/io/FileTest.dart
@@ -534,6 +534,207 @@ class FileTest {
Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
}
+ static void testFileError() {
+ bool checkOpenNonExistentFileException(e) {
+ Expect.isTrue(e is FileIOException);
+ Expect.isTrue(e.osError != null);
+ Expect.isTrue(e.toString().indexOf("Cannot open file") != -1);
+ Platform platform = new Platform();
+ if (platform.operatingSystem() == "linux") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "macos") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "windows") {
+ Expect.isTrue(
+ e.toString().indexOf(
+ "The system cannot find the file specified") != -1);
+ }
+ // File not not found has error code 2 on all supported platforms.
+ Expect.equals(2, e.osError.errorCode);
+
+ return true;
+ }
+
+ bool checkDeleteNonExistentFileException(e) {
+ Expect.isTrue(e is FileIOException);
+ Expect.isTrue(e.osError != null);
+ Expect.isTrue(e.toString().indexOf("Cannot delete file") != -1);
+ Platform platform = new Platform();
+ if (platform.operatingSystem() == "linux") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "macos") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "windows") {
+ Expect.isTrue(
+ e.toString().indexOf(
+ "The system cannot find the file specified") != -1);
+ }
+ // File not not found has error code 2 on all supported platforms.
+ Expect.equals(2, e.osError.errorCode);
+
+ return true;
+ }
+
+ bool checkCreateInNonExistentDirectoryException(e) {
+ Expect.isTrue(e is FileIOException);
+ Expect.isTrue(e.osError != null);
+ Expect.isTrue(e.toString().indexOf("Cannot create file") != -1);
+ Platform platform = new Platform();
+ if (platform.operatingSystem() == "linux") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ Expect.equals(2, e.osError.errorCode);
+ }
+ else if (platform.operatingSystem() == "macos") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ Expect.equals(2, e.osError.errorCode);
+ }
+ else if (platform.operatingSystem() == "windows") {
+ Expect.isTrue(
+ e.toString().indexOf(
+ "The system cannot find the path specified") != -1);
+ Expect.equals(3, e.osError.errorCode);
+ }
+
+ return true;
+ }
+
+ bool checkFullPathOnNonExistentDirectoryException(e) {
+ Expect.isTrue(e is FileIOException);
+ Expect.isTrue(e.osError != null);
+ Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1);
+ Platform platform = new Platform();
+ if (platform.operatingSystem() == "linux") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "macos") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "windows") {
+ Expect.isTrue(
+ e.toString().indexOf(
+ "The system cannot find the file specified") != -1);
+ }
+ // File not not found has error code 2 on all supported platforms.
+ Expect.equals(2, e.osError.errorCode);
+
+ return true;
+ }
+
+ bool checkDirectoryInNonExistentDirectoryException(e) {
+ Expect.isTrue(e is FileIOException);
+ Expect.isTrue(e.osError != null);
+ Expect.isTrue(
+ e.toString().indexOf("Cannot retrieve directory for file") != -1);
+ Platform platform = new Platform();
+ if (platform.operatingSystem() == "linux") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "macos") {
+ Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
+ }
+ else if (platform.operatingSystem() == "windows") {
+ Expect.isTrue(
+ e.toString().indexOf(
+ "The system cannot find the file specified") != -1);
+ }
+ // File not not found has error code 2 on all supported platforms.
+ Expect.equals(2, e.osError.errorCode);
+
+ return true;
+ }
+
+ var tempDir = tempDirectory.path;
+ {
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
+ var file = new File("${tempDir}/nonExistentFile1");
+
+ // Non-existing file should throw exception.
+ Expect.throws(() => file.openSync(),
+ (e) => checkOpenNonExistentFileException(e));
+
+ file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code"));
+ file.onError = (e) => checkOpenNonExistentFileException(e);
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentFile2");
+
+ // Non-existing file should throw exception.
+ Expect.throws(() => file.deleteSync(),
+ (e) => checkDeleteNonExistentFileException(e));
+
+ file.delete(() => Expect.fail("Unreachable code"));
+ file.onError = (e) => checkDeleteNonExistentFileException(e);
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentDirectory/newFile");
+
+ // Create in non-existent directory should throw exception.
+ Expect.throws(() => file.createSync(),
+ (e) => checkCreateInNonExistentDirectoryException(e));
+
+ file.create(() => Expect.fail("Unreachable code"));
+ file.onError = (e) => checkCreateInNonExistentDirectoryException(e);
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentDirectory");
+
+ // Create in non-existent directory should throw exception.
+ Expect.throws(() => file.fullPathSync(),
+ (e) => checkFullPathOnNonExistentDirectoryException(e));
+
+ file.fullPath((path) => Expect.fail("Unreachable code $path"));
+ file.onError = (e) => checkFullPathOnNonExistentDirectoryException(e);
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentDirectory/newFile");
+
+ // Create in non-existent directory should throw exception.
+ Expect.throws(() => file.directorySync(),
+ (e) => checkDirectoryInNonExistentDirectoryException(e));
+
+ file.directory((directory) => Expect.fail("Unreachable code"));
+ file.onError = (e) => checkDirectoryInNonExistentDirectoryException(e);
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentFile3");
+
+ // Non-existing file should throw exception.
+ Expect.throws(() => file.readAsBytesSync(),
+ (e) => checkOpenNonExistentFileException(e));
+
+ // TODO(sgjesse): Handle error for file.readAsBytes as well.
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentFile4");
+
+ // Non-existing file should throw exception.
+ Expect.throws(() => file.readAsTextSync(),
+ (e) => checkOpenNonExistentFileException(e));
+
+ // TODO(sgjesse): Handle error for file.readAsText as well.
+ }
+
+ {
+ var file = new File("${tempDir}/nonExistentFile5");
+
+ // Non-existing file should throw exception.
+ Expect.throws(() => file.readAsLinesSync(),
+ (e) => checkOpenNonExistentFileException(e));
+
+ // TODO(sgjesse): Handle error for file.readAsLines as well.
+ }
+ }
+
+
// Test for file length functionality.
static void testLength() {
String filename = getFilename("tests/vm/data/fixed_length_file");
@@ -1106,6 +1307,7 @@ class FileTest {
testWriteVariousLists();
testDirectory();
testDirectorySync();
+ testFileError();
});
}
}

Powered by Google App Engine
This is Rietveld 408576698