@@ -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.
@@ -312,6 +312,32 @@ function nextdir() {
312312 ) ;
313313}
314314
315+ // It must not throw error if attempt is made to copy to dest
316+ // directory with same prefix as src directory
317+ // regression test for https://github.com/nodejs/node/issues/54285
318+ {
319+ const src = nextdir ( 'prefix' ) ;
320+ const dest = nextdir ( 'prefix-a' ) ;
321+ mkdirSync ( src ) ;
322+ mkdirSync ( dest ) ;
323+ cpSync ( src , dest , mustNotMutateObjectDeep ( { recursive : true } ) ) ;
324+ }
325+
326+ // It throws error if attempt is made to copy src to dest
327+ // when src is parent directory of the parent of dest
328+ {
329+ const src = nextdir ( 'a' ) ;
330+ const destParent = nextdir ( 'a/b' ) ;
331+ const dest = nextdir ( 'a/b/c' ) ;
332+ mkdirSync ( src ) ;
333+ mkdirSync ( destParent ) ;
334+ mkdirSync ( dest ) ;
335+ assert . throws (
336+ ( ) => cpSync ( src , dest , mustNotMutateObjectDeep ( { recursive : true } ) ) ,
337+ { code : 'ERR_FS_CP_EINVAL' } ,
338+ )
339+ }
340+
315341// It throws error if attempt is made to copy to subdirectory of self.
316342{
317343 const src = './test/fixtures/copy/kitchen-sink' ;
0 commit comments