test(cleanup): fix temporary directory leaks in test suites (#26217)

This commit is contained in:
Adib234
2026-05-04 15:08:02 -04:00
committed by GitHub
parent a7beb890d0
commit 75a8de83fc
4 changed files with 50 additions and 11 deletions
@@ -93,6 +93,25 @@ export async function createTmpDir(
* Cleans up (deletes) a temporary directory and its contents.
* @param dir The absolute path to the temporary directory to clean up.
*/
export async function cleanupTmpDir(dir: string) {
await fs.rm(dir, { recursive: true, force: true });
export async function cleanupTmpDir(dir: string | undefined) {
if (!dir) {
return;
}
try {
const exists = await fs
.access(dir)
.then(() => true)
.catch(() => false);
if (exists) {
if (process.platform === 'win32') {
// Give Windows a moment to release file handles
await new Promise((resolve) => setTimeout(resolve, 100));
}
await fs.rm(dir, { recursive: true, force: true });
}
} catch {
// Ignore errors during cleanup (e.g., directory already deleted)
}
}