Files
sdk/tests/standalone/io/file_write_only_test.dart
T
Jonas Termansen 3433d8cfc3 [nnbd] Migrate standalone/io to NNBD.
The tests are also now dartfmt.

Bug: https://github.com/dart-lang/sdk/issues/40040
Change-Id: I8dece8097b37b70d47a5374dae2f3fadb0fc4b90
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134338
Commit-Queue: Jonas Termansen <sortie@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
2020-02-07 14:41:36 +00:00

84 lines
2.5 KiB
Dart

// 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, Future<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)) {
var tempDir = Directory.systemTemp.createTempSync(prefix);
try {
test(tempDir);
} finally {
tempDir.deleteSync(recursive: true);
}
}
Future expectThrowsAsync(Future future, String message) {
return future.then((r) => Expect.fail(message)).catchError((e) {});
}
Future write(Directory dir) async {
var f = new File("${dir.path}${Platform.pathSeparator}write");
var raf = await f.open(mode: FileMode.writeOnly);
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: FileMode.writeOnlyAppend);
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);
}
void writeSync(Directory dir) {
var f = new File("${dir.path}${Platform.pathSeparator}write_sync");
var raf = f.openSync(mode: FileMode.writeOnly);
raf.writeStringSync('Hello');
raf.setPositionSync(0);
raf.writeStringSync('Hello');
raf.setPositionSync(0);
Expect.throws(() => raf.readByteSync());
raf.closeSync();
}
Future openWrite(Directory dir) async {
var f = new File("${dir.path}${Platform.pathSeparator}open_write");
var sink = f.openWrite(mode: FileMode.writeOnly);
sink.write('Hello');
await sink.close();
sink = await f.openWrite(mode: FileMode.writeOnlyAppend);
sink.write('Hello');
await sink.close();
Expect.equals(f.lengthSync(), 10);
}
main() async {
asyncStart();
await withTempDir('file_write_only_test_1_', write);
withTempDirSync('file_write_only_test_2_', writeSync);
await withTempDir('file_write_only_test_3_', openWrite);
asyncEnd();
}