Skip to content

Commit 737a7d3

Browse files
committed
fs: keep return type of options.filter in cpsync consistent with doc
1 parent 52f8dcf commit 737a7d3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/internal/fs/cp/cp-sync.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const {
4646
parse,
4747
resolve,
4848
} = require('path');
49-
const { isPromise } = require('util/types');
5049

5150
function cpSyncFn(src, dest, opts) {
5251
// Warn about using preserveTimestamps on 32-bit node
@@ -64,7 +63,7 @@ function cpSyncFn(src, dest, opts) {
6463
function checkPathsSync(src, dest, opts) {
6564
if (opts.filter) {
6665
const shouldCopy = opts.filter(src, dest);
67-
if (isPromise(shouldCopy)) {
66+
if (typeof shouldCopy !== 'boolean') {
6867
throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy);
6968
}
7069
if (!shouldCopy) return { __proto__: null, skipped: true };

test/parallel/test-fs-cp.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,22 @@ if (!isWindows) {
826826
cpSync(src, dest, opts);
827827
}
828828

829+
// It will throw error for invalid return value from filter
830+
{
831+
const src = nextdir();
832+
const dest = nextdir();
833+
834+
const opts = {
835+
filter: (path) => {
836+
// Undefined is not a valid return value.
837+
},
838+
};
839+
assert.throws(
840+
() => cpSync(src, dest, opts),
841+
{ code: 'ERR_INVALID_RETURN_VALUE' }
842+
);
843+
}
844+
829845
// Copy should not throw exception if dest is invalid but filtered out.
830846
{
831847
// Create dest as a file.

0 commit comments

Comments
 (0)