Merge pull request #17431 from timvandermeij/modernize-test

Modernize the code in the `test/downloadutils.mjs` file
This commit is contained in:
Tim van der Meij 2023-12-17 22:07:09 +01:00 committed by GitHub
commit 34eb6fecac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 101 deletions

View File

@ -13,7 +13,6 @@
* 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 */
import crypto from "crypto"; import crypto from "crypto";
import fs from "fs"; import fs from "fs";
@ -25,89 +24,73 @@ function rewriteWebArchiveUrl(url) {
// Web Archive URLs need to be transformed to add `if_` after the ID. // Web Archive URLs need to be transformed to add `if_` after the ID.
// Without this, an HTML page containing an iframe with the PDF file // Without this, an HTML page containing an iframe with the PDF file
// will be served instead (issue 8920). // will be served instead (issue 8920).
var webArchiveRegex = const webArchiveRegex =
/(^https?:\/\/web\.archive\.org\/web\/)(\d+)(\/https?:\/\/.+)/g; /(^https?:\/\/web\.archive\.org\/web\/)(\d+)(\/https?:\/\/.+)/g;
var urlParts = webArchiveRegex.exec(url); const urlParts = webArchiveRegex.exec(url);
if (urlParts) { if (urlParts) {
return urlParts[1] + (urlParts[2] + "if_") + urlParts[3]; return `${urlParts[1]}${urlParts[2]}if_${urlParts[3]}`;
} }
return url; return url;
} }
function downloadFile(file, url, callback, redirects) { function downloadFile(file, url, redirects = 0) {
url = rewriteWebArchiveUrl(url); url = rewriteWebArchiveUrl(url);
const protocol = /^https:\/\//.test(url) ? https : http;
var protocol = /^https:\/\//.test(url) ? https : http; return new Promise((resolve, reject) => {
protocol protocol
.get(url, function (response) { .get(url, async function (response) {
if ( if ([301, 302, 307, 308].includes(response.statusCode)) {
response.statusCode === 301 || if (redirects > 10) {
response.statusCode === 302 || reject(new Error("Too many redirects"));
response.statusCode === 307 || return;
response.statusCode === 308 }
) { const redirectTo = urlResolve(url, response.headers.location);
if (redirects > 10) { try {
callback("Too many redirects"); await downloadFile(file, redirectTo, ++redirects);
resolve();
} catch (ex) {
reject(ex);
}
return;
} }
var redirectTo = response.headers.location;
redirectTo = urlResolve(url, redirectTo);
downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
return;
}
if (response.statusCode !== 200) { if (response.statusCode !== 200) {
callback("HTTP " + response.statusCode); reject(new Error(`HTTP ${response.statusCode}`));
return; return;
} }
var stream = fs.createWriteStream(file);
stream.on("error", function (err) { const stream = fs.createWriteStream(file);
callback(err); stream.on("error", error => reject(error));
}); stream.on("finish", () => {
response.pipe(stream); stream.end();
stream.on("finish", function () { resolve();
stream.end(); });
callback(); response.pipe(stream);
}); })
}) .on("error", error => reject(error));
.on("error", function (err) { });
callback(err);
});
} }
function downloadManifestFiles(manifest, callback) { async function downloadManifestFiles(manifest) {
function downloadNext() { const links = manifest
if (i >= links.length) { .filter(item => item.link && !fs.existsSync(item.file))
callback(); .map(item => {
return; let url = fs.readFileSync(`${item.file}.link`).toString();
}
var file = links[i].file;
var url = links[i].url;
console.log("Downloading " + url + " to " + file + "...");
downloadFile(file, url, function (err) {
if (err) {
console.error("Error during downloading of " + url + ": " + err);
fs.writeFileSync(file, ""); // making it empty file
fs.writeFileSync(file + ".error", err);
}
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+$/, ""); url = url.replace(/\s+$/, "");
return { file, url }; return { file: item.file, url };
}); });
var i = 0; for (const { file, url } of links) {
downloadNext(); console.log(`Downloading ${url} to ${file}...`);
try {
await downloadFile(file, url);
} catch (ex) {
console.error(`Error during downloading of ${url}: ${ex}`);
fs.writeFileSync(file, ""); // making it empty file
fs.writeFileSync(`${file}.error`, ex);
}
}
} }
function calculateMD5(file) { function calculateMD5(file) {

View File

@ -268,7 +268,7 @@ function examineRefImages() {
}); });
} }
function startRefTest(masterMode, showRefImages) { async function startRefTest(masterMode, showRefImages) {
function finalize() { function finalize() {
stopServer(); stopServer();
let numRuns = 0; let numRuns = 0;
@ -402,11 +402,10 @@ function startRefTest(masterMode, showRefImages) {
if (!manifest) { if (!manifest) {
return; return;
} }
if (options.noDownload) { if (!options.noDownload) {
checkRefsTmp(); await ensurePDFsDownloaded();
} else {
ensurePDFsDownloaded(checkRefsTmp);
} }
checkRefsTmp();
} }
function handleSessionTimeout(session) { function handleSessionTimeout(session) {
@ -1071,47 +1070,43 @@ async function closeSession(browser) {
} }
} }
function ensurePDFsDownloaded(callback) { async function ensurePDFsDownloaded() {
var manifest = getTestManifest(); const manifest = getTestManifest();
downloadManifestFiles(manifest, async function () { await downloadManifestFiles(manifest);
try { try {
await verifyManifestFiles(manifest); await verifyManifestFiles(manifest);
} catch { } catch {
console.log( console.log(
"Unable to verify the checksum for the files that are " + "Unable to verify the checksum for the files that are " +
"used for testing." "used for testing."
); );
console.log( console.log(
"Please re-download the files, or adjust the MD5 " + "Please re-download the files, or adjust the MD5 " +
"checksum in the manifest for the files listed above.\n" "checksum in the manifest for the files listed above.\n"
); );
if (options.strictVerify) { if (options.strictVerify) {
process.exit(1); process.exit(1);
}
} }
callback(); }
});
} }
function main() { async function main() {
if (options.statsFile) { if (options.statsFile) {
stats = []; stats = [];
} }
if (options.downloadOnly) { if (options.downloadOnly) {
ensurePDFsDownloaded(function () {}); await ensurePDFsDownloaded();
} else if (options.unitTest) { } else if (options.unitTest) {
// Allows linked PDF files in unit-tests as well. // Allows linked PDF files in unit-tests as well.
ensurePDFsDownloaded(function () { await ensurePDFsDownloaded();
startUnitTest("/test/unit/unit_test.html", "unit"); startUnitTest("/test/unit/unit_test.html", "unit");
});
} else if (options.fontTest) { } else if (options.fontTest) {
startUnitTest("/test/font/font_test.html", "font"); startUnitTest("/test/font/font_test.html", "font");
} else if (options.integration) { } else if (options.integration) {
// Allows linked PDF files in integration-tests as well. // Allows linked PDF files in integration-tests as well.
ensurePDFsDownloaded(function () { await ensurePDFsDownloaded();
startIntegrationTest(); startIntegrationTest();
});
} else { } else {
startRefTest(options.masterMode, options.reftest); startRefTest(options.masterMode, options.reftest);
} }