Modernize the verifyManifestFiles test helper function

The test helper code largely predates the introduction of modern
JavaScript features and should be refactored to improve readability.
In particular callbacks and recursive function calls 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 template strings for shorter string formatting code;
- improves the error messages to have more details.
This commit is contained in:
Tim van der Meij 2023-12-03 16:31:45 +01:00
parent c9a923066b
commit ac5667166e
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
2 changed files with 30 additions and 38 deletions

View File

@ -120,30 +120,24 @@ function calculateMD5(file) {
}); });
} }
function verifyManifestFiles(manifest, callback) { async function verifyManifestFiles(manifest) {
async function verifyNext() { let error = false;
if (i >= manifest.length) {
callback(error); for (const item of manifest) {
return; if (fs.existsSync(`${item.file}.error`)) {
}
var item = manifest[i];
if (fs.existsSync(item.file + ".error")) {
console.error( console.error(
'WARNING: File was not downloaded. See "' + item.file + '.error" file.' `WARNING: "${item.file}" was not downloaded; see "${item.file}.error" file.`
); );
error = true; error = true;
i++; continue;
verifyNext();
return;
} }
if (item.link && !fs.existsSync(item.file + ".link")) {
if (item.link && !fs.existsSync(`${item.file}.link`)) {
console.error( console.error(
`WARNING: Unneeded \`"link": true\`-entry for the "${item.id}" test.` `WARNING: Unneeded \`"link": true\`-entry for the "${item.id}" test.`
); );
error = true; error = true;
i++; continue;
verifyNext();
return;
} }
try { try {
@ -165,13 +159,11 @@ function verifyManifestFiles(manifest, callback) {
); );
error = true; error = true;
} }
i++;
verifyNext();
} }
var i = 0;
var error = false; if (error) {
verifyNext(); throw new Error("Manifest validation failed");
}
} }
export { downloadManifestFiles, verifyManifestFiles }; export { downloadManifestFiles, verifyManifestFiles };

View File

@ -1071,23 +1071,23 @@ async function closeSession(browser) {
function ensurePDFsDownloaded(callback) { function ensurePDFsDownloaded(callback) {
var manifest = getTestManifest(); var manifest = getTestManifest();
downloadManifestFiles(manifest, function () { downloadManifestFiles(manifest, async function () {
verifyManifestFiles(manifest, function (hasErrors) { try {
if (hasErrors) { await verifyManifestFiles(manifest);
console.log( } catch {
"Unable to verify the checksum for the files that are " + console.log(
"used for testing." "Unable to verify the checksum for the files that are " +
); "used for testing."
console.log( );
"Please re-download the files, or adjust the MD5 " + console.log(
"checksum in the manifest for the files listed above.\n" "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); if (options.strictVerify) {
} process.exit(1);
} }
callback(); }
}); callback();
}); });
} }