| 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 // 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("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 Directory tempDir() { | 10 Directory tempDir() { |
| 11 var d = new Directory(''); | 11 var d = new Directory(''); |
| 12 d.createTempSync(); | 12 d.createTempSync(); |
| 13 return d; | 13 return d; |
| 14 } | 14 } |
| 15 | 15 |
| 16 | 16 |
| 17 bool checkOpenNonExistentFileException(e) { | 17 bool checkNonExistentFileException(e, str) { |
| 18 Expect.isTrue(e is FileIOException); | 18 Expect.isTrue(e is FileIOException); |
| 19 Expect.isTrue(e.osError != null); | 19 Expect.isTrue(e.osError != null); |
| 20 Expect.isTrue(e.toString().indexOf("Cannot open file") != -1); | 20 Expect.isTrue(e.toString().indexOf(str) != -1); |
| 21 Platform platform = new Platform(); | 21 Platform platform = new Platform(); |
| 22 if (platform.operatingSystem() == "linux") { | 22 if (platform.operatingSystem() == "linux") { |
| 23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 24 } else if (platform.operatingSystem() == "macos") { | 24 } else if (platform.operatingSystem() == "macos") { |
| 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 26 } else if (platform.operatingSystem() == "windows") { | 26 } else if (platform.operatingSystem() == "windows") { |
| 27 Expect.isTrue( | 27 Expect.isTrue( |
| 28 e.toString().indexOf( | 28 e.toString().indexOf( |
| 29 "The system cannot find the file specified") != -1); | 29 "The system cannot find the file specified") != -1); |
| 30 } | 30 } |
| 31 // File not not found has error code 2 on all supported platforms. | 31 // File not not found has error code 2 on all supported platforms. |
| 32 Expect.equals(2, e.osError.errorCode); | 32 Expect.equals(2, e.osError.errorCode); |
| 33 | 33 |
| 34 return true; | 34 return true; |
| 35 } | 35 } |
| 36 | 36 |
| 37 |
| 38 bool checkOpenNonExistentFileException(e) { |
| 39 return checkNonExistentFileException(e, "Cannot open file"); |
| 40 } |
| 41 |
| 42 |
| 43 bool checkDeleteNonExistentFileException(e) { |
| 44 return checkNonExistentFileException(e, "Cannot delete file"); |
| 45 } |
| 46 |
| 47 |
| 48 bool checkLengthNonExistentFileException(e) { |
| 49 return checkNonExistentFileException(e, "Cannot retrieve length of file"); |
| 50 } |
| 51 |
| 52 |
| 37 void testOpenNonExistent() { | 53 void testOpenNonExistent() { |
| 38 Directory temp = tempDir(); | 54 Directory temp = tempDir(); |
| 39 ReceivePort p = new ReceivePort(); | 55 ReceivePort p = new ReceivePort(); |
| 40 p.receive((x, y) { | 56 p.receive((x, y) { |
| 41 p.close(); | 57 p.close(); |
| 42 temp.deleteRecursivelySync(); | 58 temp.deleteRecursivelySync(); |
| 43 }); | 59 }); |
| 44 var file = new File("${temp.path}/nonExistentFile"); | 60 var file = new File("${temp.path}/nonExistentFile"); |
| 45 | 61 |
| 46 // Non-existing file should throw exception. | 62 // Non-existing file should throw exception. |
| 47 Expect.throws(() => file.openSync(), | 63 Expect.throws(() => file.openSync(), |
| 48 (e) => checkOpenNonExistentFileException(e)); | 64 (e) => checkOpenNonExistentFileException(e)); |
| 49 | 65 |
| 50 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code")); | 66 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code")); |
| 51 file.onError = (e) { | 67 file.onError = (e) { |
| 52 checkOpenNonExistentFileException(e); | 68 checkOpenNonExistentFileException(e); |
| 53 p.toSendPort().send(null); | 69 p.toSendPort().send(null); |
| 54 }; | 70 }; |
| 55 } | 71 } |
| 56 | 72 |
| 57 bool checkDeleteNonExistentFileException(e) { | |
| 58 Expect.isTrue(e is FileIOException); | |
| 59 Expect.isTrue(e.osError != null); | |
| 60 Expect.isTrue(e.toString().indexOf("Cannot delete file") != -1); | |
| 61 Platform platform = new Platform(); | |
| 62 if (platform.operatingSystem() == "linux") { | |
| 63 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 64 } else if (platform.operatingSystem() == "macos") { | |
| 65 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 66 } else if (platform.operatingSystem() == "windows") { | |
| 67 Expect.isTrue( | |
| 68 e.toString().indexOf( | |
| 69 "The system cannot find the file specified") != -1); | |
| 70 } | |
| 71 // File not not found has error code 2 on all supported platforms. | |
| 72 Expect.equals(2, e.osError.errorCode); | |
| 73 | |
| 74 return true; | |
| 75 } | |
| 76 | 73 |
| 77 void testDeleteNonExistent() { | 74 void testDeleteNonExistent() { |
| 78 Directory temp = tempDir(); | 75 Directory temp = tempDir(); |
| 79 ReceivePort p = new ReceivePort(); | 76 ReceivePort p = new ReceivePort(); |
| 80 p.receive((x, y) { | 77 p.receive((x, y) { |
| 81 p.close(); | 78 p.close(); |
| 82 temp.deleteRecursivelySync(); | 79 temp.deleteRecursivelySync(); |
| 83 }); | 80 }); |
| 84 var file = new File("${temp.path}/nonExistentFile"); | 81 var file = new File("${temp.path}/nonExistentFile"); |
| 85 | 82 |
| 86 // Non-existing file should throw exception. | 83 // Non-existing file should throw exception. |
| 87 Expect.throws(() => file.deleteSync(), | 84 Expect.throws(() => file.deleteSync(), |
| 88 (e) => checkDeleteNonExistentFileException(e)); | 85 (e) => checkDeleteNonExistentFileException(e)); |
| 89 | 86 |
| 90 file.delete(() => Expect.fail("Unreachable code")); | 87 file.delete(() => Expect.fail("Unreachable code")); |
| 91 file.onError = (e) { | 88 file.onError = (e) { |
| 92 checkDeleteNonExistentFileException(e); | 89 checkDeleteNonExistentFileException(e); |
| 93 p.toSendPort().send(null); | 90 p.toSendPort().send(null); |
| 94 }; | 91 }; |
| 95 } | 92 } |
| 96 | 93 |
| 94 |
| 95 void testLengthNonExistent() { |
| 96 Directory temp = tempDir(); |
| 97 ReceivePort p = new ReceivePort(); |
| 98 p.receive((x, y) { |
| 99 p.close(); |
| 100 temp.deleteRecursivelySync(); |
| 101 }); |
| 102 var file = new File("${temp.path}/nonExistentFile"); |
| 103 |
| 104 // Non-existing file should throw exception. |
| 105 Expect.throws(() => file.lengthSync(), |
| 106 (e) => checkLengthNonExistentFileException(e)); |
| 107 |
| 108 file.length((len) => Expect.fail("Unreachable code")); |
| 109 file.onError = (e) { |
| 110 checkLengthNonExistentFileException(e); |
| 111 p.toSendPort().send(null); |
| 112 }; |
| 113 } |
| 114 |
| 115 |
| 97 bool checkCreateInNonExistentDirectoryException(e) { | 116 bool checkCreateInNonExistentDirectoryException(e) { |
| 98 Expect.isTrue(e is FileIOException); | 117 Expect.isTrue(e is FileIOException); |
| 99 Expect.isTrue(e.osError != null); | 118 Expect.isTrue(e.osError != null); |
| 100 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1); | 119 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1); |
| 101 Platform platform = new Platform(); | 120 Platform platform = new Platform(); |
| 102 if (platform.operatingSystem() == "linux") { | 121 if (platform.operatingSystem() == "linux") { |
| 103 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 122 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 104 Expect.equals(2, e.osError.errorCode); | 123 Expect.equals(2, e.osError.errorCode); |
| 105 } else if (platform.operatingSystem() == "macos") { | 124 } else if (platform.operatingSystem() == "macos") { |
| 106 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 125 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 checkFileClosedException(e); | 444 checkFileClosedException(e); |
| 426 if (--errorCount == 0) { | 445 if (--errorCount == 0) { |
| 427 p.toSendPort().send(null); | 446 p.toSendPort().send(null); |
| 428 } | 447 } |
| 429 }; | 448 }; |
| 430 } | 449 } |
| 431 | 450 |
| 432 main() { | 451 main() { |
| 433 testOpenNonExistent(); | 452 testOpenNonExistent(); |
| 434 testDeleteNonExistent(); | 453 testDeleteNonExistent(); |
| 454 testLengthNonExistent(); |
| 435 testCreateInNonExistentDirectory(); | 455 testCreateInNonExistentDirectory(); |
| 436 testFullPathOnNonExistentDirectory(); | 456 testFullPathOnNonExistentDirectory(); |
| 437 testDirectoryInNonExistentDirectory(); | 457 testDirectoryInNonExistentDirectory(); |
| 438 testReadAsBytesNonExistent(); | 458 testReadAsBytesNonExistent(); |
| 439 testReadAsTextNonExistent(); | 459 testReadAsTextNonExistent(); |
| 440 testReadAsLinesNonExistent(); | 460 testReadAsLinesNonExistent(); |
| 441 testWriteByteToReadOnlyFile(); | 461 testWriteByteToReadOnlyFile(); |
| 442 testWriteListToReadOnlyFile(); | 462 testWriteListToReadOnlyFile(); |
| 443 testTruncateReadOnlyFile(); | 463 testTruncateReadOnlyFile(); |
| 444 testOperateOnClosedFile(); | 464 testOperateOnClosedFile(); |
| 445 } | 465 } |
| OLD | NEW |