From 0324675c3ac1ad369d1c1f2902ba5f3b8b213856 Mon Sep 17 00:00:00 2001 From: Ju Liu Date: Thu, 15 Oct 2020 17:22:00 +0100 Subject: [PATCH 1/2] Make diff include which are the files we are comparing --- test/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/run.sh b/test/run.sh index 00fb935c0..7486b7b39 100755 --- a/test/run.sh +++ b/test/run.sh @@ -42,7 +42,7 @@ function runCommandAndCompareToSnapshot { echo -e "\n \e[31mbut got:\e[0m\n" cat "$TMP/$FILE" echo -e "\n \e[31mHere is the difference:\e[0m\n" - diff "$TMP/$FILE" "$SNAPSHOTS/$FILE" + diff -p "$TMP/$FILE" "$SNAPSHOTS/$FILE" exit 1 else echo -e " \e[92mOK\e[0m" From 0c4c68ffb2ff440dd26fbdc8fc98b9b85ed5c8b4 Mon Sep 17 00:00:00 2001 From: Ju Liu Date: Thu, 15 Oct 2020 17:21:40 +0100 Subject: [PATCH 2/2] Ensure we finish writing the report to stdout --- lib/report.js | 48 +++++++++++++++++++++++++++++++++++------------ lib/run-review.js | 2 +- lib/runner.js | 11 ++++++----- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/report.js b/lib/report.js index bb17b712b..e292ff172 100644 --- a/lib/report.js +++ b/lib/report.js @@ -2,7 +2,7 @@ const styledMessage = require('./styled-message'); module.exports = report; -function report(options, result) { +async function report(options, result) { if (options.report === 'json') { return print(options, jsonReport(result.errors)); } @@ -22,22 +22,46 @@ function jsonReport(errors) { function print(options, json) { if (options.reportOnOneLine) { if (json.type === 'review-errors') { - json.errors.forEach((errorForFile) => { - errorForFile.errors.forEach((error) => { - console.log( - JSON.stringify({ - path: errorForFile.path, - ...error + if (json.errors.length > 0) { + return safeConsoleLog( + json.errors + .map((errorForFile) => { + return errorForFile.errors + .map((error) => { + return JSON.stringify({ + path: errorForFile.path, + ...error + }); + }) + .join('\n'); }) - ); - }); - }); + .join('\n') + ); + } } else { - console.log(JSON.stringify(json)); + return safeConsoleLog(JSON.stringify(json)); } } else { - console.log( + return safeConsoleLog( JSON.stringify(json, null, options.debug || options.forTests ? 2 : 0) ); } } + +// Printing large outputs to stdout is not recommended because at times +// console.log is asynchronous and returns before ensuring that the whole +// output has been printed. Check out these links for more details: +// +// - https://nodejs.org/api/process.html#process_process_exit_code +// - https://github.com/nodejs/node/issues/6456 +// - https://github.com/nodejs/node/issues/19218 +// +// Using process.stdout.write and passing a function ensures that +// the whole output has been written. +function safeConsoleLog(message) { + return new Promise((resolve) => { + process.stdout.write(message + '\n', () => { + resolve(); + }); + }); +} diff --git a/lib/run-review.js b/lib/run-review.js index 53726d79a..9cf1feb41 100644 --- a/lib/run-review.js +++ b/lib/run-review.js @@ -23,7 +23,7 @@ async function runReview(options, app) { }); Benchmark.end(options, 'review'); - report(options, result); + await report(options, result); return result.success; } diff --git a/lib/runner.js b/lib/runner.js index ea6db0d8b..136192242 100755 --- a/lib/runner.js +++ b/lib/runner.js @@ -76,11 +76,12 @@ async function initializeApp(options, elmModulePath, reviewElmJson) { autofix.subscribe(options, app); if (options.watch) { AppState.subscribe(app.ports.reviewReport, (result) => { - report(options, result); - const shouldReReview = AppState.reviewFinished(); - if (shouldReReview) { - return startReview(options, app); - } + return report(options, result).then(() => { + const shouldReReview = AppState.reviewFinished(); + if (shouldReReview) { + return startReview(options, app); + } + }); }); }