Skip to content

Commit f829cc9

Browse files
xianjianlf2冼健聪
andauthored
fix: propagate emptyDir directory read errors (#1080)
Co-authored-by: 冼健聪 <[email protected]>
1 parent 53a8d1a commit f829cc9

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎lib/empty/__tests__/empty-dir-sync.test.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const os = require('os')
55
const fse = require('../..')
66
const path = require('path')
77
const assert = require('assert')
8+
const proxyquire = require('proxyquire')
89

910
/* global afterEach, beforeEach, describe, it */
1011

@@ -21,6 +22,17 @@ describe('+ emptyDir()', () => {
2122

2223
afterEach(done => fse.remove(TEST_DIR, done))
2324

25+
for (const code of ['EACCES', 'EIO']) {
26+
it(`should throw the original ${code} readdir error`, () => {
27+
const error = Object.assign(new Error('readdir failed'), { code })
28+
const { emptyDirSync } = proxyquire('..', {
29+
'../fs': { readdirSync: () => { throw error } }
30+
})
31+
32+
assert.throws(() => emptyDirSync(TEST_DIR), err => err === error)
33+
})
34+
}
35+
2436
describe('> when directory exists and contains items', () => {
2537
it('should delete all of the items', () => {
2638
// verify nothing

‎lib/empty/__tests__/empty-dir.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const os = require('os')
55
const fse = require('../..')
66
const path = require('path')
77
const assert = require('assert')
8+
const proxyquire = require('proxyquire')
89

910
/* global afterEach, beforeEach, describe, it */
1011

@@ -21,6 +22,28 @@ describe('+ emptyDir()', () => {
2122

2223
afterEach(done => fse.remove(TEST_DIR, done))
2324

25+
for (const code of ['EACCES', 'EIO']) {
26+
it(`should reject with the original ${code} readdir error`, async () => {
27+
const error = Object.assign(new Error('readdir failed'), { code })
28+
const { emptyDir } = proxyquire('..', {
29+
'../fs': { readdir: async () => { throw error } }
30+
})
31+
32+
await assert.rejects(emptyDir(TEST_DIR), err => err === error)
33+
})
34+
}
35+
36+
it('should pass readdir errors to the callback', done => {
37+
const error = Object.assign(new Error('permission denied'), { code: 'EACCES' })
38+
const { emptyDir } = proxyquire('..', {
39+
'../fs': { readdir: async () => { throw error } }
40+
})
41+
42+
emptyDir(TEST_DIR, err => {
43+
done(err === error ? undefined : new Error('original readdir error was not forwarded'))
44+
})
45+
})
46+
2447
describe('> when directory exists and contains items', () => {
2548
it('should delete all of the items', done => {
2649
// verify nothing

‎lib/empty/index.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const emptyDir = u(async function emptyDir (dir) {
1010
let items
1111
try {
1212
items = await fs.readdir(dir)
13-
} catch {
13+
} catch (err) {
14+
if (err.code !== 'ENOENT') throw err
1415
return mkdir.mkdirs(dir)
1516
}
1617

@@ -21,7 +22,8 @@ function emptyDirSync (dir) {
2122
let items
2223
try {
2324
items = fs.readdirSync(dir)
24-
} catch {
25+
} catch (err) {
26+
if (err.code !== 'ENOENT') throw err
2527
return mkdir.mkdirsSync(dir)
2628
}
2729

0 commit comments

Comments
 (0)