Skip to content

Commit 4753240

Browse files
committed
update
1 parent 7d83ae4 commit 4753240

File tree

4 files changed

+281
-162
lines changed

4 files changed

+281
-162
lines changed

scripts/DocTests.mjs

Lines changed: 156 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Path from "path";
77
import * as Belt_List from "rescript/lib/es6/belt_List.js";
88
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
99
import * as Core__List from "../src/Core__List.mjs";
10+
import * as Caml_option from "rescript/lib/es6/caml_option.js";
1011
import * as Core__Array from "../src/Core__Array.mjs";
1112
import * as RescriptCore from "../src/RescriptCore.mjs";
1213
import * as Tools_Docgen from "@rescript/tools/npm/Tools_Docgen.mjs";
@@ -72,7 +73,7 @@ function prepareCompiler() {
7273
RE_EXN_ID: "Assert_failure",
7374
_1: [
7475
"DocTests.res",
75-
128,
76+
129,
7677
9
7778
],
7879
Error: new Error()
@@ -85,7 +86,7 @@ function prepareCompiler() {
8586
RE_EXN_ID: "Assert_failure",
8687
_1: [
8788
"DocTests.res",
88-
126,
89+
127,
8990
11
9091
],
9192
Error: new Error()
@@ -98,7 +99,7 @@ function prepareCompiler() {
9899
RE_EXN_ID: "Assert_failure",
99100
_1: [
100101
"DocTests.res",
101-
126,
102+
127,
102103
11
103104
],
104105
Error: new Error()
@@ -109,7 +110,7 @@ function prepareCompiler() {
109110
RE_EXN_ID: "Assert_failure",
110111
_1: [
111112
"DocTests.res",
112-
128,
113+
129,
113114
9
114115
],
115116
Error: new Error()
@@ -131,6 +132,31 @@ function prepareCompiler() {
131132

132133
prepareCompiler();
133134

135+
async function run(command, args, options) {
136+
return await new Promise((function (resolve, _reject) {
137+
var spawn = Child_process.spawn(command, args, options !== undefined ? Caml_option.valFromOption(options) : undefined);
138+
var stdout = [];
139+
var stderr = [];
140+
spawn.stdout.on("data", (function (data) {
141+
stdout.push(data);
142+
}));
143+
spawn.stderr.on("data", (function (data) {
144+
stderr.push(data);
145+
}));
146+
spawn.once("close", (function (code, _signal) {
147+
resolve({
148+
stdout: stdout,
149+
stderr: stderr,
150+
code: code
151+
});
152+
}));
153+
}));
154+
}
155+
156+
var SpawnAsync = {
157+
run: run
158+
};
159+
134160
function createFileInTempDir(id) {
135161
return Path.join(Os.tmpdir(), id);
136162
}
@@ -148,24 +174,8 @@ async function compileTest(id, code) {
148174
"-open",
149175
"RescriptCore"
150176
];
151-
var promise = await new Promise((function (resolve, _reject) {
152-
var spawn = Child_process.spawn(bscBin, args);
153-
var stdout = [];
154-
var stderr = [];
155-
spawn.stdout.on("data", (function (data) {
156-
stdout.push(data);
157-
}));
158-
spawn.stderr.on("data", (function (data) {
159-
stderr.push(data);
160-
}));
161-
spawn.once("close", (function (_code, _signal) {
162-
resolve([
163-
stdout,
164-
stderr
165-
]);
166-
}));
167-
}));
168-
var stderr = promise[1];
177+
var match = await run(bscBin, args, undefined);
178+
var stderr = match.stderr;
169179
if (stderr.length > 0) {
170180
return {
171181
TAG: "Error",
@@ -176,38 +186,13 @@ async function compileTest(id, code) {
176186
} else {
177187
return {
178188
TAG: "Ok",
179-
_0: promise[0].map(function (e) {
189+
_0: match.stdout.map(function (e) {
180190
return e.toString();
181191
}).join("")
182192
};
183193
}
184194
}
185195

186-
async function run(command, args) {
187-
return await new Promise((function (resolve, _reject) {
188-
var spawn = Child_process.spawn(command, args);
189-
var stdout = [];
190-
var stderr = [];
191-
spawn.stdout.on("data", (function (data) {
192-
stdout.push(data);
193-
}));
194-
spawn.stderr.on("data", (function (data) {
195-
stderr.push(data);
196-
}));
197-
spawn.once("close", (function (code, _signal) {
198-
resolve({
199-
stdout: stdout,
200-
stderr: stderr,
201-
code: code
202-
});
203-
}));
204-
}));
205-
}
206-
207-
var SpawnAsync = {
208-
run: run
209-
};
210-
211196
function extractDocFromFile(file) {
212197
var toolsBin = Path.join(Path.dirname(dirname), "node_modules", ".bin", "rescript-tools");
213198
var spawn = Child_process.spawnSync(toolsBin, [
@@ -336,49 +321,142 @@ function getCodeBlocks(example) {
336321
}))), /* [] */0));
337322
}
338323

339-
async function main() {
340-
var results = await Promise.all(getExamples(extractDocFromFile("src/Core__Test.res")).map(async function (example) {
324+
async function runtimeTests(code) {
325+
var match = await run("node", [
326+
"-p",
327+
code
328+
], {
329+
cwd: compilerDir
330+
});
331+
var stderr = match.stderr;
332+
if (stderr.length > 0) {
333+
return {
334+
TAG: "Error",
335+
_0: stderr.map(function (e) {
336+
return e.toString();
337+
}).join("")
338+
};
339+
} else {
340+
return {
341+
TAG: "Ok",
342+
_0: match.stdout.map(function (e) {
343+
return e.toString();
344+
}).join("")
345+
};
346+
}
347+
}
348+
349+
function indentOutputCode(code) {
350+
var indent = " ".repeat(2);
351+
return code.split("\n").map(function (s) {
352+
return indent + s;
353+
}).join("\n");
354+
}
355+
356+
async function compilerResults() {
357+
var results = await Promise.all(getExamples(extractDocFromFile("src/RescriptCore.res")).map(async function (example) {
341358
var id = example.id.replaceAll(".", "_");
342359
var codes = getCodeBlocks(example);
343360
var results = await Promise.all(codes.map(async function (code, $$int) {
344361
var id$1 = id + "_" + $$int.toString();
345-
return await compileTest(id$1, code);
362+
return [
363+
code,
364+
await compileTest(id$1, code)
365+
];
346366
}));
347367
return [
348368
example,
349369
results
350370
];
351371
}));
352-
var errors = Belt_Array.keepMap(results, (function (param) {
353-
var errors = Belt_Array.keepMap(param[1], (function (result) {
354-
if (result.TAG === "Ok") {
355-
return ;
356-
} else {
357-
return result._0;
358-
}
359-
}));
360-
if (errors.length > 0) {
372+
var examples = results.map(function (param) {
373+
var match = Core__Array.reduce(param[1], [
374+
[],
375+
[]
376+
], (function (acc, param) {
377+
var errors = acc[1];
378+
var oks = acc[0];
379+
var result = param[1];
380+
if (result.TAG === "Ok") {
381+
return [
382+
Belt_Array.concatMany([
383+
oks,
384+
[[
385+
param[0],
386+
result._0
387+
]]
388+
]),
389+
errors
390+
];
391+
} else {
392+
return [
393+
oks,
394+
Belt_Array.concatMany([
395+
errors,
396+
[{
397+
TAG: "ReScript",
398+
error: result._0
399+
}]
400+
])
401+
];
402+
}
403+
}));
404+
return [
405+
param[0],
406+
[
407+
match[0],
408+
match[1]
409+
]
410+
];
411+
});
412+
var errors = await Promise.all(examples.map(async function (param) {
413+
var match = param[1];
414+
var nodeTests = await Promise.all(match[0].map(async function (param) {
415+
var js = param[1];
416+
return [
417+
param[0],
418+
js,
419+
await runtimeTests(js)
420+
];
421+
}));
422+
var runtimeErrors = Belt_Array.keepMap(nodeTests, (function (param) {
423+
var output = param[2];
424+
if (output.TAG === "Ok") {
425+
return ;
426+
} else {
427+
return {
428+
TAG: "Runtime",
429+
rescript: param[0],
430+
js: param[1],
431+
error: output._0
432+
};
433+
}
434+
}));
361435
return [
362436
param[0],
363-
errors
437+
runtimeErrors.concat(match[1])
364438
];
365-
}
366-
367-
}));
439+
}));
368440
errors.forEach(function (param) {
369-
var test = param[0];
441+
var example = param[0];
370442
var cyan = function (s) {
371443
return "\x1b[36m" + s + "\x1b[0m";
372444
};
373-
var other = test.kind;
445+
var other = example.kind;
374446
var kind = other === "moduleAlias" ? "module alias" : other;
375-
var errorMessage = param[1].map(function (e) {
376-
return e.split("\n").filter(function (param, i) {
377-
return i !== 2;
378-
}).join("\n");
379-
}).join("\n");
380-
var message = "\x1B[1;31merror\x1B[0m: failed to compile examples from " + kind + " " + cyan(test.id) + "\n" + errorMessage;
381-
process.stderr.write(message);
447+
var errorMessage = param[1].map(function (err) {
448+
if (err.TAG === "ReScript") {
449+
var err$1 = err.error.split("\n").filter(function (param, i) {
450+
return i !== 2;
451+
}).join("\n");
452+
return "\x1B[1;31merror\x1B[0m: failed to compile examples from " + kind + " " + cyan(example.id) + "\n" + err$1;
453+
}
454+
var indent = " ".repeat(2);
455+
return "\x1B[1;31mruntime error\x1B[0m: failed to run examples from " + kind + " " + cyan(example.id) + "\n\n" + indent + "\x1b[36mReScript\x1b[0m\n\n" + indentOutputCode(err.rescript) + "\n\n" + indent + "\x1b[36mCompiled Js\x1b[0m\n\n" + indentOutputCode(err.js) + "\n\n" + indent + "\x1B[1;31mstacktrace\x1B[0m\n\n" + indentOutputCode(err.error) + "\n";
456+
});
457+
errorMessage.forEach(function (e) {
458+
process.stderr.write(e);
459+
});
382460
});
383461
if (errors.length === 0) {
384462
return 0;
@@ -387,7 +465,7 @@ async function main() {
387465
}
388466
}
389467

390-
var exitCode = await main();
468+
var exitCode = await compilerResults();
391469

392470
process.exit(exitCode);
393471

@@ -404,13 +482,15 @@ export {
404482
makePackageJson ,
405483
rescriptJson ,
406484
prepareCompiler ,
485+
SpawnAsync ,
407486
createFileInTempDir ,
408487
compileTest ,
409-
SpawnAsync ,
410488
extractDocFromFile ,
411489
getExamples ,
412490
getCodeBlocks ,
413-
main ,
491+
runtimeTests ,
492+
indentOutputCode ,
493+
compilerResults ,
414494
exitCode ,
415495
}
416496
/* dirname Not a pure module */

0 commit comments

Comments
 (0)