Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(636)

Side by Side Diff: tests/standalone/io/file_error_test.dart

Issue 14251006: Remove AsyncError with Expando. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; 7 import "package:expect/expect.dart";
8 import "dart:io"; 8 import "dart:io";
9 import "dart:isolate"; 9 import "dart:isolate";
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 temp.deleteSync(recursive: true); 46 temp.deleteSync(recursive: true);
47 }); 47 });
48 var file = new File("${temp.path}/nonExistentFile"); 48 var file = new File("${temp.path}/nonExistentFile");
49 49
50 // Non-existing file should throw exception. 50 // Non-existing file should throw exception.
51 Expect.throws(() => file.openSync(), 51 Expect.throws(() => file.openSync(),
52 (e) => checkOpenNonExistentFileException(e)); 52 (e) => checkOpenNonExistentFileException(e));
53 53
54 var openFuture = file.open(mode: FileMode.READ); 54 var openFuture = file.open(mode: FileMode.READ);
55 openFuture.then((raf) => Expect.fail("Unreachable code")) 55 openFuture.then((raf) => Expect.fail("Unreachable code"))
56 .catchError((e) { 56 .catchError((error) {
57 checkOpenNonExistentFileException(e.error); 57 checkOpenNonExistentFileException(error);
58 p.toSendPort().send(null); 58 p.toSendPort().send(null);
59 }); 59 });
60 } 60 }
61 61
62 62
63 void testDeleteNonExistent() { 63 void testDeleteNonExistent() {
64 Directory temp = tempDir(); 64 Directory temp = tempDir();
65 ReceivePort p = new ReceivePort(); 65 ReceivePort p = new ReceivePort();
66 p.receive((x, y) { 66 p.receive((x, y) {
67 p.close(); 67 p.close();
68 temp.deleteSync(recursive: true); 68 temp.deleteSync(recursive: true);
69 }); 69 });
70 var file = new File("${temp.path}/nonExistentFile"); 70 var file = new File("${temp.path}/nonExistentFile");
71 71
72 // Non-existing file should throw exception. 72 // Non-existing file should throw exception.
73 Expect.throws(() => file.deleteSync(), 73 Expect.throws(() => file.deleteSync(),
74 (e) => checkDeleteNonExistentFileException(e)); 74 (e) => checkDeleteNonExistentFileException(e));
75 75
76 var delete = file.delete(); 76 var delete = file.delete();
77 delete.then((ignore) => Expect.fail("Unreachable code")) 77 delete.then((ignore) => Expect.fail("Unreachable code"))
78 .catchError((e) { 78 .catchError((error) {
79 checkDeleteNonExistentFileException(e.error); 79 checkDeleteNonExistentFileException(error);
80 p.toSendPort().send(null); 80 p.toSendPort().send(null);
81 }); 81 });
82 } 82 }
83 83
84 84
85 void testLengthNonExistent() { 85 void testLengthNonExistent() {
86 Directory temp = tempDir(); 86 Directory temp = tempDir();
87 ReceivePort p = new ReceivePort(); 87 ReceivePort p = new ReceivePort();
88 p.receive((x, y) { 88 p.receive((x, y) {
89 p.close(); 89 p.close();
90 temp.deleteSync(recursive: true); 90 temp.deleteSync(recursive: true);
91 }); 91 });
92 var file = new File("${temp.path}/nonExistentFile"); 92 var file = new File("${temp.path}/nonExistentFile");
93 93
94 // Non-existing file should throw exception. 94 // Non-existing file should throw exception.
95 Expect.throws(() => file.lengthSync(), 95 Expect.throws(() => file.lengthSync(),
96 (e) => checkLengthNonExistentFileException(e)); 96 (e) => checkLengthNonExistentFileException(e));
97 97
98 var lenFuture = file.length(); 98 var lenFuture = file.length();
99 lenFuture.then((len) => Expect.fail("Unreachable code")) 99 lenFuture.then((len) => Expect.fail("Unreachable code"))
100 .catchError((e) { 100 .catchError((error) {
101 checkLengthNonExistentFileException(e.error); 101 checkLengthNonExistentFileException(error);
102 p.toSendPort().send(null); 102 p.toSendPort().send(null);
103 }); 103 });
104 } 104 }
105 105
106 106
107 bool checkCreateInNonExistentDirectoryException(e) { 107 bool checkCreateInNonExistentDirectoryException(e) {
108 Expect.isTrue(e is FileIOException); 108 Expect.isTrue(e is FileIOException);
109 Expect.isTrue(e.osError != null); 109 Expect.isTrue(e.osError != null);
110 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1); 110 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1);
111 if (Platform.operatingSystem == "linux") { 111 if (Platform.operatingSystem == "linux") {
(...skipping 15 matching lines...) Expand all
127 temp.deleteSync(recursive: true); 127 temp.deleteSync(recursive: true);
128 }); 128 });
129 var file = new File("${temp.path}/nonExistentDirectory/newFile"); 129 var file = new File("${temp.path}/nonExistentDirectory/newFile");
130 130
131 // Create in non-existent directory should throw exception. 131 // Create in non-existent directory should throw exception.
132 Expect.throws(() => file.createSync(), 132 Expect.throws(() => file.createSync(),
133 (e) => checkCreateInNonExistentDirectoryException(e)); 133 (e) => checkCreateInNonExistentDirectoryException(e));
134 134
135 var create = file.create(); 135 var create = file.create();
136 create.then((ignore) => Expect.fail("Unreachable code")) 136 create.then((ignore) => Expect.fail("Unreachable code"))
137 .catchError((e) { 137 .catchError((error) {
138 checkCreateInNonExistentDirectoryException(e.error); 138 checkCreateInNonExistentDirectoryException(error);
139 p.toSendPort().send(null); 139 p.toSendPort().send(null);
140 }); 140 });
141 } 141 }
142 142
143 bool checkFullPathOnNonExistentDirectoryException(e) { 143 bool checkFullPathOnNonExistentDirectoryException(e) {
144 Expect.isTrue(e is FileIOException); 144 Expect.isTrue(e is FileIOException);
145 Expect.isTrue(e.osError != null); 145 Expect.isTrue(e.osError != null);
146 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1); 146 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1);
147 // File not not found has error code 2 on all supported platforms. 147 // File not not found has error code 2 on all supported platforms.
148 Expect.equals(2, e.osError.errorCode); 148 Expect.equals(2, e.osError.errorCode);
149 149
150 return true; 150 return true;
151 } 151 }
152 152
153 void testFullPathOnNonExistentDirectory() { 153 void testFullPathOnNonExistentDirectory() {
154 Directory temp = tempDir(); 154 Directory temp = tempDir();
155 ReceivePort p = new ReceivePort(); 155 ReceivePort p = new ReceivePort();
156 p.receive((x, y) { 156 p.receive((x, y) {
157 p.close(); 157 p.close();
158 temp.deleteSync(recursive: true); 158 temp.deleteSync(recursive: true);
159 }); 159 });
160 var file = new File("${temp.path}/nonExistentDirectory"); 160 var file = new File("${temp.path}/nonExistentDirectory");
161 161
162 // Full path non-existent directory should throw exception. 162 // Full path non-existent directory should throw exception.
163 Expect.throws(() => file.fullPathSync(), 163 Expect.throws(() => file.fullPathSync(),
164 (e) => checkFullPathOnNonExistentDirectoryException(e)); 164 (e) => checkFullPathOnNonExistentDirectoryException(e));
165 165
166 var fullPathFuture = file.fullPath(); 166 var fullPathFuture = file.fullPath();
167 fullPathFuture.then((path) => Expect.fail("Unreachable code $path")) 167 fullPathFuture.then((path) => Expect.fail("Unreachable code $path"))
168 .catchError((e) { 168 .catchError((error) {
169 checkFullPathOnNonExistentDirectoryException(e.error); 169 checkFullPathOnNonExistentDirectoryException(error);
170 p.toSendPort().send(null); 170 p.toSendPort().send(null);
171 }); 171 });
172 } 172 }
173 173
174 bool checkDirectoryInNonExistentDirectoryException(e) { 174 bool checkDirectoryInNonExistentDirectoryException(e) {
175 Expect.isTrue(e is FileIOException); 175 Expect.isTrue(e is FileIOException);
176 Expect.isTrue(e.osError != null); 176 Expect.isTrue(e.osError != null);
177 Expect.isTrue( 177 Expect.isTrue(
178 e.toString().indexOf("Cannot retrieve directory for file") != -1); 178 e.toString().indexOf("Cannot retrieve directory for file") != -1);
179 // File not not found has error code 2 on all supported platforms. 179 // File not not found has error code 2 on all supported platforms.
(...skipping 10 matching lines...) Expand all
190 temp.deleteSync(recursive: true); 190 temp.deleteSync(recursive: true);
191 }); 191 });
192 var file = new File("${temp.path}/nonExistentDirectory/newFile"); 192 var file = new File("${temp.path}/nonExistentDirectory/newFile");
193 193
194 // Create in non-existent directory should throw exception. 194 // Create in non-existent directory should throw exception.
195 Expect.throws(() => file.directorySync(), 195 Expect.throws(() => file.directorySync(),
196 (e) => checkDirectoryInNonExistentDirectoryException(e)); 196 (e) => checkDirectoryInNonExistentDirectoryException(e));
197 197
198 var dirFuture = file.directory(); 198 var dirFuture = file.directory();
199 dirFuture.then((directory) => Expect.fail("Unreachable code")) 199 dirFuture.then((directory) => Expect.fail("Unreachable code"))
200 .catchError((e) { 200 .catchError((error) {
201 checkDirectoryInNonExistentDirectoryException(e.error); 201 checkDirectoryInNonExistentDirectoryException(error);
202 p.toSendPort().send(null); 202 p.toSendPort().send(null);
203 }); 203 });
204 } 204 }
205 205
206 void testReadAsBytesNonExistent() { 206 void testReadAsBytesNonExistent() {
207 Directory temp = tempDir(); 207 Directory temp = tempDir();
208 ReceivePort p = new ReceivePort(); 208 ReceivePort p = new ReceivePort();
209 p.receive((x, y) { 209 p.receive((x, y) {
210 p.close(); 210 p.close();
211 temp.deleteSync(recursive: true); 211 temp.deleteSync(recursive: true);
212 }); 212 });
213 var file = new File("${temp.path}/nonExistentFile3"); 213 var file = new File("${temp.path}/nonExistentFile3");
214 214
215 // Non-existing file should throw exception. 215 // Non-existing file should throw exception.
216 Expect.throws(() => file.readAsBytesSync(), 216 Expect.throws(() => file.readAsBytesSync(),
217 (e) => checkOpenNonExistentFileException(e)); 217 (e) => checkOpenNonExistentFileException(e));
218 218
219 var readAsBytesFuture = file.readAsBytes(); 219 var readAsBytesFuture = file.readAsBytes();
220 readAsBytesFuture.then((data) => Expect.fail("Unreachable code")) 220 readAsBytesFuture.then((data) => Expect.fail("Unreachable code"))
221 .catchError((e) { 221 .catchError((error) {
222 checkOpenNonExistentFileException(e.error); 222 checkOpenNonExistentFileException(error);
223 p.toSendPort().send(null); 223 p.toSendPort().send(null);
224 }); 224 });
225 } 225 }
226 226
227 void testReadAsTextNonExistent() { 227 void testReadAsTextNonExistent() {
228 Directory temp = tempDir(); 228 Directory temp = tempDir();
229 ReceivePort p = new ReceivePort(); 229 ReceivePort p = new ReceivePort();
230 p.receive((x, y) { 230 p.receive((x, y) {
231 p.close(); 231 p.close();
232 temp.deleteSync(recursive: true); 232 temp.deleteSync(recursive: true);
233 }); 233 });
234 var file = new File("${temp.path}/nonExistentFile4"); 234 var file = new File("${temp.path}/nonExistentFile4");
235 235
236 // Non-existing file should throw exception. 236 // Non-existing file should throw exception.
237 Expect.throws(() => file.readAsStringSync(), 237 Expect.throws(() => file.readAsStringSync(),
238 (e) => checkOpenNonExistentFileException(e)); 238 (e) => checkOpenNonExistentFileException(e));
239 239
240 var readAsStringFuture = file.readAsString(encoding: Encoding.ASCII); 240 var readAsStringFuture = file.readAsString(encoding: Encoding.ASCII);
241 readAsStringFuture.then((data) => Expect.fail("Unreachable code")) 241 readAsStringFuture.then((data) => Expect.fail("Unreachable code"))
242 .catchError((e) { 242 .catchError((error) {
243 checkOpenNonExistentFileException(e.error); 243 checkOpenNonExistentFileException(error);
244 p.toSendPort().send(null); 244 p.toSendPort().send(null);
245 }); 245 });
246 } 246 }
247 247
248 testReadAsLinesNonExistent() { 248 testReadAsLinesNonExistent() {
249 Directory temp = tempDir(); 249 Directory temp = tempDir();
250 ReceivePort p = new ReceivePort(); 250 ReceivePort p = new ReceivePort();
251 p.receive((x, y) { 251 p.receive((x, y) {
252 p.close(); 252 p.close();
253 temp.deleteSync(recursive: true); 253 temp.deleteSync(recursive: true);
254 }); 254 });
255 var file = new File("${temp.path}/nonExistentFile5"); 255 var file = new File("${temp.path}/nonExistentFile5");
256 256
257 // Non-existing file should throw exception. 257 // Non-existing file should throw exception.
258 Expect.throws(() => file.readAsLinesSync(), 258 Expect.throws(() => file.readAsLinesSync(),
259 (e) => checkOpenNonExistentFileException(e)); 259 (e) => checkOpenNonExistentFileException(e));
260 260
261 var readAsLinesFuture = file.readAsLines(encoding: Encoding.ASCII); 261 var readAsLinesFuture = file.readAsLines(encoding: Encoding.ASCII);
262 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")) 262 readAsLinesFuture.then((data) => Expect.fail("Unreachable code"))
263 .catchError((e) { 263 .catchError((error) {
264 checkOpenNonExistentFileException(e.error); 264 checkOpenNonExistentFileException(error);
265 p.toSendPort().send(null); 265 p.toSendPort().send(null);
266 }); 266 });
267 } 267 }
268 268
269 bool checkWriteReadOnlyFileException(e) { 269 bool checkWriteReadOnlyFileException(e) {
270 Expect.isTrue(e is FileIOException); 270 Expect.isTrue(e is FileIOException);
271 Expect.isTrue(e.osError != null); 271 Expect.isTrue(e.osError != null);
272 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode); 272 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode);
273 return true; 273 return true;
274 } 274 }
(...skipping 18 matching lines...) Expand all
293 293
294 testWriteByteToReadOnlyFile() { 294 testWriteByteToReadOnlyFile() {
295 createTestFile((file, port) { 295 createTestFile((file, port) {
296 var openedFile = file.openSync(mode: FileMode.READ); 296 var openedFile = file.openSync(mode: FileMode.READ);
297 297
298 // Writing to read only file should throw an exception. 298 // Writing to read only file should throw an exception.
299 Expect.throws(() => openedFile.writeByteSync(0), 299 Expect.throws(() => openedFile.writeByteSync(0),
300 (e) => checkWriteReadOnlyFileException(e)); 300 (e) => checkWriteReadOnlyFileException(e));
301 301
302 var writeByteFuture = openedFile.writeByte(0); 302 var writeByteFuture = openedFile.writeByte(0);
303 writeByteFuture.catchError((e) { 303 writeByteFuture.catchError((error) {
304 checkWriteReadOnlyFileException(e.error); 304 checkWriteReadOnlyFileException(error);
305 openedFile.close().then((ignore) => port.send(null)); 305 openedFile.close().then((ignore) => port.send(null));
306 }); 306 });
307 }); 307 });
308 } 308 }
309 309
310 testWriteFromToReadOnlyFile() { 310 testWriteFromToReadOnlyFile() {
311 createTestFile((file, port) { 311 createTestFile((file, port) {
312 var openedFile = file.openSync(mode: FileMode.READ); 312 var openedFile = file.openSync(mode: FileMode.READ);
313 313
314 List data = [0, 1, 2, 3]; 314 List data = [0, 1, 2, 3];
315 // Writing to read only file should throw an exception. 315 // Writing to read only file should throw an exception.
316 Expect.throws(() => openedFile.writeFromSync(data, 0, data.length), 316 Expect.throws(() => openedFile.writeFromSync(data, 0, data.length),
317 (e) => checkWriteReadOnlyFileException(e)); 317 (e) => checkWriteReadOnlyFileException(e));
318 318
319 var writeFromFuture = openedFile.writeFrom(data, 0, data.length); 319 var writeFromFuture = openedFile.writeFrom(data, 0, data.length);
320 writeFromFuture.catchError((e) { 320 writeFromFuture.catchError((error) {
321 checkWriteReadOnlyFileException(e.error); 321 checkWriteReadOnlyFileException(error);
322 openedFile.close().then((ignore) => port.send(null)); 322 openedFile.close().then((ignore) => port.send(null));
323 }); 323 });
324 }); 324 });
325 } 325 }
326 326
327 testTruncateReadOnlyFile() { 327 testTruncateReadOnlyFile() {
328 createTestFile((file, port) { 328 createTestFile((file, port) {
329 var openedFile = file.openSync(mode: FileMode.WRITE); 329 var openedFile = file.openSync(mode: FileMode.WRITE);
330 openedFile.writeByteSync(0); 330 openedFile.writeByteSync(0);
331 openedFile.closeSync(); 331 openedFile.closeSync();
332 openedFile = file.openSync(mode: FileMode.READ); 332 openedFile = file.openSync(mode: FileMode.READ);
333 333
334 // Truncating read only file should throw an exception. 334 // Truncating read only file should throw an exception.
335 Expect.throws(() => openedFile.truncateSync(0), 335 Expect.throws(() => openedFile.truncateSync(0),
336 (e) => checkWriteReadOnlyFileException(e)); 336 (e) => checkWriteReadOnlyFileException(e));
337 337
338 var truncateFuture = openedFile.truncate(0); 338 var truncateFuture = openedFile.truncate(0);
339 truncateFuture.then((ignore) => Expect.fail("Unreachable code")) 339 truncateFuture.then((ignore) => Expect.fail("Unreachable code"))
340 .catchError((e) { 340 .catchError((error) {
341 checkWriteReadOnlyFileException(e.error); 341 checkWriteReadOnlyFileException(error);
342 openedFile.close().then((ignore) => port.send(null)); 342 openedFile.close().then((ignore) => port.send(null));
343 }); 343 });
344 }); 344 });
345 } 345 }
346 346
347 bool checkFileClosedException(e) { 347 bool checkFileClosedException(e) {
348 Expect.isTrue(e is FileIOException); 348 Expect.isTrue(e is FileIOException);
349 Expect.isTrue(e.toString().indexOf("File closed") != -1); 349 Expect.isTrue(e.toString().indexOf("File closed") != -1);
350 Expect.isTrue(e.osError == null); 350 Expect.isTrue(e.osError == null);
351 return true; 351 return true;
(...skipping 21 matching lines...) Expand all
373 (e) => checkFileClosedException(e)); 373 (e) => checkFileClosedException(e));
374 Expect.throws(() => openedFile.truncateSync(0), 374 Expect.throws(() => openedFile.truncateSync(0),
375 (e) => checkFileClosedException(e)); 375 (e) => checkFileClosedException(e));
376 Expect.throws(() => openedFile.lengthSync(), 376 Expect.throws(() => openedFile.lengthSync(),
377 (e) => checkFileClosedException(e)); 377 (e) => checkFileClosedException(e));
378 Expect.throws(() => openedFile.flushSync(), 378 Expect.throws(() => openedFile.flushSync(),
379 (e) => checkFileClosedException(e)); 379 (e) => checkFileClosedException(e));
380 380
381 var errorCount = 0; 381 var errorCount = 0;
382 382
383 _errorHandler(e) { 383 _errorHandler(error) {
384 checkFileClosedException(e.error); 384 checkFileClosedException(error);
385 if (--errorCount == 0) { 385 if (--errorCount == 0) {
386 port.send(null); 386 port.send(null);
387 } 387 }
388 } 388 }
389 389
390 var readByteFuture = openedFile.readByte(); 390 var readByteFuture = openedFile.readByte();
391 readByteFuture.then((byte) => Expect.fail("Unreachable code")) 391 readByteFuture.then((byte) => Expect.fail("Unreachable code"))
392 .catchError(_errorHandler); 392 .catchError(_errorHandler);
393 errorCount++; 393 errorCount++;
394 var writeByteFuture = openedFile.writeByte(0); 394 var writeByteFuture = openedFile.writeByte(0);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 errorCount++; 429 errorCount++;
430 }); 430 });
431 } 431 }
432 432
433 testRepeatedlyCloseFile() { 433 testRepeatedlyCloseFile() {
434 createTestFile((file, port) { 434 createTestFile((file, port) {
435 var openedFile = file.openSync(); 435 var openedFile = file.openSync();
436 openedFile.close().then((ignore) { 436 openedFile.close().then((ignore) {
437 var closeFuture = openedFile.close(); 437 var closeFuture = openedFile.close();
438 closeFuture.then((ignore) => null) 438 closeFuture.then((ignore) => null)
439 .catchError((e) { 439 .catchError((error) {
440 Expect.isTrue(e.error is FileIOException); 440 Expect.isTrue(error is FileIOException);
441 port.send(null); 441 port.send(null);
442 }); 442 });
443 }); 443 });
444 }); 444 });
445 } 445 }
446 446
447 testRepeatedlyCloseFileSync() { 447 testRepeatedlyCloseFileSync() {
448 createTestFile((file, port) { 448 createTestFile((file, port) {
449 var openedFile = file.openSync(); 449 var openedFile = file.openSync();
450 openedFile.closeSync(); 450 openedFile.closeSync();
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 testReadAsLinesNonExistent(); 487 testReadAsLinesNonExistent();
488 testWriteByteToReadOnlyFile(); 488 testWriteByteToReadOnlyFile();
489 testWriteFromToReadOnlyFile(); 489 testWriteFromToReadOnlyFile();
490 testTruncateReadOnlyFile(); 490 testTruncateReadOnlyFile();
491 testOperateOnClosedFile(); 491 testOperateOnClosedFile();
492 testRepeatedlyCloseFile(); 492 testRepeatedlyCloseFile();
493 testRepeatedlyCloseFileSync(); 493 testRepeatedlyCloseFileSync();
494 testReadSyncBigInt(); 494 testReadSyncBigInt();
495 testReadSyncClosedFile(); 495 testReadSyncClosedFile();
496 } 496 }
OLDNEW
« no previous file with comments | « tests/standalone/io/echo_server_stream_test.dart ('k') | tests/standalone/io/file_invalid_arguments_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698