From a209ce811d3e9f042797fce8c06bb582d836885f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 8 Jul 2023 12:44:37 +0200 Subject: [PATCH] [ESM] Convert *most* of `test`-folder to use standard modules --- gulpfile.js | 8 +-- test/{add_test.js => add_test.mjs} | 6 +- test/{downloadutils.js => downloadutils.mjs} | 16 +++-- test/font/{ttxdriver.js => ttxdriver.mjs} | 19 +++--- ...tegration-boot.js => integration-boot.mjs} | 6 +- test/{test.js => test.mjs} | 61 +++++++++---------- test/{testutils.js => testutils.mjs} | 22 ++++--- test/{webserver.js => webserver.mjs} | 11 ++-- 8 files changed, 72 insertions(+), 77 deletions(-) rename test/{add_test.js => add_test.mjs} (94%) rename test/{downloadutils.js => downloadutils.mjs} (94%) rename test/font/{ttxdriver.js => ttxdriver.mjs} (90%) rename test/{integration-boot.js => integration-boot.mjs} (95%) rename test/{test.js => test.mjs} (96%) rename test/{testutils.js => testutils.mjs} (83%) rename test/{webserver.js => webserver.mjs} (98%) diff --git a/gulpfile.js b/gulpfile.js index 20b37a092..f846905db 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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(); diff --git a/test/add_test.js b/test/add_test.mjs similarity index 94% rename from test/add_test.js rename to test/add_test.mjs index bb063f1cb..75c09221e 100644 --- a/test/add_test.js +++ b/test/add_test.mjs @@ -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/"; diff --git a/test/downloadutils.js b/test/downloadutils.mjs similarity index 94% rename from test/downloadutils.js rename to test/downloadutils.mjs index 91bc37de4..548e07ecb 100644 --- a/test/downloadutils.js +++ b/test/downloadutils.mjs @@ -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 }; diff --git a/test/font/ttxdriver.js b/test/font/ttxdriver.mjs similarity index 90% rename from test/font/ttxdriver.js rename to test/font/ttxdriver.mjs index 310512590..cdfbf7d72 100644 --- a/test/font/ttxdriver.js +++ b/test/font/ttxdriver.mjs @@ -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 }; diff --git a/test/integration-boot.js b/test/integration-boot.mjs similarity index 95% rename from test/integration-boot.js rename to test/integration-boot.mjs index 5712814f2..bc4b7e807 100644 --- a/test/integration-boot.js +++ b/test/integration-boot.mjs @@ -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 }; diff --git a/test/test.js b/test/test.mjs similarity index 96% rename from test/test.js rename to test/test.mjs index cc5c3382f..ac40983e5 100644 --- a/test/test.js +++ b/test/test.mjs @@ -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 " + diff --git a/test/testutils.js b/test/testutils.mjs similarity index 83% rename from test/testutils.js rename to test/testutils.mjs index a67472606..d5f3d0020 100644 --- a/test/testutils.js +++ b/test/testutils.mjs @@ -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 }; diff --git a/test/webserver.js b/test/webserver.mjs similarity index 98% rename from test/webserver.js rename to test/webserver.mjs index bf1f2a5e1..9a780a395 100644 --- a/test/webserver.js +++ b/test/webserver.mjs @@ -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 };