Skip to content

Commit

Permalink
🐛 fix(test): Remove test coverage 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
kwooshung committed Jan 15, 2024
1 parent da18586 commit 32501c4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/makeDir/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ describe('@/await makeDir', () => {
error.code = 'EEXIST'; // 一个典型的 EEXIST 错误代码
vi.spyOn(fs, 'mkdir').mockRejectedValueOnce(error);
const dirPath = path.join(testDirRoot, 'existentDir');
await expect(makeDir(dirPath)).toBeTruthy();
expect(await makeDir(dirPath)).toBeTruthy();
});

it('遇到非 EEXIST 错误代码时应当抛出异常', async () => {
const error = new Error('Some error') as NodeJS.ErrnoException;
error.code = 'ENOENT'; // 一个典型的非 EEXIST 错误代码
vi.spyOn(fs, 'mkdir').mockRejectedValueOnce(error);
const dirPath = path.join(testDirRoot, 'nonexistentDir');
await expect(makeDir(dirPath)).rejects.toThrow('Some error');
expect(makeDir(dirPath)).rejects.toThrow('Some error');
});
});
96 changes: 96 additions & 0 deletions src/remove/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { join } from 'path';
import fs from 'fs/promises';
import exists from '@/exists';
import write from '@/write';
import createDir from '@/makeDir';
import remove from '.';

describe('@/remove', () => {
const testDirRoot = 'testRemoveDir';

// 创建测试目录和文件的辅助函数
const setupTestDir = async (subDirs: string[], files: string[]) => {
await createDir(testDirRoot);
for (const dir of subDirs) {
await createDir(join(testDirRoot, dir));
}
for (const file of files) {
await write(join(testDirRoot, file), 'test data');
}
};

afterAll(async () => {
await remove(testDirRoot, true);
});

it('成功删除一个空目录', async () => {
await createDir(testDirRoot);
const result = await remove(testDirRoot);
expect(result).toBeTruthy();
expect(await exists(testDirRoot)).toBeFalsy();
});

it('成功删除一个含有文件的目录', async () => {
await setupTestDir([], ['test.txt']);
const result = await remove(testDirRoot);
expect(result).toBeTruthy();
expect(await exists(testDirRoot)).toBeFalsy();
});

it('成功删除多个文件的目录', async () => {
await setupTestDir([], ['test1.txt', 'test2.txt']);
const result = await remove(testDirRoot);
expect(result).toBeTruthy();
expect(await exists(testDirRoot)).toBeFalsy();
});

it('成功删除多个目录', async () => {
await setupTestDir([], ['one/aaa.txt', 'two/bbb.txt']);
const result = await remove([join(testDirRoot, 'one'), join(testDirRoot, 'two')]);
expect(result).toBeTruthy();
expect(await exists(testDirRoot)).toBeTruthy();
});

it('成功删除一个含有子目录和文件的目录', async () => {
await setupTestDir(['subDir'], ['test.txt', 'subDir/subTest.txt']);
const result = await remove(testDirRoot, true);
expect(result).toBeTruthy();
expect(await exists(testDirRoot)).toBeFalsy();
});

it('删除不存在的文件或目录', async () => {
const result = await remove('nonExistentPath');
expect(result).toBeTruthy();
});

it('不删除子目录的情况', async () => {
await setupTestDir(['subDir'], ['test.txt', 'subDir/subTest.txt']);
expect(await remove(testDirRoot, false)).toBeTruthy();
});

it('删除包含多层子目录的目录', async () => {
await setupTestDir(['subDir1', 'subDir1/subDir2'], ['subDir1/subDir2/test.txt']);
const result = await remove(testDirRoot, true);
expect(result).toBeTruthy();
expect(await exists(testDirRoot)).toBeFalsy();
});

it('处理无效路径的异常', async () => {
const invalidPath = 'path/does/not/exist';
expect(await remove(invalidPath)).toBeTruthy();
});

it('尝试触发异常,抛出标准错误', async () => {
await setupTestDir([], ['error1.txt']);
const error = new Error();
vi.spyOn(fs, 'unlink').mockRejectedValueOnce(error);

expect(remove(testDirRoot)).rejects.toThrow();
});

it('尝试触发异常,非标准错误,直接返回false', async () => {
await setupTestDir([], ['error2.txt']);
vi.spyOn(fs, 'unlink').mockRejectedValueOnce({ abc: 123 });
expect(await remove(testDirRoot)).toBeFalsy();
});
});
39 changes: 23 additions & 16 deletions src/remove/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ import exists from '@/exists';
* @param {boolean} includeSubDirs 是否包含子目录 (whether to include subdirectories)
*/
const deleteItem = async (itemPath: string, includeSubDirs: boolean): Promise<void> => {
const stat = await fs.stat(itemPath);
const stack = [itemPath];

if (stat.isDirectory()) {
const files = await fs.readdir(itemPath);
while (stack.length > 0) {
const currentPath = stack[stack.length - 1];
const stat = await fs.stat(currentPath);

if (files.length && includeSubDirs) {
// 处理所有子目录和文件
for (const file of files) {
const curPath = join(itemPath, file);
await deleteItem(curPath, includeSubDirs);
}
}
if (stat.isDirectory()) {
const files = await fs.readdir(currentPath);

// 检查目录是否为空,若为空则删除
const updatedFiles = await fs.readdir(itemPath);
if (updatedFiles.length === 0) {
await fs.rmdir(itemPath);
if (files.length > 0) {
if (includeSubDirs) {
// 将子目录和文件添加到栈中
files.forEach((file) => stack.push(join(currentPath, file)));
} else {
// 不包含子目录,跳过非空目录
stack.pop();
}
} else {
// 删除空目录并从栈中移除
await fs.rmdir(currentPath);
stack.pop();
}
} else {
// 删除文件并从栈中移除
await fs.unlink(currentPath);
stack.pop();
}
} else {
await fs.unlink(itemPath);
}
};

Expand Down

0 comments on commit 32501c4

Please sign in to comment.