Merge pull request #17367 from timvandermeij/modernize-test

Modernize the `calculateMD5` and `verifyManifestFiles` test helper functions
This commit is contained in:
Jonas Jenwald 2023-12-04 16:03:19 +01:00 committed by GitHub
commit 476cb84d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 68 deletions

View File

@ -110,79 +110,60 @@ function downloadManifestFiles(manifest, callback) {
downloadNext(); downloadNext();
} }
function calculateMD5(file, callback) { function calculateMD5(file) {
var hash = crypto.createHash("md5"); return new Promise((resolve, reject) => {
var stream = fs.createReadStream(file); const hash = crypto.createHash("md5");
stream.on("data", function (data) { const stream = fs.createReadStream(file);
hash.update(data); stream.on("data", data => hash.update(data));
}); stream.on("error", error => reject(error));
stream.on("error", function (err) { stream.on("end", () => resolve(hash.digest("hex")));
callback(err);
});
stream.on("end", function () {
var result = hash.digest("hex");
callback(null, result);
}); });
} }
function verifyManifestFiles(manifest, callback) { async function verifyManifestFiles(manifest) {
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;
} }
calculateMD5(item.file, function (err, md5) {
if (err) { try {
console.log('WARNING: Unable to open file for reading "' + err + '".'); const md5 = await calculateMD5(item.file);
error = true; if (!item.md5) {
} else if (!item.md5) {
console.error( console.error(
'WARNING: Missing md5 for file "' + `WARNING: MD5 hash missing for "${item.file}" (computed "${md5}").`
item.file +
'". ' +
'Hash for current file is "' +
md5 +
'"'
); );
error = true; error = true;
} else if (md5 !== item.md5) { } else if (md5 !== item.md5) {
console.error( console.error(
'WARNING: MD5 of file "' + `WARNING: MD5 hash mismatch for "${item.file}" (expected "${item.md5}", computed "${md5}").`
item.file +
'" does not match file. Expected "' +
item.md5 +
'" computed "' +
md5 +
'"'
); );
error = true; error = true;
} }
i++; } catch (ex) {
verifyNext(); console.log(
}); `WARNING: MD5 hash calculation failed for "${item.file}" ("${ex}").`
);
error = true;
}
}
if (error) {
throw new Error("Manifest validation failed");
} }
var i = 0;
var error = false;
verifyNext();
} }
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();
}); });
} }