|
19 | 19 | * |
20 | 20 | */ |
21 | 21 |
|
22 | | -/*global describe, it, expect, beforeEach, afterEach, awaits, awaitsFor, awaitsForDone, jasmine, spyOn */ |
| 22 | +/*global describe, it, expect, beforeEach, afterEach, awaits, awaitsFor, awaitsForDone, jasmine, spyOn, jsPromise */ |
23 | 23 |
|
24 | 24 | define(function (require, exports, module) { |
25 | 25 |
|
26 | 26 |
|
27 | 27 | var Directory = require("filesystem/Directory"), |
28 | 28 | File = require("filesystem/File"), |
29 | 29 | FileSystem = require("filesystem/FileSystem"), |
| 30 | + FileUtils = require("file/FileUtils"), |
30 | 31 | FileSystemStats = require("filesystem/FileSystemStats"), |
31 | 32 | FileSystemError = require("filesystem/FileSystemError"), |
32 | 33 | MockFileSystemImpl = require("./MockFileSystemImpl"), |
@@ -569,6 +570,84 @@ define(function (require, exports, module) { |
569 | 570 | }); |
570 | 571 | }); |
571 | 572 |
|
| 573 | + describe("unlinkEmptyDirectoryAsync tests", function () { |
| 574 | + it("should unlinkEmptyDirectoryAsync delete self if empty", async function () { |
| 575 | + let directory = fileSystem.getDirectoryForPath("/subdir2/"); |
| 576 | + await directory.createAsync(); |
| 577 | + let stat = await directory.statAsync(); |
| 578 | + expect(stat.isDirectory).toBeTrue(); |
| 579 | + |
| 580 | + await directory.unlinkEmptyDirectoryAsync(); |
| 581 | + let isExists = await directory.existsAsync(); |
| 582 | + expect(isExists).toBeFalse(); |
| 583 | + }); |
| 584 | + |
| 585 | + it("should unlinkEmptyDirectoryAsync reject if directory doesnt exist", async function () { |
| 586 | + let directory = fileSystem.getDirectoryForPath("/subdir2/"); |
| 587 | + let isExists = await directory.existsAsync(); |
| 588 | + expect(isExists).toBeFalse(); |
| 589 | + |
| 590 | + let err; |
| 591 | + try{ |
| 592 | + await directory.unlinkEmptyDirectoryAsync(); |
| 593 | + } catch (e) { |
| 594 | + err = e; |
| 595 | + } |
| 596 | + expect(err).toBe(FileSystemError.NOT_FOUND); |
| 597 | + }); |
| 598 | + |
| 599 | + async function createDirTree(dirList, fileList) { |
| 600 | + for(let dir of dirList){ |
| 601 | + let directory = fileSystem.getDirectoryForPath(dir); |
| 602 | + await directory.createAsync(); |
| 603 | + } |
| 604 | + for(let file of fileList){ |
| 605 | + let fileEntry = fileSystem.getFileForPath(file); |
| 606 | + await jsPromise(FileUtils.writeText(fileEntry, "hello", true)); |
| 607 | + } |
| 608 | + } |
| 609 | + async function verifyExists(deletedPaths, shouldExist, isFile = false) { |
| 610 | + for(let deletedPath of deletedPaths){ |
| 611 | + let entry = isFile ? fileSystem.getFileForPath(deletedPath) |
| 612 | + :fileSystem.getDirectoryForPath(deletedPath); |
| 613 | + let isExists = await entry.existsAsync(); |
| 614 | + expect(deletedPath + ":" + isExists).toBe(deletedPath + ":" + shouldExist); |
| 615 | + } |
| 616 | + } |
| 617 | + it("should unlinkEmptyDirectoryAsync delete subtree with no files", async function () { |
| 618 | + await createDirTree(["/subdir2/", "/subdir2/s3/", "/subdir2/s4/", "/subdir2/s3/s33/"], []); |
| 619 | + |
| 620 | + let directory = fileSystem.getDirectoryForPath("/subdir2/"); |
| 621 | + await directory.unlinkEmptyDirectoryAsync(); |
| 622 | + let isExists = await directory.existsAsync(); |
| 623 | + expect(isExists).toBeFalse(); |
| 624 | + }); |
| 625 | + |
| 626 | + it("should unlinkEmptyDirectoryAsync retain subtree with files", async function () { |
| 627 | + await createDirTree(["/subdir2/", "/subdir2/s3/", "/subdir2/s4/", "/subdir2/s3/s33/"], |
| 628 | + ["/subdir2/1.txt"]); |
| 629 | + |
| 630 | + let directory = fileSystem.getDirectoryForPath("/subdir2/"); |
| 631 | + await directory.unlinkEmptyDirectoryAsync(); |
| 632 | + |
| 633 | + await verifyExists(["/subdir2/s3/", "/subdir2/s4/", "/subdir2/s3/s33/"], false); |
| 634 | + await verifyExists(["/subdir2/"], true); |
| 635 | + await verifyExists(["/subdir2/1.txt"], true, true); |
| 636 | + }); |
| 637 | + |
| 638 | + it("should unlinkEmptyDirectoryAsync retain subtree with files 2", async function () { |
| 639 | + await createDirTree(["/subdir2/", "/subdir2/s3/", "/subdir2/s3/s33/", "/subdir2/s3/s33/s333/"], |
| 640 | + ["/subdir2/s3/s33/1.txt"]); |
| 641 | + |
| 642 | + let directory = fileSystem.getDirectoryForPath("/subdir2/s3/"); |
| 643 | + await directory.unlinkEmptyDirectoryAsync(); |
| 644 | + |
| 645 | + await verifyExists(["/subdir2/s3/s33/s333/"], false); |
| 646 | + await verifyExists(["/subdir2/", "/subdir2/s3/", "/subdir2/s3/s33/"], true); |
| 647 | + await verifyExists(["/subdir2/s3/s33/1.txt"], true, true); |
| 648 | + }); |
| 649 | + }); |
| 650 | + |
572 | 651 | describe("Get Free Path", function () { |
573 | 652 | it("should return suggested path as is if it doesnt exist", async function () { |
574 | 653 | let cbCalled = false, |
|
0 commit comments