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

Unified Diff: tests/standalone/io/create_recursive_test.dart

Issue 52363002: dart:io | Add 'recursive' flag to File.create and Link.create. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: tests/standalone/io/create_recursive_test.dart
diff --git a/tests/standalone/io/create_recursive_test.dart b/tests/standalone/io/create_recursive_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f257f1c8c3bcdacf8da05d9c495a9d05fec3b142
--- /dev/null
+++ b/tests/standalone/io/create_recursive_test.dart
@@ -0,0 +1,142 @@
+// 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.
+
+import 'dart:io';
+
+import "package:async_helper/async_helper.dart";
+import "package:expect/expect.dart";
+import "package:path/path.dart";
+
+main() {
+ testCreateDirectoryRecursiveSync();
+ testCreateLinkRecursiveSync();
+ testCreateFileRecursiveSync();
+ testCreateDirectoryRecursive();
+ testCreateLinkRecursive();
+ testCreateFileRecursive();
+}
+
+testCreateDirectoryRecursiveSync() {
+ var temp = Directory.systemTemp.createTempSync('directory_test');
+ try {
+ var dir = new Directory(join(temp.path, 'a', 'b', 'c'));
+ Expect.throws(() => dir.createSync());
+ dir.createSync(recursive: true);
+ Expect.isTrue(dir.existsSync());
+ // Test cases where the directory or parent directory already exists.
+ dir.deleteSync();
+ dir.createSync(recursive: true);
+ Expect.isTrue(dir.existsSync());
+ dir.createSync(recursive: true);
+ Expect.isTrue(dir.existsSync());
+ } finally {
+ temp.deleteSync(recursive: true);
+ }
+}
+
+testCreateFileRecursiveSync() {
+ var temp = Directory.systemTemp.createTempSync('directory_test');
+ try {
+ var file = new File(join(temp.path, 'a', 'b', 'c'));
+ Expect.throws(() => file.createSync());
+ file.createSync(recursive: true);
+ Expect.isTrue(file.existsSync());
+ // Test cases where the file or parent directory already exists.
+ file.deleteSync();
+ file.createSync(recursive: true);
+ Expect.isTrue(file.existsSync());
+ file.createSync(recursive: true);
+ Expect.isTrue(file.existsSync());
+ } finally {
+ temp.deleteSync(recursive: true);
+ }
+}
+
+testCreateLinkRecursiveSync() {
+ var temp = Directory.systemTemp.createTempSync('directory_test');
+ try {
+ var link = new Link(join(temp.path, 'a', 'b', 'c'));
+ Expect.throws(() => link.createSync(temp.path));
+ link.createSync(temp.path, recursive: true);
+ Expect.isTrue(link.existsSync());
+ Expect.isTrue(new Directory(link.targetSync()).existsSync());
+ // Test cases where the link or parent directory already exists.
+ link.deleteSync();
+ link.createSync(temp.path, recursive: true);
+ Expect.isTrue(link.existsSync());
+ Expect.throws(() => link.createSync(temp.path, recursive: true));
+ Expect.isTrue(link.existsSync());
+ } finally {
+ temp.deleteSync(recursive: true);
+ }
+}
+
+Future expectFutureIsTrue(Future future) =>
+ future.then((value) => Expect.isTrue(value));
+
+Future expectFileSystemException(Function f, String message) {
+ return f().then(
+ (_) => Expect.fail('Expected a FileSystemException: $message'),
+ onError: (e) => Expect.isTrue(e is FileSystemException));
+}
+
+testCreateDirectoryRecursive() {
+ asyncStart();
+ Directory.systemTemp.createTemp('dart_directory').then((temp) {
+ var dir = new Directory(join(temp.path, 'a', 'b', 'c'));
+ return expectFileSystemException(() => dir.create(), 'dir.create')
+ .then((_) => dir.create(recursive: true))
+ .then((_) => expectFutureIsTrue(dir.exists()))
+ // Test cases where the directory or parent directory already exists.
+ .then((_) => dir.delete())
+ .then((_) => dir.create(recursive: true))
+ .then((_) => expectFutureIsTrue(dir.exists()))
+ .then((_) => dir.create(recursive: true))
+ .then((_) => expectFutureIsTrue(dir.exists()))
+
+ .then((_) => asyncEnd())
+ .whenComplete(() => temp.delete(recursive: true));
+ });
+}
+
+testCreateFileRecursive() {
+ asyncStart();
+ Directory.systemTemp.createTemp('dart_directory').then((temp) {
+ var file = new File(join(temp.path, 'a', 'b', 'c'));
+ return expectFileSystemException(() => file.create(), 'file.create')
+ .then((_) => file.create(recursive: true))
+ .then((_) => expectFutureIsTrue(file.exists()))
+ // Test cases where the file or parent directory already exists.
+ .then((_) => file.delete())
+ .then((_) => file.create(recursive: true))
+ .then((_) => expectFutureIsTrue(file.exists()))
+ .then((_) => file.create(recursive: true))
+ .then((_) => expectFutureIsTrue(file.exists()))
+
+ .then((_) => asyncEnd())
+ .whenComplete(() => temp.delete(recursive: true));
+ });
+}
+
+testCreateLinkRecursive() {
+ asyncStart();
+ Directory.systemTemp.createTemp('dart_directory').then((temp) {
+ var link = new Link(join(temp.path, 'a', 'b', 'c'));
+ return expectFileSystemException(() => link.create(temp.path),
+ 'link.create')
+ .then((_) => link.create(temp.path, recursive: true))
Anders Johnsen 2013/10/30 16:19:13 indentation
+ .then((_) => expectFutureIsTrue(link.exists()))
+ // Test cases where the link or parent directory already exists.
+ .then((_) => link.delete())
+ .then((_) => link.create(temp.path, recursive: true))
+ .then((_) => expectFutureIsTrue(link.exists()))
+ .then((_) => expectFileSystemException(
+ () => link.create(temp.path, recursive: true),
Anders Johnsen 2013/10/30 16:19:13 indentation
Bill Hesse 2013/10/30 16:51:40 This was intentional. An indentation over 4 spaces
+ 'existing link.create'))
+ .then((_) => expectFutureIsTrue(link.exists()))
+
+ .then((_) => asyncEnd())
Anders Johnsen 2013/10/30 16:19:13 asyncEnd should be part of whenComplete (likewise
+ .whenComplete(() => temp.delete(recursive: true));
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698