From 97f5e21a50936e045a618834c6af128474b19676 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 7 Apr 2024 21:18:35 -0300 Subject: [PATCH] fix node invalid errors --- scripts/DocTests.mjs | 50 +++++++++++++++++++++++++++++++++++--------- scripts/DocTests.res | 32 ++++++++++++++++++---------- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/scripts/DocTests.mjs b/scripts/DocTests.mjs index 155e5cde..4a8e5ac7 100644 --- a/scripts/DocTests.mjs +++ b/scripts/DocTests.mjs @@ -329,18 +329,45 @@ async function runtimeTests(code) { cwd: compilerDir, timeout: 2000 }); + var exitCode = match.code; var stderr = match.stderr; - if (stderr.length > 0) { + var stdout = match.stdout; + var std; + var exit = 0; + if (exitCode !== null) { + if (exitCode === 0.0 && stderr.length > 0) { + std = { + TAG: "Ok", + _0: stderr + }; + } else if (exitCode === 0.0) { + std = { + TAG: "Ok", + _0: stdout + }; + } else { + exit = 1; + } + } else { + exit = 1; + } + if (exit === 1) { + std = { + TAG: "Error", + _0: stderr.length > 0 ? stderr : stdout + }; + } + if (std.TAG === "Ok") { return { - TAG: "Error", - _0: stderr.map(function (e) { + TAG: "Ok", + _0: std._0.map(function (e) { return e.toString(); }).join("") }; } else { return { - TAG: "Ok", - _0: match.stdout.map(function (e) { + TAG: "Error", + _0: std._0.map(function (e) { return e.toString(); }).join("") }; @@ -410,7 +437,7 @@ async function compilerResults() { ] ]; }); - var errors = await Promise.all(examples.map(async function (param) { + var exampleErrors = await Promise.all(examples.map(async function (param) { var match = param[1]; var nodeTests = await Promise.all(match[0].map(async function (param) { var js = param[1]; @@ -438,7 +465,7 @@ async function compilerResults() { runtimeErrors.concat(match[1]) ]; })); - errors.forEach(function (param) { + exampleErrors.forEach(function (param) { var example = param[0]; var cyan = function (s) { return "\x1b[36m" + s + "\x1b[0m"; @@ -459,10 +486,13 @@ async function compilerResults() { process.stderr.write(e); }); }); - if (errors.length === 0) { - return 0; - } else { + var someError = exampleErrors.some(function (param) { + return param[1].length > 0; + }); + if (someError) { return 1; + } else { + return 0; } } diff --git a/scripts/DocTests.res b/scripts/DocTests.res index 0e970bf2..1479a9c2 100644 --- a/scripts/DocTests.res +++ b/scripts/DocTests.res @@ -297,7 +297,7 @@ let getCodeBlocks = example => { } let runtimeTests = async code => { - let {stdout, stderr} = await SpawnAsync.run( + let {stdout, stderr, code: exitCode} = await SpawnAsync.run( ~command="node", ~args=["-e", code], ~options={ @@ -306,17 +306,25 @@ let runtimeTests = async code => { }, ) - switch Array.length(stderr) > 0 { - | true => - stderr + // Some expressions, like, `console.error("error")` is printed to stderr but + // exit code is 0 + let std = switch exitCode->Null.toOption { + | Some(exitCode) if exitCode == 0.0 && Array.length(stderr) > 0 => stderr->Ok + | Some(exitCode) if exitCode == 0.0 => stdout->Ok + | None | Some(_) => Error(Array.length(stderr) > 0 ? stderr : stdout) + } + + switch std { + | Ok(buf) => + buf ->Array.map(e => e->Buffer.toString) ->Array.join("") - ->Error - | false => - stdout + ->Ok + | Error(buf) => + buf ->Array.map(e => e->Buffer.toString) ->Array.join("") - ->Ok + ->Error } } @@ -363,7 +371,7 @@ let compilerResults = async () => { (example, (compiled, errors)) }) - let errors = + let exampleErrors = await examples ->Array.map(async ((example, (compiled, errors))) => { let nodeTests = @@ -383,7 +391,7 @@ let compilerResults = async () => { ->Promise.all // Print Errors - let () = errors->Array.forEach(((example, errors)) => { + let () = exampleErrors->Array.forEach(((example, errors)) => { let red = s => `\x1B[1;31m${s}\x1B[0m` let cyan = s => `\x1b[36m${s}\x1b[0m` let kind = switch example.kind { @@ -425,7 +433,9 @@ ${error->indentOutputCode} errorMessage->Array.forEach(e => Process.stderrWrite(e)) }) - errors->Array.length == 0 ? 0 : 1 + let someError = exampleErrors->Array.some(((_, err)) => Array.length(err) > 0) + + someError ? 1 : 0 } let exitCode = await compilerResults()