From 606d25f6461399fbca03649b96d955cf80c82891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 21 Dec 2021 01:49:49 +0100 Subject: [PATCH] Better synchronization in `@babel/cli` interactive tests --- .../executor.js | 50 +++++++++++++------ .../executor.js | 44 ++++++++++------ packages/babel-cli/test/index.js | 31 ++++++------ 3 files changed, 82 insertions(+), 43 deletions(-) diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/executor.js b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/executor.js index 56aad317fc09..ec654248d223 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/executor.js +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/executor.js @@ -1,25 +1,47 @@ const fs = require("fs"); +const assert = require("assert"); -// Wait for the initial build -sleep(300); +// For Node.js <= 10 +if (!assert.match) assert.match = (val, re) => assert(re.test(val)); -logFile("lib/index.js"); -logFile("lib/main.js"); +const run = (function* () { + let files = [yield, yield].sort(); + assert.match(files[0], /src[\\/]index.js -> lib[\\/]index.js/); + assert.match(files[1], /src[\\/]main.js -> lib[\\/]main.js/); + assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./); -fs.writeFileSync("./file.txt", "Updated!"); + logFile("lib/index.js"); + logFile("lib/main.js"); -// Wait for the new build -sleep(300); + fs.writeFileSync("./file.txt", "Updated!"); -logFile("lib/index.js"); -logFile("lib/main.js"); + files = [yield, yield].sort(); + assert.match(files[0], /src[\\/]index.js -> lib[\\/]index.js/); + assert.match(files[1], /src[\\/]main.js -> lib[\\/]main.js/); + assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./); + + logFile("lib/index.js"); + logFile("lib/main.js"); +})(); + +run.next(); + +process.stdin.on("data", function listener(chunk) { + const str = String(chunk).trim(); + if (!str) return; + + console.log(str); + + if (run.next(str).done) { + process.exit(0); + } +}); function logFile(file) { console.log("EXECUTOR", file, JSON.stringify(fs.readFileSync(file, "utf8"))); } -function sleep(ms) { - const arr = new Int32Array(new SharedArrayBuffer(4)); - arr[0] = 0; - Atomics.wait(arr, 0, 0, ms); -} +setTimeout(() => { + console.error("EXECUTOR TIMEOUT"); + process.exit(1); +}, 5000); diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch with external dependencies/executor.js b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch with external dependencies/executor.js index 56aad317fc09..474cd214405c 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch with external dependencies/executor.js +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch with external dependencies/executor.js @@ -1,25 +1,41 @@ const fs = require("fs"); +const assert = require("assert"); -// Wait for the initial build -sleep(300); +// For Node.js <= 10 +if (!assert.match) assert.match = (val, re) => assert(re.test(val)); -logFile("lib/index.js"); -logFile("lib/main.js"); +const run = function* () { + assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./); -fs.writeFileSync("./file.txt", "Updated!"); + logFile("lib/index.js"); + logFile("lib/main.js"); -// Wait for the new build -sleep(300); + fs.writeFileSync("./file.txt", "Updated!"); -logFile("lib/index.js"); -logFile("lib/main.js"); + assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./); + + logFile("lib/index.js"); + logFile("lib/main.js"); +}(); + +run.next(); + +process.stdin.on("data", function listener(chunk) { + const str = String(chunk).trim(); + if (!str) return; + + console.log(str); + + if (run.next(str).done) { + process.exit(0); + } +}); function logFile(file) { console.log("EXECUTOR", file, JSON.stringify(fs.readFileSync(file, "utf8"))); } -function sleep(ms) { - const arr = new Int32Array(new SharedArrayBuffer(4)); - arr[0] = 0; - Atomics.wait(arr, 0, 0, ms); -} +setTimeout(() => { + console.error("EXECUTOR TIMEOUT"); + process.exit(1); +}, 5000); diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index 41097f412d41..6251fd33ffd6 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -152,14 +152,6 @@ const buildTest = function (binName, testName, opts) { let stderr = ""; let stdout = ""; - spawn.stderr.on("data", function (chunk) { - stderr += chunk; - }); - - spawn.stdout.on("data", function (chunk) { - stdout += chunk; - }); - spawn.on("close", function () { let err; @@ -182,22 +174,31 @@ const buildTest = function (binName, testName, opts) { spawn.stdin.end(); } - if (opts.executor) { - const executor = child.spawn(process.execPath, [opts.executor], { - cwd: tmpLoc, - }); - - executor.stderr.on("data", function (chunk) { + const captureOutput = proc => { + proc.stderr.on("data", function (chunk) { stderr += chunk; }); - executor.stdout.on("data", function (chunk) { + proc.stdout.on("data", function (chunk) { stdout += chunk; }); + }; + + if (opts.executor) { + const executor = child.spawn(process.execPath, [opts.executor], { + cwd: tmpLoc, + }); + + spawn.stdout.pipe(executor.stdin); + spawn.stderr.pipe(executor.stdin); executor.on("close", function () { setTimeout(() => spawn.kill("SIGINT"), 250); }); + + captureOutput(executor); + } else { + captureOutput(spawn); } }; };