Files
files-hash/tests/utils.js

54 lines
1.1 KiB
JavaScript

import fs from 'fs/promises';
import path from 'path';
import os from 'os';
/**
* 创建一个临时测试目录
*
* @returns {Promise<string>} 临时目录路径
*/
export async function createTestDir() {
return await fs.mkdtemp(path.join(os.tmpdir(), 'files-hash-test-'));
}
/**
* 创建一个测试文件
*
* @param {string} filePath - 要创建的文件路径
* @param {string} content - 文件内容
*/
export async function createTestFile(filePath, content) {
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, content);
}
/**
* 清理测试文件
*
* @param {string[]} files - 要清理的文件路径数组
*/
export async function cleanupTestFiles(files) {
for (const file of files) {
try {
await fs.unlink(file);
} catch (error) {
// 忽略文件不存在的错误
}
}
}
/**
* 清理测试目录
*
* @param {string[]} dirs - 要清理的目录路径数组
*/
export async function cleanupTestDirs(dirs) {
for (const dir of dirs) {
try {
await fs.rmdir(dir, { recursive: true });
} catch (error) {
// 忽略目录不存在的错误
}
}
}