Skip to content

Commit

Permalink
Merge pull request #415 from jprichardson/move-error
Browse files Browse the repository at this point in the history
move() should error when src & dest are the same and src doesn't exist
  • Loading branch information
jprichardson committed May 4, 2017
2 parents f26a946 + 5bfcb64 commit 62d40b3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/move-sync/__tests__/move-sync.test.js
Expand Up @@ -61,6 +61,13 @@ describe('moveSync()', () => {
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})

it('should error if src and dest are the same and src does not exist', () => {
const src = `${TEST_DIR}/non-existent`
const dest = src

assert.throws(() => fse.moveSync(src, dest))
})

it('should rename a file on the same device', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-file-dest`
Expand Down
2 changes: 1 addition & 1 deletion lib/move-sync/index.js
Expand Up @@ -14,7 +14,7 @@ function moveSync (src, dest, options) {
src = path.resolve(src)
dest = path.resolve(dest)

if (src === dest) return
if (src === dest) return fs.accessSync(src)

if (isSrcSubdir(src, dest)) throw new Error(`Cannot move '${src}' into itself '${dest}'.`)

Expand Down
10 changes: 10 additions & 0 deletions lib/move/__tests__/move.test.js
Expand Up @@ -79,6 +79,16 @@ describe('move', () => {
})
})

it('should error if source and destination are the same and source does not exist', done => {
const src = `${TEST_DIR}/non-existent`
const dest = src

fse.move(src, dest, err => {
assert(err)
done()
})
})

it('should not move a directory if source and destination are the same', done => {
const src = `${TEST_DIR}/a-folder`
const dest = src
Expand Down
2 changes: 1 addition & 1 deletion lib/move/index.js
Expand Up @@ -37,7 +37,7 @@ function move (source, dest, options, callback) {

function doRename () {
if (path.resolve(source) === path.resolve(dest)) {
setImmediate(callback)
fs.access(source, callback)
} else if (overwrite) {
fs.rename(source, dest, err => {
if (!err) return callback()
Expand Down

0 comments on commit 62d40b3

Please sign in to comment.