From 50a83588b4eedda9f4fbd19aef01510b28dc96e4 Mon Sep 17 00:00:00 2001 From: Alex Nitu Date: Wed, 26 Jun 2024 13:42:06 +0300 Subject: [PATCH] fix: rimraf upgrade Additional directory deletion test case. --- package.json | 3 +-- src/lib/delete-directory.ts | 6 ++--- test/lib/delete-directory.test.ts | 39 +++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 798236a6..67e09091 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "needle": "2.9.1", "p-map": "4.0.0", "parse-link-header": "2.0.0", - "rimraf": "3.0.2", + "rimraf": "^5.0.7", "simple-git": "3.16.0", "sleep-promise": "8.0.1", "snyk-request-manager": "1.8.4", @@ -74,7 +74,6 @@ "@types/needle": "2.0.4", "@types/node": "^20.11.1", "@types/parse-link-header": "1.0.0", - "@types/rimraf": "3.0.2", "@types/split": "1.0.0", "@typescript-eslint/eslint-plugin": "4.28.1", "@typescript-eslint/parser": "4.28.1", diff --git a/src/lib/delete-directory.ts b/src/lib/delete-directory.ts index cd8e86b1..eb67a875 100644 --- a/src/lib/delete-directory.ts +++ b/src/lib/delete-directory.ts @@ -1,12 +1,10 @@ -import * as rmrf from 'rimraf'; +import { rimraf as rmrf } from 'rimraf'; import * as fs from 'fs'; export async function deleteDirectory(dir: string): Promise { try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch (e) { - await new Promise((resolve, reject) => - rmrf(dir, (err) => (err ? reject(err) : resolve())), - ); + await rmrf(dir); } } diff --git a/test/lib/delete-directory.test.ts b/test/lib/delete-directory.test.ts index bfb4e3fe..b41f13e3 100644 --- a/test/lib/delete-directory.test.ts +++ b/test/lib/delete-directory.test.ts @@ -1,15 +1,40 @@ -import { promises as fs } from 'fs'; +import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import { deleteDirectory } from '../../src/lib/delete-directory'; -test('directory is deleted with contents', async () => { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'deletion-test-')); - await fs.writeFile(path.join(dir, 'root.txt'), ''); +describe('directory deletion', () => { + afterAll(() => { + jest.restoreAllMocks(); + }); - await deleteDirectory(dir); + test('directory is deleted with contents via Node `fs`', async () => { + const dir = await fs.promises.mkdtemp( + path.join(os.tmpdir(), 'fs-deletion-test-'), + ); + await fs.promises.writeFile(path.join(dir, 'root.txt'), ''); - console.log(dir); + await deleteDirectory(dir); - await expect(fs.stat(dir)).rejects.toThrowError('no such file or directory'); + await expect(fs.promises.stat(dir)).rejects.toThrowError( + 'no such file or directory', + ); + }); + + test('directory is deleted with contents via `rimraf`', async () => { + jest.spyOn(fs, 'rmSync').mockImplementation(() => { + throw new Error('error'); + }); + + const dir = await fs.promises.mkdtemp( + path.join(os.tmpdir(), 'rimraf-deletion-test-'), + ); + await fs.promises.writeFile(path.join(dir, 'root.txt'), ''); + + await deleteDirectory(dir); + + await expect(fs.promises.stat(dir)).rejects.toThrowError( + 'no such file or directory', + ); + }); });