Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/empty/__tests__/empty-dir-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/empty/__tests__/empty-dir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/empty/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
Loading