Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure copy filter fn is not called more than needed #883

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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