| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 error handling in file I/O. | 5 // Dart test program for testing error handling in file I/O. |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 import "dart:isolate"; | 9 import "dart:isolate"; |
| 10 | 10 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 (e) => checkFullPathOnNonExistentDirectoryException(e)); | 164 (e) => checkFullPathOnNonExistentDirectoryException(e)); |
| 165 | 165 |
| 166 var fullPathFuture = file.fullPath(); | 166 var fullPathFuture = file.fullPath(); |
| 167 fullPathFuture.then((path) => Expect.fail("Unreachable code $path")) | 167 fullPathFuture.then((path) => Expect.fail("Unreachable code $path")) |
| 168 .catchError((error) { | 168 .catchError((error) { |
| 169 checkFullPathOnNonExistentDirectoryException(error); | 169 checkFullPathOnNonExistentDirectoryException(error); |
| 170 p.toSendPort().send(null); | 170 p.toSendPort().send(null); |
| 171 }); | 171 }); |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool checkDirectoryInNonExistentDirectoryException(e) { | |
| 175 Expect.isTrue(e is FileIOException); | |
| 176 Expect.isTrue(e.osError != null); | |
| 177 Expect.isTrue( | |
| 178 e.toString().indexOf("Cannot retrieve directory for file") != -1); | |
| 179 // File not not found has error code 2 on all supported platforms. | |
| 180 Expect.equals(2, e.osError.errorCode); | |
| 181 | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 void testDirectoryInNonExistentDirectory() { | |
| 186 Directory temp = tempDir(); | |
| 187 ReceivePort p = new ReceivePort(); | |
| 188 p.receive((x, y) { | |
| 189 p.close(); | |
| 190 temp.deleteSync(recursive: true); | |
| 191 }); | |
| 192 var file = new File("${temp.path}/nonExistentDirectory/newFile"); | |
| 193 | |
| 194 // Create in non-existent directory should throw exception. | |
| 195 Expect.throws(() => file.directorySync(), | |
| 196 (e) => checkDirectoryInNonExistentDirectoryException(e)); | |
| 197 | |
| 198 var dirFuture = file.directory(); | |
| 199 dirFuture.then((directory) => Expect.fail("Unreachable code")) | |
| 200 .catchError((error) { | |
| 201 checkDirectoryInNonExistentDirectoryException(error); | |
| 202 p.toSendPort().send(null); | |
| 203 }); | |
| 204 } | |
| 205 | |
| 206 void testReadAsBytesNonExistent() { | 174 void testReadAsBytesNonExistent() { |
| 207 Directory temp = tempDir(); | 175 Directory temp = tempDir(); |
| 208 ReceivePort p = new ReceivePort(); | 176 ReceivePort p = new ReceivePort(); |
| 209 p.receive((x, y) { | 177 p.receive((x, y) { |
| 210 p.close(); | 178 p.close(); |
| 211 temp.deleteSync(recursive: true); | 179 temp.deleteSync(recursive: true); |
| 212 }); | 180 }); |
| 213 var file = new File("${temp.path}/nonExistentFile3"); | 181 var file = new File("${temp.path}/nonExistentFile3"); |
| 214 | 182 |
| 215 // Non-existing file should throw exception. | 183 // Non-existing file should throw exception. |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 port.send(null); | 442 port.send(null); |
| 475 }); | 443 }); |
| 476 } | 444 } |
| 477 | 445 |
| 478 main() { | 446 main() { |
| 479 testOpenNonExistent(); | 447 testOpenNonExistent(); |
| 480 testDeleteNonExistent(); | 448 testDeleteNonExistent(); |
| 481 testLengthNonExistent(); | 449 testLengthNonExistent(); |
| 482 testCreateInNonExistentDirectory(); | 450 testCreateInNonExistentDirectory(); |
| 483 testFullPathOnNonExistentDirectory(); | 451 testFullPathOnNonExistentDirectory(); |
| 484 testDirectoryInNonExistentDirectory(); | |
| 485 testReadAsBytesNonExistent(); | 452 testReadAsBytesNonExistent(); |
| 486 testReadAsTextNonExistent(); | 453 testReadAsTextNonExistent(); |
| 487 testReadAsLinesNonExistent(); | 454 testReadAsLinesNonExistent(); |
| 488 testWriteByteToReadOnlyFile(); | 455 testWriteByteToReadOnlyFile(); |
| 489 testWriteFromToReadOnlyFile(); | 456 testWriteFromToReadOnlyFile(); |
| 490 testTruncateReadOnlyFile(); | 457 testTruncateReadOnlyFile(); |
| 491 testOperateOnClosedFile(); | 458 testOperateOnClosedFile(); |
| 492 testRepeatedlyCloseFile(); | 459 testRepeatedlyCloseFile(); |
| 493 testRepeatedlyCloseFileSync(); | 460 testRepeatedlyCloseFileSync(); |
| 494 testReadSyncBigInt(); | 461 testReadSyncBigInt(); |
| 495 testReadSyncClosedFile(); | 462 testReadSyncClosedFile(); |
| 496 } | 463 } |
| OLD | NEW |