| Index: tests/standalone/src/io/FileErrorTest.dart
|
| diff --git a/tests/standalone/src/io/FileErrorTest.dart b/tests/standalone/src/io/FileErrorTest.dart
|
| index d888458316074a82409ff2cd00727689ecc8b4ce..5ac2c3a914c0619f8a91894c5e8667eefb82a3eb 100644
|
| --- a/tests/standalone/src/io/FileErrorTest.dart
|
| +++ b/tests/standalone/src/io/FileErrorTest.dart
|
| @@ -7,10 +7,10 @@
|
| #import("dart:io");
|
| #import("dart:isolate");
|
|
|
| -String tempDir() {
|
| +Directory tempDir() {
|
| var d = new Directory('');
|
| d.createTempSync();
|
| - return d.path;
|
| + return d;
|
| }
|
|
|
|
|
| @@ -35,7 +35,7 @@ bool checkOpenNonExistentFileException(e) {
|
| }
|
|
|
| void testOpenNonExistent() {
|
| - var file = new File("${tempDir()}/nonExistentFile");
|
| + var file = new File("${tempDir().path}/nonExistentFile");
|
|
|
| // Non-existing file should throw exception.
|
| Expect.throws(() => file.openSync(),
|
| @@ -66,7 +66,7 @@ bool checkDeleteNonExistentFileException(e) {
|
| }
|
|
|
| void testDeleteNonExistent() {
|
| - var file = new File("${tempDir()}/nonExistentFile");
|
| + var file = new File("${tempDir().path}/nonExistentFile");
|
|
|
| // Non-existing file should throw exception.
|
| Expect.throws(() => file.deleteSync(),
|
| @@ -98,7 +98,7 @@ bool checkCreateInNonExistentDirectoryException(e) {
|
| }
|
|
|
| void testCreateInNonExistentDirectory() {
|
| - var file = new File("${tempDir()}/nonExistentDirectory/newFile");
|
| + var file = new File("${tempDir().path}/nonExistentDirectory/newFile");
|
|
|
| // Create in non-existent directory should throw exception.
|
| Expect.throws(() => file.createSync(),
|
| @@ -129,7 +129,7 @@ bool checkFullPathOnNonExistentDirectoryException(e) {
|
| }
|
|
|
| void testFullPathOnNonExistentDirectory() {
|
| - var file = new File("${tempDir()}/nonExistentDirectory");
|
| + var file = new File("${tempDir().path}/nonExistentDirectory");
|
|
|
| // Full path non-existent directory should throw exception.
|
| Expect.throws(() => file.fullPathSync(),
|
| @@ -161,7 +161,7 @@ bool checkDirectoryInNonExistentDirectoryException(e) {
|
| }
|
|
|
| void testDirectoryInNonExistentDirectory() {
|
| - var file = new File("${tempDir()}/nonExistentDirectory/newFile");
|
| + var file = new File("${tempDir().path}/nonExistentDirectory/newFile");
|
|
|
| // Create in non-existent directory should throw exception.
|
| Expect.throws(() => file.directorySync(),
|
| @@ -172,33 +172,186 @@ void testDirectoryInNonExistentDirectory() {
|
| }
|
|
|
| void testReadAsBytesNonExistent() {
|
| - var file = new File("${tempDir()}/nonExistentFile3");
|
| + var file = new File("${tempDir().path}/nonExistentFile3");
|
|
|
| // Non-existing file should throw exception.
|
| Expect.throws(() => file.readAsBytesSync(),
|
| (e) => checkOpenNonExistentFileException(e));
|
|
|
| - // TODO(sgjesse): Handle error for file.readAsBytes as well.
|
| + file.readAsBytes((data) => Expect.fail("Unreachable code"));
|
| + file.onError = (e) => checkOpenNonExistentFileException(e);
|
| }
|
|
|
| void testReadAsTextNonExistent() {
|
| - var file = new File("${tempDir()}/nonExistentFile4");
|
| + var file = new File("${tempDir().path}/nonExistentFile4");
|
|
|
| // Non-existing file should throw exception.
|
| Expect.throws(() => file.readAsTextSync(),
|
| (e) => checkOpenNonExistentFileException(e));
|
|
|
| - // TODO(sgjesse): Handle error for file.readAsText as well.
|
| + file.readAsText(Encoding.ASCII, (data) => Expect.fail("Unreachable code"));
|
| + file.onError = (e) => checkOpenNonExistentFileException(e);
|
| }
|
|
|
| void testReadAsLinesNonExistent() {
|
| - var file = new File("${tempDir()}/nonExistentFile5");
|
| + var file = new File("${tempDir().path}/nonExistentFile5");
|
|
|
| // Non-existing file should throw exception.
|
| Expect.throws(() => file.readAsLinesSync(),
|
| (e) => checkOpenNonExistentFileException(e));
|
|
|
| - // TODO(sgjesse): Handle error for file.readAsLines as well.
|
| + file.readAsLines(Encoding.ASCII, (data) => Expect.fail("Unreachable code"));
|
| + file.onError = (e) => checkOpenNonExistentFileException(e);
|
| +}
|
| +
|
| +bool checkWriteReadOnlyFileException(e) {
|
| + Expect.isTrue(e is FileIOException);
|
| + Expect.isTrue(e.osError != null);
|
| + Expect.isTrue(e.osError.errorCode != OSError.noErrorCode);
|
| + return true;
|
| +}
|
| +
|
| +testWriteByteToReadOnlyFile() {
|
| + Directory temp = tempDir();
|
| + ReceivePort p = new ReceivePort();
|
| + p.receive((x,y) {
|
| + p.close();
|
| + temp.deleteRecursivelySync();
|
| + });
|
| +
|
| + var file = new File("${temp.path}/test_file");
|
| + file.createSync();
|
| + var openedFile = file.openSync(FileMode.READ);
|
| +
|
| + // Writing to read only file should throw an exception.
|
| + Expect.throws(() => openedFile.writeByteSync(0),
|
| + (e) => checkWriteReadOnlyFileException(e));
|
| +
|
| + openedFile.writeByte(0);
|
| + openedFile.onError = (e) {
|
| + checkWriteReadOnlyFileException(e);
|
| + p.toSendPort().send(null);
|
| + };
|
| +}
|
| +
|
| +testWriteListToReadOnlyFile() {
|
| + Directory temp = tempDir();
|
| + ReceivePort p = new ReceivePort();
|
| + p.receive((x,y) {
|
| + p.close();
|
| + temp.deleteRecursivelySync();
|
| + });
|
| +
|
| + var file = new File("${temp.path}/test_file");
|
| + file.createSync();
|
| + var openedFile = file.openSync(FileMode.READ);
|
| +
|
| + List data = [0, 1, 2, 3];
|
| + // Writing to read only file should throw an exception.
|
| + Expect.throws(() => openedFile.writeListSync(data, 0, data.length),
|
| + (e) => checkWriteReadOnlyFileException(e));
|
| +
|
| + openedFile.writeList(data, 0, data.length);
|
| + openedFile.onError = (e) {
|
| + checkWriteReadOnlyFileException(e);
|
| + p.toSendPort().send(null);
|
| + };
|
| +}
|
| +
|
| +testTruncateReadOnlyFile() {
|
| + Directory temp = tempDir();
|
| + ReceivePort p = new ReceivePort();
|
| + p.receive((x,y) {
|
| + p.close();
|
| + temp.deleteRecursivelySync();
|
| + });
|
| +
|
| + var file = new File("${temp.path}/test_file");
|
| + file.createSync();
|
| + var openedFile = file.openSync(FileMode.READ);
|
| +
|
| + // Truncating read only file should throw an exception.
|
| + Expect.throws(() => openedFile.truncateSync(0),
|
| + (e) => checkWriteReadOnlyFileException(e));
|
| +
|
| + openedFile.truncate(0, () => Expect.fail("Unreachable code"));
|
| + openedFile.onError = (e) {
|
| + checkWriteReadOnlyFileException(e);
|
| + p.toSendPort().send(null);
|
| + };
|
| +}
|
| +
|
| +bool checkFileClosedException(e) {
|
| + Expect.isTrue(e is FileIOException);
|
| + Expect.isTrue(e.toString().indexOf("File closed") != -1);
|
| + Expect.isTrue(e.osError == null);
|
| + return true;
|
| +}
|
| +
|
| +testOperateOnClosedFile() {
|
| + Directory temp = tempDir();
|
| + ReceivePort p = new ReceivePort();
|
| + p.receive((x,y) {
|
| + p.close();
|
| + temp.deleteRecursivelySync();
|
| + });
|
| +
|
| + var file = new File("${tempDir().path}/test_file");
|
| + file.createSync();
|
| + var openedFile = file.openSync(FileMode.READ);
|
| + openedFile.closeSync();
|
| +
|
| + List data = [0, 1, 2, 3];
|
| + Expect.throws(() => openedFile.readByteSync(),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.writeByteSync(0),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.writeListSync(data, 0, data.length),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.readListSync(data, 0, data.length),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.writeStringSync("Hello"),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.positionSync(),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.setPositionSync(0),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.truncateSync(0),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.lengthSync(),
|
| + (e) => checkFileClosedException(e));
|
| + Expect.throws(() => openedFile.flushSync(),
|
| + (e) => checkFileClosedException(e));
|
| +
|
| + var errorCount = 0;
|
| + openedFile.readByte((byte) => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| + openedFile.writeByte(0);
|
| + errorCount++;
|
| + openedFile.readList(
|
| + data, 0, data.length, (bytesRead) => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| + openedFile.writeList(data, 0, data.length);
|
| + errorCount++;
|
| + openedFile.writeString("Hello");
|
| + errorCount++;
|
| + openedFile.position((position) => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| + openedFile.setPosition(0, () => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| + openedFile.truncate(0, () => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| + openedFile.length((length) => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| + openedFile.flush(() => Expect.fail("Unreachable code"));
|
| + errorCount++;
|
| +
|
| + openedFile.onError = (e) {
|
| + checkFileClosedException(e);
|
| + if (--errorCount == 0) {
|
| + p.toSendPort().send(null);
|
| + }
|
| + };
|
| }
|
|
|
| main() {
|
| @@ -210,4 +363,8 @@ main() {
|
| testReadAsBytesNonExistent();
|
| testReadAsTextNonExistent();
|
| testReadAsLinesNonExistent();
|
| + testWriteByteToReadOnlyFile();
|
| + testWriteListToReadOnlyFile();
|
| + testTruncateReadOnlyFile();
|
| + testOperateOnClosedFile();
|
| }
|
|
|