-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme-test.deno.ts
100 lines (79 loc) · 2.3 KB
/
readme-test.deno.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
// @deno-types='https://unpkg.com/@types/[email protected]/index.d.ts'
import 'https://unpkg.com/[email protected]/dist/markdown-it.js';
const md = window.markdownit();
const readme = await Deno.readTextFile("./README.md");
const tokens: any[] = md.parse(readme);
const blocksToSkip = new Set([
2,
3,
4,
5,
6,
7,
8,
12,
]);
const globalImport = `import {injectable, makeInjector, override, InjectKey, InjectedValue} from '../injector.ts';`
// Wrap each block in a function, nesting them inside each other, to avoid name collisions
// while still allowing later blocks to use things defined in earlier ones.
const code = globalImport + '\n' + tokens
.filter(t => t.type === 'fence' && t.info === 'ts')
.map(t => t.content.trim())
.map(commentOutImports)
.reduceRight((acc, b, i) => {
return wrapCodeBlock(commentOutIf(blocksToSkip.has(i), b) + '\n\n' + acc, i)
}, '')
function commentOutImports(code: string) {
return code.replaceAll(/^import .*$/mg, '// $&');
}
function wrapCodeBlock(block: string, index: number): string {
return `
(function codeBlock${index}() {
${block.trim()}
}()); // end codeBlock${index}`
}
function commentOutIf(condition: boolean, block: string) {
return condition ? `/*\n${block.replaceAll('*/', '*\\/')}\n*/` : block;
}
console.log(`======== CODE ============
${withLineNumbers(code)}
==========================`);
function withLineNumbers(code: string) {
return code
.split('\n')
.map((line, i) => `${String(i + 1).padEnd(4, ' ')} ${line}`)
.join('\n')
}
const dir = "./readme-test-files"
const file = dir + "/code.deno.ts";
await Deno.mkdir(dir);
await Deno.writeTextFile(file, code);
let success = true;
console.log("Running tests. Output:");
const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--quiet",
file,
],
stderr: "piped",
});
const rawError = await p.stderrOutput();
const errorString = new TextDecoder().decode(rawError).trim();
if (errorString) {
console.error("ERROR: Test file logged errors:");
console.error(errorString);
success = false;
}
const status = await p.status();
if (status.code !== 0) {
console.error("ERROR: Test file exited with status code " + status.code);
success = false;
}
await Deno.remove(dir, { recursive: true });
if (!success) {
Deno.exit(1);
} else {
console.log("Success!");
}