Skip to content

Commit

Permalink
Merge pull request #489 from snyk/fix/IM-132-upgrade-rimraf
Browse files Browse the repository at this point in the history
fix: rimraf upgrade
  • Loading branch information
novalex authored Jun 26, 2024
2 parents 32a27c1 + 50a8358 commit 02ce4be
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions src/lib/delete-directory.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
try {
fs.rmSync(dir, { recursive: true, force: true, maxRetries: 3 });
} catch (e) {
await new Promise<void>((resolve, reject) =>
rmrf(dir, (err) => (err ? reject(err) : resolve())),
);
await rmrf(dir);
}
}
39 changes: 32 additions & 7 deletions test/lib/delete-directory.test.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});

0 comments on commit 02ce4be

Please sign in to comment.