[ESM] Convert *most* of test-folder to use standard modules

This commit is contained in:
Jonas Jenwald 2023-07-08 12:44:37 +02:00
parent 5696c3aa3a
commit a209ce811d
8 changed files with 72 additions and 77 deletions

View File

@ -634,7 +634,7 @@ function createTestSource(testsName, { bot = false, xfaOnly = false } = {}) {
const PDF_TEST = process.env.PDF_TEST || "test_manifest.json";
let forceNoChrome = false;
const args = ["test.js"];
const args = ["test.mjs"];
switch (testsName) {
case "browser":
if (!bot) {
@ -686,7 +686,7 @@ function makeRef(done, bot) {
console.log("### Creating reference images");
let forceNoChrome = false;
const args = ["test.js", "--masterMode"];
const args = ["test.mjs", "--masterMode"];
if (bot) {
const os = process.env.OS;
if (/windows/i.test(os)) {
@ -1985,11 +1985,11 @@ gulp.task(
gulp.series("dev-sandbox")
);
},
function createServer() {
async function createServer() {
console.log();
console.log("### Starting local server");
const WebServer = require("./test/webserver.js").WebServer;
const { WebServer } = await import("./test/webserver.mjs");
const server = new WebServer();
server.port = 8888;
server.start();

View File

@ -1,6 +1,6 @@
const fs = require("fs");
const crypto = require("crypto");
const execSync = require("child_process").execSync;
import crypto from "crypto";
import { execSync } from "child_process";
import fs from "fs";
const testManifest = "test/test_manifest.json";
const pdfFolder = "test/pdfs/";

View File

@ -15,12 +15,11 @@
*/
/* eslint-disable no-var */
"use strict";
var fs = require("fs");
var crypto = require("crypto");
var http = require("http");
var https = require("https");
import crypto from "crypto";
import fs from "fs";
import http from "http";
import https from "https";
import { resolve as urlResolve } from "url";
function rewriteWebArchiveUrl(url) {
// Web Archive URLs need to be transformed to add `if_` after the ID.
@ -51,7 +50,7 @@ function downloadFile(file, url, callback, redirects) {
callback("Too many redirects");
}
var redirectTo = response.headers.location;
redirectTo = require("url").resolve(url, redirectTo);
redirectTo = urlResolve(url, redirectTo);
downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
return;
}
@ -186,5 +185,4 @@ function verifyManifestFiles(manifest, callback) {
verifyNext();
}
exports.downloadManifestFiles = downloadManifestFiles;
exports.verifyManifestFiles = verifyManifestFiles;
export { downloadManifestFiles, verifyManifestFiles };

View File

@ -14,11 +14,12 @@
* limitations under the License.
*/
"use strict";
import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
import { spawn } from "child_process";
const fs = require("fs");
const path = require("path");
const spawn = require("child_process").spawn;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ttxResourcesHome = path.join(__dirname, "..", "ttx");
@ -64,11 +65,7 @@ function runTtx(ttxResourcesHomePath, fontPath, registerOnCancel, callback) {
});
}
exports.translateFont = function translateFont(
content,
registerOnCancel,
callback
) {
function translateFont(content, registerOnCancel, callback) {
const buffer = Buffer.from(content, "base64");
const taskId = (nextTTXTaskId++).toString();
const fontPath = path.join(ttxResourcesHome, taskId + ".otf");
@ -87,4 +84,6 @@ exports.translateFont = function translateFont(
fs.unlinkSync(resultPath);
}
});
};
}
export { translateFont };

View File

@ -13,9 +13,7 @@
* limitations under the License.
*/
"use strict";
const Jasmine = require("jasmine");
import Jasmine from "jasmine";
async function runTests(results) {
const jasmine = new Jasmine();
@ -57,4 +55,4 @@ async function runTests(results) {
return jasmine.execute();
}
exports.runTests = runTests;
export { runTests };

View File

@ -15,18 +15,24 @@
*/
/* eslint-disable no-var */
"use strict";
import { copySubtreeSync, ensureDirSync, removeDirSync } from "./testutils.mjs";
import {
downloadManifestFiles,
verifyManifestFiles,
} from "./downloadutils.mjs";
import dns from "dns";
import fs from "fs";
import os from "os";
import path from "path";
import puppeteer from "puppeteer";
import readline from "readline";
import rimraf from "rimraf";
import { translateFont } from "./font/ttxdriver.mjs";
import url from "url";
import { WebServer } from "./webserver.mjs";
import yargs from "yargs";
var WebServer = require("./webserver.js").WebServer;
var path = require("path");
var fs = require("fs");
var os = require("os");
var puppeteer = require("puppeteer");
var url = require("url");
var testUtils = require("./testutils.js");
const dns = require("dns");
const readline = require("readline");
const yargs = require("yargs");
const rimrafSync = rimraf.sync;
// Chrome uses host `127.0.0.1` in the browser's websocket endpoint URL while
// Firefox uses `localhost`, which before Node.js 17 also resolved to the IPv4
@ -44,7 +50,7 @@ if (dns.setDefaultResultOrder !== undefined) {
}
function parseOptions() {
yargs
const parsedArgs = yargs(process.argv)
.usage("Usage: $0")
.option("downloadOnly", {
default: false,
@ -186,11 +192,7 @@ function parseOptions() {
);
});
const result = yargs.argv;
if (result.help) {
yargs.showHelp();
process.exit(0);
}
const result = parsedArgs.argv;
result.testfilter = Array.isArray(result.testfilter)
? result.testfilter
: [result.testfilter];
@ -219,9 +221,9 @@ function monitorBrowserTimeout(session, onTimeout) {
function updateRefImages() {
function sync(removeTmp) {
console.log(" Updating ref/ ... ");
testUtils.copySubtreeSync(refsTmpDir, refsDir);
copySubtreeSync(refsTmpDir, refsDir);
if (removeTmp) {
testUtils.removeDirSync(refsTmpDir);
removeDirSync(refsTmpDir);
}
console.log("done");
}
@ -325,7 +327,7 @@ function startRefTest(masterMode, showRefImages) {
fs.unlinkSync(eqLog);
}
if (fs.existsSync(testResultDir)) {
testUtils.removeDirSync(testResultDir);
removeDirSync(testResultDir);
}
startTime = Date.now();
@ -356,7 +358,7 @@ function startRefTest(masterMode, showRefImages) {
function checkRefsTmp() {
if (masterMode && fs.existsSync(refsTmpDir)) {
if (options.noPrompts) {
testUtils.removeDirSync(refsTmpDir);
removeDirSync(refsTmpDir);
setup();
return;
}
@ -368,7 +370,7 @@ function startRefTest(masterMode, showRefImages) {
"SHOULD THIS SCRIPT REMOVE tmp/? THINK CAREFULLY [yn] ",
function (answer) {
if (answer.toLowerCase() === "y") {
testUtils.removeDirSync(refsTmpDir);
removeDirSync(refsTmpDir);
}
setup();
reader.close();
@ -483,7 +485,7 @@ function checkEq(task, results, browser, masterMode) {
" != reference rendering"
);
testUtils.ensureDirSync(testSnapshotDir);
ensureDirSync(testSnapshotDir);
fs.writeFileSync(
path.join(testSnapshotDir, page + 1 + ".png"),
testSnapshot
@ -521,7 +523,7 @@ function checkEq(task, results, browser, masterMode) {
browser,
taskId
);
testUtils.ensureDirSync(tmpSnapshotDir);
ensureDirSync(tmpSnapshotDir);
fs.writeFileSync(
path.join(tmpSnapshotDir, page + 1 + ".png"),
testSnapshot
@ -811,7 +813,7 @@ async function startIntegrationTest() {
onAllSessionsClosed = onAllSessionsClosedAfterTests("integration");
startServer();
const { runTests } = require("./integration-boot.js");
const { runTests } = await import("./integration-boot.mjs");
await startBrowsers(function (session) {
session.numRuns = 0;
session.numErrors = 0;
@ -844,7 +846,6 @@ function unitTestPostHandler(req, res) {
});
req.on("end", function () {
if (pathname === "/ttx") {
var translateFont = require("./font/ttxdriver.js").translateFont;
var onCancel = null,
ttxTimeout = 10000;
var timeoutId = setTimeout(function () {
@ -1021,8 +1022,7 @@ async function closeSession(browser) {
});
if (allClosed) {
if (tempDir) {
const rimraf = require("rimraf");
rimraf.sync(tempDir);
rimrafSync(tempDir);
}
onAllSessionsClosed?.();
}
@ -1030,10 +1030,9 @@ async function closeSession(browser) {
}
function ensurePDFsDownloaded(callback) {
var downloadUtils = require("./downloadutils.js");
var manifest = getTestManifest();
downloadUtils.downloadManifestFiles(manifest, function () {
downloadUtils.verifyManifestFiles(manifest, function (hasErrors) {
downloadManifestFiles(manifest, function () {
verifyManifestFiles(manifest, function (hasErrors) {
if (hasErrors) {
console.log(
"Unable to verify the checksum for the files that are " +

View File

@ -14,20 +14,20 @@
* limitations under the License.
*/
"use strict";
import fs from "fs";
import path from "path";
import rimraf from "rimraf";
const fs = require("fs");
const path = require("path");
const rimrafSync = require("rimraf").sync;
const rimrafSync = rimraf.sync;
exports.removeDirSync = function removeDirSync(dir) {
function removeDirSync(dir) {
fs.readdirSync(dir); // Will throw if dir is not a directory
rimrafSync(dir, {
disableGlob: true,
});
};
}
exports.copySubtreeSync = function copySubtreeSync(src, dest) {
function copySubtreeSync(src, dest) {
const files = fs.readdirSync(src);
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
@ -42,9 +42,9 @@ exports.copySubtreeSync = function copySubtreeSync(src, dest) {
fs.writeFileSync(file, fs.readFileSync(srcFile));
}
});
};
}
exports.ensureDirSync = function ensureDirSync(dir) {
function ensureDirSync(dir) {
if (fs.existsSync(dir)) {
return;
}
@ -61,4 +61,6 @@ exports.ensureDirSync = function ensureDirSync(dir) {
fs.mkdirSync(parts.slice(0, i).join(path.sep));
i++;
}
};
}
export { copySubtreeSync, ensureDirSync, removeDirSync };

View File

@ -15,16 +15,15 @@
*/
/* eslint-disable no-var */
"use strict";
var http = require("http");
var path = require("path");
var fs = require("fs");
import fs from "fs";
import http from "http";
import path from "path";
var mimeTypes = {
".css": "text/css",
".html": "text/html",
".js": "application/javascript",
".mjs": "application/javascript",
".json": "application/json",
".svg": "image/svg+xml",
".pdf": "application/pdf",
@ -352,4 +351,4 @@ function crossOriginHandler(req, res) {
}
}
exports.WebServer = WebServer;
export { WebServer };