| 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 directory I/O. | 5 // Dart test program for testing error handling in directory 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 return new Directory('').createTempSync(); | 11 return new Directory('').createTempSync(); |
| 12 } | 12 } |
| 13 | 13 |
| 14 | 14 |
| 15 bool checkCreateInNonExistentFileException(e) { | 15 bool checkCreateInNonExistentFileException(e) { |
| 16 Expect.isTrue(e is DirectoryIOException); | 16 Expect.isTrue(e is DirectoryIOException); |
| 17 Expect.isTrue(e.osError != null); | 17 Expect.isTrue(e.osError != null); |
| 18 Expect.isTrue(e.toString().indexOf("Creation failed") != -1); | 18 Expect.isTrue(e.toString().indexOf("Creation failed") != -1); |
| 19 if (Platform.operatingSystem == "linux") { | 19 if (Platform.operatingSystem == "linux") { |
| 20 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 21 Expect.equals(2, e.osError.errorCode); | 20 Expect.equals(2, e.osError.errorCode); |
| 22 } else if (Platform.operatingSystem == "macos") { | 21 } else if (Platform.operatingSystem == "macos") { |
| 23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 24 Expect.equals(2, e.osError.errorCode); | 22 Expect.equals(2, e.osError.errorCode); |
| 25 } else if (Platform.operatingSystem == "windows") { | 23 } else if (Platform.operatingSystem == "windows") { |
| 26 Expect.isTrue( | |
| 27 e.toString().indexOf( | |
| 28 "The system cannot find the path specified") != -1); | |
| 29 Expect.equals(3, e.osError.errorCode); | 24 Expect.equals(3, e.osError.errorCode); |
| 30 } | 25 } |
| 31 | 26 |
| 32 return true; | 27 return true; |
| 33 } | 28 } |
| 34 | 29 |
| 35 | 30 |
| 36 void testCreateInNonExistent(Directory temp, Function done) { | 31 void testCreateInNonExistent(Directory temp, Function done) { |
| 37 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 32 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
| 38 Expect.throws(() => inNonExistent.createSync(), | 33 Expect.throws(() => inNonExistent.createSync(), |
| 39 (e) => checkCreateInNonExistentFileException(e)); | 34 (e) => checkCreateInNonExistentFileException(e)); |
| 40 | 35 |
| 41 inNonExistent.create().handleException((e) { | 36 inNonExistent.create().handleException((e) { |
| 42 checkCreateInNonExistentFileException(e); | 37 checkCreateInNonExistentFileException(e); |
| 43 done(); | 38 done(); |
| 44 return true; | 39 return true; |
| 45 }); | 40 }); |
| 46 } | 41 } |
| 47 | 42 |
| 48 | 43 |
| 49 bool checkCreateTempInNonExistentFileException(e) { | 44 bool checkCreateTempInNonExistentFileException(e) { |
| 50 Expect.isTrue(e is DirectoryIOException); | 45 Expect.isTrue(e is DirectoryIOException); |
| 51 Expect.isTrue(e.osError != null); | 46 Expect.isTrue(e.osError != null); |
| 52 Expect.isTrue(e.toString().indexOf( | |
| 53 "Creation of temporary directory failed") != -1); | |
| 54 if (Platform.operatingSystem == "linux") { | 47 if (Platform.operatingSystem == "linux") { |
| 55 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 56 Expect.equals(2, e.osError.errorCode); | 48 Expect.equals(2, e.osError.errorCode); |
| 57 } else if (Platform.operatingSystem == "macos") { | 49 } else if (Platform.operatingSystem == "macos") { |
| 58 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 59 Expect.equals(2, e.osError.errorCode); | 50 Expect.equals(2, e.osError.errorCode); |
| 60 } else if (Platform.operatingSystem == "windows") { | 51 } else if (Platform.operatingSystem == "windows") { |
| 61 Expect.isTrue( | |
| 62 e.toString().indexOf( | |
| 63 "The system cannot find the path specified") != -1); | |
| 64 Expect.equals(3, e.osError.errorCode); | 52 Expect.equals(3, e.osError.errorCode); |
| 65 } | 53 } |
| 66 | 54 |
| 67 return true; | 55 return true; |
| 68 } | 56 } |
| 69 | 57 |
| 70 | 58 |
| 71 void testCreateTempInNonExistent(Directory temp, Function done) { | 59 void testCreateTempInNonExistent(Directory temp, Function done) { |
| 72 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 60 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
| 73 Expect.throws(() => nonExistent.createTempSync(), | 61 Expect.throws(() => nonExistent.createTempSync(), |
| 74 (e) => checkCreateTempInNonExistentFileException(e)); | 62 (e) => checkCreateTempInNonExistentFileException(e)); |
| 75 | 63 |
| 76 nonExistent.createTemp().handleException((e) { | 64 nonExistent.createTemp().handleException((e) { |
| 77 checkCreateTempInNonExistentFileException(e); | 65 checkCreateTempInNonExistentFileException(e); |
| 78 done(); | 66 done(); |
| 79 return true; | 67 return true; |
| 80 }); | 68 }); |
| 81 } | 69 } |
| 82 | 70 |
| 83 | 71 |
| 84 bool checkDeleteNonExistentFileException(e) { | 72 bool checkDeleteNonExistentFileException(e) { |
| 85 Expect.isTrue(e is DirectoryIOException); | 73 Expect.isTrue(e is DirectoryIOException); |
| 86 Expect.isTrue(e.osError != null); | 74 Expect.isTrue(e.osError != null); |
| 87 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | |
| 88 if (Platform.operatingSystem == "linux") { | |
| 89 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 90 } else if (Platform.operatingSystem == "macos") { | |
| 91 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 92 } else if (Platform.operatingSystem == "windows") { | |
| 93 Expect.isTrue( | |
| 94 e.toString().indexOf( | |
| 95 "The system cannot find the file specified") != -1); | |
| 96 } | |
| 97 // File not not found has error code 2 on all supported platforms. | 75 // File not not found has error code 2 on all supported platforms. |
| 98 Expect.equals(2, e.osError.errorCode); | 76 Expect.equals(2, e.osError.errorCode); |
| 99 | 77 |
| 100 return true; | 78 return true; |
| 101 } | 79 } |
| 102 | 80 |
| 103 | 81 |
| 104 void testDeleteNonExistent(Directory temp, Function done) { | 82 void testDeleteNonExistent(Directory temp, Function done) { |
| 105 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 83 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 106 Expect.throws(() => nonExistent.deleteSync(), | 84 Expect.throws(() => nonExistent.deleteSync(), |
| 107 (e) => checkDeleteNonExistentFileException(e)); | 85 (e) => checkDeleteNonExistentFileException(e)); |
| 108 | 86 |
| 109 nonExistent.delete().handleException((e) { | 87 nonExistent.delete().handleException((e) { |
| 110 checkDeleteNonExistentFileException(e); | 88 checkDeleteNonExistentFileException(e); |
| 111 done(); | 89 done(); |
| 112 return true; | 90 return true; |
| 113 }); | 91 }); |
| 114 } | 92 } |
| 115 | 93 |
| 116 | 94 |
| 117 bool checkDeleteRecursivelyNonExistentFileException(e) { | 95 bool checkDeleteRecursivelyNonExistentFileException(e) { |
| 118 Expect.isTrue(e is DirectoryIOException); | 96 Expect.isTrue(e is DirectoryIOException); |
| 119 Expect.isTrue(e.osError != null); | 97 Expect.isTrue(e.osError != null); |
| 120 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | 98 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); |
| 121 if (Platform.operatingSystem == "linux") { | 99 if (Platform.operatingSystem == "linux") { |
| 122 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 123 Expect.equals(2, e.osError.errorCode); | 100 Expect.equals(2, e.osError.errorCode); |
| 124 } else if (Platform.operatingSystem == "macos") { | 101 } else if (Platform.operatingSystem == "macos") { |
| 125 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 126 Expect.equals(2, e.osError.errorCode); | 102 Expect.equals(2, e.osError.errorCode); |
| 127 } else if (Platform.operatingSystem == "windows") { | 103 } else if (Platform.operatingSystem == "windows") { |
| 128 Expect.isTrue( | |
| 129 e.toString().indexOf( | |
| 130 "The system cannot find the path specified") != -1); | |
| 131 Expect.equals(3, e.osError.errorCode); | 104 Expect.equals(3, e.osError.errorCode); |
| 132 } | 105 } |
| 133 | 106 |
| 134 return true; | 107 return true; |
| 135 } | 108 } |
| 136 | 109 |
| 137 | 110 |
| 138 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { | 111 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { |
| 139 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 112 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 140 Expect.throws(() => nonExistent.deleteSync(recursive: true), | 113 Expect.throws(() => nonExistent.deleteSync(recursive: true), |
| 141 (e) => checkDeleteRecursivelyNonExistentFileException(e)); | 114 (e) => checkDeleteRecursivelyNonExistentFileException(e)); |
| 142 | 115 |
| 143 nonExistent.delete(recursive: true).handleException((e) { | 116 nonExistent.delete(recursive: true).handleException((e) { |
| 144 checkDeleteRecursivelyNonExistentFileException(e); | 117 checkDeleteRecursivelyNonExistentFileException(e); |
| 145 done(); | 118 done(); |
| 146 return true; | 119 return true; |
| 147 }); | 120 }); |
| 148 } | 121 } |
| 149 | 122 |
| 150 | 123 |
| 151 bool checkListNonExistentFileException(e) { | 124 bool checkListNonExistentFileException(e) { |
| 152 Expect.isTrue(e is DirectoryIOException); | 125 Expect.isTrue(e is DirectoryIOException); |
| 153 Expect.isTrue(e.osError != null); | 126 Expect.isTrue(e.osError != null); |
| 154 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); | 127 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); |
| 155 if (Platform.operatingSystem == "linux") { | 128 if (Platform.operatingSystem == "linux") { |
| 156 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 157 Expect.equals(2, e.osError.errorCode); | 129 Expect.equals(2, e.osError.errorCode); |
| 158 } else if (Platform.operatingSystem == "macos") { | 130 } else if (Platform.operatingSystem == "macos") { |
| 159 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | |
| 160 Expect.equals(2, e.osError.errorCode); | 131 Expect.equals(2, e.osError.errorCode); |
| 161 } else if (Platform.operatingSystem == "windows") { | 132 } else if (Platform.operatingSystem == "windows") { |
| 162 Expect.isTrue( | |
| 163 e.toString().indexOf( | |
| 164 "The system cannot find the path specified") != -1); | |
| 165 Expect.equals(3, e.osError.errorCode); | 133 Expect.equals(3, e.osError.errorCode); |
| 166 } | 134 } |
| 167 | 135 |
| 168 return true; | 136 return true; |
| 169 } | 137 } |
| 170 | 138 |
| 171 | 139 |
| 172 void testListNonExistent(Directory temp, Function done) { | 140 void testListNonExistent(Directory temp, Function done) { |
| 173 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 141 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 174 var lister = nonExistent.list(); | 142 var lister = nonExistent.list(); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 main() { | 216 main() { |
| 249 runTest(testCreateInNonExistent); | 217 runTest(testCreateInNonExistent); |
| 250 runTest(testCreateTempInNonExistent); | 218 runTest(testCreateTempInNonExistent); |
| 251 runTest(testDeleteNonExistent); | 219 runTest(testDeleteNonExistent); |
| 252 runTest(testDeleteRecursivelyNonExistent); | 220 runTest(testDeleteRecursivelyNonExistent); |
| 253 runTest(testListNonExistent); | 221 runTest(testListNonExistent); |
| 254 runTest(testRenameNonExistent); | 222 runTest(testRenameNonExistent); |
| 255 runTest(testRenameFileAsDirectory); | 223 runTest(testRenameFileAsDirectory); |
| 256 runTest(testRenameOverwriteFile); | 224 runTest(testRenameOverwriteFile); |
| 257 } | 225 } |
| OLD | NEW |