Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion common/src/testing/mocks/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mock } from 'bun:test'

import type { CodebuffFileSystem } from '../../types/filesystem'
import type { Mock } from 'bun:test'
import type { PathLike , Stats } from 'node:fs'
import type { PathLike, Stats } from 'node:fs'

export interface CreateMockFsOptions {
files?: Record<string, string>
Expand All @@ -14,6 +14,7 @@ export interface CreateMockFsOptions {
path: string,
options?: { recursive?: boolean },
) => Promise<string | undefined>
realpathImpl?: (path: string) => Promise<string>
statImpl?: (path: string) => Promise<Stats>
}

Expand All @@ -31,6 +32,7 @@ export interface MockFsWithMocks {
options?: { recursive?: boolean },
) => Promise<string | undefined>
>
realpath: Mock<(path: PathLike) => Promise<string>>
stat: Mock<(path: PathLike) => Promise<Stats>>
}

Expand All @@ -43,6 +45,7 @@ export function createMockFs(options: CreateMockFsOptions = {}): MockFs {
readdirImpl,
writeFileImpl,
mkdirImpl,
realpathImpl,
statImpl,
} = options

Expand Down Expand Up @@ -79,6 +82,20 @@ export function createMockFs(options: CreateMockFsOptions = {}): MockFs {
return undefined
}

const defaultRealpath = async (path: PathLike): Promise<string> => {
const pathStr = String(path)
const isKnownPath =
pathStr in writtenFiles ||
pathStr in directories ||
createdDirs.has(pathStr)

if (!isKnownPath) {
throw new Error(`Path not found: ${pathStr}`)
}

return pathStr
}

const defaultStat = async (path: PathLike): Promise<Stats> => {
const pathStr = String(path)
const isFile = pathStr in writtenFiles
Expand Down Expand Up @@ -134,6 +151,10 @@ export function createMockFs(options: CreateMockFsOptions = {}): MockFs {
mkdirImpl(String(path), opts)
: defaultMkdir

const realpathFn = realpathImpl
? async (path: PathLike) => realpathImpl(String(path))
: defaultRealpath

const statFn = statImpl
? async (path: PathLike) => statImpl(String(path))
: defaultStat
Expand All @@ -143,6 +164,7 @@ export function createMockFs(options: CreateMockFsOptions = {}): MockFs {
readdir: mock(readdirFn),
writeFile: mock(writeFileFn),
mkdir: mock(mkdirFn),
realpath: mock(realpathFn),
stat: mock(statFn),
} as unknown as MockFs
}
Expand All @@ -153,6 +175,7 @@ export function restoreMockFs(mockFs: MockFs): void {
mocks.readdir.mockRestore()
mocks.writeFile.mockRestore()
mocks.mkdir.mockRestore()
mocks.realpath.mockRestore()
mocks.stat.mockRestore()
}

Expand All @@ -162,5 +185,6 @@ export function clearMockFs(mockFs: MockFs): void {
mocks.readdir.mockClear()
mocks.writeFile.mockClear()
mocks.mkdir.mockClear()
mocks.realpath.mockClear()
mocks.stat.mockClear()
}
8 changes: 7 additions & 1 deletion common/src/types/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ import type fs from 'fs'
*/
export type CodebuffFileSystem = Pick<
typeof fs.promises,
'mkdir' | 'readdir' | 'readFile' | 'stat' | 'unlink' | 'writeFile'
| 'mkdir'
| 'readdir'
| 'readFile'
| 'realpath'
| 'stat'
| 'unlink'
| 'writeFile'
>
Loading
Loading