Skip to content

Commit

Permalink
throw error if src and dest are the same to avoid zeroing out + test
Browse files Browse the repository at this point in the history
  • Loading branch information
glortho committed Mar 15, 2016
1 parent 2b72bc3 commit 4df3d35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/copy/__tests__/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ describe('fs-extra', function () {
})

describe('+ copy()', function () {

it('should return an error if src and dest are the same', function (done) {
var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy')
var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fse.copy(fileSrc, fileDest, function (err) {
assert.equal(err.message, 'Source and destination must not be the same.')
done()
})
})

describe('> when the source is a file', function () {
it('should copy the file asynchronously', function (done) {
var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
Expand Down
6 changes: 6 additions & 0 deletions lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function copy (src, dest, options, callback) {
callback = callback || function () {}
options = options || {}

// don't allow src and dest to be the same
var basePath = process.cwd()
var currentPath = path.resolve(basePath, src)
var targetPath = path.resolve(basePath, dest)
if ( currentPath === targetPath ) return callback( new Error('Source and destination must not be the same.'))

fs.lstat(src, function (err, stats) {
if (err) return callback(err)

Expand Down

0 comments on commit 4df3d35

Please sign in to comment.