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

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

Issue 10392023: Change dart:io to use Future for one-shot operations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Adding stable test binaries Created 8 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
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 // 4 //
5 // Directory listing test. 5 // Directory listing test.
6 6
7 #import("dart:io"); 7 #import("dart:io");
8 #import("dart:isolate"); 8 #import("dart:isolate");
9 9
10 class DirectoryTest { 10 class DirectoryTest {
11 static void testListing() { 11 static void testListing() {
12 bool listedDir = false; 12 bool listedDir = false;
13 bool listedFile = false; 13 bool listedFile = false;
14 14
15 Directory directory = new Directory(""); 15 Directory directory = new Directory("").createTempSync();
16 directory.createTempSync();
17 Directory subDirectory = new Directory("${directory.path}/subdir"); 16 Directory subDirectory = new Directory("${directory.path}/subdir");
18 Expect.isFalse(subDirectory.existsSync()); 17 Expect.isFalse(subDirectory.existsSync());
19 subDirectory.createSync(); 18 subDirectory.createSync();
20 File f = new File('${subDirectory.path}/file.txt'); 19 File f = new File('${subDirectory.path}/file.txt');
21 Expect.isFalse(f.existsSync()); 20 Expect.isFalse(f.existsSync());
22 f.createSync(); 21 f.createSync();
23 22
24 directory.onDir = (dir) { 23 var lister = directory.list(recursive: true);
24
25 lister.onDir = (dir) {
25 listedDir = true; 26 listedDir = true;
26 Expect.isTrue(dir.contains(directory.path)); 27 Expect.isTrue(dir.contains(directory.path));
27 Expect.isTrue(dir.contains('subdir')); 28 Expect.isTrue(dir.contains('subdir'));
28 }; 29 };
29 30
30 directory.onFile = (f) { 31 lister.onFile = (f) {
31 listedFile = true; 32 listedFile = true;
32 Expect.isTrue(f.contains(directory.path)); 33 Expect.isTrue(f.contains(directory.path));
33 Expect.isTrue(f.contains('subdir')); 34 Expect.isTrue(f.contains('subdir'));
34 Expect.isTrue(f.contains('file.txt')); 35 Expect.isTrue(f.contains('file.txt'));
35 }; 36 };
36 37
37 directory.onDone = (completed) { 38 lister.onDone = (completed) {
38 Expect.isTrue(completed, "directory listing did not complete"); 39 Expect.isTrue(completed, "directory listing did not complete");
39 Expect.isTrue(listedDir, "directory not found"); 40 Expect.isTrue(listedDir, "directory not found");
40 Expect.isTrue(listedFile, "file not found"); 41 Expect.isTrue(listedFile, "file not found");
41 directory.deleteRecursively(() { 42 directory.deleteRecursively().then((ignore) {
42 f.exists((exists) => Expect.isFalse(exists)); 43 f.exists().then((exists) => Expect.isFalse(exists));
43 directory.exists((exists) => Expect.isFalse(exists)); 44 directory.exists().then((exists) => Expect.isFalse(exists));
44 subDirectory.exists((exists) => Expect.isFalse(exists)); 45 subDirectory.exists().then((exists) => Expect.isFalse(exists));
45 }); 46 });
46 }; 47 };
47 48
48 directory.onError = (e) {
49 Expect.fail("error listing directory: $e");
50 };
51
52 directory.list(recursive: true);
53
54 // Listing is asynchronous, so nothing should be listed at this 49 // Listing is asynchronous, so nothing should be listed at this
55 // point. 50 // point.
56 Expect.isFalse(listedDir); 51 Expect.isFalse(listedDir);
57 Expect.isFalse(listedFile); 52 Expect.isFalse(listedFile);
58 } 53 }
59 54
60 static void testListNonExistent() { 55 static void testListNonExistent() {
61 Directory d = new Directory(""); 56 setupListerHandlers(DirectoryLister lister) {
62 d.onError = (e) { 57 // Test that listing a non-existing directory fails.
63 Expect.fail("Directory error: $e"); 58 lister.onError = (e) {
64 }; 59 Expect.isTrue(e is DirectoryIOException);
65 d.createTemp(() { 60 };
66 d.delete(() { 61 lister.onFile = (file) {
67 // Test that listing a non-existing directory fails. 62 Expect.fail("Listing of non-existing directory should fail");
68 d.onError = (e) { 63 };
69 Expect.isTrue(e is DirectoryIOException); 64 lister.onDir = (dir) {
70 }; 65 Expect.fail("Listing of non-existing directory should fail");
71 d.onFile = (file) { 66 };
72 Expect.fail("Listing of non-existing directory should fail"); 67 lister.onDone = (success) {
73 }; 68 Expect.isFalse(success);
74 d.onDir = (dir) { 69 };
75 Expect.fail("Listing of non-existing directory should fail"); 70 }
76 }; 71 new Directory("").createTemp().then((d) {
77 d.onDone = (done) { 72 d.delete().then((ignore) {
78 Expect.isFalse(done); 73 setupListerHandlers(d.list());
79 }; 74 setupListerHandlers(d.list(recursive: true));
80 d.list();
81 d.list(recursive: true);
82 }); 75 });
83 }); 76 });
84 } 77 }
85 78
86 static void testListTooLongName() { 79 static void testListTooLongName() {
87 Directory d = new Directory(""); 80 new Directory("").createTemp().then((d) {
88 d.onError = (e) { 81 var errors = 0;
89 Expect.fail("Directory error: $e"); 82 setupListHandlers(DirectoryLister lister) {
90 }; 83 lister.onError = (e) {
91 d.createTemp(() { 84 Expect.isTrue(e is DirectoryIOException);
85 if (++errors == 2) {
86 d.deleteRecursively();
87 }
88 };
89 lister.onFile = (file) {
90 Expect.fail("Listing of non-existing directory should fail");
91 };
92 lister.onDir = (dir) {
93 Expect.fail("Listing of non-existing directory should fail");
94 };
95 lister.onDone = (success) {
96 Expect.isFalse(success);
97 };
98 }
92 var subDirName = 'subdir'; 99 var subDirName = 'subdir';
93 var subDir = new Directory("${d.path}/$subDirName"); 100 var subDir = new Directory("${d.path}/$subDirName");
94 subDir.onError = (e) { 101 subDir.create().then((ignore) {
95 Expect.fail("Directory error: $e");
96 };
97 subDir.create(() {
98 // Construct a long string of the form 102 // Construct a long string of the form
99 // 'tempdir/subdir/../subdir/../subdir'. 103 // 'tempdir/subdir/../subdir/../subdir'.
100 var buffer = new StringBuffer(); 104 var buffer = new StringBuffer();
101 buffer.add(subDir.path); 105 buffer.add(subDir.path);
102 for (var i = 0; i < 1000; i++) { 106 for (var i = 0; i < 1000; i++) {
103 buffer.add("/../${subDirName}"); 107 buffer.add("/../${subDirName}");
104 } 108 }
105 var long = new Directory("${buffer.toString()}"); 109 var long = new Directory("${buffer.toString()}");
106 var errors = 0; 110 setupListHandlers(long.list());
107 long.onError = (e) { 111 setupListHandlers(long.list(recursive: true));
108 Expect.isTrue(e is DirectoryIOException);
109 if (++errors == 2) {
110 d.deleteRecursively(() => null);
111 }
112 };
113 long.onFile = (file) {
114 Expect.fail("Listing of non-existing directory should fail");
115 };
116 long.onDir = (dir) {
117 Expect.fail("Listing of non-existing directory should fail");
118 };
119 long.onDone = (done) {
120 Expect.isFalse(done);
121 };
122 long.list();
123 long.list(recursive: true);
124 }); 112 });
125 }); 113 });
126 } 114 }
127 115
128 static void testDeleteNonExistent() { 116 static void testDeleteNonExistent() {
129 Directory d = new Directory(""); 117 // Test that deleting a non-existing directory fails.
130 d.onError = (e) { 118 setupFutureHandlers(future) {
131 Expect.fail("Directory error: $e"); 119 future.handleException((e) {
132 }; 120 Expect.isTrue(e is DirectoryIOException);
133 d.createTemp(() { 121 return true;
134 d.delete(() { 122 });
135 // Test that deleting a non-existing directory fails. 123 future.then((ignore) {
136 d.onError = (e) { 124 Expect.fail("Deletion of non-existing directory should fail");
137 Expect.isTrue(e is DirectoryIOException); 125 });
138 }; 126 }
139 d.delete(() { 127
140 Expect.fail("Deletion of non-existing directory should fail"); 128 new Directory("").createTemp().then((d) {
141 }); 129 d.delete().then((ignore) {
142 d.deleteRecursively(() { 130 setupFutureHandlers(d.delete());
143 Expect.fail("Deletion of non-existing directory should fail"); 131 setupFutureHandlers(d.deleteRecursively());
144 });
145 }); 132 });
146 }); 133 });
147 } 134 }
148 135
149 static void testDeleteTooLongName() { 136 static void testDeleteTooLongName() {
150 Directory d = new Directory(""); 137 var port = new ReceivePort();
151 d.onError = (e) { 138 new Directory("").createTemp().then((d) {
152 Expect.fail("Directory error: $e");
153 };
154 d.createTemp(() {
155 var subDirName = 'subdir'; 139 var subDirName = 'subdir';
156 var subDir = new Directory("${d.path}/$subDirName"); 140 var subDir = new Directory("${d.path}/$subDirName");
157 subDir.onError = (e) { 141 subDir.create().then((ignore) {
158 Expect.fail("Directory error: $e"); 142 // Construct a long string of the form
159 }; 143 // 'tempdir/subdir/../subdir/../subdir'.
160 subDir.create(() { 144 var buffer = new StringBuffer();
161 // Construct a long string of the form 145 buffer.add(subDir.path);
162 // 'tempdir/subdir/../subdir/../subdir'. 146 for (var i = 0; i < 1000; i++) {
163 var buffer = new StringBuffer(); 147 buffer.add("/../${subDirName}");
164 buffer.add(subDir.path);
165 for (var i = 0; i < 1000; i++) {
166 buffer.add("/../${subDirName}");
167 }
168 var long = new Directory("${buffer.toString()}");
169 var errors = 0;
170 long.onError = (e) {
171 Expect.isTrue(e is DirectoryIOException);
172 if (++errors == 2) {
173 d.deleteRecursively(() => null);
174 } 148 }
175 }; 149 var long = new Directory("${buffer.toString()}");
176 long.delete(() { 150 var errors = 0;
177 Expect.fail("Deletion of a directory with a long name should fail"); 151 onError(e) {
152 Expect.isTrue(e is DirectoryIOException);
153 if (++errors == 2) {
154 d.deleteRecursively().then((ignore) => port.close());
155 }
156 return true;
157 }
158 long.delete().handleException(onError);
159 long.deleteRecursively().handleException(onError);
178 }); 160 });
179 long.deleteRecursively(() {
180 Expect.fail("Deletion of a directory with a long name should fail");
181 });
182 });
183 }); 161 });
184 } 162 }
185 163
186 static void testDeleteNonExistentSync() { 164 static void testDeleteNonExistentSync() {
187 Directory d = new Directory(""); 165 Directory d = new Directory("").createTempSync();
188 d.createTempSync();
189 d.deleteSync(); 166 d.deleteSync();
190 Expect.throws(d.deleteSync); 167 Expect.throws(d.deleteSync);
191 Expect.throws(() => d.deleteRecursivelySync()); 168 Expect.throws(() => d.deleteRecursivelySync());
192 } 169 }
193 170
194 static void testDeleteTooLongNameSync() { 171 static void testDeleteTooLongNameSync() {
195 Directory d = new Directory(""); 172 Directory d = new Directory("").createTempSync();
196 d.createTempSync();
197 var subDirName = 'subdir'; 173 var subDirName = 'subdir';
198 var subDir = new Directory("${d.path}/$subDirName"); 174 var subDir = new Directory("${d.path}/$subDirName");
199 subDir.createSync(); 175 subDir.createSync();
200 // Construct a long string of the form 176 // Construct a long string of the form
201 // 'tempdir/subdir/../subdir/../subdir'. 177 // 'tempdir/subdir/../subdir/../subdir'.
202 var buffer = new StringBuffer(); 178 var buffer = new StringBuffer();
203 buffer.add(subDir.path); 179 buffer.add(subDir.path);
204 for (var i = 0; i < 1000; i++) { 180 for (var i = 0; i < 1000; i++) {
205 buffer.add("/../${subDirName}"); 181 buffer.add("/../${subDirName}");
206 } 182 }
207 var long = new Directory("${buffer.toString()}"); 183 var long = new Directory("${buffer.toString()}");
208 Expect.throws(long.deleteSync); 184 Expect.throws(long.deleteSync);
209 Expect.throws(() => long.deleteRecursivelySync()); 185 Expect.throws(() => long.deleteRecursivelySync());
186 d.deleteRecursivelySync();
210 } 187 }
211 188
212 static void testExistsCreateDelete() { 189 static void testExistsCreateDelete() {
213 Directory d = new Directory(""); 190 new Directory("").createTemp().then((d) {
214 d.createTemp(() { 191 d.exists().then((bool exists) {
215 d.exists((bool exists) {
216 Expect.isTrue(exists); 192 Expect.isTrue(exists);
217 Directory created = new Directory("${d.path}/subdir"); 193 Directory created = new Directory("${d.path}/subdir");
218 created.create(() { 194 created.create().then((ignore) {
219 created.exists((bool exists) { 195 created.exists().then((bool exists) {
220 Expect.isTrue(exists); 196 Expect.isTrue(exists);
221 created.delete(() { 197 created.delete().then((ignore) {
222 created.exists((bool exists) { 198 created.exists().then((bool exists) {
223 Expect.isFalse(exists); 199 Expect.isFalse(exists);
224 d.delete(() { 200 d.delete().then((ignore) {
225 d.exists((bool exists) { 201 d.exists().then((bool exists) {
226 Expect.isFalse(exists); 202 Expect.isFalse(exists);
227 }); 203 });
228 }); 204 });
229 }); 205 });
230 }); 206 });
231 }); 207 });
232 }); 208 });
233 }); 209 });
234 }); 210 });
235 } 211 }
236 212
237 static void testExistsCreateDeleteSync() { 213 static void testExistsCreateDeleteSync() {
238 Directory d = new Directory(""); 214 Directory d = new Directory("").createTempSync();
239 d.createTempSync();
240 Expect.isTrue(d.existsSync()); 215 Expect.isTrue(d.existsSync());
241 Directory created = new Directory("${d.path}/subdir"); 216 Directory created = new Directory("${d.path}/subdir");
242 created.createSync(); 217 created.createSync();
243 Expect.isTrue(created.existsSync()); 218 Expect.isTrue(created.existsSync());
244 created.deleteSync(); 219 created.deleteSync();
245 Expect.isFalse(created.existsSync()); 220 Expect.isFalse(created.existsSync());
246 d.deleteSync(); 221 d.deleteSync();
247 Expect.isFalse(d.existsSync()); 222 Expect.isFalse(d.existsSync());
248 } 223 }
249 224
250 static void testCreateTemp() { 225 static void testCreateTemp() {
251 Directory tempDir1 = new Directory("/tmp/dart_temp_dir_"); 226 Directory tempDir1;
252 Directory tempDir2 = new Directory("/tmp/dart_temp_dir_"); 227 Directory tempDir2;
253 bool stage1aDone = false; 228 bool stage1aDone = false;
254 bool stage1bDone = false; 229 bool stage1bDone = false;
255 bool emptyTemplateTestRunning = false; 230 bool emptyTemplateTestRunning = false;
256 231
257 // Stages 0 through 2 run twice, the second time with an empty path. 232 // Stages 0 through 2 run twice, the second time with an empty path.
258 Function stage0; 233 Function stage0;
259 Function stage1a; 234 Function stage1a;
260 Function stage1b; 235 Function stage1b;
261 Function stage2; 236 Function stage2;
262 Function stage3; // Loops to stage 0. 237 Function stage3; // Loops to stage 0.
263 238
264 Function error(e) {
265 Expect.fail("Directory onError: $e");
266 }
267
268 stage0 = () { 239 stage0 = () {
269 tempDir1.onError = error; 240 var dir = new Directory("/tmp/dart_temp_dir_");
270 tempDir1.createTemp(stage1a); 241 dir.createTemp().then(stage1a);
271 tempDir2.onError = error; 242 dir.createTemp().then(stage1b);
272 tempDir2.createTemp(stage1b);
273 }; 243 };
274 244
275 stage1a = () { 245 stage1a = (temp) {
246 tempDir1 = temp;
276 stage1aDone = true; 247 stage1aDone = true;
277 Expect.isTrue(tempDir1.existsSync()); 248 Expect.isTrue(tempDir1.existsSync());
278 if (stage1bDone) { 249 if (stage1bDone) {
279 stage2(); 250 stage2();
280 } 251 }
281 }; 252 };
282 253
283 stage1b = () { 254 stage1b = (temp) {
255 tempDir2 = temp;
284 stage1bDone = true; 256 stage1bDone = true;
285 Expect.isTrue(tempDir2.existsSync()); 257 Expect.isTrue(tempDir2.existsSync());
286 if (stage1aDone) { 258 if (stage1aDone) {
287 stage2(); 259 stage2();
288 } 260 }
289 }; 261 };
290 262
291 stage2 = () { 263 stage2 = () {
292 Expect.notEquals(tempDir1.path, tempDir2.path); 264 Expect.notEquals(tempDir1.path, tempDir2.path);
293 tempDir1.deleteSync(); 265 tempDir1.deleteSync();
(...skipping 18 matching lines...) Expand all
312 284
313 if (new Directory("/tmp").existsSync()) { 285 if (new Directory("/tmp").existsSync()) {
314 stage0(); 286 stage0();
315 } else { 287 } else {
316 emptyTemplateTestRunning = true; 288 emptyTemplateTestRunning = true;
317 stage3(); 289 stage3();
318 } 290 }
319 } 291 }
320 292
321 static void testCreateDeleteTemp() { 293 static void testCreateDeleteTemp() {
322 Directory tempDirectory = new Directory(""); 294 new Directory("").createTemp().then((tempDirectory) {
323 tempDirectory.createTemp(() {
324 String filename = tempDirectory.path + 295 String filename = tempDirectory.path +
325 Platform.pathSeparator + "dart_testfile"; 296 Platform.pathSeparator + "dart_testfile";
326 File file = new File(filename); 297 File file = new File(filename);
327 Expect.isFalse(file.existsSync()); 298 Expect.isFalse(file.existsSync());
328 file.onError = (e) { 299 file.create().then((ignore) {
329 Expect.fail("testCreateTemp file.onError called: $e"); 300 file.exists().then((exists) {
330 };
331 file.create(() {
332 file.exists((bool exists) {
333 Expect.isTrue(exists); 301 Expect.isTrue(exists);
334 // Try to delete the directory containing the file - should throw. 302 // Try to delete the directory containing the file - should throw.
335 Expect.throws(tempDirectory.deleteSync); 303 Expect.throws(tempDirectory.deleteSync);
336 Expect.isTrue(tempDirectory.existsSync()); 304 Expect.isTrue(tempDirectory.existsSync());
337 305
338 // Delete the file, and then delete the directory. 306 // Delete the file, and then delete the directory.
339 file.delete(() { 307 file.delete().then((ignore) {
340 tempDirectory.deleteSync(); 308 tempDirectory.deleteSync();
341 Expect.isFalse(tempDirectory.existsSync()); 309 Expect.isFalse(tempDirectory.existsSync());
342 }); 310 });
343 }); 311 });
344 }); 312 });
345 }); 313 });
346 } 314 }
347 315
348 static void testCurrent() { 316 static void testCurrent() {
349 Directory current = new Directory.current(); 317 Directory current = new Directory.current();
(...skipping 17 matching lines...) Expand all
367 testCurrent(); 335 testCurrent();
368 } 336 }
369 } 337 }
370 338
371 339
372 class NestedTempDirectoryTest { 340 class NestedTempDirectoryTest {
373 List<Directory> createdDirectories; 341 List<Directory> createdDirectories;
374 Directory current; 342 Directory current;
375 343
376 NestedTempDirectoryTest.run() 344 NestedTempDirectoryTest.run()
377 : createdDirectories = new List<Directory>(), 345 : createdDirectories = new List<Directory>() {
378 current = new Directory("") { 346 new Directory("").createTemp().then(createPhaseCallback);
379 current.onError = errorCallback;
380 current.createTemp(createPhaseCallback);
381 } 347 }
382 348
383 void errorCallback(e) { 349 void createPhaseCallback(temp) {
384 Expect.fail("Error callback called in NestedTempDirectoryTest: $e"); 350 createdDirectories.add(temp);
385 }
386
387 void createPhaseCallback() {
388 createdDirectories.add(current);
389 int nestingDepth = 6; 351 int nestingDepth = 6;
390 var os = Platform.operatingSystem; 352 var os = Platform.operatingSystem;
391 if (os == "windows") nestingDepth = 2; 353 if (os == "windows") nestingDepth = 2;
392 if (createdDirectories.length < nestingDepth) { 354 if (createdDirectories.length < nestingDepth) {
393 current = new Directory( 355 temp = new Directory(
394 current.path + "/nested_temp_dir_${createdDirectories.length}_"); 356 '${temp.path}/nested_temp_dir_${createdDirectories.length}_');
395 current.onError = errorCallback; 357 temp.createTemp().then(createPhaseCallback);
396 current.createTemp(createPhaseCallback);
397 } else { 358 } else {
398 deletePhaseCallback(); 359 deletePhaseCallback();
399 } 360 }
400 } 361 }
401 362
402 void deletePhaseCallback() { 363 void deletePhaseCallback() {
403 if (!createdDirectories.isEmpty()) { 364 if (!createdDirectories.isEmpty()) {
404 current = createdDirectories.removeLast(); 365 current = createdDirectories.removeLast();
405 current.deleteSync(); 366 current.deleteSync();
406 deletePhaseCallback(); 367 deletePhaseCallback();
(...skipping 26 matching lines...) Expand all
433 Expect.throws(new Directory(location).createTempSync, 394 Expect.throws(new Directory(location).createTempSync,
434 (e) => e is DirectoryIOException); 395 (e) => e is DirectoryIOException);
435 } 396 }
436 } 397 }
437 398
438 399
439 testCreateTempError() { 400 testCreateTempError() {
440 var location = illegalTempDirectoryLocation(); 401 var location = illegalTempDirectoryLocation();
441 if (location == null) return; 402 if (location == null) return;
442 403
443 var resultPort = new ReceivePort(); 404 var port = new ReceivePort();
444 resultPort.receive((String message, ignored) { 405 var future = new Directory(location).createTemp();
445 resultPort.close(); 406 future.handleException((e) => port.close());
446 Expect.equals("error", message);
447 });
448
449 Directory dir = new Directory(location);
450 dir.onError = (e) { resultPort.toSendPort().send("error"); };
451 dir.createTemp(() => resultPort.toSendPort().send("success"));
452 } 407 }
453 408
454 409
455 main() { 410 main() {
456 DirectoryTest.testMain(); 411 DirectoryTest.testMain();
457 NestedTempDirectoryTest.testMain(); 412 NestedTempDirectoryTest.testMain();
458 testCreateTempErrorSync(); 413 testCreateTempErrorSync();
459 testCreateTempError(); 414 testCreateTempError();
460 } 415 }
OLDNEW
« no previous file with comments | « tests/standalone/io/directory_invalid_arguments_test.dart ('k') | tests/standalone/io/file_error_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698