Skip to content

Commit

Permalink
Merge pull request #230 from glortho/master
Browse files Browse the repository at this point in the history
throw error if src and dest are the same to avoid zeroing out + test
  • Loading branch information
jprichardson committed Mar 16, 2016
2 parents 2b72bc3 + d863f5f commit b399ea1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/copy/__tests__/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ 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 b399ea1

Please sign in to comment.