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

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

Issue 23886004: Update dart:io tests to use asyncStart/asyncEnd (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 7 years, 3 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 "package:expect/expect.dart";
8 import "dart:async"; 7 import "dart:async";
9 import "dart:io"; 8 import "dart:io";
10 import "dart:isolate"; 9
10 import "package:async_helper/async_helper.dart";
11 import "package:expect/expect.dart";
11 12
12 class DirectoryTest { 13 class DirectoryTest {
13 static void testListing() { 14 static void testListing() {
14 bool listedDir = false; 15 bool listedDir = false;
15 bool listedFile = false; 16 bool listedFile = false;
16 17
17 Directory directory = new Directory("").createTempSync(); 18 Directory directory = new Directory("").createTempSync();
18 Directory subDirectory = new Directory("${directory.path}/subdir"); 19 Directory subDirectory = new Directory("${directory.path}/subdir");
19 Expect.isTrue('$directory'.contains(directory.path)); 20 Expect.isTrue('$directory'.contains(directory.path));
20 Expect.isFalse(subDirectory.existsSync()); 21 Expect.isFalse(subDirectory.existsSync());
(...skipping 23 matching lines...) Expand all
44 Expect.equals(listedFile, recursive); 45 Expect.equals(listedFile, recursive);
45 Expect.isTrue(listedDir); 46 Expect.isTrue(listedDir);
46 listedFile = false; 47 listedFile = false;
47 listedDir = false; 48 listedDir = false;
48 } 49 }
49 50
50 testSyncListing(true); 51 testSyncListing(true);
51 testSyncListing(false); 52 testSyncListing(false);
52 Expect.equals(f.fullPathSync(), fLong.fullPathSync()); 53 Expect.equals(f.fullPathSync(), fLong.fullPathSync());
53 54
54 var listingDonePort = new ReceivePort(); 55 asyncStart();
55 directory.list(recursive: true).listen( 56 directory.list(recursive: true).listen(
56 (FileSystemEntity entity) { 57 (FileSystemEntity entity) {
57 if (entity is File) { 58 if (entity is File) {
58 var path = entity.path; 59 var path = entity.path;
59 listedFile = true; 60 listedFile = true;
60 Expect.isTrue(path.contains(directory.path)); 61 Expect.isTrue(path.contains(directory.path));
61 Expect.isTrue(path.contains('subdir')); 62 Expect.isTrue(path.contains('subdir'));
62 Expect.isTrue(path.contains('file.txt')); 63 Expect.isTrue(path.contains('file.txt'));
63 } else { 64 } else {
64 var path = entity.path; 65 var path = entity.path;
65 Expect.isTrue(entity is Directory); 66 Expect.isTrue(entity is Directory);
66 listedDir = true; 67 listedDir = true;
67 Expect.isTrue(path.contains(directory.path)); 68 Expect.isTrue(path.contains(directory.path));
68 Expect.isTrue(path.contains('subdir')); 69 Expect.isTrue(path.contains('subdir'));
69 } 70 }
70 }, 71 },
71 onDone: () { 72 onDone: () {
72 Expect.isTrue(listedDir, "directory not found"); 73 Expect.isTrue(listedDir, "directory not found");
73 Expect.isTrue(listedFile, "file not found"); 74 Expect.isTrue(listedFile, "file not found");
74 directory.delete(recursive: true).then((ignore) { 75 directory.delete(recursive: true).then((ignore) {
75 f.exists().then((exists) => Expect.isFalse(exists)); 76 f.exists().then((exists) => Expect.isFalse(exists));
76 directory.exists().then((exists) => Expect.isFalse(exists)); 77 directory.exists().then((exists) => Expect.isFalse(exists));
77 subDirectory.exists().then((exists) => Expect.isFalse(exists)); 78 subDirectory.exists().then((exists) => Expect.isFalse(exists));
78 listingDonePort.close(); 79 asyncEnd();
79 }); 80 });
80 }); 81 });
81 82
82 // Listing is asynchronous, so nothing should be listed at this 83 // Listing is asynchronous, so nothing should be listed at this
83 // point. 84 // point.
84 Expect.isFalse(listedDir); 85 Expect.isFalse(listedDir);
85 Expect.isFalse(listedFile); 86 Expect.isFalse(listedFile);
86 } 87 }
87 88
88 static void testListingTailingPaths() { 89 static void testListingTailingPaths() {
(...skipping 26 matching lines...) Expand all
115 d.delete().then((ignore) { 116 d.delete().then((ignore) {
116 setupListerHandlers(d.list()); 117 setupListerHandlers(d.list());
117 setupListerHandlers(d.list(recursive: true)); 118 setupListerHandlers(d.list(recursive: true));
118 }); 119 });
119 }); 120 });
120 } 121 }
121 122
122 static void testListTooLongName() { 123 static void testListTooLongName() {
123 new Directory("").createTemp().then((d) { 124 new Directory("").createTemp().then((d) {
124 var errors = 0; 125 var errors = 0;
125 var port = new ReceivePort(); 126 asyncStart();
126 setupListHandlers(Stream<FileSystemEntity> stream) { 127 setupListHandlers(Stream<FileSystemEntity> stream) {
127 stream.listen( 128 stream.listen(
128 (_) => Expect.fail("Listing of non-existing directory should fail"), 129 (_) => Expect.fail("Listing of non-existing directory should fail"),
129 onError: (error) { 130 onError: (error) {
130 Expect.isTrue(error is DirectoryException); 131 Expect.isTrue(error is DirectoryException);
131 if (++errors == 2) { 132 if (++errors == 2) {
132 d.delete(recursive: true).then((_) { 133 d.delete(recursive: true).then((_) {
133 port.close(); 134 asyncEnd();
134 }); 135 });
135 } 136 }
136 }); 137 });
137 } 138 }
138 var subDirName = 'subdir'; 139 var subDirName = 'subdir';
139 var subDir = new Directory("${d.path}/$subDirName"); 140 var subDir = new Directory("${d.path}/$subDirName");
140 subDir.create().then((ignore) { 141 subDir.create().then((ignore) {
141 // Construct a long string of the form 142 // Construct a long string of the form
142 // 'tempdir/subdir/../subdir/../subdir'. 143 // 'tempdir/subdir/../subdir/../subdir'.
143 var buffer = new StringBuffer(); 144 var buffer = new StringBuffer();
(...skipping 20 matching lines...) Expand all
164 165
165 new Directory("").createTemp().then((d) { 166 new Directory("").createTemp().then((d) {
166 d.delete().then((ignore) { 167 d.delete().then((ignore) {
167 setupFutureHandlers(d.delete()); 168 setupFutureHandlers(d.delete());
168 setupFutureHandlers(d.delete(recursive: true)); 169 setupFutureHandlers(d.delete(recursive: true));
169 }); 170 });
170 }); 171 });
171 } 172 }
172 173
173 static void testDeleteTooLongName() { 174 static void testDeleteTooLongName() {
174 var port = new ReceivePort(); 175 asyncStart();
175 new Directory("").createTemp().then((d) { 176 new Directory("").createTemp().then((d) {
176 var subDirName = 'subdir'; 177 var subDirName = 'subdir';
177 var subDir = new Directory("${d.path}/$subDirName"); 178 var subDir = new Directory("${d.path}/$subDirName");
178 subDir.create().then((ignore) { 179 subDir.create().then((ignore) {
179 // Construct a long string of the form 180 // Construct a long string of the form
180 // 'tempdir/subdir/../subdir/../subdir'. 181 // 'tempdir/subdir/../subdir/../subdir'.
181 var buffer = new StringBuffer(); 182 var buffer = new StringBuffer();
182 buffer.write(subDir.path); 183 buffer.write(subDir.path);
183 for (var i = 0; i < 1000; i++) { 184 for (var i = 0; i < 1000; i++) {
184 buffer.write("/../${subDirName}"); 185 buffer.write("/../${subDirName}");
185 } 186 }
186 var long = new Directory("${buffer.toString()}"); 187 var long = new Directory("${buffer.toString()}");
187 var errors = 0; 188 var errors = 0;
188 onError(error) { 189 onError(error) {
189 Expect.isTrue(error is DirectoryException); 190 Expect.isTrue(error is DirectoryException);
190 if (++errors == 2) { 191 if (++errors == 2) {
191 d.delete(recursive: true).then((ignore) => port.close()); 192 d.delete(recursive: true).then((_) => asyncEnd());
192 } 193 }
193 return true; 194 return true;
194 } 195 }
195 long.delete().catchError(onError); 196 long.delete().catchError(onError);
196 long.delete(recursive: true).catchError(onError); 197 long.delete(recursive: true).catchError(onError);
197 }); 198 });
198 }); 199 });
199 } 200 }
200 201
201 static void testDeleteNonExistentSync() { 202 static void testDeleteNonExistentSync() {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 }, 348 },
348 onDone: () { 349 onDone: () {
349 Expect.equals(2, count); 350 Expect.equals(2, count);
350 l.deleteSync(); 351 l.deleteSync();
351 d.deleteSync(); 352 d.deleteSync();
352 tmp.deleteSync(); 353 tmp.deleteSync();
353 }); 354 });
354 } 355 }
355 356
356 static void testCreateTemp([String template = ""]) { 357 static void testCreateTemp([String template = ""]) {
357 var port = new ReceivePort(); 358 asyncStart();
358 Directory dir = new Directory(template); 359 Directory dir = new Directory(template);
359 Future.wait([dir.createTemp(), dir.createTemp()]) 360 Future.wait([dir.createTemp(), dir.createTemp()])
360 .then((tempDirs) { 361 .then((tempDirs) {
361 Expect.notEquals(tempDirs[0].path, tempDirs[1].path); 362 Expect.notEquals(tempDirs[0].path, tempDirs[1].path);
362 for (Directory t in tempDirs) { 363 for (Directory t in tempDirs) {
363 Expect.isTrue(t.existsSync()); 364 Expect.isTrue(t.existsSync());
364 t.deleteSync(); 365 t.deleteSync();
365 Expect.isFalse(t.existsSync()); 366 Expect.isFalse(t.existsSync());
366 } 367 }
367 port.close(); 368 asyncEnd();
368 }); 369 });
369 } 370 }
370 371
371 static void testCreateTempTemplate() { 372 static void testCreateTempTemplate() {
372 if (new Directory("/tmp").existsSync()) { 373 if (new Directory("/tmp").existsSync()) {
373 testCreateTemp("/tmp/dart_temp_dir_"); 374 testCreateTemp("/tmp/dart_temp_dir_");
374 } 375 }
375 } 376 }
376 377
377 static void testCreateDeleteTemp() { 378 static void testCreateDeleteTemp() {
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 Expect.throws(new Directory(location).createTempSync, 494 Expect.throws(new Directory(location).createTempSync,
494 (e) => e is DirectoryException); 495 (e) => e is DirectoryException);
495 } 496 }
496 } 497 }
497 498
498 499
499 testCreateTempError() { 500 testCreateTempError() {
500 var location = illegalTempDirectoryLocation(); 501 var location = illegalTempDirectoryLocation();
501 if (location == null) return; 502 if (location == null) return;
502 503
503 var port = new ReceivePort(); 504 asyncStart();
504 var future = new Directory(location).createTemp(); 505 var future = new Directory(location).createTemp();
505 future.catchError((e) => port.close()); 506 future.catchError((_) => asyncEnd());
506 } 507 }
507 508
508 509
509 testCreateExistingSync() { 510 testCreateExistingSync() {
510 // Test that creating an existing directory succeeds. 511 // Test that creating an existing directory succeeds.
511 var d = new Directory(''); 512 var d = new Directory('');
512 var temp = d.createTempSync(); 513 var temp = d.createTempSync();
513 var subDir = new Directory('${temp.path}/flaf'); 514 var subDir = new Directory('${temp.path}/flaf');
514 Expect.isFalse(subDir.existsSync()); 515 Expect.isFalse(subDir.existsSync());
515 subDir.createSync(); 516 subDir.createSync();
516 Expect.isTrue(subDir.existsSync()); 517 Expect.isTrue(subDir.existsSync());
517 subDir.createSync(); 518 subDir.createSync();
518 Expect.isTrue(subDir.existsSync()); 519 Expect.isTrue(subDir.existsSync());
519 temp.deleteSync(recursive: true); 520 temp.deleteSync(recursive: true);
520 } 521 }
521 522
522 523
523 testCreateExisting() { 524 testCreateExisting() {
524 // Test that creating an existing directory succeeds. 525 // Test that creating an existing directory succeeds.
525 var port = new ReceivePort(); 526 asyncStart();
526 var d = new Directory(''); 527 var d = new Directory('');
527 d.createTemp().then((temp) { 528 d.createTemp().then((temp) {
528 var subDir = new Directory('${temp.path}/flaf'); 529 var subDir = new Directory('${temp.path}/flaf');
529 subDir.exists().then((dirExists) { 530 subDir.exists().then((dirExists) {
530 Expect.isFalse(dirExists); 531 Expect.isFalse(dirExists);
531 subDir.create().then((_) { 532 subDir.create().then((_) {
532 subDir.exists().then((dirExists) { 533 subDir.exists().then((dirExists) {
533 Expect.isTrue(dirExists); 534 Expect.isTrue(dirExists);
534 subDir.create().then((_) { 535 subDir.create().then((_) {
535 subDir.exists().then((dirExists) { 536 subDir.exists().then((dirExists) {
536 Expect.isTrue(dirExists); 537 Expect.isTrue(dirExists);
537 temp.delete(recursive: true).then((_) { 538 temp.delete(recursive: true).then((_) {
538 port.close(); 539 asyncEnd();
539 }); 540 });
540 }); 541 });
541 }); 542 });
542 }); 543 });
543 }); 544 });
544 }); 545 });
545 }); 546 });
546 } 547 }
547 548
548 549
549 testCreateDirExistingFileSync() { 550 testCreateDirExistingFileSync() {
550 // Test that creating an existing directory succeeds. 551 // Test that creating an existing directory succeeds.
551 var d = new Directory(''); 552 var d = new Directory('');
552 var temp = d.createTempSync(); 553 var temp = d.createTempSync();
553 var path = '${temp.path}/flaf'; 554 var path = '${temp.path}/flaf';
554 var file = new File(path); 555 var file = new File(path);
555 file.createSync(); 556 file.createSync();
556 Expect.isTrue(file.existsSync()); 557 Expect.isTrue(file.existsSync());
557 Expect.throws(new Directory(path).createSync, 558 Expect.throws(new Directory(path).createSync,
558 (e) => e is DirectoryException); 559 (e) => e is DirectoryException);
559 temp.deleteSync(recursive: true); 560 temp.deleteSync(recursive: true);
560 } 561 }
561 562
562 563
563 testCreateDirExistingFile() { 564 testCreateDirExistingFile() {
564 // Test that creating an existing directory succeeds. 565 // Test that creating an existing directory succeeds.
565 var port = new ReceivePort(); 566 asyncStart();
566 var d = new Directory(''); 567 var d = new Directory('');
567 d.createTemp().then((temp) { 568 d.createTemp().then((temp) {
568 var path = '${temp.path}/flaf'; 569 var path = '${temp.path}/flaf';
569 var file = new File(path); 570 var file = new File(path);
570 var subDir = new Directory(path); 571 var subDir = new Directory(path);
571 file.create().then((_) { 572 file.create().then((_) {
572 subDir.create() 573 subDir.create()
573 .then((_) { Expect.fail("dir create should fail on existing file"); }) 574 .then((_) { Expect.fail("dir create should fail on existing file"); })
574 .catchError((error) { 575 .catchError((error) {
575 Expect.isTrue(error is DirectoryException); 576 Expect.isTrue(error is DirectoryException);
576 temp.delete(recursive: true).then((_) { 577 temp.delete(recursive: true).then((_) {
577 port.close(); 578 asyncEnd();
578 }); 579 });
579 }); 580 });
580 }); 581 });
581 }); 582 });
582 } 583 }
583 584
584 585
585 testCreateRecursiveSync() { 586 testCreateRecursiveSync() {
586 var temp = new Directory('').createTempSync(); 587 var temp = new Directory('').createTempSync();
587 var d = new Directory('${temp.path}/a/b/c'); 588 var d = new Directory('${temp.path}/a/b/c');
588 d.createSync(recursive: true); 589 d.createSync(recursive: true);
589 Expect.isTrue(new Directory('${temp.path}/a').existsSync()); 590 Expect.isTrue(new Directory('${temp.path}/a').existsSync());
590 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync()); 591 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync());
591 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync()); 592 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync());
592 temp.deleteSync(recursive: true); 593 temp.deleteSync(recursive: true);
593 } 594 }
594 595
595 596
596 testCreateRecursive() { 597 testCreateRecursive() {
597 var port = new ReceivePort(); 598 asyncStart();
598 new Directory('').createTemp().then((temp) { 599 new Directory('').createTemp().then((temp) {
599 var d = new Directory('${temp.path}/a/b/c'); 600 var d = new Directory('${temp.path}/a/b/c');
600 d.create(recursive: true).then((_) { 601 d.create(recursive: true).then((_) {
601 Expect.isTrue(new Directory('${temp.path}/a').existsSync()); 602 Expect.isTrue(new Directory('${temp.path}/a').existsSync());
602 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync()); 603 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync());
603 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync()); 604 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync());
604 temp.deleteSync(recursive: true); 605 temp.deleteSync(recursive: true);
605 port.close(); 606 asyncEnd();
606 }); 607 });
607 }); 608 });
608 } 609 }
609 610
610 611
611 testRename() { 612 testRename() {
612 var d = new Directory(''); 613 var d = new Directory('');
613 var temp1 = d.createTempSync(); 614 var temp1 = d.createTempSync();
614 var temp2 = d.createTempSync(); 615 var temp2 = d.createTempSync();
615 var temp3 = temp1.renameSync(temp2.path); 616 var temp3 = temp1.renameSync(temp2.path);
(...skipping 17 matching lines...) Expand all
633 testCreateTempErrorSync(); 634 testCreateTempErrorSync();
634 testCreateTempError(); 635 testCreateTempError();
635 testCreateExistingSync(); 636 testCreateExistingSync();
636 testCreateExisting(); 637 testCreateExisting();
637 testCreateDirExistingFileSync(); 638 testCreateDirExistingFileSync();
638 testCreateDirExistingFile(); 639 testCreateDirExistingFile();
639 testCreateRecursive(); 640 testCreateRecursive();
640 testCreateRecursiveSync(); 641 testCreateRecursiveSync();
641 testRename(); 642 testRename();
642 } 643 }
OLDNEW
« no previous file with comments | « tests/standalone/io/directory_non_ascii_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