-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.ts
302 lines (272 loc) · 8.29 KB
/
runner.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
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
import { filePathsToFiles } from "./files.ts";
import { Block, File, GlobalData, Meta } from "./types.ts";
import { consumeBodies, fetchBlock } from "./fetchBlock.ts";
import { assertResponse } from "./assertResponse.ts";
import * as fmt from "https://deno.land/std@0.160.0/fmt/colors.ts";
import { wait } from "https://deno.land/x/wait@0.1.12/mod.ts";
import { relative } from "https://deno.land/std@0.160.0/path/posix.ts";
import { getDisplayIndex, printBlock, printErrorsSummary } from "./print.ts";
import { ms } from "https://deno.land/x/ms@v0.1.0/ms.ts";
// import ms from "npm:ms";
import {
parseMetaFromText,
parseRequestFromText,
parseResponseFromText,
} from "./parseBlockText.ts";
import * as assertions from "https://deno.land/std@0.160.0/testing/asserts.ts";
const noop = (..._: unknown[]): void => { };
export async function runner(
filePaths: string[],
defaultMeta: Meta,
failFast = false,
): Promise<{ files: File[]; exitCode: number; onlyMode: string[] }> {
const files: File[] = await filePathsToFiles(filePaths);
let successfulBlocks = 0;
let failedBlocks = 0;
let ignoredBlocks = 0;
const blocksDone: Block[] = [];
const startGlobalTime = Date.now();
const globalData: GlobalData = {
meta: {
...defaultMeta,
get ignore() {
return undefined;
// do not save ignore in global meta
},
get only() {
// do not save only in global meta
return undefined;
},
},
_files: files,
_blocksDone: {},
_blocksAlreadyReferenced: {},
};
const onlyMode = [];
// parse all metadata first
for (const file of files) {
file.relativePath = relative(Deno.cwd(), file.path);
for (const block of file.blocks) {
try {
const meta = await parseMetaFromText(block.text, {
...globalData,
...block,
...assertions,
});
block.meta._relativeFilePath ??= file.relativePath;
if (meta.only) {
onlyMode.push(
`${block.meta._relativeFilePath}:${block.meta._startLine}`,
);
}
block.meta = {
...globalData.meta,
...block.meta,
...meta,
};
} catch (error) {
block.error = error;
block.meta.isErrorBlock = true;
}
}
}
if (onlyMode.length) {
for (const file of files) {
for (const block of file.blocks) {
if (!block.meta.only) {
block.meta.ignore = true;
}
}
}
}
for (const file of files) {
const relativePath = file.relativePath;
const path = fmt.dim(`running ${relativePath}`);
let pathSpinner;
if (getDisplayIndex(defaultMeta) === 0) {
// display none
} else if (getDisplayIndex(defaultMeta) === 1) {
pathSpinner = wait({ text: path });
pathSpinner.start();
} else {
console.info(path);
}
let _isFirstBlock = true;
for (const block of file.blocks) {
block.meta._relativeFilePath = relativePath;
block.meta._isFirstBlock = _isFirstBlock;
if (_isFirstBlock) {
_isFirstBlock = false;
}
const [...blocks] = await runBlock(block, globalData);
blocksDone.push(...blocks);
if (block.meta._isIgnoredBlock) {
ignoredBlocks++;
}
if (block.meta._isFailedBlock) {
failedBlocks++;
}
if (block.meta._isSuccessfulBlock) {
successfulBlocks++;
}
if (failFast && failedBlocks) {
if (getDisplayIndex(defaultMeta) !== 0) {
printErrorsSummary(blocksDone);
}
const status = block.actualResponse?.status || 1;
console.error(fmt.red(`\nFAIL FAST: exiting with status ${status}`));
return {
files,
exitCode: status,
onlyMode,
}
}
}
pathSpinner?.stop();
pathSpinner?.clear();
}
if (getDisplayIndex(defaultMeta) !== 0) {
printErrorsSummary(blocksDone);
const statusText = failedBlocks
? fmt.bgRed(" FAIL ")
: fmt.bgBrightGreen(" PASS ");
const totalBlocks = successfulBlocks + failedBlocks + ignoredBlocks;
const elapsedGlobalTime = Date.now() - startGlobalTime;
const prettyGlobalTime = fmt.dim(`(${ms(elapsedGlobalTime)})`);
console.info();
console.info(
fmt.bold(`${statusText}`),
`${fmt.white(String(totalBlocks))} tests, ${fmt.green(String(successfulBlocks))
} passed, ${fmt.red(String(failedBlocks))} failed, ${fmt.yellow(String(ignoredBlocks))
} ignored ${prettyGlobalTime}`,
);
}
globalData._blocksDone = {}; // clean up blocks referenced
return { files, exitCode: failedBlocks, onlyMode };
}
async function runBlock(
block: Block,
globalData: GlobalData,
): Promise<Block[]> {
const startTime = Date.now();
let spinner;
const blocksDone = [block];
if (block.meta._isDoneBlock) {
return [];
}
try {
if (getDisplayIndex(block.meta) >= 2) {
spinner = wait({
prefix: fmt.dim("-"),
text: fmt.dim(block.description),
color: "cyan",
spinner: "dots4",
interval: 200,
discardStdin: true,
});
} else {
spinner = {
start: noop,
stopAndPersist: noop,
update: noop,
text: '',
};
}
if (block.meta.ref) {
const blockReferenced = globalData._files.flatMap((file) => file.blocks)
.find((b) => b.meta.name === block.meta.ref);
if (!blockReferenced) {
spinner?.start();
throw new Error(`Block referenced not found: ${block.meta.ref}`);
} else {
// Evict infinity loop
if (
globalData
._blocksAlreadyReferenced[blockReferenced.meta.name as string]
) {
return [];
// throw new Error(`Block referenced already referenced: ${block.meta.ref}`);
}
globalData._blocksAlreadyReferenced[block.meta.ref as string] =
blockReferenced;
const [...blocks] = await runBlock(blockReferenced, globalData);
blocksDone.push(...blocks);
}
}
block.meta = {
...globalData.meta,
...block.meta,
};
block.request = await parseRequestFromText(block, {
...globalData._blocksDone,
...block,
...assertions,
});
spinner.text = fmt.white(block.description);
if (block.meta._isFirstBlock && !block.request) {
globalData.meta = { ...globalData.meta, ...block.meta };
}
if (!block.request) {
block.meta._isEmptyBlock = true;
return blocksDone;
}
spinner?.start();
if (block.meta.ignore) {
block.meta._isIgnoredBlock = true;
spinner?.stopAndPersist({
symbol: fmt.yellow(""),
text: fmt.yellow(block.description),
});
return blocksDone;
}
if (block.error) {
throw block.error;
}
await fetchBlock(block);
block.expectedResponse = await parseResponseFromText(
block.text,
{
...globalData._blocksDone,
...block,
...assertions,
body: await block.actualResponse?.getBody(),
// body: block.body,
response: block.response,
},
);
if (block.expectedResponse) {
await assertResponse(block);
}
const _elapsedTime = Date.now() - startTime;
block.meta._elapsedTime = _elapsedTime;
const status = String(block.actualResponse?.status);
spinner?.stopAndPersist({
symbol: fmt.green("✓"),
text: fmt.green(block.description) + ` ${fmt.bold(status)}` + fmt.dim(` ${ms(_elapsedTime)}`),
});
block.meta._isSuccessfulBlock = true;
return blocksDone;
} catch (error) {
block.error = error;
const _elapsedTime = Date.now() - startTime;
block.meta._elapsedTime = _elapsedTime;
const status = String(block.actualResponse?.status || '');
const statusText = status ? fmt.bold(' '+ status) : fmt.bold(' ERR');
const prettyTime = fmt.dim(` ${ms(_elapsedTime)}`);
spinner?.stopAndPersist({
symbol: fmt.brightRed("✖"),
text: fmt.red(block.description) + statusText + prettyTime,
});
block.meta._isFailedBlock = true;
return blocksDone;
} finally {
await printBlock(block);
await consumeBodies(block);
block.meta._isDoneBlock = true;
if (block.meta.name) {
const name = block.meta.name as string;
block.body = await block.actualResponse?.getBody();
globalData._blocksDone[name] = block;
}
}
}