Merge pull request #14387 from timvandermeij/test-utils

Modernize the test utilities
This commit is contained in:
Tim van der Meij 2021-12-18 16:40:56 +01:00 committed by GitHub
commit a2ae56f394
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 93 deletions

View File

@ -25,6 +25,7 @@ var puppeteer = require("puppeteer");
var url = require("url"); var url = require("url");
var testUtils = require("./testutils.js"); var testUtils = require("./testutils.js");
const dns = require("dns"); const dns = require("dns");
const readline = require("readline");
const yargs = require("yargs"); const yargs = require("yargs");
// Chrome uses host `127.0.0.1` in the browser's websocket endpoint URL while // Chrome uses host `127.0.0.1` in the browser's websocket endpoint URL while
@ -229,14 +230,17 @@ function updateRefImages() {
sync(false); // don't remove tmp/ for botio sync(false); // don't remove tmp/ for botio
return; return;
} }
testUtils.confirm(
const reader = readline.createInterface(process.stdin, process.stdout);
reader.question(
"Would you like to update the master copy in ref/? [yn] ", "Would you like to update the master copy in ref/? [yn] ",
function (confirmed) { function (answer) {
if (confirmed) { if (answer.toLowerCase() === "y") {
sync(true); sync(true);
} else { } else {
console.log(" OK, not updating."); console.log(" OK, not updating.");
} }
reader.close();
} }
); );
} }
@ -358,13 +362,16 @@ function startRefTest(masterMode, showRefImages) {
} }
console.log("Temporary snapshot dir tmp/ is still around."); console.log("Temporary snapshot dir tmp/ is still around.");
console.log("tmp/ can be removed if it has nothing you need."); console.log("tmp/ can be removed if it has nothing you need.");
testUtils.confirm(
const reader = readline.createInterface(process.stdin, process.stdout);
reader.question(
"SHOULD THIS SCRIPT REMOVE tmp/? THINK CAREFULLY [yn] ", "SHOULD THIS SCRIPT REMOVE tmp/? THINK CAREFULLY [yn] ",
function (confirmed) { function (answer) {
if (confirmed) { if (answer.toLowerCase() === "y") {
testUtils.removeDirSync(refsTmpDir); testUtils.removeDirSync(refsTmpDir);
} }
setup(); setup();
reader.close();
} }
); );
} else { } else {

View File

@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint-disable no-var */
"use strict"; "use strict";
var fs = require("fs"); const fs = require("fs");
var path = require("path"); const path = require("path");
var rimrafSync = require("rimraf").sync; const rimrafSync = require("rimraf").sync;
exports.removeDirSync = function removeDirSync(dir) { exports.removeDirSync = function removeDirSync(dir) {
fs.readdirSync(dir); // Will throw if dir is not a directory fs.readdirSync(dir); // Will throw if dir is not a directory
@ -29,14 +28,14 @@ exports.removeDirSync = function removeDirSync(dir) {
}; };
exports.copySubtreeSync = function copySubtreeSync(src, dest) { exports.copySubtreeSync = function copySubtreeSync(src, dest) {
var files = fs.readdirSync(src); const files = fs.readdirSync(src);
if (!fs.existsSync(dest)) { if (!fs.existsSync(dest)) {
fs.mkdirSync(dest); fs.mkdirSync(dest);
} }
files.forEach(function (filename) { files.forEach(function (filename) {
var srcFile = path.join(src, filename); const srcFile = path.join(src, filename);
var file = path.join(dest, filename); const file = path.join(dest, filename);
var stats = fs.statSync(srcFile); const stats = fs.statSync(srcFile);
if (stats.isDirectory()) { if (stats.isDirectory()) {
copySubtreeSync(srcFile, file); copySubtreeSync(srcFile, file);
} else { } else {
@ -49,8 +48,8 @@ exports.ensureDirSync = function ensureDirSync(dir) {
if (fs.existsSync(dir)) { if (fs.existsSync(dir)) {
return; return;
} }
var parts = dir.split(path.sep), const parts = dir.split(path.sep);
i = parts.length; let i = parts.length;
while (i > 1 && !fs.existsSync(parts.slice(0, i - 1).join(path.sep))) { while (i > 1 && !fs.existsSync(parts.slice(0, i - 1).join(path.sep))) {
i--; i--;
} }
@ -63,80 +62,3 @@ exports.ensureDirSync = function ensureDirSync(dir) {
i++; i++;
} }
}; };
var stdinBuffer = "",
endOfStdin = false,
stdinInitialized = false;
var stdinOnLineCallbacks = [];
function handleStdinBuffer() {
var callback;
if (endOfStdin) {
if (stdinBuffer && stdinOnLineCallbacks.length > 0) {
callback = stdinOnLineCallbacks.shift();
callback(stdinBuffer);
stdinBuffer = null;
}
while (stdinOnLineCallbacks.length > 0) {
callback = stdinOnLineCallbacks.shift();
callback();
}
return;
}
while (stdinOnLineCallbacks.length > 0) {
var i = stdinBuffer.indexOf("\n");
if (i < 0) {
return;
}
callback = stdinOnLineCallbacks.shift();
var result = stdinBuffer.substring(0, i + 1);
stdinBuffer = stdinBuffer.substring(i + 1);
callback(result);
}
// all callbacks handled, stop stdin processing
process.stdin.pause();
}
function initStdin() {
process.stdin.setEncoding("utf8");
process.stdin.on("data", function (chunk) {
stdinBuffer += chunk;
handleStdinBuffer();
});
process.stdin.on("end", function () {
endOfStdin = true;
handleStdinBuffer();
});
}
exports.prompt = function prompt(message, callback) {
if (!stdinInitialized) {
process.stdin.resume();
initStdin();
stdinInitialized = true;
} else if (stdinOnLineCallbacks.length === 0) {
process.stdin.resume();
}
process.stdout.write(message);
stdinOnLineCallbacks.push(callback);
handleStdinBuffer();
};
exports.confirm = function confirm(message, callback) {
exports.prompt(message, function (answer) {
if (answer === undefined) {
callback();
return;
}
if (answer[0].toLowerCase() === "y") {
callback(true);
} else if (answer[0].toLowerCase() === "n") {
callback(false);
} else {
confirm(message, callback);
}
});
};