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

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

Issue 11748017: Add synchronous directory listing to dart:io Directory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Avoid memory leak of DirectorySyncLister. Created 7 years, 11 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("").createTempSync(); 15 Directory directory = new Directory("").createTempSync();
16 Directory subDirectory = new Directory("${directory.path}/subdir"); 16 Directory subDirectory = new Directory("${directory.path}/subdir");
17 Expect.isTrue('$directory'.contains(directory.path)); 17 Expect.isTrue('$directory'.contains(directory.path));
18 Expect.isFalse(subDirectory.existsSync()); 18 Expect.isFalse(subDirectory.existsSync());
19 subDirectory.createSync(); 19 subDirectory.createSync();
20 Expect.isTrue(subDirectory.existsSync()); 20 Expect.isTrue(subDirectory.existsSync());
21 File f = new File('${subDirectory.path}/file.txt'); 21 File f = new File('${subDirectory.path}/file.txt');
22 Expect.isFalse(f.existsSync()); 22 Expect.isFalse(f.existsSync());
23 f.createSync(); 23 f.createSync();
24 24
25 void testSyncListing(bool recursive) {
26 for (var entry in directory.listSync(recursive: recursive)) {
27 if (entry is File) {
28 Expect.isTrue(entry.name.contains(directory.path));
29 Expect.isTrue(entry.name.contains('subdir'));
30 Expect.isTrue(entry.name.contains('file.txt'));
31 Expect.isFalse(listedFile);
32 listedFile = true;
33 } else {
34 Expect.isTrue(entry is Directory);
35 Expect.isTrue(entry.path.contains(directory.path));
36 Expect.isTrue(entry.path.contains('subdir'));
37 Expect.isFalse(listedDir);
38 listedDir = true;
39 }
40 }
41 Expect.equals(listedFile, recursive);
42 Expect.isTrue(listedDir);
43 listedFile = false;
44 listedDir = false;
45 }
46
47 testSyncListing(true);
48 testSyncListing(false);
49
25 var lister = directory.list(recursive: true); 50 var lister = directory.list(recursive: true);
26 51
27 lister.onDir = (dir) { 52 lister.onDir = (dir) {
28 listedDir = true; 53 listedDir = true;
29 Expect.isTrue(dir.contains(directory.path)); 54 Expect.isTrue(dir.contains(directory.path));
30 Expect.isTrue(dir.contains('subdir')); 55 Expect.isTrue(dir.contains('subdir'));
31 }; 56 };
32 57
33 lister.onFile = (f) { 58 lister.onFile = (f) {
34 listedFile = true; 59 listedFile = true;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 Expect.fail("Listing of non-existing directory should fail"); 92 Expect.fail("Listing of non-existing directory should fail");
68 }; 93 };
69 lister.onDone = (success) { 94 lister.onDone = (success) {
70 Expect.isFalse(success); 95 Expect.isFalse(success);
71 }; 96 };
72 } 97 }
73 new Directory("").createTemp().then((d) { 98 new Directory("").createTemp().then((d) {
74 d.delete().then((ignore) { 99 d.delete().then((ignore) {
75 setupListerHandlers(d.list()); 100 setupListerHandlers(d.list());
76 setupListerHandlers(d.list(recursive: true)); 101 setupListerHandlers(d.list(recursive: true));
102 Expect.throws(() => d.listSync(), (e) => e is DirectoryIOException);
103 Expect.throws(() => d.listSync(recursive: true),
104 (e) => e is DirectoryIOException);
77 }); 105 });
78 }); 106 });
79 } 107 }
80 108
81 static void testListTooLongName() { 109 static void testListTooLongName() {
82 new Directory("").createTemp().then((d) { 110 new Directory("").createTemp().then((d) {
83 var errors = 0; 111 var errors = 0;
84 setupListHandlers(DirectoryLister lister) { 112 setupListHandlers(DirectoryLister lister) {
85 lister.onError = (e) { 113 lister.onError = (e) {
86 Expect.isTrue(e is DirectoryIOException); 114 Expect.isTrue(e is DirectoryIOException);
(...skipping 15 matching lines...) Expand all
102 var subDir = new Directory("${d.path}/$subDirName"); 130 var subDir = new Directory("${d.path}/$subDirName");
103 subDir.create().then((ignore) { 131 subDir.create().then((ignore) {
104 // Construct a long string of the form 132 // Construct a long string of the form
105 // 'tempdir/subdir/../subdir/../subdir'. 133 // 'tempdir/subdir/../subdir/../subdir'.
106 var buffer = new StringBuffer(); 134 var buffer = new StringBuffer();
107 buffer.add(subDir.path); 135 buffer.add(subDir.path);
108 for (var i = 0; i < 1000; i++) { 136 for (var i = 0; i < 1000; i++) {
109 buffer.add("/../${subDirName}"); 137 buffer.add("/../${subDirName}");
110 } 138 }
111 var long = new Directory("${buffer.toString()}"); 139 var long = new Directory("${buffer.toString()}");
140 Expect.throws(() => long.listSync(),
141 (e) => e is DirectoryIOException);
142 Expect.throws(() => long.listSync(recursive: true),
143 (e) => e is DirectoryIOException);
112 setupListHandlers(long.list()); 144 setupListHandlers(long.list());
113 setupListHandlers(long.list(recursive: true)); 145 setupListHandlers(long.list(recursive: true));
114 }); 146 });
115 }); 147 });
116 } 148 }
117 149
118 static void testDeleteNonExistent() { 150 static void testDeleteNonExistent() {
119 // Test that deleting a non-existing directory fails. 151 // Test that deleting a non-existing directory fails.
120 setupFutureHandlers(future) { 152 setupFutureHandlers(future) {
121 future.handleException((e) { 153 future.handleException((e) {
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 testCreateTempErrorSync(); 621 testCreateTempErrorSync();
590 testCreateTempError(); 622 testCreateTempError();
591 testCreateExistingSync(); 623 testCreateExistingSync();
592 testCreateExisting(); 624 testCreateExisting();
593 testCreateDirExistingFileSync(); 625 testCreateDirExistingFileSync();
594 testCreateDirExistingFile(); 626 testCreateDirExistingFile();
595 testCreateRecursive(); 627 testCreateRecursive();
596 testCreateRecursiveSync(); 628 testCreateRecursiveSync();
597 testRename(); 629 testRename();
598 } 630 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698