-
Notifications
You must be signed in to change notification settings - Fork 121
/
Util.ts
208 lines (195 loc) · 6.92 KB
/
Util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import type { Dirent, Stats } from 'fs';
import { PassThrough, Readable } from 'stream';
import type { SystemError } from '../../src/util/errors/SystemError';
const portNames = [
// Integration
'Conditions',
'DynamicPods',
'Identity',
'LpdHandlerWithAuth',
'LpdHandlerWithoutAuth',
'Middleware',
'PodCreation',
'RedisResourceLocker',
'RestrictedIdentity',
'ServerFetch',
'SetupMemory',
'SparqlStorage',
'Subdomains',
'WebSocketsProtocol',
// Unit
'BaseHttpServerFactory',
] as const;
export function getPort(name: typeof portNames[number]): number {
const idx = portNames.indexOf(name);
// Just in case something doesn't listen to the typings
if (idx < 0) {
throw new Error(`Unknown port name ${name}`);
}
return 6000 + idx;
}
export function describeIf(envFlag: string, name: string, fn: () => void): void {
const flag = `TEST_${envFlag.toUpperCase()}`;
const enabled = !/^(|0|false)$/iu.test(process.env[flag] ?? '');
// eslint-disable-next-line jest/valid-describe, jest/valid-title, jest/no-disabled-tests
return enabled ? describe(name, fn) : describe.skip(name, fn);
}
/**
* Mocks (some) functions of the fs system library.
* It is important that you call `jest.mock('fs');` in your test file before calling this!!!
*
* This function will return an object of which the `data` field corresponds to the contents of the root folder.
* The file system can be "reset" by assigning an empty object (`{}`) to the data field.
*
* Only files and directories are supported.
* Files are stored as strings, directories as objects with the keys corresponding to its contents.
* File path `/folder/folder2/file` will correspond to `data['folder']['folder2']['file']`.
* This can both be used to check if a file/directory was created,
* or to specify in advance certain files on the "file system".
*
* Data streams will be converted to strings for files by concatenating the contents.
*
* @param rootFilepath - The name of the root folder in which fs will start.
* @param time - The date object to use for time functions (currently only mtime from lstats)
*/
export function mockFs(rootFilepath?: string, time?: Date): { data: any } {
const cache: { data: any } = { data: {}};
rootFilepath = rootFilepath ?? 'folder';
time = time ?? new Date();
// eslint-disable-next-line unicorn/consistent-function-scoping
function throwSystemError(code: string): void {
const error = new Error('error') as SystemError;
error.code = code;
error.syscall = 'this exists for isSystemError';
throw error;
}
function getFolder(path: string): { folder: any; name: string } {
let parts = path.slice(rootFilepath!.length).split('/').filter((part): boolean => part.length > 0);
if (parts.length === 0) {
return { folder: cache, name: 'data' };
}
const name = parts.slice(-1)[0];
parts = parts.slice(0, -1);
let folder = cache.data;
parts.forEach((part): any => {
if (typeof folder === 'string') {
throwSystemError('ENOTDIR');
}
folder = folder[part];
if (!folder) {
throwSystemError('ENOENT');
}
});
return { folder, name };
}
const mock = {
createReadStream(path: string): any {
const { folder, name } = getFolder(path);
return Readable.from([ folder[name] ]);
},
createWriteStream(path: string): any {
const { folder, name } = getFolder(path);
folder[name] = '';
const stream = new PassThrough();
stream.on('data', (data): any => {
folder[name] += data;
});
stream.on('end', (): any => stream.emit('finish'));
return stream;
},
promises: {
async stat(path: string): Promise<Stats> {
return this.lstat(await this.realpath(path));
},
async lstat(path: string): Promise<Stats> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
return {
isFile: (): boolean => typeof folder[name] === 'string',
isDirectory: (): boolean => typeof folder[name] === 'object',
isSymbolicLink: (): boolean => typeof folder[name] === 'symbol',
size: typeof folder[name] === 'string' ? folder[name].length : 0,
mtime: time,
} as Stats;
},
async unlink(path: string): Promise<void> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
if (!(await this.lstat(path)).isFile()) {
throwSystemError('EISDIR');
}
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete folder[name];
},
async symlink(target: string, path: string): Promise<void> {
const { folder, name } = getFolder(path);
folder[name] = Symbol(target);
},
async realpath(path: string): Promise<string> {
const { folder, name } = getFolder(path);
const entry = folder[name];
return typeof entry === 'symbol' ? entry.description ?? 'invalid' : path;
},
async rmdir(path: string): Promise<void> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
if (Object.keys(folder[name]).length > 0) {
throwSystemError('ENOTEMPTY');
}
if (!(await this.lstat(path)).isDirectory()) {
throwSystemError('ENOTDIR');
}
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete folder[name];
},
async readdir(path: string): Promise<string[]> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
return Object.keys(folder[name]);
},
async* opendir(path: string): AsyncIterableIterator<Dirent> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
for (const [ child, entry ] of Object.entries(folder[name])) {
yield {
name: child,
isFile: (): boolean => typeof entry === 'string',
isDirectory: (): boolean => typeof entry === 'object',
isSymbolicLink: (): boolean => typeof entry === 'symbol',
} as Dirent;
}
},
async mkdir(path: string): Promise<void> {
const { folder, name } = getFolder(path);
if (folder[name]) {
throwSystemError('EEXIST');
}
folder[name] = {};
},
async readFile(path: string): Promise<string> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
return folder[name];
},
async writeFile(path: string, data: string): Promise<void> {
const { folder, name } = getFolder(path);
folder[name] = data;
},
},
};
const fs = jest.requireMock('fs');
Object.assign(fs, mock);
return cache;
}