Repository: jprichardson/node-fs-extra
Pull request #541 (2018-01-16 09:04:21)

fix function isSrcSubdir in the copy-sync.js and copy.js
The problem occurs when:
`src = /tmp/path/src`
`dest = /prefix/tmp/path/src`

coveralls: 
[![Coverage Status](https://coveralls.io/builds/15066574/badge)](https://coveralls.io/builds/15066574)

Coverage increased (+0.1%) to 85.498% when pulling **7b4d20baa2b8bd67f3c2e438a7d6797fab4023de on kolgotko:master** into **fab376ec2bd4d4420806644c1b37a7ac487d17ec on jprichardson:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/15068083/badge)](https://coveralls.io/builds/15068083)

Coverage increased (+0.1%) to 85.498% when pulling **ce2e729adbf411088e71139c143139c699ebc0e9 on kolgotko:master** into **fab376ec2bd4d4420806644c1b37a7ac487d17ec on jprichardson:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/15068283/badge)](https://coveralls.io/builds/15068283)

Coverage increased (+0.1%) to 85.498% when pulling **6fa1c43351c6518ee768204adb7e9229a6279f81 on kolgotko:master** into **fab376ec2bd4d4420806644c1b37a7ac487d17ec on jprichardson:master**.

RyanZim: I'm not sure I fully understand this change; why is the behavior different on Windows?
coveralls: 
[![Coverage Status](https://coveralls.io/builds/15070551/badge)](https://coveralls.io/builds/15070551)

Coverage increased (+0.1%) to 85.498% when pulling **801bd121d56400beb8313aab337100d2389da6c2 on kolgotko:master** into **fab376ec2bd4d4420806644c1b37a7ac487d17ec on jprichardson:master**.

kolgotko: Now behavior is tested for Windows too.
manidlou: Thanks @kolgotko! Will you squash this please?
coveralls: 
[![Coverage Status](https://coveralls.io/builds/15082217/badge)](https://coveralls.io/builds/15082217)

Coverage increased (+0.1%) to 85.498% when pulling **067c1cd0e18e79d58f6be798f3ddd8aa3e909a83 on kolgotko:master** into **fab376ec2bd4d4420806644c1b37a7ac487d17ec on jprichardson:master**.

kolgotko: That is how it should be?

Patch:
diff --git a/README.md b/README.md
index 9146fee..df20d4a 100644
--- a/README.md
+++ b/README.md
@@ -167,6 +167,7 @@ If you want to watch for changes to files or directories, then you should use [c
 
 ### Misc.
 
+- [fs-extra-debug](https://github.com/jdxcode/fs-extra-debug) - Send your fs-extra calls to [debug](https://npmjs.org/package/debug).
 - [mfs](https://github.com/cadorn/mfs) - Monitor your fs-extra calls.
 
 
diff --git a/lib/copy-sync/copy-sync.js b/lib/copy-sync/copy-sync.js
index c4742db..02ab9a5 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/copy.js b/lib/copy/copy.js
index fd9687f..5dfeb07 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


Test patch:
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 fdac189..a3347cc 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/__tests__/copy-prevent-copying-into-itself.test.js b/lib/copy/__tests__/copy-prevent-copying-into-itself.test.js
index 9fe0ef0..b9dc315 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)