(options: CreateMockFsOptions = {})
| 36 | |
| 37 | /** Creates a mock filesystem compatible with CodebuffFileSystem. */ |
| 38 | export function createMockFs(options: CreateMockFsOptions = {}): MockFs { |
| 39 | const { |
| 40 | files = {}, |
| 41 | directories = {}, |
| 42 | readFileImpl, |
| 43 | readdirImpl, |
| 44 | writeFileImpl, |
| 45 | mkdirImpl, |
| 46 | statImpl, |
| 47 | } = options |
| 48 | |
| 49 | const writtenFiles: Record<string, string> = { ...files } |
| 50 | const createdDirs: Set<string> = new Set(Object.keys(directories)) |
| 51 | |
| 52 | const defaultReadFile = async (path: PathLike): Promise<string> => { |
| 53 | const pathStr = String(path) |
| 54 | if (pathStr in writtenFiles) { |
| 55 | return writtenFiles[pathStr] |
| 56 | } |
| 57 | throw new Error(`File not found: ${pathStr}`) |
| 58 | } |
| 59 | |
| 60 | const defaultReaddir = async (path: PathLike): Promise<string[]> => { |
| 61 | const pathStr = String(path) |
| 62 | if (pathStr in directories) { |
| 63 | return directories[pathStr] |
| 64 | } |
| 65 | throw new Error(`Directory not found: ${pathStr}`) |
| 66 | } |
| 67 | |
| 68 | const defaultWriteFile = async ( |
| 69 | path: PathLike, |
| 70 | data: string, |
| 71 | ): Promise<void> => { |
| 72 | const pathStr = String(path) |
| 73 | writtenFiles[pathStr] = data |
| 74 | } |
| 75 | |
| 76 | const defaultMkdir = async (path: PathLike): Promise<string | undefined> => { |
| 77 | const pathStr = String(path) |
| 78 | createdDirs.add(pathStr) |
| 79 | return undefined |
| 80 | } |
| 81 | |
| 82 | const defaultStat = async (path: PathLike): Promise<Stats> => { |
| 83 | const pathStr = String(path) |
| 84 | const isFile = pathStr in writtenFiles |
| 85 | const isDir = pathStr in directories || createdDirs.has(pathStr) |
| 86 | |
| 87 | if (!isFile && !isDir) { |
| 88 | throw new Error(`Path not found: ${pathStr}`) |
| 89 | } |
| 90 | |
| 91 | return { |
| 92 | isFile: () => isFile, |
| 93 | isDirectory: () => isDir, |
| 94 | isBlockDevice: () => false, |
| 95 | isCharacterDevice: () => false, |
no outgoing calls
no test coverage detected