Skip to content

Commit

Permalink
fix: Error: ENOTEMPTY: directory not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Sep 4, 2023
1 parent b3adedf commit 63ed271
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ async function _emptyDir(
const currentPath = join(parent, item.name);

if (item.isDirectory()) {
return _emptyDir(fullPath, currentPath, options).then(files => {
if (!files.length) {
return _emptyDir(fullPath, currentPath, options).then(async files => {
results.push(...files);
if (!(await fsPromises.readdir(fullPath)).length) {
return fsPromises.rmdir(fullPath);
}
results.push(...files);
});
}
results.push(currentPath);
Expand Down
72 changes: 71 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ function createDummyFolder(path) {
return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key]));
}

function createAnotherDummyFolder(path) {
const filesMap = {
[join('folder', '.txt')]: 'txt',
[join('folder', '.js')]: 'js',
};
return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key]));
}

describe('fs', () => {
const tmpDir = join(__dirname, 'fs_tmp');

Expand All @@ -49,6 +57,25 @@ describe('fs', () => {
}
});

it('existsSync()', () => {
const exist = fs.existsSync(tmpDir);
exist.should.eql(true);
});

it('existsSync() - path is required', () => {
try {
fs.existsSync();
should.fail();
} catch (err) {
err.message.should.eql('path is required!');
}
});

it('existsSync() - not exist', () => {
const exist = fs.existsSync(join(__dirname, 'fs_tmp1'));
exist.should.eql(false);
});

it('mkdirs()', async () => {
const target = join(tmpDir, 'a', 'b', 'c');

Expand Down Expand Up @@ -414,7 +441,7 @@ describe('fs', () => {
const target = join(tmpDir, 'test');

await createDummyFolder(target);
const files = fs.listDirSync(target, {ignorePattern: /\.js/});
const files = fs.listDirSync(target, { ignorePattern: /\.js/ });
files.should.eql(['e.txt', join('folder', 'h.txt')]);

await fs.rmdir(target);
Expand Down Expand Up @@ -463,6 +490,17 @@ describe('fs', () => {
await fs.unlink(target);
});

it('readFile() - do not escape', async () => {
const target = join(tmpDir, 'test.txt');
const body = 'foo\r\nbar';

await fs.writeFile(target, body);
const result = await fs.readFile(target, { escape: '' });
result.should.eql('foo\r\nbar');

await fs.unlink(target);
});

it('readFileSync()', async () => {
const target = join(tmpDir, 'test.txt');
const body = 'test';
Expand Down Expand Up @@ -505,6 +543,17 @@ describe('fs', () => {
await fs.unlink(target);
});

it('readFileSync() - do not escape', async () => {
const target = join(tmpDir, 'test.txt');
const body = 'foo\r\nbar';

await fs.writeFile(target, body);
const result = fs.readFileSync(target, { escape: '' });
result.should.eql('foo\r\nbar');

await fs.unlink(target);
});

it('unlink()', async () => {
const target = join(tmpDir, 'test-unlink');

Expand Down Expand Up @@ -550,6 +599,27 @@ describe('fs', () => {
await fs.rmdir(target);
});

it('emptyDir() - empty nothing', async () => {
const target = join(tmpDir, 'test');

const checkExistsMap = {
[join('folder', '.txt')]: true,
[join('folder', '.js')]: true,
};

await createAnotherDummyFolder(target);
const files = await fs.emptyDir(target);
files.should.eql([]);

const paths = Object.keys(checkExistsMap);
for (const path of paths) {
const exist = await fs.exists(join(target, path));
exist.should.eql(checkExistsMap[path]);
}

await fs.rmdir(target);
});

it('emptyDir() - path is required', async () => {
try {
await fs.emptyDir();
Expand Down

0 comments on commit 63ed271

Please sign in to comment.