Skip to content

Commit

Permalink
fix function isSrcSubdir in the copy-sync.js and copy.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kolgotko committed Jan 17, 2018
1 parent fab376e commit 067c1cd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
Expand Up @@ -63,6 +63,14 @@ describe('+ copySync() - prevent copying into itself', () => {

describe('> when source is a directory', () => {
describe('>> when dest is a directory', () => {
it(`of not itself`, done => {
const dest = path.join(TEST_DIR, src.replace(/^\w:/, ''))
return testSuccess(src, dest, done)
})
it(`of itself`, done => {
const dest = path.join(src, 'dest')
return testError(src, dest, done)
})
it(`should copy the directory successfully when dest is 'src_dest'`, done => {
const dest = path.join(TEST_DIR, 'src_dest')
return testSuccess(src, dest, done)
Expand Down
15 changes: 6 additions & 9 deletions lib/copy-sync/copy-sync.js
Expand Up @@ -195,15 +195,12 @@ function checkDest (dest) {
// return true if dest is a subdir of src, otherwise false.
// extract dest base dir and check if that is the same as src basename
function isSrcSubdir (src, dest) {
const baseDir = dest.split(path.dirname(src) + path.sep)[1]
if (baseDir) {
const destBasename = baseDir.split(path.sep)[0]
if (destBasename) {
return src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src)
}
return false
}
return false
let srcArray = src.split(path.sep)
let destArray = dest.split(path.sep)

return srcArray.reduce((acc, current, i) => {
return acc && destArray[i] === current
}, true)
}

module.exports = copySync
8 changes: 8 additions & 0 deletions lib/copy/__tests__/copy-prevent-copying-into-itself.test.js
Expand Up @@ -63,6 +63,14 @@ describe('+ copy() - prevent copying into itself', () => {

describe('> when source is a directory', () => {
describe('>> when dest is a directory', () => {
it(`of not itself`, done => {
const dest = path.join(TEST_DIR, src.replace(/^\w:/, ''))
return testSuccess(src, dest, done)
})
it(`of itself`, done => {
const dest = path.join(src, 'dest')
return testError(src, dest, done)
})
it(`should copy the directory successfully when dest is 'src_dest'`, done => {
const dest = path.join(TEST_DIR, 'src_dest')
return testSuccess(src, dest, done)
Expand Down
15 changes: 6 additions & 9 deletions lib/copy/copy.js
Expand Up @@ -250,15 +250,12 @@ function checkDest (dest, cb) {
// return true if dest is a subdir of src, otherwise false.
// extract dest base dir and check if that is the same as src basename
function isSrcSubdir (src, dest) {
const baseDir = dest.split(path.dirname(src) + path.sep)[1]
if (baseDir) {
const destBasename = baseDir.split(path.sep)[0]
if (destBasename) {
return src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src)
}
return false
}
return false
let srcArray = src.split(path.sep)
let destArray = dest.split(path.sep)

return srcArray.reduce((acc, current, i) => {
return acc && destArray[i] === current
}, true)
}

module.exports = copy

0 comments on commit 067c1cd

Please sign in to comment.