Skip to content

Commit

Permalink
copy*(): throw err if src and dest are not the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed Mar 26, 2020
1 parent a571007 commit 35b078e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
15 changes: 15 additions & 0 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,20 @@ describe('+ copySync() / file', () => {
assert.strictEqual(destData, destDataNew)
})
})

describe('> when dest exists and is a directory', () => {
it('should throw error', () => {
const src = path.join(TEST_DIR, 'file.txt')
const dest = path.join(TEST_DIR, 'dir')
fs.ensureFileSync(src)
fs.ensureDirSync(dest)

try {
fs.copySync(src, dest)
} catch (err) {
assert.strictEqual(err.message, `Cannot overwrite directory '${dest}' with non-directory '${src}'.`)
}
})
})
})
})
3 changes: 0 additions & 3 deletions lib/copy-sync/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ function setDestTimestamps (src, dest) {

function onDir (srcStat, destStat, src, dest, opts) {
if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
if (destStat && !destStat.isDirectory()) {
throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
}
return copyDir(src, dest, opts)
}

Expand Down
14 changes: 14 additions & 0 deletions lib/copy/__tests__/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ describe('fs-extra', () => {
})
})
})

describe('> when dest exists and is a directory', () => {
it('should return an error', done => {
const src = path.join(TEST_DIR, 'file.txt')
const dest = path.join(TEST_DIR, 'dir')
fse.ensureFileSync(src)
fse.ensureDirSync(dest)

fse.copy(src, dest, err => {
assert.strictEqual(err.message, `Cannot overwrite directory '${dest}' with non-directory '${src}'.`)
done()
})
})
})
})

describe('> when src is a directory', () => {
Expand Down
3 changes: 0 additions & 3 deletions lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ function setDestTimestamps (src, dest, cb) {

function onDir (srcStat, destStat, src, dest, opts, cb) {
if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts, cb)
if (destStat && !destStat.isDirectory()) {
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`))
}
return copyDir(src, dest, opts, cb)
}

Expand Down
28 changes: 24 additions & 4 deletions lib/util/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ function checkPaths (src, dest, funcName, cb) {
util.callbackify(getStats)(src, dest, (err, stats) => {
if (err) return cb(err)
const { srcStat, destStat } = stats
if (destStat && areIdentical(srcStat, destStat)) {
return cb(new Error('Source and destination must not be the same.'))

if (destStat) {
if (areIdentical(srcStat, destStat)) {
return cb(new Error('Source and destination must not be the same.'))
}
if (srcStat.isDirectory() && !destStat.isDirectory()) {
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`))
}
if (!srcStat.isDirectory() && destStat.isDirectory()) {
return cb(new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`))
}
}

if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
return cb(new Error(errMsg(src, dest, funcName)))
}
Expand All @@ -47,9 +57,19 @@ function checkPaths (src, dest, funcName, cb) {

function checkPathsSync (src, dest, funcName) {
const { srcStat, destStat } = getStatsSync(src, dest)
if (destStat && areIdentical(srcStat, destStat)) {
throw new Error('Source and destination must not be the same.')

if (destStat) {
if (areIdentical(srcStat, destStat)) {
throw new Error('Source and destination must not be the same.')
}
if (srcStat.isDirectory() && !destStat.isDirectory()) {
throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
}
if (!srcStat.isDirectory() && destStat.isDirectory()) {
throw new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`)
}
}

if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
throw new Error(errMsg(src, dest, funcName))
}
Expand Down

0 comments on commit 35b078e

Please sign in to comment.