| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // Directory listing test. | |
| 6 | |
| 7 #import("dart:io"); | |
| 8 #import("dart:isolate"); | |
| 9 | |
| 10 class DirectoryTest { | |
| 11 static void testListing() { | |
| 12 bool listedDir = false; | |
| 13 bool listedFile = false; | |
| 14 | |
| 15 Directory directory = new Directory(""); | |
| 16 directory.createTempSync(); | |
| 17 Directory subDirectory = new Directory("${directory.path}/subdir"); | |
| 18 Expect.isFalse(subDirectory.existsSync()); | |
| 19 subDirectory.createSync(); | |
| 20 File f = new File('${subDirectory.path}/file.txt'); | |
| 21 Expect.isFalse(f.existsSync()); | |
| 22 f.createSync(); | |
| 23 | |
| 24 directory.onDir = (dir) { | |
| 25 listedDir = true; | |
| 26 Expect.isTrue(dir.contains(directory.path)); | |
| 27 Expect.isTrue(dir.contains('subdir')); | |
| 28 }; | |
| 29 | |
| 30 directory.onFile = (f) { | |
| 31 listedFile = true; | |
| 32 Expect.isTrue(f.contains(directory.path)); | |
| 33 Expect.isTrue(f.contains('subdir')); | |
| 34 Expect.isTrue(f.contains('file.txt')); | |
| 35 }; | |
| 36 | |
| 37 directory.onDone = (completed) { | |
| 38 Expect.isTrue(completed, "directory listing did not complete"); | |
| 39 Expect.isTrue(listedDir, "directory not found"); | |
| 40 Expect.isTrue(listedFile, "file not found"); | |
| 41 directory.deleteRecursively(() { | |
| 42 f.exists((exists) => Expect.isFalse(exists)); | |
| 43 directory.exists((exists) => Expect.isFalse(exists)); | |
| 44 subDirectory.exists((exists) => Expect.isFalse(exists)); | |
| 45 }); | |
| 46 }; | |
| 47 | |
| 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 | |
| 55 // point. | |
| 56 Expect.isFalse(listedDir); | |
| 57 Expect.isFalse(listedFile); | |
| 58 } | |
| 59 | |
| 60 static void testListNonExistent() { | |
| 61 Directory d = new Directory(""); | |
| 62 d.onError = (e) { | |
| 63 Expect.fail("Directory error: $e"); | |
| 64 }; | |
| 65 d.createTemp(() { | |
| 66 d.delete(() { | |
| 67 // Test that listing a non-existing directory fails. | |
| 68 d.onError = (e) { | |
| 69 Expect.isTrue(e is DirectoryIOException); | |
| 70 }; | |
| 71 d.onFile = (file) { | |
| 72 Expect.fail("Listing of non-existing directory should fail"); | |
| 73 }; | |
| 74 d.onDir = (dir) { | |
| 75 Expect.fail("Listing of non-existing directory should fail"); | |
| 76 }; | |
| 77 d.onDone = (done) { | |
| 78 Expect.isFalse(done); | |
| 79 }; | |
| 80 d.list(); | |
| 81 d.list(recursive: true); | |
| 82 }); | |
| 83 }); | |
| 84 } | |
| 85 | |
| 86 static void testListTooLongName() { | |
| 87 Directory d = new Directory(""); | |
| 88 d.onError = (e) { | |
| 89 Expect.fail("Directory error: $e"); | |
| 90 }; | |
| 91 d.createTemp(() { | |
| 92 var subDirName = 'subdir'; | |
| 93 var subDir = new Directory("${d.path}/$subDirName"); | |
| 94 subDir.onError = (e) { | |
| 95 Expect.fail("Directory error: $e"); | |
| 96 }; | |
| 97 subDir.create(() { | |
| 98 // Construct a long string of the form | |
| 99 // 'tempdir/subdir/../subdir/../subdir'. | |
| 100 var buffer = new StringBuffer(); | |
| 101 buffer.add(subDir.path); | |
| 102 for (var i = 0; i < 1000; i++) { | |
| 103 buffer.add("/../${subDirName}"); | |
| 104 } | |
| 105 var long = new Directory("${buffer.toString()}"); | |
| 106 var errors = 0; | |
| 107 long.onError = (e) { | |
| 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 }); | |
| 125 }); | |
| 126 } | |
| 127 | |
| 128 static void testDeleteNonExistent() { | |
| 129 Directory d = new Directory(""); | |
| 130 d.onError = (e) { | |
| 131 Expect.fail("Directory error: $e"); | |
| 132 }; | |
| 133 d.createTemp(() { | |
| 134 d.delete(() { | |
| 135 // Test that deleting a non-existing directory fails. | |
| 136 d.onError = (e) { | |
| 137 Expect.isTrue(e is DirectoryIOException); | |
| 138 }; | |
| 139 d.delete(() { | |
| 140 Expect.fail("Deletion of non-existing directory should fail"); | |
| 141 }); | |
| 142 d.deleteRecursively(() { | |
| 143 Expect.fail("Deletion of non-existing directory should fail"); | |
| 144 }); | |
| 145 }); | |
| 146 }); | |
| 147 } | |
| 148 | |
| 149 static void testDeleteTooLongName() { | |
| 150 Directory d = new Directory(""); | |
| 151 d.onError = (e) { | |
| 152 Expect.fail("Directory error: $e"); | |
| 153 }; | |
| 154 d.createTemp(() { | |
| 155 var subDirName = 'subdir'; | |
| 156 var subDir = new Directory("${d.path}/$subDirName"); | |
| 157 subDir.onError = (e) { | |
| 158 Expect.fail("Directory error: $e"); | |
| 159 }; | |
| 160 subDir.create(() { | |
| 161 // Construct a long string of the form | |
| 162 // 'tempdir/subdir/../subdir/../subdir'. | |
| 163 var buffer = new StringBuffer(); | |
| 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 } | |
| 175 }; | |
| 176 long.delete(() { | |
| 177 Expect.fail("Deletion of a directory with a long name should fail"); | |
| 178 }); | |
| 179 long.deleteRecursively(() { | |
| 180 Expect.fail("Deletion of a directory with a long name should fail"); | |
| 181 }); | |
| 182 }); | |
| 183 }); | |
| 184 } | |
| 185 | |
| 186 static void testDeleteNonExistentSync() { | |
| 187 Directory d = new Directory(""); | |
| 188 d.createTempSync(); | |
| 189 d.deleteSync(); | |
| 190 Expect.throws(d.deleteSync); | |
| 191 Expect.throws(() => d.deleteRecursivelySync()); | |
| 192 } | |
| 193 | |
| 194 static void testDeleteTooLongNameSync() { | |
| 195 Directory d = new Directory(""); | |
| 196 d.createTempSync(); | |
| 197 var subDirName = 'subdir'; | |
| 198 var subDir = new Directory("${d.path}/$subDirName"); | |
| 199 subDir.createSync(); | |
| 200 // Construct a long string of the form | |
| 201 // 'tempdir/subdir/../subdir/../subdir'. | |
| 202 var buffer = new StringBuffer(); | |
| 203 buffer.add(subDir.path); | |
| 204 for (var i = 0; i < 1000; i++) { | |
| 205 buffer.add("/../${subDirName}"); | |
| 206 } | |
| 207 var long = new Directory("${buffer.toString()}"); | |
| 208 Expect.throws(long.deleteSync); | |
| 209 Expect.throws(() => long.deleteRecursivelySync()); | |
| 210 } | |
| 211 | |
| 212 static void testExistsCreateDelete() { | |
| 213 Directory d = new Directory(""); | |
| 214 d.createTemp(() { | |
| 215 d.exists((bool exists) { | |
| 216 Expect.isTrue(exists); | |
| 217 Directory created = new Directory("${d.path}/subdir"); | |
| 218 created.create(() { | |
| 219 created.exists((bool exists) { | |
| 220 Expect.isTrue(exists); | |
| 221 created.delete(() { | |
| 222 created.exists((bool exists) { | |
| 223 Expect.isFalse(exists); | |
| 224 d.delete(() { | |
| 225 d.exists((bool exists) { | |
| 226 Expect.isFalse(exists); | |
| 227 }); | |
| 228 }); | |
| 229 }); | |
| 230 }); | |
| 231 }); | |
| 232 }); | |
| 233 }); | |
| 234 }); | |
| 235 } | |
| 236 | |
| 237 static void testExistsCreateDeleteSync() { | |
| 238 Directory d = new Directory(""); | |
| 239 d.createTempSync(); | |
| 240 Expect.isTrue(d.existsSync()); | |
| 241 Directory created = new Directory("${d.path}/subdir"); | |
| 242 created.createSync(); | |
| 243 Expect.isTrue(created.existsSync()); | |
| 244 created.deleteSync(); | |
| 245 Expect.isFalse(created.existsSync()); | |
| 246 d.deleteSync(); | |
| 247 Expect.isFalse(d.existsSync()); | |
| 248 } | |
| 249 | |
| 250 static void testCreateTemp() { | |
| 251 Directory tempDir1 = new Directory("/tmp/dart_temp_dir_"); | |
| 252 Directory tempDir2 = new Directory("/tmp/dart_temp_dir_"); | |
| 253 bool stage1aDone = false; | |
| 254 bool stage1bDone = false; | |
| 255 bool emptyTemplateTestRunning = false; | |
| 256 | |
| 257 // Stages 0 through 2 run twice, the second time with an empty path. | |
| 258 Function stage0; | |
| 259 Function stage1a; | |
| 260 Function stage1b; | |
| 261 Function stage2; | |
| 262 Function stage3; // Loops to stage 0. | |
| 263 | |
| 264 Function error(e) { | |
| 265 Expect.fail("Directory onError: $e"); | |
| 266 } | |
| 267 | |
| 268 stage0 = () { | |
| 269 tempDir1.onError = error; | |
| 270 tempDir1.createTemp(stage1a); | |
| 271 tempDir2.onError = error; | |
| 272 tempDir2.createTemp(stage1b); | |
| 273 }; | |
| 274 | |
| 275 stage1a = () { | |
| 276 stage1aDone = true; | |
| 277 Expect.isTrue(tempDir1.existsSync()); | |
| 278 if (stage1bDone) { | |
| 279 stage2(); | |
| 280 } | |
| 281 }; | |
| 282 | |
| 283 stage1b = () { | |
| 284 stage1bDone = true; | |
| 285 Expect.isTrue(tempDir2.existsSync()); | |
| 286 if (stage1aDone) { | |
| 287 stage2(); | |
| 288 } | |
| 289 }; | |
| 290 | |
| 291 stage2 = () { | |
| 292 Expect.notEquals(tempDir1.path, tempDir2.path); | |
| 293 tempDir1.deleteSync(); | |
| 294 tempDir2.deleteSync(); | |
| 295 Expect.isFalse(tempDir1.existsSync()); | |
| 296 Expect.isFalse(tempDir2.existsSync()); | |
| 297 if (!emptyTemplateTestRunning) { | |
| 298 emptyTemplateTestRunning = true; | |
| 299 stage3(); | |
| 300 } else { | |
| 301 // Done with test. | |
| 302 } | |
| 303 }; | |
| 304 | |
| 305 stage3 = () { | |
| 306 tempDir1 = new Directory(""); | |
| 307 tempDir2 = new Directory(""); | |
| 308 stage1aDone = false; | |
| 309 stage1bDone = false; | |
| 310 stage0(); | |
| 311 }; | |
| 312 | |
| 313 if (new Directory("/tmp").existsSync()) { | |
| 314 stage0(); | |
| 315 } else { | |
| 316 emptyTemplateTestRunning = true; | |
| 317 stage3(); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 static void testCreateDeleteTemp() { | |
| 322 Directory tempDirectory = new Directory(""); | |
| 323 tempDirectory.createTemp(() { | |
| 324 String filename = tempDirectory.path + | |
| 325 Platform.pathSeparator() + "dart_testfile"; | |
| 326 File file = new File(filename); | |
| 327 Expect.isFalse(file.existsSync()); | |
| 328 file.onError = (e) { | |
| 329 Expect.fail("testCreateTemp file.onError called: $e"); | |
| 330 }; | |
| 331 file.create(() { | |
| 332 file.exists((bool exists) { | |
| 333 Expect.isTrue(exists); | |
| 334 // Try to delete the directory containing the file - should throw. | |
| 335 Expect.throws(tempDirectory.deleteSync); | |
| 336 Expect.isTrue(tempDirectory.existsSync()); | |
| 337 | |
| 338 // Delete the file, and then delete the directory. | |
| 339 file.delete(() { | |
| 340 tempDirectory.deleteSync(); | |
| 341 Expect.isFalse(tempDirectory.existsSync()); | |
| 342 }); | |
| 343 }); | |
| 344 }); | |
| 345 }); | |
| 346 } | |
| 347 | |
| 348 static void testCurrent() { | |
| 349 Directory current = new Directory.current(); | |
| 350 if (Platform.operatingSystem() != "windows") { | |
| 351 Expect.equals("/", current.path.substring(0, 1)); | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 static void testMain() { | |
| 356 testListing(); | |
| 357 testListNonExistent(); | |
| 358 testListTooLongName(); | |
| 359 testDeleteNonExistent(); | |
| 360 testDeleteTooLongName(); | |
| 361 testDeleteNonExistentSync(); | |
| 362 testDeleteTooLongNameSync(); | |
| 363 testExistsCreateDelete(); | |
| 364 testExistsCreateDeleteSync(); | |
| 365 testCreateTemp(); | |
| 366 testCreateDeleteTemp(); | |
| 367 testCurrent(); | |
| 368 } | |
| 369 } | |
| 370 | |
| 371 | |
| 372 class NestedTempDirectoryTest { | |
| 373 List<Directory> createdDirectories; | |
| 374 Directory current; | |
| 375 | |
| 376 NestedTempDirectoryTest.run() | |
| 377 : createdDirectories = new List<Directory>(), | |
| 378 current = new Directory("") { | |
| 379 current.onError = errorCallback; | |
| 380 current.createTemp(createPhaseCallback); | |
| 381 } | |
| 382 | |
| 383 void errorCallback(e) { | |
| 384 Expect.fail("Error callback called in NestedTempDirectoryTest: $e"); | |
| 385 } | |
| 386 | |
| 387 void createPhaseCallback() { | |
| 388 createdDirectories.add(current); | |
| 389 int nestingDepth = 6; | |
| 390 var os = Platform.operatingSystem(); | |
| 391 if (os == "windows") nestingDepth = 2; | |
| 392 if (createdDirectories.length < nestingDepth) { | |
| 393 current = new Directory( | |
| 394 current.path + "/nested_temp_dir_${createdDirectories.length}_"); | |
| 395 current.onError = errorCallback; | |
| 396 current.createTemp(createPhaseCallback); | |
| 397 } else { | |
| 398 deletePhaseCallback(); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 void deletePhaseCallback() { | |
| 403 if (!createdDirectories.isEmpty()) { | |
| 404 current = createdDirectories.removeLast(); | |
| 405 current.deleteSync(); | |
| 406 deletePhaseCallback(); | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 static void testMain() { | |
| 411 new NestedTempDirectoryTest.run(); | |
| 412 new NestedTempDirectoryTest.run(); | |
| 413 } | |
| 414 } | |
| 415 | |
| 416 | |
| 417 String illegalTempDirectoryLocation() { | |
| 418 // Determine a platform specific illegal location for a temporary directory. | |
| 419 var os = Platform.operatingSystem(); | |
| 420 if (os == "linux" || os == "macos") { | |
| 421 return "/dev/zero/"; | |
| 422 } | |
| 423 if (os == "windows") { | |
| 424 return "*"; | |
| 425 } | |
| 426 return null; | |
| 427 } | |
| 428 | |
| 429 | |
| 430 testCreateTempErrorSync() { | |
| 431 var location = illegalTempDirectoryLocation(); | |
| 432 if (location != null) { | |
| 433 Expect.throws(new Directory(location).createTempSync, | |
| 434 (e) => e is DirectoryIOException); | |
| 435 } | |
| 436 } | |
| 437 | |
| 438 | |
| 439 testCreateTempError() { | |
| 440 var location = illegalTempDirectoryLocation(); | |
| 441 if (location == null) return; | |
| 442 | |
| 443 var resultPort = new ReceivePort(); | |
| 444 resultPort.receive((String message, ignored) { | |
| 445 resultPort.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 } | |
| 453 | |
| 454 | |
| 455 main() { | |
| 456 DirectoryTest.testMain(); | |
| 457 NestedTempDirectoryTest.testMain(); | |
| 458 testCreateTempErrorSync(); | |
| 459 testCreateTempError(); | |
| 460 } | |
| OLD | NEW |