From a0d44c1773a72870ecfa1ff378b80a388fb445ef Mon Sep 17 00:00:00 2001 From: Igor Bykov Date: Wed, 21 Feb 2018 03:08:50 +0300 Subject: [PATCH] Fix function isSrcSubdir in the copy-sync.js and copy.js (#541) --- .../copy-sync-prevent-copying-into-itself.test.js | 8 ++++++++ lib/copy-sync/copy-sync.js | 15 ++++++--------- .../copy-prevent-copying-into-itself.test.js | 8 ++++++++ lib/copy/copy.js | 15 ++++++--------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/copy-sync/__tests__/copy-sync-prevent-copying-into-itself.test.js b/lib/copy-sync/__tests__/copy-sync-prevent-copying-into-itself.test.js index fdac1892..a3347cc9 100644 --- a/lib/copy-sync/__tests__/copy-sync-prevent-copying-into-itself.test.js +++ b/lib/copy-sync/__tests__/copy-sync-prevent-copying-into-itself.test.js @@ -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) diff --git a/lib/copy-sync/copy-sync.js b/lib/copy-sync/copy-sync.js index c4742db8..02ab9a5e 100644 --- a/lib/copy-sync/copy-sync.js +++ b/lib/copy-sync/copy-sync.js @@ -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 diff --git a/lib/copy/__tests__/copy-prevent-copying-into-itself.test.js b/lib/copy/__tests__/copy-prevent-copying-into-itself.test.js index 9fe0ef0e..b9dc3155 100644 --- a/lib/copy/__tests__/copy-prevent-copying-into-itself.test.js +++ b/lib/copy/__tests__/copy-prevent-copying-into-itself.test.js @@ -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) diff --git a/lib/copy/copy.js b/lib/copy/copy.js index fd9687fb..5dfeb073 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -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