-
Notifications
You must be signed in to change notification settings - Fork 0
/
respec-gib.stress-test.node.mts
220 lines (190 loc) Β· 8.01 KB
/
respec-gib.stress-test.node.mts
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
/**
* @module stress-test respecs
*
* This test should include all respecs in order to really put the library
* through its paces.
*
* Running all parameter sets is time/resource consuming though, and this should
* only be done before a check-in to source control (and/or a CI process if I
* ever get that going).
*/
import { readdir, open } from 'node:fs/promises';
import { statSync } from 'node:fs';
import * as pathUtils from 'path';
import { pretty, extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { getGlobalRespecGib } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
// #region settings
const timerName = '[respec time to complete]';
console.time(timerName);
/**
* This is how I enable/disable verbose logging. Do with it what you will.
*/
const logalot = false;
const lc = import.meta.url;
/** set this to the root of the respecs to look at */
const RESPEC_ROOT_DIR_RELATIVE_TO_BASE = './dist';
/** change this to suit your naming convention */
const RESPEC_FILE_REG_EXP = /^.+respec\.mjs$/; // stress test ALL respecs
// const RESPEC_FILE_REG_EXP = /^.*respec-gib.respec\.mjs$/; // example
/**
* If on, will first load a file and see if there is an extra respecful
* `respecfully`/`ifWe` block. Use these if you want to focus on a single or
* subset of respecs.
*
* If there are no extra respecful blocks found in an entire file, that file
* will be skipped.
*
* Note: this only is a flag to search through respec files.
*
* stress test should not look for shortcuts to testing (?)
*/
const LOOK_FOR_EXTRA_RESPEC = false;
if (LOOK_FOR_EXTRA_RESPEC) {
console.warn(`${lc} stress test has look for extra respec set to true (W: a00b06fdb71a4ccfac1b788454d65d61)`);
}
/**
* The names of the functions that indicate that we want to focus on just those
* blocks.
*
* ATOW, for first run implementation here, I am implementing it such that it
* will filter out files that don't have these indicators. The respec files that
* do have these will execute fully, but the output will only include these
* particular blocks.
*/
const EXTRA_RESPEC_FUNCTION_NAMES: string[] = ['respecfullyDear', 'ifWeMight'];
// #endregion settings
// #region 1. get respec paths
const basePath = process.cwd();
const srcPath = pathUtils.join(basePath, RESPEC_ROOT_DIR_RELATIVE_TO_BASE);
if (logalot) { console.log(`cwd: ${process.cwd()}`); }
if (logalot) { console.log(`basePath: ${basePath}`); }
if (logalot) { console.log(`srcPath: ${srcPath}`); }
const respecGib = getGlobalRespecGib();
const allRespecPaths = await getRespecFileFullPaths(srcPath, []);
if (logalot) { console.log(`allRespecPaths: ${allRespecPaths} (I: f5182a455375a8cf2aa6e1127a082423)`); }
let filteredRespecPaths: string[] | undefined = undefined;
if (LOOK_FOR_EXTRA_RESPEC) {
const hasExtraRespecPromises = allRespecPaths.map(async respecPath => {
const hasExtra = await respecFileHasExtraRespec(respecPath);
return [respecPath, hasExtra] as [string, boolean];
});
const resPathHasExtraTuples = await Promise.all(hasExtraRespecPromises);
filteredRespecPaths = resPathHasExtraTuples
.filter(([_respecPath, hasExtra]) => hasExtra)
.map(([respecPath, _hasExtra]) => respecPath);
// if there are no files that have extra respec then we do all files
if (filteredRespecPaths.length === 0) {
if (logalot) { console.log(`filteredRespecPaths is empty. doing allRespecPaths found (I: b98f54656899646025eecb4c028ab523)`); }
filteredRespecPaths = allRespecPaths.concat();
} else {
console.log(`filteredRespecPaths for extra respec: ${filteredRespecPaths} (I: b98f54656899646025eecb4c028ab523)`);
respecGib.extraRespecOnly = true;
}
}
// #endregion 1. get respec paths
respecGib.allRespecPaths = allRespecPaths;
respecGib.filteredRespecPaths = filteredRespecPaths;
const respecPaths = filteredRespecPaths ?? allRespecPaths;
respecGib.respecPaths = respecPaths;
if (logalot) { console.log(`respecPaths found:\n${respecPaths}`); }
// #region 2. execute paths' respective respecs
// for now, we'll do sequentially, but in the future we could conceivable farm
// these out to other node processes, or at least Promise.all
for (let i = 0; i < respecPaths.length; i++) {
const respecPath = respecPaths[i];
if (logalot) { console.log(respecPath); }
const esm = await import(respecPath);
if (logalot) { console.log(pretty(Object.keys(esm))); }
}
const skippedRespecPathCount = respecGib.allRespecPaths.length - respecGib.respecPaths.length;
if (skippedRespecPathCount > 0) {
console.log('');
console.error('\x1b[33m%s\x1b[0m', `${skippedRespecPathCount} respec files completely skipped.`); // yellow
}
if (respecGib.ifWeBlocksSkipped > 0) {
console.log('');
console.error('\x1b[33m%s\x1b[0m', `${respecGib.ifWeBlocksSkipped} ifWe blocks ran but skipped reporting`); // yellow
}
if (respecGib.errorMsgs.length === 0) {
console.log('');
console.error('\x1b[32m%s\x1b[0m', `ππ nothing but respec ππ`); // green
} else {
console.log('');
console.error('\x1b[31m%s\x1b[0m', `ππ DISrespec found ππ`); // red
for (const errorMsg of respecGib.errorMsgs) {
console.error('\x1b[31m%s\x1b[0m', errorMsg); // red
}
}
// #endregion 2. execute paths' respective respecs
// #region helper functions
/**
* builds a list of respec file paths, recursively traversing subdirectories
* starting from `dirPath`.
*
* @param dirPath a full path corresponding to a directory
* @param found respec paths already found (used in recursive calls)
* @returns list of all respec paths according to the respec regexp constant {@link RESPEC_FILE_REG_EXP}
*/
async function getRespecFileFullPaths(dirPath: string, found: string[]): Promise<string[]> {
const lc = `[${getRespecFileFullPaths.name}][${dirPath}]`;
try {
if (logalot) { console.log(`${lc} starting... (I: 16026290523925f79ba1933847e2a623)`); }
found ??= [];
const children = await readdir(dirPath);
if (logalot) { for (let i = 0; i < children.length; i++) { console.log(children[i]); } }
const files: string[] = [];
const dirs: string[] = [];
children.forEach(name => {
const fullPath = pathUtils.join(dirPath, name);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
// symbolic link could create a loop
if (!stat.isSymbolicLink()) { dirs.push(fullPath); }
} else if (!!name.match(RESPEC_FILE_REG_EXP)) {
files.push(fullPath);
}
});
found = found.concat(files);
for (let i = 0; i < dirs.length; i++) {
const subfound = await getRespecFileFullPaths(dirs[i], found);
found = found.concat(subfound);
}
return Array.from(new Set(found)); // unique
} catch (error) {
console.error(`${lc} ${extractErrorMsg(error)}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
/**
* Searches through the file (without importing it) for extra respecful
* functions.
*
* @param respecPath
* @returns true if extra respecful functions found in file
*/
async function respecFileHasExtraRespec(respecPath: string): Promise<boolean> {
const lc = `[${respecFileHasExtraRespec.name}]`;
try {
if (logalot) { console.log(`${lc} starting... (I: 61f3221917ba77175efa305b14defc23)`); }
const file = await open(respecPath);
for await (const line of file.readLines()) {
const hasExtraRespecInLine =
EXTRA_RESPEC_FUNCTION_NAMES.some(fnName => {
if (line.includes(`${fnName}(`)) { return true; }
});
if (hasExtraRespecInLine) {
return true;
}
}
return false;
} catch (error) {
console.error(`${lc} ${extractErrorMsg(error)}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
// #endregion helper functions
console.timeEnd(timerName);