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

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

Issue 14642012: dart:io | Add asynchronous versions of the methods of FileSystemEntity and Link. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add new async test file. Created 7 years, 7 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
« sdk/lib/io/file_system_entity.dart ('K') | « sdk/lib/io/link.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "package:expect/expect.dart";
6 import "dart:io";
7 import "dart:isolate";
8
9
10 class FutureExpect {
11 static Future isTrue(Future<bool> result) =>
12 result.then((value) => Expect.isTrue(value));
13 static Future isFalse(Future<bool> result) =>
14 result.then((value) => Expect.isFalse(value));
15 static Future equals(expected, Future result) =>
16 result.then((value) => Expect.equals(expected, value));
17 static Future listEquals(expected, Future result) =>
18 result.then((value) => Expect.listEquals(expected, value));
19 static Future throws(Future result) =>
20 result.then((value) {
21 throw new ExpectException(
22 "FutureExpect.throws received $value instead of an exception");
23 }, onError: (_) => null);
24 }
25
26
27 Future testFileExistsCreate() =>
28 new Directory('').createTemp().then((temp) {
29 var x = '${temp.path}${Platform.pathSeparator}x';
30 var y = '${temp.path}${Platform.pathSeparator}y';
31 return new Link(y).create(x)
32 .then((link) => Expect.equals(y, link.path))
33 .then((_) => FutureExpect.isFalse(new File(y).exists()))
34 .then((_) => FutureExpect.isFalse(new File(x).exists()))
35 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y)))
36 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x)))
37 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND,
38 FileSystemEntity.type(y)))
39 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND,
40 FileSystemEntity.type(x)))
41 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK,
42 FileSystemEntity.type(y,
43 followLinks: false)))
44 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND,
45 FileSystemEntity.type(x,
46 followLinks: false)))
47 .then((_) => FutureExpect.equals(x, new Link(y).target()))
48 .then((_) => new File(y).create())
49 .then((yFile) => Expect.equals(y, yFile.path))
50 .then((_) => FutureExpect.isTrue(new File(y).exists()))
51 .then((_) => FutureExpect.isTrue(new File(x).exists()))
52 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y)))
53 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x)))
54 .then((_) => FutureExpect.isTrue(FileSystemEntity.isFile(y)))
55 .then((_) => FutureExpect.isTrue(FileSystemEntity.isFile(x)))
56 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE,
57 FileSystemEntity.type(y)))
58 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE,
59 FileSystemEntity.type(x)))
60 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK,
61 FileSystemEntity.type(y,
62 followLinks: false)))
63 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE,
64 FileSystemEntity.type(x,
65 followLinks: false)))
66 .then((_) => FutureExpect.equals(x, new Link(y).target()))
67 .then((_) => new File(x).delete())
68 .then((xDeletedFile) => Expect.equals(x, xDeletedFile.path))
69 .then((_) => new Directory(x).create())
70 .then((xCreatedDirectory) => Expect.equals(x, xCreatedDirectory.path))
71 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y)))
72 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x)))
73 .then((_) => FutureExpect.isTrue(FileSystemEntity.isDirectory(y)))
74 .then((_) => FutureExpect.isTrue(FileSystemEntity.isDirectory(x)))
75 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY,
76 FileSystemEntity.type(y)))
77 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY,
78 FileSystemEntity.type(x)))
79 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK,
80 FileSystemEntity.type(y,
81 followLinks: false)))
82 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY,
83 FileSystemEntity.type(x,
84 followLinks: false)))
85 .then((_) => FutureExpect.equals(x, new Link(y).target()))
86 .then((_) => new Link(y).delete())
87 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(y)))
88 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x)))
89 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND,
90 FileSystemEntity.type(y)))
91 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY,
92 FileSystemEntity.type(x)))
93 .then((_) => FutureExpect.throws(new Link(y).target()))
94 .then((_) => temp.delete(recursive: true));
95 });
96
97
98 Future testFileDelete() =>
99 new Directory('').createTemp().then((temp) {
100 var x = '${temp.path}${Platform.pathSeparator}x';
101 var y = '${temp.path}${Platform.pathSeparator}y';
102 return new File(x).create()
103 .then((_) => new Link(y).create(x))
104 .then((_) => FutureExpect.isTrue(new File(x).exists()))
105 .then((_) => FutureExpect.isTrue(new File(y).exists()))
106 .then((_) => new File(y).delete())
107 .then((_) => FutureExpect.isTrue(new File(x).exists()))
108 .then((_) => FutureExpect.isFalse(new File(y).exists()))
109 .then((_) => new Link(y).create(x))
110 .then((_) => FutureExpect.isTrue(new File(x).exists()))
111 .then((_) => FutureExpect.isTrue(new File(y).exists()))
112 .then((_) => new File(y).delete())
113 .then((_) => FutureExpect.isTrue(new File(x).exists()))
114 .then((_) => FutureExpect.isFalse(new File(y).exists()))
115 .then((_) => temp.delete(recursive: true));
116 });
117
118
119 Future testFileWriteRead() =>
120 new Directory('').createTemp().then((temp) {
121 var x = '${temp.path}${Platform.pathSeparator}x';
122 var y = '${temp.path}${Platform.pathSeparator}y';
123 var data = "asdf".codeUnits;
124 return new File(x).create()
125 .then((_) => new Link(y).create(x))
126 .then((_) =>
127 (new File(y).openWrite(mode: FileMode.WRITE)..add(data)).close())
128 .then((_) => FutureExpect.listEquals(data, new File(y).readAsBytes()))
129 .then((_) => FutureExpect.listEquals(data, new File(x).readAsBytes()))
130 .then((_) => temp.deleteSync(recursive: true));
131 });
132
133
134 Future testDirectoryExistsCreate() =>
135 new Directory('').createTemp().then((temp) {
136 var x = '${temp.path}${Platform.pathSeparator}x';
137 var y = '${temp.path}${Platform.pathSeparator}y';
138 return new Link(y).create(x)
139 .then((_) => FutureExpect.isFalse(new Directory(x).exists()))
140 .then((_) => FutureExpect.isFalse(new Directory(y).exists()))
141 .then((_) => FutureExpect.throws(new Directory(y).create()))
142 .then((_) => temp.deleteSync(recursive: true));
143 });
144
145
146
147 Future testDirectoryDelete() =>
148 new Directory('').createTemp().then((temp) =>
149 new Directory('').createTemp().then((temp2) {
150 var y = '${temp.path}${Platform.pathSeparator}y';
151 var x = '${temp2.path}${Platform.pathSeparator}x';
152 var link = new Directory(y);
153 return new File(x).create()
154 .then((_) => new Link(y).create(temp2.path))
155 .then((_) => FutureExpect.isTrue(link.exists()))
156 .then((_) => FutureExpect.isTrue(temp2.exists()))
157 .then((_) => link.delete())
158 .then((_) => FutureExpect.isFalse(link.exists()))
159 .then((_) => FutureExpect.isTrue(temp2.exists()))
160 .then((_) => new Link(y).create(temp2.path))
161 .then((_) => FutureExpect.isTrue(link.exists()))
162 .then((_) => temp.deleteSync(recursive: true))
163 .then((_) => FutureExpect.isFalse(link.exists()))
164 .then((_) => FutureExpect.isFalse(temp.exists()))
165 .then((_) => FutureExpect.isTrue(temp2.exists()))
166 .then((_) => FutureExpect.isTrue(new File(x).exists()))
167 .then((_) => temp2.delete(recursive: true));
168 }));
169
170
171 testDirectoryListing() =>
172 new Directory('').createTemp().then((temp) =>
173 new Directory('').createTemp().then((temp2) {
174 var sep = Platform.pathSeparator;
175 var y = '${temp.path}${sep}y';
176 var x = '${temp2.path}${sep}x';
177 return new File(x).create()
178 .then((_) => new Link(y).create(temp2.path))
179 .then((_) => temp.list(recursive: true).singleWhere(
180 (entry) => entry is File))
181 .then((file) => Expect.isTrue(file.path.endsWith('$y${sep}x')))
182 .then((_) => temp.list(recursive: true).singleWhere(
183 (entry) => entry is Directory))
184 .then((dir) => Expect.isTrue(dir.path.endsWith('y')))
185 .then((_) => temp.delete(recursive: true))
186 .then((_) => temp2.delete(recursive: true));
187 }));
188
189
190 testDirectoryListingBrokenLink() =>
191 new Directory('').createTemp().then((temp) {
192 var x = '${temp.path}${Platform.pathSeparator}x';
193 var link = '${temp.path}${Platform.pathSeparator}link';
194 var doesNotExist = 'this_thing_does_not_exist';
195 bool sawFile = false;
196 bool sawLink = false;
197 return new File(x).create()
198 .then((_) => new Link(link).create(doesNotExist))
199 .then((_) => temp.list(recursive: true).forEach(
200 (entity) {
201 if (entity is File) {
202 Expect.isFalse(sawFile);
203 sawFile = true;
204 Expect.isTrue(entity.path.endsWith(x));
205 } else {
206 Expect.isTrue(entity is Link);
207 Expect.isFalse(sawLink);
208 sawLink = true;
209 Expect.isTrue(entity.path.endsWith(link));
210 }
211 return true;
212 }))
213 .then((_) => temp.delete(recursive: true));
214 });
215
216
217 main() {
218 ReceivePort keepAlive = new ReceivePort();
219 testFileExistsCreate()
220 .then((_) => testFileDelete())
221 .then((_) => testFileWriteRead())
222 .then((_) => testDirectoryExistsCreate())
223 .then((_) => testDirectoryDelete())
224 .then((_) => testDirectoryListing())
225 .then((_) => testDirectoryListingBrokenLink())
226 .then((_) => keepAlive.close());
227 }
OLDNEW
« sdk/lib/io/file_system_entity.dart ('K') | « sdk/lib/io/link.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698