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() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 bool checkLengthNonExistentFileException(e) { | 45 bool checkLengthNonExistentFileException(e) { |
46 return checkNonExistentFileException(e, "Cannot retrieve length of file"); | 46 return checkNonExistentFileException(e, "Cannot retrieve length of file"); |
47 } | 47 } |
48 | 48 |
49 | 49 |
50 void testOpenNonExistent() { | 50 void testOpenNonExistent() { |
51 Directory temp = tempDir(); | 51 Directory temp = tempDir(); |
52 ReceivePort p = new ReceivePort(); | 52 ReceivePort p = new ReceivePort(); |
53 p.receive((x, y) { | 53 p.receive((x, y) { |
54 p.close(); | 54 p.close(); |
55 temp.deleteRecursivelySync(); | 55 temp.deleteSync(recursive: true); |
56 }); | 56 }); |
57 var file = new File("${temp.path}/nonExistentFile"); | 57 var file = new File("${temp.path}/nonExistentFile"); |
58 | 58 |
59 // Non-existing file should throw exception. | 59 // Non-existing file should throw exception. |
60 Expect.throws(() => file.openSync(), | 60 Expect.throws(() => file.openSync(), |
61 (e) => checkOpenNonExistentFileException(e)); | 61 (e) => checkOpenNonExistentFileException(e)); |
62 | 62 |
63 var openFuture = file.open(FileMode.READ); | 63 var openFuture = file.open(FileMode.READ); |
64 openFuture.then((raf) => Expect.fail("Unreachable code")); | 64 openFuture.then((raf) => Expect.fail("Unreachable code")); |
65 openFuture.handleException((e) { | 65 openFuture.handleException((e) { |
66 checkOpenNonExistentFileException(e); | 66 checkOpenNonExistentFileException(e); |
67 p.toSendPort().send(null); | 67 p.toSendPort().send(null); |
68 return true; | 68 return true; |
69 }); | 69 }); |
70 } | 70 } |
71 | 71 |
72 | 72 |
73 void testDeleteNonExistent() { | 73 void testDeleteNonExistent() { |
74 Directory temp = tempDir(); | 74 Directory temp = tempDir(); |
75 ReceivePort p = new ReceivePort(); | 75 ReceivePort p = new ReceivePort(); |
76 p.receive((x, y) { | 76 p.receive((x, y) { |
77 p.close(); | 77 p.close(); |
78 temp.deleteRecursivelySync(); | 78 temp.deleteSync(recursive: true); |
79 }); | 79 }); |
80 var file = new File("${temp.path}/nonExistentFile"); | 80 var file = new File("${temp.path}/nonExistentFile"); |
81 | 81 |
82 // Non-existing file should throw exception. | 82 // Non-existing file should throw exception. |
83 Expect.throws(() => file.deleteSync(), | 83 Expect.throws(() => file.deleteSync(), |
84 (e) => checkDeleteNonExistentFileException(e)); | 84 (e) => checkDeleteNonExistentFileException(e)); |
85 | 85 |
86 var delete = file.delete(); | 86 var delete = file.delete(); |
87 delete.then((ignore) => Expect.fail("Unreachable code")); | 87 delete.then((ignore) => Expect.fail("Unreachable code")); |
88 delete.handleException((e) { | 88 delete.handleException((e) { |
89 checkDeleteNonExistentFileException(e); | 89 checkDeleteNonExistentFileException(e); |
90 p.toSendPort().send(null); | 90 p.toSendPort().send(null); |
91 return true; | 91 return true; |
92 }); | 92 }); |
93 } | 93 } |
94 | 94 |
95 | 95 |
96 void testLengthNonExistent() { | 96 void testLengthNonExistent() { |
97 Directory temp = tempDir(); | 97 Directory temp = tempDir(); |
98 ReceivePort p = new ReceivePort(); | 98 ReceivePort p = new ReceivePort(); |
99 p.receive((x, y) { | 99 p.receive((x, y) { |
100 p.close(); | 100 p.close(); |
101 temp.deleteRecursivelySync(); | 101 temp.deleteSync(recursive: true); |
102 }); | 102 }); |
103 var file = new File("${temp.path}/nonExistentFile"); | 103 var file = new File("${temp.path}/nonExistentFile"); |
104 | 104 |
105 // Non-existing file should throw exception. | 105 // Non-existing file should throw exception. |
106 Expect.throws(() => file.lengthSync(), | 106 Expect.throws(() => file.lengthSync(), |
107 (e) => checkLengthNonExistentFileException(e)); | 107 (e) => checkLengthNonExistentFileException(e)); |
108 | 108 |
109 var lenFuture = file.length(); | 109 var lenFuture = file.length(); |
110 lenFuture.then((len) => Expect.fail("Unreachable code")); | 110 lenFuture.then((len) => Expect.fail("Unreachable code")); |
111 lenFuture.handleException((e) { | 111 lenFuture.handleException((e) { |
(...skipping 22 matching lines...) Expand all Loading... |
134 } | 134 } |
135 | 135 |
136 return true; | 136 return true; |
137 } | 137 } |
138 | 138 |
139 void testCreateInNonExistentDirectory() { | 139 void testCreateInNonExistentDirectory() { |
140 Directory temp = tempDir(); | 140 Directory temp = tempDir(); |
141 ReceivePort p = new ReceivePort(); | 141 ReceivePort p = new ReceivePort(); |
142 p.receive((x, y) { | 142 p.receive((x, y) { |
143 p.close(); | 143 p.close(); |
144 temp.deleteRecursivelySync(); | 144 temp.deleteSync(recursive: true); |
145 }); | 145 }); |
146 var file = new File("${temp.path}/nonExistentDirectory/newFile"); | 146 var file = new File("${temp.path}/nonExistentDirectory/newFile"); |
147 | 147 |
148 // Create in non-existent directory should throw exception. | 148 // Create in non-existent directory should throw exception. |
149 Expect.throws(() => file.createSync(), | 149 Expect.throws(() => file.createSync(), |
150 (e) => checkCreateInNonExistentDirectoryException(e)); | 150 (e) => checkCreateInNonExistentDirectoryException(e)); |
151 | 151 |
152 var create = file.create(); | 152 var create = file.create(); |
153 create.then((ignore) => Expect.fail("Unreachable code")); | 153 create.then((ignore) => Expect.fail("Unreachable code")); |
154 create.handleException((e) { | 154 create.handleException((e) { |
(...skipping 20 matching lines...) Expand all Loading... |
175 Expect.equals(2, e.osError.errorCode); | 175 Expect.equals(2, e.osError.errorCode); |
176 | 176 |
177 return true; | 177 return true; |
178 } | 178 } |
179 | 179 |
180 void testFullPathOnNonExistentDirectory() { | 180 void testFullPathOnNonExistentDirectory() { |
181 Directory temp = tempDir(); | 181 Directory temp = tempDir(); |
182 ReceivePort p = new ReceivePort(); | 182 ReceivePort p = new ReceivePort(); |
183 p.receive((x, y) { | 183 p.receive((x, y) { |
184 p.close(); | 184 p.close(); |
185 temp.deleteRecursivelySync(); | 185 temp.deleteSync(recursive: true); |
186 }); | 186 }); |
187 var file = new File("${temp.path}/nonExistentDirectory"); | 187 var file = new File("${temp.path}/nonExistentDirectory"); |
188 | 188 |
189 // Full path non-existent directory should throw exception. | 189 // Full path non-existent directory should throw exception. |
190 Expect.throws(() => file.fullPathSync(), | 190 Expect.throws(() => file.fullPathSync(), |
191 (e) => checkFullPathOnNonExistentDirectoryException(e)); | 191 (e) => checkFullPathOnNonExistentDirectoryException(e)); |
192 | 192 |
193 var fullPathFuture = file.fullPath(); | 193 var fullPathFuture = file.fullPath(); |
194 fullPathFuture.then((path) => Expect.fail("Unreachable code $path")); | 194 fullPathFuture.then((path) => Expect.fail("Unreachable code $path")); |
195 fullPathFuture.handleException((e) { | 195 fullPathFuture.handleException((e) { |
(...skipping 21 matching lines...) Expand all Loading... |
217 Expect.equals(2, e.osError.errorCode); | 217 Expect.equals(2, e.osError.errorCode); |
218 | 218 |
219 return true; | 219 return true; |
220 } | 220 } |
221 | 221 |
222 void testDirectoryInNonExistentDirectory() { | 222 void testDirectoryInNonExistentDirectory() { |
223 Directory temp = tempDir(); | 223 Directory temp = tempDir(); |
224 ReceivePort p = new ReceivePort(); | 224 ReceivePort p = new ReceivePort(); |
225 p.receive((x, y) { | 225 p.receive((x, y) { |
226 p.close(); | 226 p.close(); |
227 temp.deleteRecursivelySync(); | 227 temp.deleteSync(recursive: true); |
228 }); | 228 }); |
229 var file = new File("${temp.path}/nonExistentDirectory/newFile"); | 229 var file = new File("${temp.path}/nonExistentDirectory/newFile"); |
230 | 230 |
231 // Create in non-existent directory should throw exception. | 231 // Create in non-existent directory should throw exception. |
232 Expect.throws(() => file.directorySync(), | 232 Expect.throws(() => file.directorySync(), |
233 (e) => checkDirectoryInNonExistentDirectoryException(e)); | 233 (e) => checkDirectoryInNonExistentDirectoryException(e)); |
234 | 234 |
235 var dirFuture = file.directory(); | 235 var dirFuture = file.directory(); |
236 dirFuture.then((directory) => Expect.fail("Unreachable code")); | 236 dirFuture.then((directory) => Expect.fail("Unreachable code")); |
237 dirFuture.handleException((e) { | 237 dirFuture.handleException((e) { |
238 checkDirectoryInNonExistentDirectoryException(e); | 238 checkDirectoryInNonExistentDirectoryException(e); |
239 p.toSendPort().send(null); | 239 p.toSendPort().send(null); |
240 return true; | 240 return true; |
241 }); | 241 }); |
242 } | 242 } |
243 | 243 |
244 void testReadAsBytesNonExistent() { | 244 void testReadAsBytesNonExistent() { |
245 Directory temp = tempDir(); | 245 Directory temp = tempDir(); |
246 ReceivePort p = new ReceivePort(); | 246 ReceivePort p = new ReceivePort(); |
247 p.receive((x, y) { | 247 p.receive((x, y) { |
248 p.close(); | 248 p.close(); |
249 temp.deleteRecursivelySync(); | 249 temp.deleteSync(recursive: true); |
250 }); | 250 }); |
251 var file = new File("${temp.path}/nonExistentFile3"); | 251 var file = new File("${temp.path}/nonExistentFile3"); |
252 | 252 |
253 // Non-existing file should throw exception. | 253 // Non-existing file should throw exception. |
254 Expect.throws(() => file.readAsBytesSync(), | 254 Expect.throws(() => file.readAsBytesSync(), |
255 (e) => checkOpenNonExistentFileException(e)); | 255 (e) => checkOpenNonExistentFileException(e)); |
256 | 256 |
257 var readAsBytesFuture = file.readAsBytes(); | 257 var readAsBytesFuture = file.readAsBytes(); |
258 readAsBytesFuture.then((data) => Expect.fail("Unreachable code")); | 258 readAsBytesFuture.then((data) => Expect.fail("Unreachable code")); |
259 readAsBytesFuture.handleException((e) { | 259 readAsBytesFuture.handleException((e) { |
260 checkOpenNonExistentFileException(e); | 260 checkOpenNonExistentFileException(e); |
261 p.toSendPort().send(null); | 261 p.toSendPort().send(null); |
262 return true; | 262 return true; |
263 }); | 263 }); |
264 } | 264 } |
265 | 265 |
266 void testReadAsTextNonExistent() { | 266 void testReadAsTextNonExistent() { |
267 Directory temp = tempDir(); | 267 Directory temp = tempDir(); |
268 ReceivePort p = new ReceivePort(); | 268 ReceivePort p = new ReceivePort(); |
269 p.receive((x, y) { | 269 p.receive((x, y) { |
270 p.close(); | 270 p.close(); |
271 temp.deleteRecursivelySync(); | 271 temp.deleteSync(recursive: true); |
272 }); | 272 }); |
273 var file = new File("${temp.path}/nonExistentFile4"); | 273 var file = new File("${temp.path}/nonExistentFile4"); |
274 | 274 |
275 // Non-existing file should throw exception. | 275 // Non-existing file should throw exception. |
276 Expect.throws(() => file.readAsTextSync(), | 276 Expect.throws(() => file.readAsTextSync(), |
277 (e) => checkOpenNonExistentFileException(e)); | 277 (e) => checkOpenNonExistentFileException(e)); |
278 | 278 |
279 var readAsTextFuture = file.readAsText(Encoding.ASCII); | 279 var readAsTextFuture = file.readAsText(Encoding.ASCII); |
280 readAsTextFuture.then((data) => Expect.fail("Unreachable code")); | 280 readAsTextFuture.then((data) => Expect.fail("Unreachable code")); |
281 readAsTextFuture.handleException((e) { | 281 readAsTextFuture.handleException((e) { |
282 checkOpenNonExistentFileException(e); | 282 checkOpenNonExistentFileException(e); |
283 p.toSendPort().send(null); | 283 p.toSendPort().send(null); |
284 return true; | 284 return true; |
285 }); | 285 }); |
286 } | 286 } |
287 | 287 |
288 testReadAsLinesNonExistent() { | 288 testReadAsLinesNonExistent() { |
289 Directory temp = tempDir(); | 289 Directory temp = tempDir(); |
290 ReceivePort p = new ReceivePort(); | 290 ReceivePort p = new ReceivePort(); |
291 p.receive((x, y) { | 291 p.receive((x, y) { |
292 p.close(); | 292 p.close(); |
293 temp.deleteRecursivelySync(); | 293 temp.deleteSync(recursive: true); |
294 }); | 294 }); |
295 var file = new File("${temp.path}/nonExistentFile5"); | 295 var file = new File("${temp.path}/nonExistentFile5"); |
296 | 296 |
297 // Non-existing file should throw exception. | 297 // Non-existing file should throw exception. |
298 Expect.throws(() => file.readAsLinesSync(), | 298 Expect.throws(() => file.readAsLinesSync(), |
299 (e) => checkOpenNonExistentFileException(e)); | 299 (e) => checkOpenNonExistentFileException(e)); |
300 | 300 |
301 var readAsLinesFuture = file.readAsLines(Encoding.ASCII); | 301 var readAsLinesFuture = file.readAsLines(Encoding.ASCII); |
302 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")); | 302 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")); |
303 readAsLinesFuture.handleException((e) { | 303 readAsLinesFuture.handleException((e) { |
(...skipping 12 matching lines...) Expand all Loading... |
316 | 316 |
317 | 317 |
318 // Create a test file in a temporary directory. Setup a port to signal | 318 // Create a test file in a temporary directory. Setup a port to signal |
319 // when the temporary directory should be deleted. Pass the file and | 319 // when the temporary directory should be deleted. Pass the file and |
320 // the port to the callback argument. | 320 // the port to the callback argument. |
321 createTestFile(callback) { | 321 createTestFile(callback) { |
322 Directory temp = tempDir(); | 322 Directory temp = tempDir(); |
323 ReceivePort p = new ReceivePort(); | 323 ReceivePort p = new ReceivePort(); |
324 p.receive((x, y) { | 324 p.receive((x, y) { |
325 p.close(); | 325 p.close(); |
326 temp.deleteRecursivelySync(); | 326 temp.deleteSync(recursive: true); |
327 }); | 327 }); |
328 | 328 |
329 var file = new File("${temp.path}/test_file"); | 329 var file = new File("${temp.path}/test_file"); |
330 file.createSync(); | 330 file.createSync(); |
331 callback(file, p.toSendPort()); | 331 callback(file, p.toSendPort()); |
332 } | 332 } |
333 | 333 |
334 | 334 |
335 testWriteByteToReadOnlyFile() { | 335 testWriteByteToReadOnlyFile() { |
336 createTestFile((file, port) { | 336 createTestFile((file, port) { |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 testReadAsBytesNonExistent(); | 510 testReadAsBytesNonExistent(); |
511 testReadAsTextNonExistent(); | 511 testReadAsTextNonExistent(); |
512 testReadAsLinesNonExistent(); | 512 testReadAsLinesNonExistent(); |
513 testWriteByteToReadOnlyFile(); | 513 testWriteByteToReadOnlyFile(); |
514 testWriteListToReadOnlyFile(); | 514 testWriteListToReadOnlyFile(); |
515 testTruncateReadOnlyFile(); | 515 testTruncateReadOnlyFile(); |
516 testOperateOnClosedFile(); | 516 testOperateOnClosedFile(); |
517 testRepeatedlyCloseFile(); | 517 testRepeatedlyCloseFile(); |
518 testRepeatedlyCloseFileSync(); | 518 testRepeatedlyCloseFileSync(); |
519 } | 519 } |
OLD | NEW |