Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix function isSrcSubdir in the copy-sync.js and copy.js #541

Merged
merged 1 commit into from Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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