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

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

Issue 11364097: Allow Directory.create to create all missing path components. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Reupload Created 8 years, 1 month 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 {
(...skipping 22 matching lines...) Expand all
33 listedFile = true; 33 listedFile = true;
34 Expect.isTrue(f.contains(directory.path)); 34 Expect.isTrue(f.contains(directory.path));
35 Expect.isTrue(f.contains('subdir')); 35 Expect.isTrue(f.contains('subdir'));
36 Expect.isTrue(f.contains('file.txt')); 36 Expect.isTrue(f.contains('file.txt'));
37 }; 37 };
38 38
39 lister.onDone = (completed) { 39 lister.onDone = (completed) {
40 Expect.isTrue(completed, "directory listing did not complete"); 40 Expect.isTrue(completed, "directory listing did not complete");
41 Expect.isTrue(listedDir, "directory not found"); 41 Expect.isTrue(listedDir, "directory not found");
42 Expect.isTrue(listedFile, "file not found"); 42 Expect.isTrue(listedFile, "file not found");
43 directory.deleteRecursively().then((ignore) { 43 directory.delete(recursive: true).then((ignore) {
44 f.exists().then((exists) => Expect.isFalse(exists)); 44 f.exists().then((exists) => Expect.isFalse(exists));
45 directory.exists().then((exists) => Expect.isFalse(exists)); 45 directory.exists().then((exists) => Expect.isFalse(exists));
46 subDirectory.exists().then((exists) => Expect.isFalse(exists)); 46 subDirectory.exists().then((exists) => Expect.isFalse(exists));
47 }); 47 });
48 }; 48 };
49 49
50 // Listing is asynchronous, so nothing should be listed at this 50 // Listing is asynchronous, so nothing should be listed at this
51 // point. 51 // point.
52 Expect.isFalse(listedDir); 52 Expect.isFalse(listedDir);
53 Expect.isFalse(listedFile); 53 Expect.isFalse(listedFile);
(...skipping 23 matching lines...) Expand all
77 }); 77 });
78 } 78 }
79 79
80 static void testListTooLongName() { 80 static void testListTooLongName() {
81 new Directory("").createTemp().then((d) { 81 new Directory("").createTemp().then((d) {
82 var errors = 0; 82 var errors = 0;
83 setupListHandlers(DirectoryLister lister) { 83 setupListHandlers(DirectoryLister lister) {
84 lister.onError = (e) { 84 lister.onError = (e) {
85 Expect.isTrue(e is DirectoryIOException); 85 Expect.isTrue(e is DirectoryIOException);
86 if (++errors == 2) { 86 if (++errors == 2) {
87 d.deleteRecursively(); 87 d.delete(recursive: true);
88 } 88 }
89 }; 89 };
90 lister.onFile = (file) { 90 lister.onFile = (file) {
91 Expect.fail("Listing of non-existing directory should fail"); 91 Expect.fail("Listing of non-existing directory should fail");
92 }; 92 };
93 lister.onDir = (dir) { 93 lister.onDir = (dir) {
94 Expect.fail("Listing of non-existing directory should fail"); 94 Expect.fail("Listing of non-existing directory should fail");
95 }; 95 };
96 lister.onDone = (success) { 96 lister.onDone = (success) {
97 Expect.isFalse(success); 97 Expect.isFalse(success);
(...skipping 24 matching lines...) Expand all
122 return true; 122 return true;
123 }); 123 });
124 future.then((ignore) { 124 future.then((ignore) {
125 Expect.fail("Deletion of non-existing directory should fail"); 125 Expect.fail("Deletion of non-existing directory should fail");
126 }); 126 });
127 } 127 }
128 128
129 new Directory("").createTemp().then((d) { 129 new Directory("").createTemp().then((d) {
130 d.delete().then((ignore) { 130 d.delete().then((ignore) {
131 setupFutureHandlers(d.delete()); 131 setupFutureHandlers(d.delete());
132 setupFutureHandlers(d.deleteRecursively()); 132 setupFutureHandlers(d.delete(recursive: true));
133 }); 133 });
134 }); 134 });
135 } 135 }
136 136
137 static void testDeleteTooLongName() { 137 static void testDeleteTooLongName() {
138 var port = new ReceivePort(); 138 var port = new ReceivePort();
139 new Directory("").createTemp().then((d) { 139 new Directory("").createTemp().then((d) {
140 var subDirName = 'subdir'; 140 var subDirName = 'subdir';
141 var subDir = new Directory("${d.path}/$subDirName"); 141 var subDir = new Directory("${d.path}/$subDirName");
142 subDir.create().then((ignore) { 142 subDir.create().then((ignore) {
143 // Construct a long string of the form 143 // Construct a long string of the form
144 // 'tempdir/subdir/../subdir/../subdir'. 144 // 'tempdir/subdir/../subdir/../subdir'.
145 var buffer = new StringBuffer(); 145 var buffer = new StringBuffer();
146 buffer.add(subDir.path); 146 buffer.add(subDir.path);
147 for (var i = 0; i < 1000; i++) { 147 for (var i = 0; i < 1000; i++) {
148 buffer.add("/../${subDirName}"); 148 buffer.add("/../${subDirName}");
149 } 149 }
150 var long = new Directory("${buffer.toString()}"); 150 var long = new Directory("${buffer.toString()}");
151 var errors = 0; 151 var errors = 0;
152 onError(e) { 152 onError(e) {
153 Expect.isTrue(e is DirectoryIOException); 153 Expect.isTrue(e is DirectoryIOException);
154 if (++errors == 2) { 154 if (++errors == 2) {
155 d.deleteRecursively().then((ignore) => port.close()); 155 d.delete(recursive: true).then((ignore) => port.close());
156 } 156 }
157 return true; 157 return true;
158 } 158 }
159 long.delete().handleException(onError); 159 long.delete().handleException(onError);
160 long.deleteRecursively().handleException(onError); 160 long.delete(recursive: true).handleException(onError);
161 }); 161 });
162 }); 162 });
163 } 163 }
164 164
165 static void testDeleteNonExistentSync() { 165 static void testDeleteNonExistentSync() {
166 Directory d = new Directory("").createTempSync(); 166 Directory d = new Directory("").createTempSync();
167 d.deleteSync(); 167 d.deleteSync();
168 Expect.throws(d.deleteSync); 168 Expect.throws(d.deleteSync);
169 Expect.throws(() => d.deleteRecursivelySync()); 169 Expect.throws(() => d.deleteSync(recursive: true));
170 } 170 }
171 171
172 static void testDeleteTooLongNameSync() { 172 static void testDeleteTooLongNameSync() {
173 Directory d = new Directory("").createTempSync(); 173 Directory d = new Directory("").createTempSync();
174 var subDirName = 'subdir'; 174 var subDirName = 'subdir';
175 var subDir = new Directory("${d.path}/$subDirName"); 175 var subDir = new Directory("${d.path}/$subDirName");
176 subDir.createSync(); 176 subDir.createSync();
177 // Construct a long string of the form 177 // Construct a long string of the form
178 // 'tempdir/subdir/../subdir/../subdir'. 178 // 'tempdir/subdir/../subdir/../subdir'.
179 var buffer = new StringBuffer(); 179 var buffer = new StringBuffer();
180 buffer.add(subDir.path); 180 buffer.add(subDir.path);
181 for (var i = 0; i < 1000; i++) { 181 for (var i = 0; i < 1000; i++) {
182 buffer.add("/../${subDirName}"); 182 buffer.add("/../${subDirName}");
183 } 183 }
184 var long = new Directory("${buffer.toString()}"); 184 var long = new Directory("${buffer.toString()}");
185 Expect.throws(long.deleteSync); 185 Expect.throws(long.deleteSync);
186 Expect.throws(() => long.deleteRecursivelySync()); 186 Expect.throws(() => long.deleteSync(recursive: true));
187 d.deleteRecursivelySync(); 187 d.deleteSync(recursive: true);
188 } 188 }
189 189
190 static void testDeleteSymlink() { 190 static void testDeleteSymlink() {
191 // temp/ 191 // temp/
192 // a/ 192 // a/
193 // file.txt 193 // file.txt
194 // b/ 194 // b/
195 // a_link -> a 195 // a_link -> a
196 var d = new Directory("").createTempSync(); 196 var d = new Directory("").createTempSync();
197 var a = new Directory("${d.path}/a"); 197 var a = new Directory("${d.path}/a");
(...skipping 11 matching lines...) Expand all
209 var cmd = "ln"; 209 var cmd = "ln";
210 var args = ['-s', "${d.path}/b/a_link", "${d.path}/a"]; 210 var args = ['-s', "${d.path}/b/a_link", "${d.path}/a"];
211 211
212 if (Platform.operatingSystem == "windows") { 212 if (Platform.operatingSystem == "windows") {
213 cmd = "cmd"; 213 cmd = "cmd";
214 args = ["/c", "mklink", "/j", "${d.path}\\b\\a_link", "${d.path}\\a"]; 214 args = ["/c", "mklink", "/j", "${d.path}\\b\\a_link", "${d.path}\\a"];
215 } 215 }
216 216
217 Process.run(cmd, args).then((_) { 217 Process.run(cmd, args).then((_) {
218 // Delete the directory containing the junction. 218 // Delete the directory containing the junction.
219 b.deleteRecursivelySync(); 219 b.deleteSync(recursive: true);
220 220
221 // We should not have recursed through a_link into a. 221 // We should not have recursed through a_link into a.
222 Expect.isTrue(f.existsSync()); 222 Expect.isTrue(f.existsSync());
223 223
224 // Clean up after ourselves. 224 // Clean up after ourselves.
225 d.deleteRecursivelySync(); 225 d.deleteSync(recursive: true);
226 }); 226 });
227 } 227 }
228 228
229 static void testExistsCreateDelete() { 229 static void testExistsCreateDelete() {
230 new Directory("").createTemp().then((d) { 230 new Directory("").createTemp().then((d) {
231 d.exists().then((bool exists) { 231 d.exists().then((bool exists) {
232 Expect.isTrue(exists); 232 Expect.isTrue(exists);
233 Directory created = new Directory("${d.path}/subdir"); 233 Directory created = new Directory("${d.path}/subdir");
234 created.create().then((ignore) { 234 created.create().then((ignore) {
235 created.exists().then((bool exists) { 235 created.exists().then((bool exists) {
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 testCreateExistingSync() { 463 testCreateExistingSync() {
464 // Test that creating an existing directory succeeds. 464 // Test that creating an existing directory succeeds.
465 var d = new Directory(''); 465 var d = new Directory('');
466 var temp = d.createTempSync(); 466 var temp = d.createTempSync();
467 var subDir = new Directory('${temp.path}/flaf'); 467 var subDir = new Directory('${temp.path}/flaf');
468 Expect.isFalse(subDir.existsSync()); 468 Expect.isFalse(subDir.existsSync());
469 subDir.createSync(); 469 subDir.createSync();
470 Expect.isTrue(subDir.existsSync()); 470 Expect.isTrue(subDir.existsSync());
471 subDir.createSync(); 471 subDir.createSync();
472 Expect.isTrue(subDir.existsSync()); 472 Expect.isTrue(subDir.existsSync());
473 temp.deleteRecursivelySync(); 473 temp.deleteSync(recursive: true);
474 } 474 }
475 475
476 476
477 testCreateExisting() { 477 testCreateExisting() {
478 // Test that creating an existing directory succeeds. 478 // Test that creating an existing directory succeeds.
479 var port = new ReceivePort(); 479 var port = new ReceivePort();
480 var d = new Directory(''); 480 var d = new Directory('');
481 d.createTemp().then((temp) { 481 d.createTemp().then((temp) {
482 var subDir = new Directory('${temp.path}/flaf'); 482 var subDir = new Directory('${temp.path}/flaf');
483 subDir.exists().then((dirExists) { 483 subDir.exists().then((dirExists) {
484 Expect.isFalse(dirExists); 484 Expect.isFalse(dirExists);
485 subDir.create().then((_) { 485 subDir.create().then((_) {
486 subDir.exists().then((dirExists) { 486 subDir.exists().then((dirExists) {
487 Expect.isTrue(dirExists); 487 Expect.isTrue(dirExists);
488 subDir.create().then((_) { 488 subDir.create().then((_) {
489 subDir.exists().then((dirExists) { 489 subDir.exists().then((dirExists) {
490 Expect.isTrue(dirExists); 490 Expect.isTrue(dirExists);
491 temp.deleteRecursively().then((_) { 491 temp.delete(recursive: true).then((_) {
492 port.close(); 492 port.close();
493 }); 493 });
494 }); 494 });
495 }); 495 });
496 }); 496 });
497 }); 497 });
498 }); 498 });
499 }); 499 });
500 } 500 }
501 501
502 502
503 testCreateDirExistingFileSync() { 503 testCreateDirExistingFileSync() {
504 // Test that creating an existing directory succeeds. 504 // Test that creating an existing directory succeeds.
505 var d = new Directory(''); 505 var d = new Directory('');
506 var temp = d.createTempSync(); 506 var temp = d.createTempSync();
507 var path = '${temp.path}/flaf'; 507 var path = '${temp.path}/flaf';
508 var file = new File(path); 508 var file = new File(path);
509 file.createSync(); 509 file.createSync();
510 Expect.isTrue(file.existsSync()); 510 Expect.isTrue(file.existsSync());
511 Expect.throws(new Directory(path).createSync, 511 Expect.throws(new Directory(path).createSync,
512 (e) => e is DirectoryIOException); 512 (e) => e is DirectoryIOException);
513 temp.deleteRecursivelySync(); 513 temp.deleteSync(recursive: true);
514 } 514 }
515 515
516 516
517 testCreateDirExistingFile() { 517 testCreateDirExistingFile() {
518 // Test that creating an existing directory succeeds. 518 // Test that creating an existing directory succeeds.
519 var port = new ReceivePort(); 519 var port = new ReceivePort();
520 var d = new Directory(''); 520 var d = new Directory('');
521 d.createTemp().then((temp) { 521 d.createTemp().then((temp) {
522 var path = '${temp.path}/flaf'; 522 var path = '${temp.path}/flaf';
523 var file = new File(path); 523 var file = new File(path);
524 var subDir = new Directory(path); 524 var subDir = new Directory(path);
525 file.create().then((_) { 525 file.create().then((_) {
526 subDir.create() 526 subDir.create()
527 ..then((_) { Expect.fail("dir create should fail on existing file"); }) 527 ..then((_) { Expect.fail("dir create should fail on existing file"); })
528 ..handleException((e) { 528 ..handleException((e) {
529 Expect.isTrue(e is DirectoryIOException); 529 Expect.isTrue(e is DirectoryIOException);
530 temp.deleteRecursively().then((_) { 530 temp.delete(recursive: true).then((_) {
531 port.close(); 531 port.close();
532 }); 532 });
533 return true; 533 return true;
534 }); 534 });
535 }); 535 });
536 }); 536 });
537 } 537 }
538 538
539 539
540 testCreateRecursiveSync() {
541 var temp = new Directory('').createTempSync();
542 var d = new Directory('${temp.path}/a/b/c');
543 d.createSync(recursive: true);
544 Expect.isTrue(new Directory('${temp.path}/a').existsSync());
545 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync());
546 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync());
547 temp.deleteSync(recursive: true);
548 }
549
550
551 testCreateRecursive() {
552 var port = new ReceivePort();
553 new Directory('').createTemp().then((temp) {
554 var d = new Directory('${temp.path}/a/b/c');
555 d.create(recursive: true).then((_) {
556 Expect.isTrue(new Directory('${temp.path}/a').existsSync());
557 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync());
558 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync());
559 temp.deleteSync(recursive: true);
560 port.close();
561 });
562 });
563 }
564
565
540 testRename() { 566 testRename() {
541 var d = new Directory(''); 567 var d = new Directory('');
542 var temp1 = d.createTempSync(); 568 var temp1 = d.createTempSync();
543 var temp2 = d.createTempSync(); 569 var temp2 = d.createTempSync();
544 var temp3 = temp1.renameSync(temp2.path); 570 var temp3 = temp1.renameSync(temp2.path);
545 Expect.isFalse(temp1.existsSync()); 571 Expect.isFalse(temp1.existsSync());
546 Expect.isTrue(temp2.existsSync()); 572 Expect.isTrue(temp2.existsSync());
547 Expect.equals(temp3.path, temp2.path); 573 Expect.equals(temp3.path, temp2.path);
548 574
549 temp2.rename(temp1.path).then((temp4) { 575 temp2.rename(temp1.path).then((temp4) {
550 Expect.isFalse(temp3.existsSync()); 576 Expect.isFalse(temp3.existsSync());
551 Expect.isFalse(temp2.existsSync()); 577 Expect.isFalse(temp2.existsSync());
552 Expect.isTrue(temp1.existsSync()); 578 Expect.isTrue(temp1.existsSync());
553 Expect.isTrue(temp4.existsSync()); 579 Expect.isTrue(temp4.existsSync());
554 Expect.equals(temp1.path, temp4.path); 580 Expect.equals(temp1.path, temp4.path);
555 temp1.deleteRecursivelySync(); 581 temp1.deleteSync(recursive: true);
556 }); 582 });
557 } 583 }
558 584
559 main() { 585 main() {
560 DirectoryTest.testMain(); 586 DirectoryTest.testMain();
561 NestedTempDirectoryTest.testMain(); 587 NestedTempDirectoryTest.testMain();
562 testCreateTempErrorSync(); 588 testCreateTempErrorSync();
563 testCreateTempError(); 589 testCreateTempError();
564 testCreateExistingSync(); 590 testCreateExistingSync();
565 testCreateExisting(); 591 testCreateExisting();
566 testCreateDirExistingFileSync(); 592 testCreateDirExistingFileSync();
567 testCreateDirExistingFile(); 593 testCreateDirExistingFile();
594 testCreateRecursive();
595 testCreateRecursiveSync();
568 testRename(); 596 testRename();
569 } 597 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698