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.
This commit is contained in:
parent
86bee4409a
commit
0a10a7b57b
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user