-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathfile_loader.test.ts
More file actions
400 lines (359 loc) · 12.2 KB
/
file_loader.test.ts
File metadata and controls
400 lines (359 loc) · 12.2 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import assert from 'node:assert/strict';
import path from 'node:path';
import { isClass } from 'is-type-of';
import yaml from 'js-yaml';
import { describe, it, expect } from 'vitest';
import { FileLoader, CaseStyle } from '../../src/loader/file_loader.ts';
import { getFilepath } from '../helper.ts';
const dirBase = getFilepath('load_dirs');
describe('test/loader/file_loader.test.ts', () => {
it('should load files with package.json#exports', async () => {
const directory = path.join(__dirname, '../../../../plugins/mock/src/app/middleware');
const services: Record<string, any> = {};
await new FileLoader({
directory,
target: services,
}).load();
expect(services.clusterAppMock).toBeDefined();
});
it('should load files', async () => {
const services: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'services'),
target: services,
}).load();
assert(services.dir.abc);
assert(services.dir.service);
assert(services.foo);
assert(services.fooBarHello);
assert(services.fooService);
assert(services.hyphenDir.a);
assert(services.underscoreDir.a);
assert(services.userProfile);
assert('load' in services.dir.service);
assert('app' in services.dir.service);
assert.equal(services.dir.service.load, true);
await Promise.all([
new Promise<void>((resolve) => {
services.foo.get((err: Error, v: string) => {
assert.ifError(err);
assert.equal(v, 'bar');
resolve();
});
}),
new Promise<void>((resolve) => {
services.userProfile.getByName('mk2', (err: Error, user: object) => {
assert.ifError(err);
assert.deepEqual(user, { name: 'mk2' });
resolve();
});
}),
]);
});
it('should not overwrite property', async () => {
const app = {
services: {
foo: {},
},
};
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'services'),
target: app.services,
}).load();
}, /can't overwrite property 'foo'/);
});
it('should not overwrite property from loading', async () => {
const app: Record<string, any> = { services: {} };
await assert.rejects(async () => {
await new FileLoader({
directory: [path.join(dirBase, 'services'), path.join(dirBase, 'overwrite_services')],
target: app.services,
}).load();
}, /can't overwrite property 'foo'/);
});
it('should overwrite property from loading', async () => {
const app = { services: {} };
await new FileLoader({
directory: [path.join(dirBase, 'services'), path.join(dirBase, 'overwrite_services')],
override: true,
target: app.services,
}).load();
});
it('should loading without call function', async () => {
const app: Record<string, any> = { services: {} };
await new FileLoader({
directory: path.join(dirBase, 'services'),
target: app.services,
call: false,
}).load();
assert.deepEqual(app.services.fooService(), { a: 1 });
});
it('should loading without call es6 class', async () => {
const app: Record<string, any> = { services: {} };
await new FileLoader({
directory: path.join(dirBase, 'class'),
target: app.services,
}).load();
assert.throws(() => {
app.services.UserProxy();
}, /cannot be invoked without 'new'|Cannot call a class constructor without/);
const instance = new app.services.UserProxy();
assert.deepEqual(instance.getUser(), { name: 'xiaochen.gaoxc' });
});
it('should loading without call babel class', async () => {
const app: Record<string, any> = { services: {} };
await new FileLoader({
directory: path.join(dirBase, 'babel'),
target: app.services,
}).load();
const instance = new app.services.UserProxy();
assert.deepEqual(instance.getUser(), { name: 'xiaochen.gaoxc' });
});
it('should only load property match the filers', async () => {
const app: Record<string, any> = { middlewares: {} };
await new FileLoader({
directory: [path.join(dirBase, 'middlewares/default'), path.join(dirBase, 'middlewares/app')],
target: app.middlewares,
call: false,
// filters: [ 'm1', 'm2', 'dm1', 'dm2' ],
}).load();
assert(app.middlewares.m1);
assert(app.middlewares.m2);
assert(app.middlewares.dm1);
assert(app.middlewares.dm2);
});
it('should support ignore string', async () => {
const app: Record<string, any> = { services: {} };
await new FileLoader({
directory: path.join(dirBase, 'ignore'),
target: app.services,
ignore: 'util/**',
}).load();
assert.equal(app.services.a.a, 1);
});
it('should support ignore array', async () => {
const app: Record<string, any> = { services: {} };
await new FileLoader({
directory: path.join(dirBase, 'ignore'),
target: app.services,
ignore: ['util/a.js', 'util/b/b.js'],
}).load();
assert.equal(app.services.a.a, 1);
});
it('should support lowercase first letter', async () => {
const app: Record<string, any> = { services: {} };
await new FileLoader({
directory: path.join(dirBase, 'lowercase'),
target: app.services,
lowercaseFirst: true,
}).load();
assert(app.services.someClass);
assert(app.services.someDir);
assert(app.services.someDir.someSubClass);
});
it('should support options.initializer with es6 class', async () => {
const app: Record<string, any> = { dao: {} };
await new FileLoader({
directory: path.join(dirBase, 'dao'),
target: app.dao,
ignore: 'util/**',
initializer(exports: any, opt) {
return new exports(app, opt.path);
},
}).load();
assert(app.dao.TestClass);
assert.deepEqual(app.dao.TestClass.user, { name: 'kai.fangk' });
assert.equal(app.dao.TestClass.app, app);
assert.equal(app.dao.TestClass.path, path.join(dirBase, 'dao/TestClass.js'));
assert.deepEqual(app.dao.testFunction.user, { name: 'kai.fangk' });
assert.deepEqual(app.dao.testReturnFunction.user, { name: 'kai.fangk' });
});
it('should support options.initializer custom type', async () => {
const app: Record<string, any> = { yml: {} };
await new FileLoader({
directory: path.join(dirBase, 'yml'),
match: '**/*.yml',
target: app.yml,
initializer(exports: any) {
return yaml.load(exports.toString());
},
}).load();
assert(app.yml.config);
assert.deepEqual(app.yml.config.map, { a: 1, b: 2 });
});
it('should pass es6 module', async () => {
const app: Record<string, any> = { model: {} };
await new FileLoader({
directory: path.join(dirBase, 'es6_module'),
target: app.model,
}).load();
assert.equal(app.model.mod.a, 1);
});
it('should pass ts module', async () => {
const app: Record<string, any> = { model: {} };
await new FileLoader({
directory: path.join(dirBase, 'ts_module'),
target: app.model,
}).load();
assert.equal(app.model.mod.a, 1);
assert.equal(app.model.mod2.foo, 'bar');
assert(app.model.mod2.HelloFoo);
assert.equal(app.model.mod3.ok, true);
assert.equal(app.model.mod3.foo, 'bar');
});
it('should contain syntax error filepath', async () => {
const app: Record<string, any> = { model: {} };
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'syntax_error'),
target: app.model,
}).load();
}, /error: Unexpected identifier|Expected|A 'yield' expression is only allowed in a generator body/);
});
it('should throw when directory contains dot', async () => {
const mod = {};
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'error/dotdir'),
target: mod,
}).load();
}, /dot.dir is not match 'a-z0-9_-' in dot.dir\/a.js/);
});
it('should throw when directory contains underscore', async () => {
const mod: Record<string, any> = {};
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'error/underscore-dir'),
target: mod,
}).load();
}, /_underscore is not match 'a-z0-9_-' in _underscore\/a.js/);
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'error/underscore-file-in-dir'),
target: mod,
}).load();
}, /_a is not match 'a-z0-9_-' in dir\/_a.js/);
});
it('should throw when file starts with underscore', async () => {
const mod: Record<string, any> = {};
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'error/underscore-file'),
target: mod,
}).load();
}, /_private is not match 'a-z0-9_-' in _private.js/);
});
describe('caseStyle', () => {
it('should load when caseStyle = upper', async () => {
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'camelize'),
target,
caseStyle: CaseStyle.upper,
}).load();
assert(target.FooBar1);
assert(target.FooBar2);
assert(target.FooBar3);
assert(target.FooBar4);
});
it('should load when caseStyle = camel', async () => {
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'camelize'),
target,
caseStyle: CaseStyle.camel,
}).load();
assert(target.fooBar1);
assert(target.fooBar2);
assert(target.FooBar3);
assert(target.fooBar4);
});
it('should load when caseStyle = lower', async () => {
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'camelize'),
target,
caseStyle: CaseStyle.lower,
}).load();
assert(target.fooBar1);
assert(target.fooBar2);
assert(target.fooBar3);
assert(target.fooBar4);
});
it('should load when caseStyle is function', async () => {
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'camelize'),
target,
caseStyle(filepath) {
return filepath
.replace('.js', '')
.split('/')
.map((property) => property.replaceAll('_', ''));
},
}).load();
assert(target.foobar1);
assert(target.fooBar2);
assert(target.FooBar3);
assert(target['foo-bar4']);
});
it('should throw when caseStyle do not return array', async () => {
const target: Record<string, any> = {};
await assert.rejects(async () => {
await new FileLoader({
directory: path.join(dirBase, 'camelize'),
target,
caseStyle(filepath: string) {
return filepath as any;
},
}).load();
}, /caseStyle expect an array, but got/);
});
it('should be overridden by lowercaseFirst', async () => {
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'camelize'),
target,
caseStyle: CaseStyle.upper,
lowercaseFirst: true,
}).load();
assert(target.fooBar1);
assert(target.fooBar2);
assert(target.fooBar3);
assert(target.fooBar4);
});
});
it('should load files with inject', async () => {
const inject: Record<string, any> = {};
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'inject'),
target,
inject,
}).load();
assert.equal(inject.b, true);
const instance = new target.a(inject);
assert(instance);
assert.equal(inject.a, true);
});
it('should load files with filter', async () => {
const target: Record<string, any> = {};
await new FileLoader({
directory: path.join(dirBase, 'filter'),
target,
filter(obj) {
return Array.isArray(obj);
},
}).load();
assert.deepEqual(Object.keys(target), ['arr']);
await new FileLoader({
directory: path.join(dirBase, 'filter'),
target,
filter(obj) {
return isClass(obj);
},
}).load();
assert.deepEqual(Object.keys(target), ['arr', 'class']);
});
});