Skip to content

Commit

Permalink
Ensure copy filter fn is not called more than needed (#883)
Browse files Browse the repository at this point in the history
Fixes #809
  • Loading branch information
RyanZim committed Apr 16, 2021
1 parent c8815e3 commit af35401
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Expand Up @@ -104,6 +104,25 @@ describe('+ copySync() / file', () => {
assert(fs.existsSync(destFile3))
})

it('should not call filter fn more than needed', () => {
const src = path.join(TEST_DIR, 'foo')

fs.writeFileSync(src, '')

const dest = path.join(TEST_DIR, 'bar')

let filterCallCount = 0
const filter = () => {
filterCallCount++
return true
}

fs.copySync(src, dest, filter)

assert.strictEqual(filterCallCount, 1)
assert(fs.existsSync(dest))
})

describe('> when the destination dir does not exist', () => {
it('should create the destination directory and copy the file', () => {
const src = path.join(TEST_DIR, 'file.txt')
Expand Down
2 changes: 1 addition & 1 deletion lib/copy-sync/copy-sync.js
Expand Up @@ -30,7 +30,7 @@ function handleFilterAndCopy (destStat, src, dest, opts) {
if (opts.filter && !opts.filter(src, dest)) return
const destParent = path.dirname(dest)
if (!fs.existsSync(destParent)) mkdirsSync(destParent)
return startCopy(destStat, src, dest, opts)
return getStats(destStat, src, dest, opts)
}

function startCopy (destStat, src, dest, opts) {
Expand Down
19 changes: 19 additions & 0 deletions lib/copy/__tests__/copy.test.js
Expand Up @@ -283,6 +283,25 @@ describe('fs-extra', () => {
})
})

it('should not call filter fn more than needed', done => {
const src = path.join(TEST_DIR, 'foo')
fs.writeFileSync(src, '')
const dest = path.join(TEST_DIR, 'bar')

let filterCallCount = 0
const filter = () => {
filterCallCount++
return true
}

fse.copy(src, dest, filter, err => {
assert(!err)
assert.strictEqual(filterCallCount, 1)
assert(fs.existsSync(dest))
done()
})
})

it('accepts options object in place of filter', done => {
const srcFile1 = path.join(TEST_DIR, '1.jade')
fs.writeFileSync(srcFile1, '')
Expand Down
4 changes: 2 additions & 2 deletions lib/copy/copy.js
Expand Up @@ -42,10 +42,10 @@ function checkParentDir (destStat, src, dest, opts, cb) {
const destParent = path.dirname(dest)
pathExists(destParent, (err, dirExists) => {
if (err) return cb(err)
if (dirExists) return startCopy(destStat, src, dest, opts, cb)
if (dirExists) return getStats(destStat, src, dest, opts, cb)
mkdirs(destParent, err => {
if (err) return cb(err)
return startCopy(destStat, src, dest, opts, cb)
return getStats(destStat, src, dest, opts, cb)
})
})
}
Expand Down

0 comments on commit af35401

Please sign in to comment.