Skip to content

Commit af35401

Browse files
authored
Ensure copy filter fn is not called more than needed (#883)
Fixes #809
1 parent c8815e3 commit af35401

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

lib/copy-sync/__tests__/copy-sync-file.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ describe('+ copySync() / file', () => {
104104
assert(fs.existsSync(destFile3))
105105
})
106106

107+
it('should not call filter fn more than needed', () => {
108+
const src = path.join(TEST_DIR, 'foo')
109+
110+
fs.writeFileSync(src, '')
111+
112+
const dest = path.join(TEST_DIR, 'bar')
113+
114+
let filterCallCount = 0
115+
const filter = () => {
116+
filterCallCount++
117+
return true
118+
}
119+
120+
fs.copySync(src, dest, filter)
121+
122+
assert.strictEqual(filterCallCount, 1)
123+
assert(fs.existsSync(dest))
124+
})
125+
107126
describe('> when the destination dir does not exist', () => {
108127
it('should create the destination directory and copy the file', () => {
109128
const src = path.join(TEST_DIR, 'file.txt')

lib/copy-sync/copy-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function handleFilterAndCopy (destStat, src, dest, opts) {
3030
if (opts.filter && !opts.filter(src, dest)) return
3131
const destParent = path.dirname(dest)
3232
if (!fs.existsSync(destParent)) mkdirsSync(destParent)
33-
return startCopy(destStat, src, dest, opts)
33+
return getStats(destStat, src, dest, opts)
3434
}
3535

3636
function startCopy (destStat, src, dest, opts) {

lib/copy/__tests__/copy.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,25 @@ describe('fs-extra', () => {
283283
})
284284
})
285285

286+
it('should not call filter fn more than needed', done => {
287+
const src = path.join(TEST_DIR, 'foo')
288+
fs.writeFileSync(src, '')
289+
const dest = path.join(TEST_DIR, 'bar')
290+
291+
let filterCallCount = 0
292+
const filter = () => {
293+
filterCallCount++
294+
return true
295+
}
296+
297+
fse.copy(src, dest, filter, err => {
298+
assert(!err)
299+
assert.strictEqual(filterCallCount, 1)
300+
assert(fs.existsSync(dest))
301+
done()
302+
})
303+
})
304+
286305
it('accepts options object in place of filter', done => {
287306
const srcFile1 = path.join(TEST_DIR, '1.jade')
288307
fs.writeFileSync(srcFile1, '')

lib/copy/copy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ function checkParentDir (destStat, src, dest, opts, cb) {
4242
const destParent = path.dirname(dest)
4343
pathExists(destParent, (err, dirExists) => {
4444
if (err) return cb(err)
45-
if (dirExists) return startCopy(destStat, src, dest, opts, cb)
45+
if (dirExists) return getStats(destStat, src, dest, opts, cb)
4646
mkdirs(destParent, err => {
4747
if (err) return cb(err)
48-
return startCopy(destStat, src, dest, opts, cb)
48+
return getStats(destStat, src, dest, opts, cb)
4949
})
5050
})
5151
}

0 commit comments

Comments
 (0)