(options: { [path: string]: (string | FileDescriptor) })
| 11 | const { readFile, exists, readFileSync, existsSync, contains, containsSync } = fs |
| 12 | |
| 13 | export function mock (options: { [path: string]: (string | FileDescriptor) }) { |
| 14 | forOwn(options, (val, key) => { |
| 15 | files[resolve(key)] = isString(val) |
| 16 | ? { mode: '33188', content: val } |
| 17 | : val as FileDescriptor |
| 18 | }); |
| 19 | (fs as any).readFile = async function (path: string) { |
| 20 | return fs.readFileSync(path) |
| 21 | }; |
| 22 | (fs as any).readFileSync = function (path: string) { |
| 23 | const file = files[path] |
| 24 | if (file === undefined) throw new Error('ENOENT') |
| 25 | if (file.mode === '0000') throw new Error('EACCES') |
| 26 | return file.content |
| 27 | }; |
| 28 | (fs as any).exists = async function (path: string) { |
| 29 | return fs.existsSync(path) |
| 30 | }; |
| 31 | (fs as any).existsSync = function (path: string) { |
| 32 | return !!files[path] |
| 33 | }; |
| 34 | (fs as any).contains = async (root: string, file: string) => { |
| 35 | root = resolve(root) |
| 36 | if (!root.endsWith(sep)) root += sep |
| 37 | return file.startsWith(root) |
| 38 | }; |
| 39 | (fs as any).containsSync = (root: string, file: string) => { |
| 40 | root = resolve(root) |
| 41 | if (!root.endsWith(sep)) root += sep |
| 42 | return file.startsWith(root) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | export function restore () { |
| 47 | files = {}; |
no test coverage detected
searching dependent graphs…