| 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 var d = new Directory(''); | 11 return new Directory('').createTempSync(); |
| 12 d.createTempSync(); | |
| 13 return d; | |
| 14 } | 12 } |
| 15 | 13 |
| 16 | 14 |
| 17 bool checkCreateInNonExistentFileException(e) { | 15 bool checkCreateInNonExistentFileException(e) { |
| 18 Expect.isTrue(e is DirectoryIOException); | 16 Expect.isTrue(e is DirectoryIOException); |
| 19 Expect.isTrue(e.osError != null); | 17 Expect.isTrue(e.osError != null); |
| 20 Expect.isTrue(e.toString().indexOf("Creation failed") != -1); | 18 Expect.isTrue(e.toString().indexOf("Creation failed") != -1); |
| 21 if (Platform.operatingSystem == "linux") { | 19 if (Platform.operatingSystem == "linux") { |
| 22 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 20 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 23 Expect.equals(2, e.osError.errorCode); | 21 Expect.equals(2, e.osError.errorCode); |
| 24 } else if (Platform.operatingSystem == "macos") { | 22 } else if (Platform.operatingSystem == "macos") { |
| 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 26 Expect.equals(2, e.osError.errorCode); | 24 Expect.equals(2, e.osError.errorCode); |
| 27 } else if (Platform.operatingSystem == "windows") { | 25 } else if (Platform.operatingSystem == "windows") { |
| 28 Expect.isTrue( | 26 Expect.isTrue( |
| 29 e.toString().indexOf( | 27 e.toString().indexOf( |
| 30 "The system cannot find the path specified") != -1); | 28 "The system cannot find the path specified") != -1); |
| 31 Expect.equals(3, e.osError.errorCode); | 29 Expect.equals(3, e.osError.errorCode); |
| 32 } | 30 } |
| 33 | 31 |
| 34 return true; | 32 return true; |
| 35 } | 33 } |
| 36 | 34 |
| 37 | 35 |
| 38 void testCreateInNonExistent(Directory temp, Function done) { | 36 void testCreateInNonExistent(Directory temp, Function done) { |
| 39 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 37 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
| 40 Expect.throws(() => inNonExistent.createSync(), | 38 Expect.throws(() => inNonExistent.createSync(), |
| 41 (e) => checkCreateInNonExistentFileException(e)); | 39 (e) => checkCreateInNonExistentFileException(e)); |
| 42 | 40 |
| 43 inNonExistent.create(() => Expect.fail("Unreachable code")); | 41 inNonExistent.create().handleException((e) { |
| 44 inNonExistent.onError = (e) { | |
| 45 checkCreateInNonExistentFileException(e); | 42 checkCreateInNonExistentFileException(e); |
| 46 done(); | 43 done(); |
| 47 }; | 44 return true; |
| 45 }); |
| 48 } | 46 } |
| 49 | 47 |
| 50 | 48 |
| 51 bool checkCreateTempInNonExistentFileException(e) { | 49 bool checkCreateTempInNonExistentFileException(e) { |
| 52 Expect.isTrue(e is DirectoryIOException); | 50 Expect.isTrue(e is DirectoryIOException); |
| 53 Expect.isTrue(e.osError != null); | 51 Expect.isTrue(e.osError != null); |
| 54 Expect.isTrue(e.toString().indexOf( | 52 Expect.isTrue(e.toString().indexOf( |
| 55 "Creation of temporary directory failed") != -1); | 53 "Creation of temporary directory failed") != -1); |
| 56 if (Platform.operatingSystem == "linux") { | 54 if (Platform.operatingSystem == "linux") { |
| 57 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 55 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 68 | 66 |
| 69 return true; | 67 return true; |
| 70 } | 68 } |
| 71 | 69 |
| 72 | 70 |
| 73 void testCreateTempInNonExistent(Directory temp, Function done) { | 71 void testCreateTempInNonExistent(Directory temp, Function done) { |
| 74 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 72 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
| 75 Expect.throws(() => nonExistent.createTempSync(), | 73 Expect.throws(() => nonExistent.createTempSync(), |
| 76 (e) => checkCreateTempInNonExistentFileException(e)); | 74 (e) => checkCreateTempInNonExistentFileException(e)); |
| 77 | 75 |
| 78 nonExistent.createTemp(() => Expect.fail("Unreachable code")); | 76 nonExistent.createTemp().handleException((e) { |
| 79 nonExistent.onError = (e) { | |
| 80 checkCreateTempInNonExistentFileException(e); | 77 checkCreateTempInNonExistentFileException(e); |
| 81 done(); | 78 done(); |
| 82 }; | 79 return true; |
| 80 }); |
| 83 } | 81 } |
| 84 | 82 |
| 85 | 83 |
| 86 bool checkDeleteNonExistentFileException(e) { | 84 bool checkDeleteNonExistentFileException(e) { |
| 87 Expect.isTrue(e is DirectoryIOException); | 85 Expect.isTrue(e is DirectoryIOException); |
| 88 Expect.isTrue(e.osError != null); | 86 Expect.isTrue(e.osError != null); |
| 89 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | 87 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); |
| 90 if (Platform.operatingSystem == "linux") { | 88 if (Platform.operatingSystem == "linux") { |
| 91 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 89 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 92 } else if (Platform.operatingSystem == "macos") { | 90 } else if (Platform.operatingSystem == "macos") { |
| 93 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 91 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 94 } else if (Platform.operatingSystem == "windows") { | 92 } else if (Platform.operatingSystem == "windows") { |
| 95 Expect.isTrue( | 93 Expect.isTrue( |
| 96 e.toString().indexOf( | 94 e.toString().indexOf( |
| 97 "The system cannot find the file specified") != -1); | 95 "The system cannot find the file specified") != -1); |
| 98 } | 96 } |
| 99 // File not not found has error code 2 on all supported platforms. | 97 // File not not found has error code 2 on all supported platforms. |
| 100 Expect.equals(2, e.osError.errorCode); | 98 Expect.equals(2, e.osError.errorCode); |
| 101 | 99 |
| 102 return true; | 100 return true; |
| 103 } | 101 } |
| 104 | 102 |
| 105 | 103 |
| 106 void testDeleteNonExistent(Directory temp, Function done) { | 104 void testDeleteNonExistent(Directory temp, Function done) { |
| 107 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 105 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 108 Expect.throws(() => nonExistent.deleteSync(), | 106 Expect.throws(() => nonExistent.deleteSync(), |
| 109 (e) => checkDeleteNonExistentFileException(e)); | 107 (e) => checkDeleteNonExistentFileException(e)); |
| 110 | 108 |
| 111 nonExistent.delete(() => Expect.fail("Unreachable code")); | 109 nonExistent.delete().handleException((e) { |
| 112 nonExistent.onError = (e) { | |
| 113 checkDeleteNonExistentFileException(e); | 110 checkDeleteNonExistentFileException(e); |
| 114 done(); | 111 done(); |
| 115 }; | 112 return true; |
| 113 }); |
| 116 } | 114 } |
| 117 | 115 |
| 118 | 116 |
| 119 bool checkDeleteRecursivelyNonExistentFileException(e) { | 117 bool checkDeleteRecursivelyNonExistentFileException(e) { |
| 120 Expect.isTrue(e is DirectoryIOException); | 118 Expect.isTrue(e is DirectoryIOException); |
| 121 Expect.isTrue(e.osError != null); | 119 Expect.isTrue(e.osError != null); |
| 122 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | 120 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); |
| 123 if (Platform.operatingSystem == "linux") { | 121 if (Platform.operatingSystem == "linux") { |
| 124 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 122 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 125 Expect.equals(2, e.osError.errorCode); | 123 Expect.equals(2, e.osError.errorCode); |
| 126 } else if (Platform.operatingSystem == "macos") { | 124 } else if (Platform.operatingSystem == "macos") { |
| 127 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 125 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 128 Expect.equals(2, e.osError.errorCode); | 126 Expect.equals(2, e.osError.errorCode); |
| 129 } else if (Platform.operatingSystem == "windows") { | 127 } else if (Platform.operatingSystem == "windows") { |
| 130 Expect.isTrue( | 128 Expect.isTrue( |
| 131 e.toString().indexOf( | 129 e.toString().indexOf( |
| 132 "The system cannot find the path specified") != -1); | 130 "The system cannot find the path specified") != -1); |
| 133 Expect.equals(3, e.osError.errorCode); | 131 Expect.equals(3, e.osError.errorCode); |
| 134 } | 132 } |
| 135 | 133 |
| 136 return true; | 134 return true; |
| 137 } | 135 } |
| 138 | 136 |
| 139 | 137 |
| 140 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { | 138 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { |
| 141 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 139 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 142 Expect.throws(() => nonExistent.deleteRecursivelySync(), | 140 Expect.throws(() => nonExistent.deleteRecursivelySync(), |
| 143 (e) => checkDeleteRecursivelyNonExistentFileException(e)); | 141 (e) => checkDeleteRecursivelyNonExistentFileException(e)); |
| 144 | 142 |
| 145 nonExistent.deleteRecursively(() => Expect.fail("Unreachable code")); | 143 nonExistent.deleteRecursively().handleException((e) { |
| 146 nonExistent.onError = (e) { | |
| 147 checkDeleteRecursivelyNonExistentFileException(e); | 144 checkDeleteRecursivelyNonExistentFileException(e); |
| 148 done(); | 145 done(); |
| 149 }; | 146 return true; |
| 147 }); |
| 150 } | 148 } |
| 151 | 149 |
| 152 | 150 |
| 153 bool checkListNonExistentFileException(e) { | 151 bool checkListNonExistentFileException(e) { |
| 154 Expect.isTrue(e is DirectoryIOException); | 152 Expect.isTrue(e is DirectoryIOException); |
| 155 Expect.isTrue(e.osError != null); | 153 Expect.isTrue(e.osError != null); |
| 156 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); | 154 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); |
| 157 if (Platform.operatingSystem == "linux") { | 155 if (Platform.operatingSystem == "linux") { |
| 158 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 156 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 159 Expect.equals(2, e.osError.errorCode); | 157 Expect.equals(2, e.osError.errorCode); |
| 160 } else if (Platform.operatingSystem == "macos") { | 158 } else if (Platform.operatingSystem == "macos") { |
| 161 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 159 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 162 Expect.equals(2, e.osError.errorCode); | 160 Expect.equals(2, e.osError.errorCode); |
| 163 } else if (Platform.operatingSystem == "windows") { | 161 } else if (Platform.operatingSystem == "windows") { |
| 164 Expect.isTrue( | 162 Expect.isTrue( |
| 165 e.toString().indexOf( | 163 e.toString().indexOf( |
| 166 "The system cannot find the path specified") != -1); | 164 "The system cannot find the path specified") != -1); |
| 167 Expect.equals(3, e.osError.errorCode); | 165 Expect.equals(3, e.osError.errorCode); |
| 168 } | 166 } |
| 169 | 167 |
| 170 return true; | 168 return true; |
| 171 } | 169 } |
| 172 | 170 |
| 173 | 171 |
| 174 void testListNonExistent(Directory temp, Function done) { | 172 void testListNonExistent(Directory temp, Function done) { |
| 175 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 173 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 176 nonExistent.list(); | 174 var lister = nonExistent.list(); |
| 177 nonExistent.onError = (e) { | 175 lister.onError = (e) { |
| 178 checkListNonExistentFileException(e); | 176 checkListNonExistentFileException(e); |
| 179 done(); | 177 done(); |
| 180 }; | 178 }; |
| 181 } | 179 } |
| 182 | 180 |
| 183 | 181 |
| 184 void runTest(Function test) { | 182 void runTest(Function test) { |
| 185 // Create a temporary directory for the test. | 183 // Create a temporary directory for the test. |
| 186 var temp = new Directory(''); | 184 var temp = new Directory('').createTempSync(); |
| 187 temp.createTempSync(); | |
| 188 | 185 |
| 189 // Wait for the test to finish and delete the temporary directory. | 186 // Wait for the test to finish and delete the temporary directory. |
| 190 ReceivePort p = new ReceivePort(); | 187 ReceivePort p = new ReceivePort(); |
| 191 p.receive((x,y) { | 188 p.receive((x,y) { |
| 192 p.close(); | 189 p.close(); |
| 193 temp.deleteRecursivelySync(); | 190 temp.deleteRecursivelySync(); |
| 194 }); | 191 }); |
| 195 | 192 |
| 196 // Run the test. | 193 // Run the test. |
| 197 test(temp, () => p.toSendPort().send(null)); | 194 test(temp, () => p.toSendPort().send(null)); |
| 198 } | 195 } |
| 199 | 196 |
| 200 | 197 |
| 201 main() { | 198 main() { |
| 202 runTest(testCreateInNonExistent); | 199 runTest(testCreateInNonExistent); |
| 203 runTest(testCreateTempInNonExistent); | 200 runTest(testCreateTempInNonExistent); |
| 204 runTest(testDeleteNonExistent); | 201 runTest(testDeleteNonExistent); |
| 205 runTest(testDeleteRecursivelyNonExistent); | 202 runTest(testDeleteRecursivelyNonExistent); |
| 206 runTest(testListNonExistent); | 203 runTest(testListNonExistent); |
| 207 } | 204 } |
| OLD | NEW |