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