OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // Dart test program for testing error handling in file I/O. |
| 6 |
| 7 #import("dart:io"); |
| 8 #import("dart:isolate"); |
| 9 |
| 10 String tempDir() { |
| 11 var d = new Directory(''); |
| 12 d.createTempSync(); |
| 13 return d.path; |
| 14 } |
| 15 |
| 16 |
| 17 bool checkOpenNonExistentFileException(e) { |
| 18 Expect.isTrue(e is FileIOException); |
| 19 Expect.isTrue(e.osError != null); |
| 20 Expect.isTrue(e.toString().indexOf("Cannot open file") != -1); |
| 21 Platform platform = new Platform(); |
| 22 if (platform.operatingSystem() == "linux") { |
| 23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 24 } else if (platform.operatingSystem() == "macos") { |
| 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 26 } else if (platform.operatingSystem() == "windows") { |
| 27 Expect.isTrue( |
| 28 e.toString().indexOf( |
| 29 "The system cannot find the file specified") != -1); |
| 30 } |
| 31 // File not not found has error code 2 on all supported platforms. |
| 32 Expect.equals(2, e.osError.errorCode); |
| 33 |
| 34 return true; |
| 35 } |
| 36 |
| 37 void testOpenNonExistent() { |
| 38 var file = new File("${tempDir()}/nonExistentFile"); |
| 39 |
| 40 // Non-existing file should throw exception. |
| 41 Expect.throws(() => file.openSync(), |
| 42 (e) => checkOpenNonExistentFileException(e)); |
| 43 |
| 44 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code")); |
| 45 file.onError = (e) => checkOpenNonExistentFileException(e); |
| 46 } |
| 47 |
| 48 bool checkDeleteNonExistentFileException(e) { |
| 49 Expect.isTrue(e is FileIOException); |
| 50 Expect.isTrue(e.osError != null); |
| 51 Expect.isTrue(e.toString().indexOf("Cannot delete file") != -1); |
| 52 Platform platform = new Platform(); |
| 53 if (platform.operatingSystem() == "linux") { |
| 54 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 55 } else if (platform.operatingSystem() == "macos") { |
| 56 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 57 } else if (platform.operatingSystem() == "windows") { |
| 58 Expect.isTrue( |
| 59 e.toString().indexOf( |
| 60 "The system cannot find the file specified") != -1); |
| 61 } |
| 62 // File not not found has error code 2 on all supported platforms. |
| 63 Expect.equals(2, e.osError.errorCode); |
| 64 |
| 65 return true; |
| 66 } |
| 67 |
| 68 void testDeleteNonExistent() { |
| 69 var file = new File("${tempDir()}/nonExistentFile"); |
| 70 |
| 71 // Non-existing file should throw exception. |
| 72 Expect.throws(() => file.deleteSync(), |
| 73 (e) => checkDeleteNonExistentFileException(e)); |
| 74 |
| 75 file.delete(() => Expect.fail("Unreachable code")); |
| 76 file.onError = (e) => checkDeleteNonExistentFileException(e); |
| 77 } |
| 78 |
| 79 bool checkCreateInNonExistentDirectoryException(e) { |
| 80 Expect.isTrue(e is FileIOException); |
| 81 Expect.isTrue(e.osError != null); |
| 82 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1); |
| 83 Platform platform = new Platform(); |
| 84 if (platform.operatingSystem() == "linux") { |
| 85 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 86 Expect.equals(2, e.osError.errorCode); |
| 87 } else if (platform.operatingSystem() == "macos") { |
| 88 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 89 Expect.equals(2, e.osError.errorCode); |
| 90 } else if (platform.operatingSystem() == "windows") { |
| 91 Expect.isTrue( |
| 92 e.toString().indexOf( |
| 93 "The system cannot find the path specified") != -1); |
| 94 Expect.equals(3, e.osError.errorCode); |
| 95 } |
| 96 |
| 97 return true; |
| 98 } |
| 99 |
| 100 void testCreateInNonExistentDirectory() { |
| 101 var file = new File("${tempDir()}/nonExistentDirectory/newFile"); |
| 102 |
| 103 // Create in non-existent directory should throw exception. |
| 104 Expect.throws(() => file.createSync(), |
| 105 (e) => checkCreateInNonExistentDirectoryException(e)); |
| 106 |
| 107 file.create(() => Expect.fail("Unreachable code")); |
| 108 file.onError = (e) => checkCreateInNonExistentDirectoryException(e); |
| 109 } |
| 110 |
| 111 bool checkFullPathOnNonExistentDirectoryException(e) { |
| 112 Expect.isTrue(e is FileIOException); |
| 113 Expect.isTrue(e.osError != null); |
| 114 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1); |
| 115 Platform platform = new Platform(); |
| 116 if (platform.operatingSystem() == "linux") { |
| 117 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 118 } else if (platform.operatingSystem() == "macos") { |
| 119 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 120 } else if (platform.operatingSystem() == "windows") { |
| 121 Expect.isTrue( |
| 122 e.toString().indexOf( |
| 123 "The system cannot find the file specified") != -1); |
| 124 } |
| 125 // File not not found has error code 2 on all supported platforms. |
| 126 Expect.equals(2, e.osError.errorCode); |
| 127 |
| 128 return true; |
| 129 } |
| 130 |
| 131 void testFullPathOnNonExistentDirectory() { |
| 132 var file = new File("${tempDir()}/nonExistentDirectory"); |
| 133 |
| 134 // Full path non-existent directory should throw exception. |
| 135 Expect.throws(() => file.fullPathSync(), |
| 136 (e) => checkFullPathOnNonExistentDirectoryException(e)); |
| 137 |
| 138 file.fullPath((path) => Expect.fail("Unreachable code $path")); |
| 139 file.onError = (e) => checkFullPathOnNonExistentDirectoryException(e); |
| 140 } |
| 141 |
| 142 bool checkDirectoryInNonExistentDirectoryException(e) { |
| 143 Expect.isTrue(e is FileIOException); |
| 144 Expect.isTrue(e.osError != null); |
| 145 Expect.isTrue( |
| 146 e.toString().indexOf("Cannot retrieve directory for file") != -1); |
| 147 Platform platform = new Platform(); |
| 148 if (platform.operatingSystem() == "linux") { |
| 149 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 150 } else if (platform.operatingSystem() == "macos") { |
| 151 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 152 } else if (platform.operatingSystem() == "windows") { |
| 153 Expect.isTrue( |
| 154 e.toString().indexOf( |
| 155 "The system cannot find the file specified") != -1); |
| 156 } |
| 157 // File not not found has error code 2 on all supported platforms. |
| 158 Expect.equals(2, e.osError.errorCode); |
| 159 |
| 160 return true; |
| 161 } |
| 162 |
| 163 void testDirectoryInNonExistentDirectory() { |
| 164 var file = new File("${tempDir()}/nonExistentDirectory/newFile"); |
| 165 |
| 166 // Create in non-existent directory should throw exception. |
| 167 Expect.throws(() => file.directorySync(), |
| 168 (e) => checkDirectoryInNonExistentDirectoryException(e)); |
| 169 |
| 170 file.directory((directory) => Expect.fail("Unreachable code")); |
| 171 file.onError = (e) => checkDirectoryInNonExistentDirectoryException(e); |
| 172 } |
| 173 |
| 174 void testReadAsBytesNonExistent() { |
| 175 var file = new File("${tempDir()}/nonExistentFile3"); |
| 176 |
| 177 // Non-existing file should throw exception. |
| 178 Expect.throws(() => file.readAsBytesSync(), |
| 179 (e) => checkOpenNonExistentFileException(e)); |
| 180 |
| 181 // TODO(sgjesse): Handle error for file.readAsBytes as well. |
| 182 } |
| 183 |
| 184 void testReadAsTextNonExistent() { |
| 185 var file = new File("${tempDir()}/nonExistentFile4"); |
| 186 |
| 187 // Non-existing file should throw exception. |
| 188 Expect.throws(() => file.readAsTextSync(), |
| 189 (e) => checkOpenNonExistentFileException(e)); |
| 190 |
| 191 // TODO(sgjesse): Handle error for file.readAsText as well. |
| 192 } |
| 193 |
| 194 void testReadAsLinesNonExistent() { |
| 195 var file = new File("${tempDir()}/nonExistentFile5"); |
| 196 |
| 197 // Non-existing file should throw exception. |
| 198 Expect.throws(() => file.readAsLinesSync(), |
| 199 (e) => checkOpenNonExistentFileException(e)); |
| 200 |
| 201 // TODO(sgjesse): Handle error for file.readAsLines as well. |
| 202 } |
| 203 |
| 204 main() { |
| 205 testOpenNonExistent(); |
| 206 testDeleteNonExistent(); |
| 207 testCreateInNonExistentDirectory(); |
| 208 testFullPathOnNonExistentDirectory(); |
| 209 testDirectoryInNonExistentDirectory(); |
| 210 testReadAsBytesNonExistent(); |
| 211 testReadAsTextNonExistent(); |
| 212 testReadAsLinesNonExistent(); |
| 213 } |
OLD | NEW |