Index: tests/standalone/io/file_write_only_test.dart |
diff --git a/tests/standalone/io/file_write_only_test.dart b/tests/standalone/io/file_write_only_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c06be4741ef80a9a58aa2ec1b7ce16f26b92d8f5 |
--- /dev/null |
+++ b/tests/standalone/io/file_write_only_test.dart |
@@ -0,0 +1,84 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+// |
+// Dart test program for testing file I/O. |
+ |
+import 'dart:async'; |
+import 'dart:io'; |
+ |
+import "package:async_helper/async_helper.dart"; |
+import "package:expect/expect.dart"; |
+ |
+Future withTempDir(String prefix, void test(Directory dir)) async { |
+ var tempDir = Directory.systemTemp.createTempSync(prefix); |
+ try { |
+ await test(tempDir); |
+ } finally { |
+ tempDir.deleteSync(recursive: true); |
+ } |
+} |
+ |
+void withTempDirSync(String prefix, void test(Directory dir)) async { |
+ var tempDir = Directory.systemTemp.createTempSync(prefix); |
+ try { |
+ test(tempDir); |
+ } finally { |
+ tempDir.deleteSync(recursive: true); |
+ } |
+} |
+ |
+Futire expectThrowsAsync(Future future, String message) { |
+ return future.then((r) => Expect.fail(message)) |
+ .catchError((e) {}); |
+} |
+ |
+ |
+Future write(Directory dir) async { |
+ var f = new File('x'); |
+ var raf = await f.open(mode: WRITE_ONLY); |
+ await raf.writeString('Hello'); |
+ await raf.setPosition(0); |
+ await raf.writeString('Hello'); |
+ await raf.setPosition(0); |
+ await expectThrowsAsync( |
+ raf.readByte(), 'Read from write only file succeeded'); |
+ await raf.close(); |
+ raf = await f.open(mode: WRITE_ONLY_APPEND); |
+ await raf.writeString('Hello'); |
+ await expectThrowsAsync( |
+ raf.readByte(), 'Read from write only file succeeded'); |
+ await raf.setPosition(0); |
+ await raf.writeString('Hello'); |
+ await raf.close(); |
+ Expect.equals(f.lengthSync(), 10); |
+} |
+ |
+Future writeSync(Directory dir) { |
+ var f = new File('x'); |
+ var raf = f.openSync(mode: WRITE_ONLY); |
+ raf.writeStringSync('Hello'); |
+ raf.setPositionSync(0); |
+ raf.writeStringSync('Hello'); |
+ raf.setPositionSync(0); |
+ Expect.throws(() => raf.readByteSync()); |
+} |
+ |
+Future openWrite(Directory dir) async { |
+ var f = new File('x'); |
+ var sink = f.openWrite(mode: WRITE_ONLY); |
+ sink.write('Hello'); |
+ await sink.close(); |
+ sink = await f.openWrite(mode: WRITE_ONLY_APPEND); |
+ sink.write('Hello'); |
+ await sink.close(); |
+ Expect.equals(f.lengthSync(), 10); |
+} |
+ |
+main() async { |
+ asyncStart(); |
+ await withTempDir('file_write_only_test', write); |
+ withTempDirSync('file_write_only_test', writeSync); |
+ await withTempDir('file_write_only_test', openWrite); |
+ asyncEnd(); |
+} |