@nestia/e2e 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,270 +1,270 @@
1
- import chalk from "chalk";
2
- import cli from "cli";
3
- import fs from "fs";
4
- import NodePath from "path";
5
-
6
- import { StopWatch } from "./StopWatch";
7
-
8
- /**
9
- * Dynamic Executor running prefixed functions.
10
- *
11
- * `DynamicExecutor` runs every prefixed functions in a specific directory.
12
- * However, if you want to run only specific functions, you can use
13
- * `--include` or `--exclude` option in the CLI (Command Line Interface) level.
14
- *
15
- * When you want to see example utilization cases, see the below example links.
16
- *
17
- * @example https://github.com/samchon/backend/blob/master/src/test/index.ts
18
- * @example https://github.com/samchon/nestia-template/blob/master/src/test/index.ts
19
- * @author Jeongho Nam - https://github.com/samchon
20
- */
21
- export namespace DynamicExecutor {
22
- /**
23
- * Function type of a prefixed.
24
- *
25
- * @template Arguments Type of parameters
26
- * @template Ret Type of return value
27
- */
28
- export interface Closure<Arguments extends any[], Ret = any> {
29
- (...args: Arguments): Promise<Ret>;
30
- }
31
-
32
- /**
33
- * Options for dynamic executor.
34
- */
35
- export interface IOptions<Parameters extends any[], Ret = any> {
36
- /**
37
- * Prefix of function name.
38
- *
39
- * Every prefixed function will be executed.
40
- *
41
- * In other words, if a function name doesn't start with the prefix, then it would never be executed.
42
- */
43
- prefix: string;
44
-
45
- /**
46
- * Get parameters of a function.
47
- *
48
- * @param name Function name
49
- * @returns Parameters
50
- */
51
- parameters: (name: string) => Parameters;
52
-
53
- /**
54
- * Wrapper of test function.
55
- *
56
- * If you specify this `wrapper` property, every dynamic functions
57
- * loaded and called by this `DynamicExecutor` would be wrapped by
58
- * the `wrapper` function.
59
- *
60
- * @param name Function name
61
- * @param closure Function to be executed
62
- * @returns Wrapper function
63
- */
64
- wrapper?: (
65
- name: string,
66
- closure: Closure<Parameters, Ret>,
67
- ) => Promise<any>;
68
-
69
- /**
70
- * Whether to show elapsed time on `console` or not.
71
- *
72
- * @default true
73
- */
74
- showElapsedTime?: boolean;
75
- }
76
-
77
- /**
78
- * Report, result of dynamic execution.
79
- */
80
- export interface IReport {
81
- /**
82
- * Location path of dynamic functions.
83
- */
84
- location: string;
85
-
86
- /**
87
- * Execution results of dynamic functions.
88
- */
89
- executions: IReport.IExecution[];
90
-
91
- /**
92
- * Total elapsed time.
93
- */
94
- time: number;
95
- }
96
- export namespace IReport {
97
- /**
98
- * Execution result of a dynamic function.
99
- */
100
- export interface IExecution {
101
- /**
102
- * Name of function.
103
- */
104
- name: string;
105
-
106
- /**
107
- * Location path of the function.
108
- */
109
- location: string;
110
-
111
- /**
112
- * Error when occured.
113
- */
114
- error: Error | null;
115
-
116
- /**
117
- * Elapsed time.
118
- */
119
- time: number;
120
- }
121
- }
122
-
123
- /**
124
- * Prepare dynamic executor in strict mode.
125
- *
126
- * In strict mode, if any error occurs, the program will be terminated directly.
127
- * Otherwise, {@link validate} mode does not terminate when error occurs, but
128
- * just archive the error log.
129
- *
130
- * @param options Options of dynamic executor
131
- * @returns Runner of dynamic functions with specific location
132
- */
133
- export const assert =
134
- <Arguments extends any[]>(options: IOptions<Arguments>) =>
135
- /**
136
- * Run dynamic executor.
137
- *
138
- * @param path Location of prefixed functions
139
- */
140
- (path: string): Promise<IReport> =>
141
- main(options)(true)(path);
142
-
143
- /**
144
- * Prepare dynamic executor in loose mode.
145
- *
146
- * In loose mode, the program would not be terminated even when error occurs.
147
- * Instead, the error would be archived and returns as a list. Otherwise,
148
- * {@link assert} mode terminates the program directly when error occurs.
149
- *
150
- * @param options Options of dynamic executor
151
- * @returns Runner of dynamic functions with specific location
152
- */
153
- export const validate =
154
- <Arguments extends any[]>(options: IOptions<Arguments>) =>
155
- /**
156
- * Run dynamic executor.
157
- *
158
- * @param path Location of prefix functions
159
- * @returns List of errors
160
- */
161
- (path: string): Promise<IReport> =>
162
- main(options)(false)(path);
163
-
164
- const main =
165
- <Arguments extends any[]>(options: IOptions<Arguments>) =>
166
- (assert: boolean) =>
167
- async (path: string) => {
168
- const command: ICommand = cli.parse();
169
- const report: IReport = {
170
- location: path,
171
- time: Date.now(),
172
- executions: [],
173
- };
174
-
175
- const executor = execute(options)(command)(report)(assert);
176
- const iterator = iterate(executor);
177
- await iterator(path);
178
-
179
- report.time = Date.now() - report.time;
180
- return report;
181
- };
182
-
183
- const iterate = <Arguments extends any[]>(
184
- executor: (path: string, modulo: Module<Arguments>) => Promise<void>,
185
- ) => {
186
- const visitor = async (path: string): Promise<void> => {
187
- const directory: string[] = await fs.promises.readdir(path);
188
- for (const file of directory) {
189
- const location: string = NodePath.resolve(`${path}/${file}`);
190
- const stats: fs.Stats = await fs.promises.lstat(location);
191
-
192
- if (stats.isDirectory() === true) {
193
- await visitor(location);
194
- continue;
195
- } else if (file.substr(-3) !== `.${EXTENSION}`) continue;
196
-
197
- const modulo: Module<Arguments> = await import(location);
198
- await executor(location, modulo);
199
- }
200
- };
201
- return visitor;
202
- };
203
-
204
- const execute =
205
- <Arguments extends any[]>(options: IOptions<Arguments>) =>
206
- (command: ICommand) =>
207
- (report: IReport) =>
208
- (assert: boolean) =>
209
- async (location: string, modulo: Module<Arguments>): Promise<void> => {
210
- for (const key in modulo) {
211
- if (command.exclude && key.indexOf(command.exclude) !== -1)
212
- continue;
213
- else if (command.include && key.indexOf(command.include) === -1)
214
- continue;
215
- else if (
216
- key.substring(0, options.prefix.length) !== options.prefix
217
- )
218
- continue;
219
- else if (!(modulo[key] instanceof Function)) continue;
220
-
221
- const closure: Closure<Arguments> = modulo[key];
222
- const func = async () => {
223
- if (options.wrapper !== undefined)
224
- await options.wrapper(key, closure);
225
- else await closure(...options.parameters(key));
226
- };
227
- const label: string = chalk.greenBright(key);
228
-
229
- const result: IReport.IExecution = {
230
- name: key,
231
- location,
232
- error: null,
233
- time: Date.now(),
234
- };
235
- report.executions.push(result);
236
-
237
- try {
238
- if (options.showElapsedTime === false) {
239
- await func();
240
- result.time = Date.now() - result.time;
241
- console.log(` - ${label}`);
242
- } else {
243
- result.time = await StopWatch.measure(func);
244
- console.log(
245
- ` - ${label}: ${chalk.yellowBright(
246
- result.time.toLocaleString(),
247
- )} ms`,
248
- );
249
- }
250
- } catch (exp) {
251
- if (!(exp instanceof Error)) return;
252
-
253
- result.time = Date.now() - result.time;
254
- result.error = exp;
255
-
256
- console.log(` - ${label} -> ${chalk.redBright(exp.name)}`);
257
- if (assert === true) throw exp;
258
- }
259
- }
260
- };
261
-
262
- interface ICommand {
263
- include?: string;
264
- exclude?: string;
265
- }
266
- interface Module<Arguments extends any[]> {
267
- [key: string]: Closure<Arguments>;
268
- }
269
- const EXTENSION: string = __filename.substring(__filename.length - 2);
270
- }
1
+ import chalk from "chalk";
2
+ import cli from "cli";
3
+ import fs from "fs";
4
+ import NodePath from "path";
5
+
6
+ import { StopWatch } from "./StopWatch";
7
+
8
+ /**
9
+ * Dynamic Executor running prefixed functions.
10
+ *
11
+ * `DynamicExecutor` runs every prefixed functions in a specific directory.
12
+ * However, if you want to run only specific functions, you can use
13
+ * `--include` or `--exclude` option in the CLI (Command Line Interface) level.
14
+ *
15
+ * When you want to see example utilization cases, see the below example links.
16
+ *
17
+ * @example https://github.com/samchon/nestia-template/blob/master/src/test/index.ts
18
+ * @example https://github.com/samchon/backend/blob/master/src/test/index.ts
19
+ * @author Jeongho Nam - https://github.com/samchon
20
+ */
21
+ export namespace DynamicExecutor {
22
+ /**
23
+ * Function type of a prefixed.
24
+ *
25
+ * @template Arguments Type of parameters
26
+ * @template Ret Type of return value
27
+ */
28
+ export interface Closure<Arguments extends any[], Ret = any> {
29
+ (...args: Arguments): Promise<Ret>;
30
+ }
31
+
32
+ /**
33
+ * Options for dynamic executor.
34
+ */
35
+ export interface IOptions<Parameters extends any[], Ret = any> {
36
+ /**
37
+ * Prefix of function name.
38
+ *
39
+ * Every prefixed function will be executed.
40
+ *
41
+ * In other words, if a function name doesn't start with the prefix, then it would never be executed.
42
+ */
43
+ prefix: string;
44
+
45
+ /**
46
+ * Get parameters of a function.
47
+ *
48
+ * @param name Function name
49
+ * @returns Parameters
50
+ */
51
+ parameters: (name: string) => Parameters;
52
+
53
+ /**
54
+ * Wrapper of test function.
55
+ *
56
+ * If you specify this `wrapper` property, every dynamic functions
57
+ * loaded and called by this `DynamicExecutor` would be wrapped by
58
+ * the `wrapper` function.
59
+ *
60
+ * @param name Function name
61
+ * @param closure Function to be executed
62
+ * @returns Wrapper function
63
+ */
64
+ wrapper?: (
65
+ name: string,
66
+ closure: Closure<Parameters, Ret>,
67
+ ) => Promise<any>;
68
+
69
+ /**
70
+ * Whether to show elapsed time on `console` or not.
71
+ *
72
+ * @default true
73
+ */
74
+ showElapsedTime?: boolean;
75
+ }
76
+
77
+ /**
78
+ * Report, result of dynamic execution.
79
+ */
80
+ export interface IReport {
81
+ /**
82
+ * Location path of dynamic functions.
83
+ */
84
+ location: string;
85
+
86
+ /**
87
+ * Execution results of dynamic functions.
88
+ */
89
+ executions: IReport.IExecution[];
90
+
91
+ /**
92
+ * Total elapsed time.
93
+ */
94
+ time: number;
95
+ }
96
+ export namespace IReport {
97
+ /**
98
+ * Execution result of a dynamic function.
99
+ */
100
+ export interface IExecution {
101
+ /**
102
+ * Name of function.
103
+ */
104
+ name: string;
105
+
106
+ /**
107
+ * Location path of the function.
108
+ */
109
+ location: string;
110
+
111
+ /**
112
+ * Error when occured.
113
+ */
114
+ error: Error | null;
115
+
116
+ /**
117
+ * Elapsed time.
118
+ */
119
+ time: number;
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Prepare dynamic executor in strict mode.
125
+ *
126
+ * In strict mode, if any error occurs, the program will be terminated directly.
127
+ * Otherwise, {@link validate} mode does not terminate when error occurs, but
128
+ * just archive the error log.
129
+ *
130
+ * @param options Options of dynamic executor
131
+ * @returns Runner of dynamic functions with specific location
132
+ */
133
+ export const assert =
134
+ <Arguments extends any[]>(options: IOptions<Arguments>) =>
135
+ /**
136
+ * Run dynamic executor.
137
+ *
138
+ * @param path Location of prefixed functions
139
+ */
140
+ (path: string): Promise<IReport> =>
141
+ main(options)(true)(path);
142
+
143
+ /**
144
+ * Prepare dynamic executor in loose mode.
145
+ *
146
+ * In loose mode, the program would not be terminated even when error occurs.
147
+ * Instead, the error would be archived and returns as a list. Otherwise,
148
+ * {@link assert} mode terminates the program directly when error occurs.
149
+ *
150
+ * @param options Options of dynamic executor
151
+ * @returns Runner of dynamic functions with specific location
152
+ */
153
+ export const validate =
154
+ <Arguments extends any[]>(options: IOptions<Arguments>) =>
155
+ /**
156
+ * Run dynamic executor.
157
+ *
158
+ * @param path Location of prefix functions
159
+ * @returns List of errors
160
+ */
161
+ (path: string): Promise<IReport> =>
162
+ main(options)(false)(path);
163
+
164
+ const main =
165
+ <Arguments extends any[]>(options: IOptions<Arguments>) =>
166
+ (assert: boolean) =>
167
+ async (path: string) => {
168
+ const command: ICommand = cli.parse();
169
+ const report: IReport = {
170
+ location: path,
171
+ time: Date.now(),
172
+ executions: [],
173
+ };
174
+
175
+ const executor = execute(options)(command)(report)(assert);
176
+ const iterator = iterate(executor);
177
+ await iterator(path);
178
+
179
+ report.time = Date.now() - report.time;
180
+ return report;
181
+ };
182
+
183
+ const iterate = <Arguments extends any[]>(
184
+ executor: (path: string, modulo: Module<Arguments>) => Promise<void>,
185
+ ) => {
186
+ const visitor = async (path: string): Promise<void> => {
187
+ const directory: string[] = await fs.promises.readdir(path);
188
+ for (const file of directory) {
189
+ const location: string = NodePath.resolve(`${path}/${file}`);
190
+ const stats: fs.Stats = await fs.promises.lstat(location);
191
+
192
+ if (stats.isDirectory() === true) {
193
+ await visitor(location);
194
+ continue;
195
+ } else if (file.substr(-3) !== `.${EXTENSION}`) continue;
196
+
197
+ const modulo: Module<Arguments> = await import(location);
198
+ await executor(location, modulo);
199
+ }
200
+ };
201
+ return visitor;
202
+ };
203
+
204
+ const execute =
205
+ <Arguments extends any[]>(options: IOptions<Arguments>) =>
206
+ (command: ICommand) =>
207
+ (report: IReport) =>
208
+ (assert: boolean) =>
209
+ async (location: string, modulo: Module<Arguments>): Promise<void> => {
210
+ for (const key in modulo) {
211
+ if (command.exclude && key.indexOf(command.exclude) !== -1)
212
+ continue;
213
+ else if (command.include && key.indexOf(command.include) === -1)
214
+ continue;
215
+ else if (
216
+ key.substring(0, options.prefix.length) !== options.prefix
217
+ )
218
+ continue;
219
+ else if (!(modulo[key] instanceof Function)) continue;
220
+
221
+ const closure: Closure<Arguments> = modulo[key];
222
+ const func = async () => {
223
+ if (options.wrapper !== undefined)
224
+ await options.wrapper(key, closure);
225
+ else await closure(...options.parameters(key));
226
+ };
227
+ const label: string = chalk.greenBright(key);
228
+
229
+ const result: IReport.IExecution = {
230
+ name: key,
231
+ location,
232
+ error: null,
233
+ time: Date.now(),
234
+ };
235
+ report.executions.push(result);
236
+
237
+ try {
238
+ if (options.showElapsedTime === false) {
239
+ await func();
240
+ result.time = Date.now() - result.time;
241
+ console.log(` - ${label}`);
242
+ } else {
243
+ result.time = await StopWatch.measure(func);
244
+ console.log(
245
+ ` - ${label}: ${chalk.yellowBright(
246
+ result.time.toLocaleString(),
247
+ )} ms`,
248
+ );
249
+ }
250
+ } catch (exp) {
251
+ if (!(exp instanceof Error)) return;
252
+
253
+ result.time = Date.now() - result.time;
254
+ result.error = exp;
255
+
256
+ console.log(` - ${label} -> ${chalk.redBright(exp.name)}`);
257
+ if (assert === true) throw exp;
258
+ }
259
+ }
260
+ };
261
+
262
+ interface ICommand {
263
+ include?: string;
264
+ exclude?: string;
265
+ }
266
+ interface Module<Arguments extends any[]> {
267
+ [key: string]: Closure<Arguments>;
268
+ }
269
+ const EXTENSION: string = __filename.substring(__filename.length - 2);
270
+ }