From 0a10a7b57b3aaa020283118e9f69c883dddc0863 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sun, 17 Dec 2023 20:26:53 +0100 Subject: [PATCH] Modernize the `downloadManifestFiles`/`ensurePDFsDownloaded` test helper functions The test helper code largely predates the introduction of modern JavaScript features and should be refactored to improve readability. In particular callbacks make the code harder to understand and maintain. This commit: - replaces the callback argument with returning a promise; - replaces the recursive function calls with a simple loop; - uses `const`/`let` instead of `var`; - uses arrow functions for shorter code; - uses template strings for shorter string formatting code. --- test/downloadutils.mjs | 37 ++++++++------------------ test/test.mjs | 59 +++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 58 deletions(-) diff --git a/test/downloadutils.mjs b/test/downloadutils.mjs index c389b40d8..f14cadb90 100644 --- a/test/downloadutils.mjs +++ b/test/downloadutils.mjs @@ -73,15 +73,17 @@ function downloadFile(file, url, redirects = 0) { }); } -function downloadManifestFiles(manifest, callback) { - async function downloadNext() { - if (i >= links.length) { - callback(); - return; - } - var file = links[i].file; - var url = links[i].url; - console.log("Downloading " + url + " to " + file + "..."); +async function downloadManifestFiles(manifest) { + const links = manifest + .filter(item => item.link && !fs.existsSync(item.file)) + .map(item => { + let url = fs.readFileSync(`${item.file}.link`).toString(); + url = url.replace(/\s+$/, ""); + return { file: item.file, url }; + }); + + for (const { file, url } of links) { + console.log(`Downloading ${url} to ${file}...`); try { await downloadFile(file, url); } catch (ex) { @@ -89,24 +91,7 @@ function downloadManifestFiles(manifest, callback) { fs.writeFileSync(file, ""); // making it empty file fs.writeFileSync(`${file}.error`, ex); } - i++; - downloadNext(); } - - var links = manifest - .filter(function (item) { - return item.link && !fs.existsSync(item.file); - }) - .map(function (item) { - var file = item.file; - var linkfile = file + ".link"; - var url = fs.readFileSync(linkfile).toString(); - url = url.replace(/\s+$/, ""); - return { file, url }; - }); - - var i = 0; - downloadNext(); } function calculateMD5(file) { diff --git a/test/test.mjs b/test/test.mjs index 711adfa01..9595d63a7 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -268,7 +268,7 @@ function examineRefImages() { }); } -function startRefTest(masterMode, showRefImages) { +async function startRefTest(masterMode, showRefImages) { function finalize() { stopServer(); let numRuns = 0; @@ -402,11 +402,10 @@ function startRefTest(masterMode, showRefImages) { if (!manifest) { return; } - if (options.noDownload) { - checkRefsTmp(); - } else { - ensurePDFsDownloaded(checkRefsTmp); + if (!options.noDownload) { + await ensurePDFsDownloaded(); } + checkRefsTmp(); } function handleSessionTimeout(session) { @@ -1071,47 +1070,43 @@ async function closeSession(browser) { } } -function ensurePDFsDownloaded(callback) { - var manifest = getTestManifest(); - downloadManifestFiles(manifest, async function () { - try { - await verifyManifestFiles(manifest); - } catch { - console.log( - "Unable to verify the checksum for the files that are " + - "used for testing." - ); - console.log( - "Please re-download the files, or adjust the MD5 " + - "checksum in the manifest for the files listed above.\n" - ); - if (options.strictVerify) { - process.exit(1); - } +async function ensurePDFsDownloaded() { + const manifest = getTestManifest(); + await downloadManifestFiles(manifest); + try { + await verifyManifestFiles(manifest); + } catch { + console.log( + "Unable to verify the checksum for the files that are " + + "used for testing." + ); + console.log( + "Please re-download the files, or adjust the MD5 " + + "checksum in the manifest for the files listed above.\n" + ); + if (options.strictVerify) { + process.exit(1); } - callback(); - }); + } } -function main() { +async function main() { if (options.statsFile) { stats = []; } if (options.downloadOnly) { - ensurePDFsDownloaded(function () {}); + await ensurePDFsDownloaded(); } else if (options.unitTest) { // Allows linked PDF files in unit-tests as well. - ensurePDFsDownloaded(function () { - startUnitTest("/test/unit/unit_test.html", "unit"); - }); + await ensurePDFsDownloaded(); + startUnitTest("/test/unit/unit_test.html", "unit"); } else if (options.fontTest) { startUnitTest("/test/font/font_test.html", "font"); } else if (options.integration) { // Allows linked PDF files in integration-tests as well. - ensurePDFsDownloaded(function () { - startIntegrationTest(); - }); + await ensurePDFsDownloaded(); + startIntegrationTest(); } else { startRefTest(options.masterMode, options.reftest); }