Skip to content

Commit

Permalink
Better synchronization in @babel/cli interactive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 21, 2021
1 parent 67006b4 commit 606d25f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 43 deletions.
@@ -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);
@@ -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);
31 changes: 16 additions & 15 deletions packages/babel-cli/test/index.js
Expand Up @@ -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;

Expand All @@ -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);
}
};
};
Expand Down

0 comments on commit 606d25f

Please sign in to comment.