From 4eaf19874cfb4b76fb72bc77af224b9326ac7641 Mon Sep 17 00:00:00 2001 From: Michael Tiller Date: Wed, 26 Feb 2014 16:09:57 -0500 Subject: [PATCH] This fixes (what I think) is a bug in copySync The issue is that copySync applies the filter **only** at the root level of a recursive copy. This adds a test (that failed with the original code) that checks to make sure that the filter is applied recursively. The patch to lib/copy.js then addresses this issue with a simple fix and the tests pass again with the patch. --- lib/copy.js | 2 +- test/copy.test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/copy.js b/lib/copy.js index 7a29f3e..385e14c 100644 --- a/lib/copy.js +++ b/lib/copy.js @@ -73,7 +73,7 @@ function copySync(src, dest, filter) { if (!destExists) mkdir.mkdirsSync(dest); var contents = fs.readdirSync(src); contents.forEach(function (content) { - copySync(src + "/" + content, dest + "/" + content); + copySync(src + "/" + content, dest + "/" + content, filter); }); } } diff --git a/test/copy.test.js b/test/copy.test.js index 55e0f9c..8f995cb 100644 --- a/test/copy.test.js +++ b/test/copy.test.js @@ -248,6 +248,42 @@ describe('fs-extra', function() { done() }); + it("should should apply filter recursively", function(done) { + var FILES = 2, + src = path.join(DIR, 'src'), + dest = path.join(DIR, 'dest'), + filter = /0$/i, + i, j; + mkdir.sync(src); + for (i = 0; i < FILES; ++i) + testutil.createFileWithData(path.join(src, i.toString()), SIZE); + var subdir = path.join(src, 'subdir'); + mkdir.sync(subdir); + for (i = 0; i < FILES; ++i) + testutil.createFileWithData(path.join(subdir, i.toString()), SIZE); + fs.copySync(src, dest, filter); + T(fs.existsSync(dest)); + T(FILES>1); + + for (i = 0; i < FILES; ++i) { + if (i==0) { + T(fs.existsSync(path.join(dest, i.toString()))) + } else { + T(!fs.existsSync(path.join(dest, i.toString()))) + } + }; + + var destSub = path.join(dest, 'subdir'); + for (j = 0; j < FILES; ++j) { + if (j==0) { + T(fs.existsSync(path.join(destSub, j.toString()))); + } else { + T(!fs.existsSync(path.join(destSub, j.toString()))); + } + } + + done() + }); describe("> when the destination dir does not exist", function() { it("should create the destination directory and copy the file", function(done) { var src = path.join(DIR, 'data/');