Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensureFile & ensureFileSync should error when the path is a directory #466

Merged
merged 1 commit into from Jul 20, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/ensure/__tests__/ensure.test.js
Expand Up @@ -45,6 +45,19 @@ describe('fs-extra', () => {
})
})
})

describe('> when there is a directory at that path', () => {
it('should error', done => {
const p = path.join(TEST_DIR, 'somedir')
fs.mkdirSync(p)

fse.ensureFile(p, err => {
assert(err)
assert.equal(err.code, 'EISDIR')
done()
})
})
})
})

describe('+ ensureFileSync()', () => {
Expand All @@ -68,6 +81,22 @@ describe('fs-extra', () => {
assert(fs.existsSync(file))
})
})

describe('> when there is a directory at that path', () => {
it('should error', () => {
const p = path.join(TEST_DIR, 'somedir2')
fs.mkdirSync(p)

assert.throws(() => {
try {
fse.ensureFileSync(p)
} catch (e) {
assert.equal(e.code, 'EISDIR')
throw e
}
})
})
})
})

describe('+ ensureDir()', () => {
Expand Down
11 changes: 7 additions & 4 deletions lib/ensure/file.js
Expand Up @@ -14,9 +14,8 @@ function createFile (file, callback) {
})
}

pathExists(file, (err, fileExists) => {
if (err) return callback(err)
if (fileExists) return callback()
fs.stat(file, (err, stats) => { // eslint-disable-line handle-callback-err
if (!err && stats.isFile()) return callback()
const dir = path.dirname(file)
pathExists(dir, (err, dirExists) => {
if (err) return callback(err)
Expand All @@ -30,7 +29,11 @@ function createFile (file, callback) {
}

function createFileSync (file) {
if (fs.existsSync(file)) return
let stats
try {
stats = fs.statSync(file)
} catch (e) {}
if (stats && stats.isFile()) return

const dir = path.dirname(file)
if (!fs.existsSync(dir)) {
Expand Down