-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional directory deletion test case.
- Loading branch information
Showing
3 changed files
with
35 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
}); | ||
}); |