mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
33 lines
866 B
TypeScript
33 lines
866 B
TypeScript
import { isString, forOwn } from '../../src/util/underscore'
|
|
import fs from '../../src/fs/node'
|
|
|
|
type fileDescriptor = { mode: string, content: string }
|
|
|
|
let files: { [path: string]: fileDescriptor } = {}
|
|
const readFile = fs.readFile
|
|
const exists = fs.exists
|
|
|
|
export function mock (options: { [path: string]: (string | fileDescriptor) }) {
|
|
forOwn(options, (val, key) => {
|
|
files[key] = isString(val)
|
|
? { mode: '33188', content: val as string }
|
|
: val as fileDescriptor
|
|
})
|
|
fs.readFile = async function (path) {
|
|
const file = files[path]
|
|
if (file === undefined) throw new Error('ENOENT')
|
|
if (file.mode === '0000') throw new Error('EACCES')
|
|
return file.content
|
|
}
|
|
|
|
fs.exists = async function (path: string) {
|
|
return !!files[path]
|
|
}
|
|
}
|
|
|
|
export function restore () {
|
|
files = {}
|
|
fs.readFile = readFile
|
|
fs.exists = exists
|
|
}
|