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

move() should error when src & dest are the same and src doesn't exist #415

Merged
merged 1 commit into from May 4, 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
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