22// SPDX-License-Identifier: Apache-2.0
33
44import { describe , it , expect , beforeEach , vi } from "vitest" ;
5+ import path from "node:path" ;
56import type { PluginLogger } from "../index.js" ;
67
78// ---------------------------------------------------------------------------
@@ -28,12 +29,28 @@ function addSymlink(p: string): void {
2829}
2930
3031vi . mock ( "node:fs" , async ( importOriginal ) => {
31- const original = await importOriginal ( ) ;
32+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33+ const original = ( await importOriginal ( ) ) as any ;
3234 return {
3335 ...original ,
3436 existsSync : ( p : string ) => store . has ( p ) ,
35- mkdirSync : vi . fn ( ( p : string ) => {
36- addDir ( p ) ;
37+ mkdirSync : vi . fn ( ( p : string , options ?: { recursive ?: boolean } ) => {
38+ if ( options ?. recursive ) {
39+ let current = "" ;
40+ const parts = p . split ( "/" ) ;
41+ for ( const part of parts ) {
42+ if ( ! part && ! current ) {
43+ current = "/" ;
44+ continue ;
45+ }
46+ current = path . join ( current , part ) ;
47+ if ( ! store . has ( current ) ) {
48+ addDir ( current ) ;
49+ }
50+ }
51+ } else {
52+ addDir ( p ) ;
53+ }
3754 } ) ,
3855 readFileSync : ( p : string ) => {
3956 const entry = store . get ( p ) ;
@@ -43,14 +60,16 @@ vi.mock("node:fs", async (importOriginal) => {
4360 writeFileSync : vi . fn ( ( p : string , data : string ) => {
4461 store . set ( p , { type : "file" , content : data } ) ;
4562 } ) ,
63+ chmodSync : vi . fn ( ) ,
4664 copyFileSync : vi . fn ( ( src : string , dest : string ) => {
4765 const entry = store . get ( src ) ;
4866 if ( ! entry ) throw new Error ( `ENOENT: ${ src } ` ) ;
4967 store . set ( dest , { ...entry } ) ;
5068 } ) ,
5169 cpSync : vi . fn ( ( src : string , dest : string ) => {
52- // Shallow copy: copy all entries whose path starts with src
53- for ( const [ k , v ] of store ) {
70+ // Recursive copy: find all entries starting with src
71+ const entries = [ ...store . entries ( ) ] ;
72+ for ( const [ k , v ] of entries ) {
5473 if ( k === src || k . startsWith ( src + "/" ) ) {
5574 const relative = k . slice ( src . length ) ;
5675 store . set ( dest + relative , { ...v } ) ;
@@ -59,7 +78,8 @@ vi.mock("node:fs", async (importOriginal) => {
5978 } ) ,
6079 rmSync : vi . fn ( ) ,
6180 renameSync : vi . fn ( ( oldPath : string , newPath : string ) => {
62- for ( const [ k , v ] of store ) {
81+ const entries = [ ...store . entries ( ) ] ;
82+ for ( const [ k , v ] of entries ) {
6383 if ( k === oldPath || k . startsWith ( oldPath + "/" ) ) {
6484 const relative = k . slice ( oldPath . length ) ;
6585 store . set ( newPath + relative , v ) ;
0 commit comments