Skip to content

Commit

Permalink
lib/move: bugfix when dest is a directory, clobber is true, should cl…
Browse files Browse the repository at this point in the history
…obber. Closes #114
  • Loading branch information
jprichardson committed Mar 8, 2015
1 parent 03a2111 commit a026e41
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/move.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//most (all at this time) of this code was written by Andrew Kelley
//licensed under the BSD license: see
// most of this code was written by Andrew Kelley
// licensed under the BSD license: see
// https://github.com/andrewrk/node-mv/blob/master/package.json

// this needs a cleanup

var fs = require('graceful-fs')
var ncp = require('./_copy').ncp
var path = require('path')
Expand Down Expand Up @@ -35,6 +37,16 @@ function mv(source, dest, options, callback){
if (clobber) {
fs.rename(source, dest, function(err) {
if (!err) return callback()

if (err.code === 'ENOTEMPTY') {
rimraf(dest, function(err) {
if (err) return callback(err)
options.clobber = false // just clobbered it, no need to do it again
mv(source, dest, options, callback)
})
return
}

if (err.code !== 'EXDEV') return callback(err)
moveFileAcrossDevice(source, dest, clobber, limit, callback)
})
Expand Down
38 changes: 37 additions & 1 deletion test/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,43 @@ describe("move", function() {
})

describe('> when clobber = true', function() {

describe('> when dest is a directory', function() {
it('should clobber the destination', function(done) {
// use fixtures dir as dest since it has stuff
var dest = FIXTURES_DIR
var paths = fs.readdirSync(dest)

// verify dest has stuff
assert(paths.indexOf('a-file') >= 0)
assert(paths.indexOf('a-folder') >= 0)

// create new source dir
var src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fse.mkdirsSync(path.join(src, 'some-folder'))
fs.writeFileSync(path.join(src, 'some-file'), 'hi')

// verify source has stuff
paths = fs.readdirSync(src)
assert(paths.indexOf('some-file') >= 0)
assert(paths.indexOf('some-folder') >= 0)

fse.move(src, dest, {clobber: true}, function(err) {
if (err) return done(err)

// verify dest does not have old stuff
var paths = fs.readdirSync(dest)
assert(paths.indexOf('a-file') == -1)
assert(paths.indexOf('a-folder') == -1)

// verify dest has new stuff
assert(paths.indexOf('some-file') >= 0)
assert(paths.indexOf('some-folder') >= 0)

done()
})
})
})
})
})
})
Expand Down

0 comments on commit a026e41

Please sign in to comment.