|
1 |
| -import path from 'path' |
| 1 | +import * as memfs from 'memfs' |
2 | 2 |
|
3 |
| -const fs = { |
4 |
| - ...jest.requireActual('fs'), |
5 |
| -} |
6 |
| - |
7 |
| -let mockFiles = {} |
8 |
| - |
9 |
| -const pathSeparator = path.sep |
10 |
| - |
11 |
| -const getParentDir = (path) => { |
12 |
| - return path.substring(0, path.lastIndexOf(pathSeparator)) |
13 |
| -} |
14 |
| - |
15 |
| -const makeParentDirs = (path) => { |
16 |
| - const parentDir = getParentDir(path) |
17 |
| - if (parentDir && !(parentDir in mockFiles)) { |
18 |
| - mockFiles[parentDir] = undefined |
19 |
| - makeParentDirs(parentDir) |
20 |
| - } |
21 |
| -} |
22 |
| - |
23 |
| -/** |
24 |
| - * This is a custom function that our tests can use during setup to specify |
25 |
| - * what the files on the "mock" filesystem should look like when any of the |
26 |
| - * `fs` APIs are used. |
27 |
| - * |
28 |
| - * Sets the state of the mocked file system |
29 |
| - * @param newMockFiles - {[filepath]: contents} |
30 |
| - */ |
31 |
| -fs.__setMockFiles = (newMockFiles) => { |
32 |
| - mockFiles = { ...newMockFiles } |
33 |
| - |
34 |
| - // Generate all the directories which implicitly exist |
35 |
| - Object.keys(mockFiles).forEach((mockPath) => { |
36 |
| - if (mockPath.includes(pathSeparator)) { |
37 |
| - makeParentDirs(mockPath) |
38 |
| - } |
39 |
| - }) |
40 |
| -} |
41 |
| - |
42 |
| -fs.__getMockFiles = () => { |
43 |
| - return mockFiles |
44 |
| -} |
45 |
| - |
46 |
| -fs.readFileSync = (path) => { |
47 |
| - // In prisma v4.3.0, prisma format uses a Wasm module. See https://github.com/prisma/prisma/releases/tag/4.3.0. |
48 |
| - // We shouldn't mock this, so we'll use the real fs.readFileSync. |
49 |
| - // Prisma v5.0.0 seems to have added the schema_build Wasm module. |
50 |
| - if ( |
51 |
| - path.includes('prisma_fmt_build_bg.wasm') || |
52 |
| - path.includes('prisma_schema_build_bg.wasm') |
53 |
| - ) { |
54 |
| - return jest.requireActual('fs').readFileSync(path) |
55 |
| - } |
56 |
| - |
57 |
| - if (path in mockFiles) { |
58 |
| - return mockFiles[path] |
59 |
| - } else { |
60 |
| - const fakeError = new Error( |
61 |
| - `Error: ENOENT: no such file or directory, open '${path}'` |
62 |
| - ) |
63 |
| - fakeError.errno = -2 |
64 |
| - fakeError.syscall = 'open' |
65 |
| - fakeError.code = 'ENOENT' |
66 |
| - fakeError.path = path |
67 |
| - throw fakeError |
68 |
| - } |
69 |
| -} |
70 |
| - |
71 |
| -fs.writeFileSync = (path, contents) => { |
72 |
| - const parentDir = getParentDir(path) |
73 |
| - if (parentDir && !fs.existsSync(parentDir)) { |
74 |
| - const fakeError = new Error( |
75 |
| - `Error: ENOENT: no such file or directory, open '${path}'` |
76 |
| - ) |
77 |
| - fakeError.errno = -2 |
78 |
| - fakeError.syscall = 'open' |
79 |
| - fakeError.code = 'ENOENT' |
80 |
| - fakeError.path = path |
81 |
| - throw fakeError |
82 |
| - } |
83 |
| - mockFiles[path] = contents |
84 |
| -} |
85 |
| - |
86 |
| -fs.appendFileSync = (path, contents) => { |
87 |
| - if (path in mockFiles) { |
88 |
| - mockFiles[path] = mockFiles[path] + contents |
89 |
| - } else { |
90 |
| - fs.writeFileSync(path, contents) |
91 |
| - } |
92 |
| -} |
93 |
| - |
94 |
| -fs.rmSync = (path, options = {}) => { |
95 |
| - if (fs.existsSync(path)) { |
96 |
| - if (options.recursive) { |
97 |
| - Object.keys(mockFiles).forEach((mockedPath) => { |
98 |
| - if (mockedPath.startsWith(path)) { |
99 |
| - delete mockFiles[mockedPath] |
100 |
| - } |
101 |
| - }) |
102 |
| - } else { |
103 |
| - if (mockFiles[path] === undefined) { |
104 |
| - const children = fs.readdirSync(path) |
105 |
| - if (children.length !== 0) { |
106 |
| - const fakeError = new Error( |
107 |
| - `NodeError [SystemError]: Path is a directory: rm returned EISDIR (is a directory) ${path}` |
108 |
| - ) |
109 |
| - fakeError.errno = 21 |
110 |
| - fakeError.syscall = 'rm' |
111 |
| - fakeError.code = 'ERR_FS_EISDIR' |
112 |
| - fakeError.path = path |
113 |
| - throw fakeError |
114 |
| - } |
115 |
| - } |
116 |
| - delete mockFiles[path] |
117 |
| - } |
118 |
| - } else { |
119 |
| - const fakeError = new Error( |
120 |
| - `Error: ENOENT: no such file or directory, stat '${path}'` |
121 |
| - ) |
122 |
| - fakeError.errno = -2 |
123 |
| - fakeError.syscall = 'stat' |
124 |
| - fakeError.code = 'ENOENT' |
125 |
| - fakeError.path = path |
126 |
| - throw fakeError |
127 |
| - } |
128 |
| -} |
129 |
| - |
130 |
| -fs.unlinkSync = (path) => { |
131 |
| - if (path in mockFiles) { |
132 |
| - delete mockFiles[path] |
133 |
| - } else { |
134 |
| - const fakeError = new Error( |
135 |
| - `Error: ENOENT: no such file or directory, stat '${path}'` |
136 |
| - ) |
137 |
| - fakeError.errno = -2 |
138 |
| - fakeError.syscall = 'unlink' |
139 |
| - fakeError.code = 'ENOENT' |
140 |
| - fakeError.path = path |
141 |
| - throw fakeError |
142 |
| - } |
143 |
| -} |
144 |
| - |
145 |
| -fs.existsSync = (path) => { |
146 |
| - return path in mockFiles |
147 |
| -} |
148 |
| - |
149 |
| -fs.copyFileSync = (src, dist) => { |
150 |
| - fs.writeFileSync(dist, fs.readFileSync(src)) |
151 |
| -} |
152 |
| - |
153 |
| -fs.readdirSync = (path) => { |
154 |
| - if (!fs.existsSync(path)) { |
155 |
| - const fakeError = new Error( |
156 |
| - `Error: ENOENT: no such file or directory, scandir '${path}'` |
157 |
| - ) |
158 |
| - fakeError.errno = -2 |
159 |
| - fakeError.syscall = 'scandir' |
160 |
| - fakeError.code = 'ENOENT' |
161 |
| - fakeError.path = path |
162 |
| - throw fakeError |
163 |
| - } |
164 |
| - |
165 |
| - if (mockFiles[path] !== undefined) { |
166 |
| - const fakeError = new Error( |
167 |
| - `Error: ENOTDIR: not a directory, scandir '${path}'` |
168 |
| - ) |
169 |
| - fakeError.errno = -20 |
170 |
| - fakeError.syscall = 'scandir' |
171 |
| - fakeError.code = 'ENOTDIR' |
172 |
| - fakeError.path = path |
173 |
| - throw fakeError |
174 |
| - } |
175 |
| - |
176 |
| - const content = [] |
177 |
| - Object.keys(mockFiles).forEach((mockedPath) => { |
178 |
| - const childPath = mockedPath.substring(path.length + 1) |
179 |
| - if ( |
180 |
| - mockedPath.startsWith(path) && |
181 |
| - !childPath.includes(pathSeparator) && |
182 |
| - childPath |
183 |
| - ) { |
184 |
| - content.push(childPath) |
185 |
| - } |
186 |
| - }) |
187 |
| - return content |
188 |
| -} |
189 |
| - |
190 |
| -fs.mkdirSync = (path, options = {}) => { |
191 |
| - if (options.recursive) { |
192 |
| - makeParentDirs(path) |
193 |
| - } |
194 |
| - // Directories are represented as paths with an "undefined" value |
195 |
| - fs.writeFileSync(path, undefined) |
196 |
| -} |
197 |
| - |
198 |
| -fs.rmdirSync = (path, options = {}) => { |
199 |
| - if (!fs.existsSync(path)) { |
200 |
| - const fakeError = new Error( |
201 |
| - `Error: ENOENT: no such file or directory, rmdir '${path}'` |
202 |
| - ) |
203 |
| - fakeError.errno = -2 |
204 |
| - fakeError.syscall = 'rmdir' |
205 |
| - fakeError.code = 'ENOENT' |
206 |
| - fakeError.path = path |
207 |
| - throw fakeError |
208 |
| - } |
209 |
| - |
210 |
| - if (mockFiles[path] !== undefined) { |
211 |
| - const fakeError = new Error( |
212 |
| - `Error: ENOTDIR: not a directory, rmdir '${path}'` |
213 |
| - ) |
214 |
| - fakeError.errno = -20 |
215 |
| - fakeError.syscall = 'rmdir' |
216 |
| - fakeError.code = 'ENOTDIR' |
217 |
| - fakeError.path = path |
218 |
| - throw fakeError |
219 |
| - } |
220 |
| - |
221 |
| - fs.rmSync(path, options) |
222 |
| -} |
223 |
| - |
224 |
| -module.exports = fs |
| 3 | +export * from 'memfs' |
| 4 | +export default memfs.fs |
0 commit comments