From 041f41d3366748021302362fa685087c33d273dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=BC=E5=81=A5=E8=81=AA?= Date: Thu, 10 Sep 2026 11:35:48 +0800 Subject: [PATCH] fix: propagate emptyDir directory read errors --- lib/empty/__tests__/empty-dir-sync.test.js | 12 +++++++++++ lib/empty/__tests__/empty-dir.test.js | 23 ++++++++++++++++++++++ lib/empty/index.js | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/empty/__tests__/empty-dir-sync.test.js b/lib/empty/__tests__/empty-dir-sync.test.js index 88387b3c..43932775 100644 --- a/lib/empty/__tests__/empty-dir-sync.test.js +++ b/lib/empty/__tests__/empty-dir-sync.test.js @@ -5,6 +5,7 @@ const os = require('os') const fse = require('../..') const path = require('path') const assert = require('assert') +const proxyquire = require('proxyquire') /* global afterEach, beforeEach, describe, it */ @@ -21,6 +22,17 @@ describe('+ emptyDir()', () => { afterEach(done => fse.remove(TEST_DIR, done)) + for (const code of ['EACCES', 'EIO']) { + it(`should throw the original ${code} readdir error`, () => { + const error = Object.assign(new Error('readdir failed'), { code }) + const { emptyDirSync } = proxyquire('..', { + '../fs': { readdirSync: () => { throw error } } + }) + + assert.throws(() => emptyDirSync(TEST_DIR), err => err === error) + }) + } + describe('> when directory exists and contains items', () => { it('should delete all of the items', () => { // verify nothing diff --git a/lib/empty/__tests__/empty-dir.test.js b/lib/empty/__tests__/empty-dir.test.js index f7dcf28f..16c3d719 100644 --- a/lib/empty/__tests__/empty-dir.test.js +++ b/lib/empty/__tests__/empty-dir.test.js @@ -5,6 +5,7 @@ const os = require('os') const fse = require('../..') const path = require('path') const assert = require('assert') +const proxyquire = require('proxyquire') /* global afterEach, beforeEach, describe, it */ @@ -21,6 +22,28 @@ describe('+ emptyDir()', () => { afterEach(done => fse.remove(TEST_DIR, done)) + for (const code of ['EACCES', 'EIO']) { + it(`should reject with the original ${code} readdir error`, async () => { + const error = Object.assign(new Error('readdir failed'), { code }) + const { emptyDir } = proxyquire('..', { + '../fs': { readdir: async () => { throw error } } + }) + + await assert.rejects(emptyDir(TEST_DIR), err => err === error) + }) + } + + it('should pass readdir errors to the callback', done => { + const error = Object.assign(new Error('permission denied'), { code: 'EACCES' }) + const { emptyDir } = proxyquire('..', { + '../fs': { readdir: async () => { throw error } } + }) + + emptyDir(TEST_DIR, err => { + done(err === error ? undefined : new Error('original readdir error was not forwarded')) + }) + }) + describe('> when directory exists and contains items', () => { it('should delete all of the items', done => { // verify nothing diff --git a/lib/empty/index.js b/lib/empty/index.js index b4a2e823..22a6a924 100644 --- a/lib/empty/index.js +++ b/lib/empty/index.js @@ -10,7 +10,8 @@ const emptyDir = u(async function emptyDir (dir) { let items try { items = await fs.readdir(dir) - } catch { + } catch (err) { + if (err.code !== 'ENOENT') throw err return mkdir.mkdirs(dir) } @@ -21,7 +22,8 @@ function emptyDirSync (dir) { let items try { items = fs.readdirSync(dir) - } catch { + } catch (err) { + if (err.code !== 'ENOENT') throw err return mkdir.mkdirsSync(dir) }