Skip to content

Commit

Permalink
move() should error when src & dest are the same and src doesn't exist
Browse files Browse the repository at this point in the history
Fixes #414; bug introduced in #378
  • Loading branch information
RyanZim committed May 2, 2017
1 parent f26a946 commit 5bfcb64
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 5bfcb64

Please sign in to comment.