| OLD | NEW |
| 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:async"; |
| 7 import "dart:io"; | 8 import "dart:io"; |
| 8 import "dart:isolate"; | 9 import "dart:isolate"; |
| 9 | 10 |
| 10 class DirectoryTest { | 11 class DirectoryTest { |
| 11 static void testListing() { | 12 static void testListing() { |
| 12 bool listedDir = false; | 13 bool listedDir = false; |
| 13 bool listedFile = false; | 14 bool listedFile = false; |
| 14 | 15 |
| 15 Directory directory = new Directory("").createTempSync(); | 16 Directory directory = new Directory("").createTempSync(); |
| 16 Directory subDirectory = new Directory("${directory.path}/subdir"); | 17 Directory subDirectory = new Directory("${directory.path}/subdir"); |
| 17 Expect.isTrue('$directory'.contains(directory.path)); | 18 Expect.isTrue('$directory'.contains(directory.path)); |
| 18 Expect.isFalse(subDirectory.existsSync()); | 19 Expect.isFalse(subDirectory.existsSync()); |
| 19 subDirectory.createSync(); | 20 subDirectory.createSync(); |
| 20 Expect.isTrue(subDirectory.existsSync()); | 21 Expect.isTrue(subDirectory.existsSync()); |
| 21 File f = new File('${subDirectory.path}/file.txt'); | 22 File f = new File('${subDirectory.path}/file.txt'); |
| 23 File fLong = new File('${directory.path}/subdir/../subdir/file.txt'); |
| 22 Expect.isFalse(f.existsSync()); | 24 Expect.isFalse(f.existsSync()); |
| 23 f.createSync(); | 25 f.createSync(); |
| 24 | 26 |
| 25 void testSyncListing(bool recursive) { | 27 void testSyncListing(bool recursive) { |
| 26 for (var entry in directory.listSync(recursive: recursive)) { | 28 for (var entry in directory.listSync(recursive: recursive)) { |
| 27 if (entry is File) { | 29 if (entry is File) { |
| 28 Expect.isTrue(entry.name.contains(directory.path)); | 30 Expect.isTrue(entry.name.contains(directory.path)); |
| 29 Expect.isTrue(entry.name.contains('subdir')); | 31 Expect.isTrue(entry.name.contains('subdir')); |
| 30 Expect.isTrue(entry.name.contains('file.txt')); | 32 Expect.isTrue(entry.name.contains('file.txt')); |
| 31 Expect.isFalse(listedFile); | 33 Expect.isFalse(listedFile); |
| 32 listedFile = true; | 34 listedFile = true; |
| 33 } else { | 35 } else { |
| 34 Expect.isTrue(entry is Directory); | 36 Expect.isTrue(entry is Directory); |
| 35 Expect.isTrue(entry.path.contains(directory.path)); | 37 Expect.isTrue(entry.path.contains(directory.path)); |
| 36 Expect.isTrue(entry.path.contains('subdir')); | 38 Expect.isTrue(entry.path.contains('subdir')); |
| 37 Expect.isFalse(listedDir); | 39 Expect.isFalse(listedDir); |
| 38 listedDir = true; | 40 listedDir = true; |
| 39 } | 41 } |
| 40 } | 42 } |
| 41 Expect.equals(listedFile, recursive); | 43 Expect.equals(listedFile, recursive); |
| 42 Expect.isTrue(listedDir); | 44 Expect.isTrue(listedDir); |
| 43 listedFile = false; | 45 listedFile = false; |
| 44 listedDir = false; | 46 listedDir = false; |
| 45 } | 47 } |
| 46 | 48 |
| 47 testSyncListing(true); | 49 testSyncListing(true); |
| 48 testSyncListing(false); | 50 testSyncListing(false); |
| 51 Expect.equals(f.fullPathSync(), fLong.fullPathSync()); |
| 49 | 52 |
| 50 var lister = directory.list(recursive: true); | 53 var lister = directory.list(recursive: true); |
| 51 | 54 |
| 52 lister.onDir = (dir) { | 55 lister.onDir = (dir) { |
| 53 listedDir = true; | 56 listedDir = true; |
| 54 Expect.isTrue(dir.contains(directory.path)); | 57 Expect.isTrue(dir.contains(directory.path)); |
| 55 Expect.isTrue(dir.contains('subdir')); | 58 Expect.isTrue(dir.contains('subdir')); |
| 56 }; | 59 }; |
| 57 | 60 |
| 58 lister.onFile = (f) { | 61 lister.onFile = (f) { |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 Expect.isTrue(d2.existsSync()); | 284 Expect.isTrue(d2.existsSync()); |
| 282 Directory created = new Directory("${d.path}/subdir"); | 285 Directory created = new Directory("${d.path}/subdir"); |
| 283 created.createSync(); | 286 created.createSync(); |
| 284 Expect.isTrue(created.existsSync()); | 287 Expect.isTrue(created.existsSync()); |
| 285 created.deleteSync(); | 288 created.deleteSync(); |
| 286 Expect.isFalse(created.existsSync()); | 289 Expect.isFalse(created.existsSync()); |
| 287 d.deleteSync(); | 290 d.deleteSync(); |
| 288 Expect.isFalse(d.existsSync()); | 291 Expect.isFalse(d.existsSync()); |
| 289 } | 292 } |
| 290 | 293 |
| 291 static void testCreateTemp() { | 294 static void testCreateTemp([String template = ""]) { |
| 292 Directory tempDir1; | 295 var port = new ReceivePort(); |
| 293 Directory tempDir2; | 296 Directory dir = new Directory(template); |
| 294 bool stage1aDone = false; | 297 Future.wait([dir.createTemp(), dir.createTemp()]) |
| 295 bool stage1bDone = false; | 298 .then((tempDirs) { |
| 296 bool emptyTemplateTestRunning = false; | 299 Expect.notEquals(tempDirs[0].path, tempDirs[1].path); |
| 300 for (Directory t in tempDirs) { |
| 301 Expect.isTrue(t.existsSync()); |
| 302 t.deleteSync(); |
| 303 Expect.isFalse(t.existsSync()); |
| 304 } |
| 305 port.close(); |
| 306 }); |
| 307 } |
| 297 | 308 |
| 298 // Stages 0 through 2 run twice, the second time with an empty path. | 309 static void testCreateTempTemplate() { |
| 299 Function stage0; | |
| 300 Function stage1a; | |
| 301 Function stage1b; | |
| 302 Function stage2; | |
| 303 Function stage3; // Loops to stage 0. | |
| 304 | |
| 305 stage0 = () { | |
| 306 var dir = new Directory("/tmp/dart_temp_dir_"); | |
| 307 dir.createTemp().then(stage1a); | |
| 308 dir.createTemp().then(stage1b); | |
| 309 }; | |
| 310 | |
| 311 stage1a = (temp) { | |
| 312 tempDir1 = temp; | |
| 313 stage1aDone = true; | |
| 314 Expect.isTrue(tempDir1.existsSync()); | |
| 315 if (stage1bDone) { | |
| 316 stage2(); | |
| 317 } | |
| 318 }; | |
| 319 | |
| 320 stage1b = (temp) { | |
| 321 tempDir2 = temp; | |
| 322 stage1bDone = true; | |
| 323 Expect.isTrue(tempDir2.existsSync()); | |
| 324 if (stage1aDone) { | |
| 325 stage2(); | |
| 326 } | |
| 327 }; | |
| 328 | |
| 329 stage2 = () { | |
| 330 Expect.notEquals(tempDir1.path, tempDir2.path); | |
| 331 tempDir1.deleteSync(); | |
| 332 tempDir2.deleteSync(); | |
| 333 Expect.isFalse(tempDir1.existsSync()); | |
| 334 Expect.isFalse(tempDir2.existsSync()); | |
| 335 if (!emptyTemplateTestRunning) { | |
| 336 emptyTemplateTestRunning = true; | |
| 337 stage3(); | |
| 338 } else { | |
| 339 // Done with test. | |
| 340 } | |
| 341 }; | |
| 342 | |
| 343 stage3 = () { | |
| 344 tempDir1 = new Directory(""); | |
| 345 tempDir2 = new Directory(""); | |
| 346 stage1aDone = false; | |
| 347 stage1bDone = false; | |
| 348 stage0(); | |
| 349 }; | |
| 350 | |
| 351 if (new Directory("/tmp").existsSync()) { | 310 if (new Directory("/tmp").existsSync()) { |
| 352 stage0(); | 311 testCreateTemp("/tmp/dart_temp_dir_"); |
| 353 } else { | |
| 354 emptyTemplateTestRunning = true; | |
| 355 stage3(); | |
| 356 } | 312 } |
| 357 } | 313 } |
| 358 | 314 |
| 359 static void testCreateDeleteTemp() { | 315 static void testCreateDeleteTemp() { |
| 360 new Directory("").createTemp().then((tempDirectory) { | 316 new Directory("").createTemp().then((tempDirectory) { |
| 361 String filename = | 317 String filename = |
| 362 "${tempDirectory.path}${Platform.pathSeparator}dart_testfile"; | 318 "${tempDirectory.path}${Platform.pathSeparator}dart_testfile"; |
| 363 File file = new File(filename); | 319 File file = new File(filename); |
| 364 Expect.isFalse(file.existsSync()); | 320 Expect.isFalse(file.existsSync()); |
| 365 file.create().then((ignore) { | 321 file.create().then((ignore) { |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 testCreateTempErrorSync(); | 567 testCreateTempErrorSync(); |
| 612 testCreateTempError(); | 568 testCreateTempError(); |
| 613 testCreateExistingSync(); | 569 testCreateExistingSync(); |
| 614 testCreateExisting(); | 570 testCreateExisting(); |
| 615 testCreateDirExistingFileSync(); | 571 testCreateDirExistingFileSync(); |
| 616 testCreateDirExistingFile(); | 572 testCreateDirExistingFile(); |
| 617 testCreateRecursive(); | 573 testCreateRecursive(); |
| 618 testCreateRecursiveSync(); | 574 testCreateRecursiveSync(); |
| 619 testRename(); | 575 testRename(); |
| 620 } | 576 } |
| OLD | NEW |