@@ -24,8 +24,8 @@ import tmpdir from '../common/tmpdir.js';
2424tmpdir . refresh ( ) ;
2525
2626let dirc = 0 ;
27- function nextdir ( ) {
28- return tmpdir . resolve ( `copy_${ ++ dirc } ` ) ;
27+ function nextdir ( dirname ) {
28+ return tmpdir . resolve ( dirname || `copy_${ ++ dirc } ` ) ;
2929}
3030
3131// Synchronous implementation of copy.
@@ -320,6 +320,45 @@ function nextdir() {
320320 ) ;
321321}
322322
323+ // It must not throw error if attempt is made to copy to dest
324+ // directory with same prefix as src directory
325+ // regression test for https://github.com/nodejs/node/issues/54285
326+ {
327+ const src = nextdir ( 'prefix' ) ;
328+ const dest = nextdir ( 'prefix-a' ) ;
329+ mkdirSync ( src ) ;
330+ mkdirSync ( dest ) ;
331+ cpSync ( src , dest , mustNotMutateObjectDeep ( { recursive : true } ) ) ;
332+ }
333+
334+ // It must not throw error if attempt is made to copy to dest
335+ // directory if the parent of dest has same prefix as src directory
336+ // regression test for https://github.com/nodejs/node/issues/54285
337+ {
338+ const src = nextdir ( 'aa' ) ;
339+ const destParent = nextdir ( 'aaa' ) ;
340+ const dest = nextdir ( 'aaa/aabb' ) ;
341+ mkdirSync ( src ) ;
342+ mkdirSync ( destParent ) ;
343+ mkdirSync ( dest ) ;
344+ cpSync ( src , dest , mustNotMutateObjectDeep ( { recursive : true } ) ) ;
345+ }
346+
347+ // It throws error if attempt is made to copy src to dest
348+ // when src is parent directory of the parent of dest
349+ {
350+ const src = nextdir ( 'a' ) ;
351+ const destParent = nextdir ( 'a/b' ) ;
352+ const dest = nextdir ( 'a/b/c' ) ;
353+ mkdirSync ( src ) ;
354+ mkdirSync ( destParent ) ;
355+ mkdirSync ( dest ) ;
356+ assert . throws (
357+ ( ) => cpSync ( src , dest , mustNotMutateObjectDeep ( { recursive : true } ) ) ,
358+ { code : 'ERR_FS_CP_EINVAL' } ,
359+ ) ;
360+ }
361+
323362// It throws error if attempt is made to copy to subdirectory of self.
324363{
325364 const src = './test/fixtures/copy/kitchen-sink' ;
0 commit comments