Merge pull request #21038 from Snuffleupagus/add_test-calculateMD5

Use the `calculateMD5` helper, from `test/downloadutils.mjs`, in `test/add_test.mjs`
This commit is contained in:
Tim van der Meij 2026-04-04 14:01:55 +02:00 committed by GitHub
commit e10f11bd3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 47 deletions

View File

@ -1,4 +1,4 @@
import crypto from "crypto"; import { calculateMD5 } from "./downloadutils.mjs";
import { execSync } from "child_process"; import { execSync } from "child_process";
import fs from "fs"; import fs from "fs";
@ -22,36 +22,15 @@ if (!fs.existsSync(file)) {
throw new Error(`PDF file does not exist '${file}'.`); throw new Error(`PDF file does not exist '${file}'.`);
} }
function calculateMD5(pdfFile, callback) {
const hash = crypto.createHash("md5");
const stream = fs.createReadStream(pdfFile);
stream.on("data", function (data) {
hash.update(data);
});
stream.on("error", function (err) {
callback(err);
});
stream.on("end", function () {
const result = hash.digest("hex");
callback(null, result);
});
}
function getRandomArbitrary(min, max) { function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min) + min); return Math.floor(Math.random() * (max - min) + min);
} }
calculateMD5(file, (err, md5) => { const md5 = await calculateMD5(file);
if (err) {
throw new Error(err);
}
let contents = fs.readFileSync(gitIgnore, "utf8").split("\n"); let contents = fs.readFileSync(gitIgnore, "utf8").split("\n");
const randomLine = getRandomArbitrary(10, contents.length - 2); const randomLine = getRandomArbitrary(10, contents.length - 2);
contents.splice( contents.splice(randomLine, 0, "!" + file.substring(file.lastIndexOf("/") + 1));
randomLine,
0,
"!" + file.substring(file.lastIndexOf("/") + 1)
);
fs.writeFileSync("test/pdfs/.gitignore", contents.join("\n")); fs.writeFileSync("test/pdfs/.gitignore", contents.join("\n"));
contents = fs.readFileSync(testManifest, "utf8"); contents = fs.readFileSync(testManifest, "utf8");
@ -71,4 +50,3 @@ calculateMD5(file, (err, md5) => {
fs.writeFileSync("test/test_manifest.json", out); fs.writeFileSync("test/test_manifest.json", out);
execSync(`git add ${testManifest} ${gitIgnore}`); execSync(`git add ${testManifest} ${gitIgnore}`);
execSync(`git add ${file}`); execSync(`git add ${file}`);
});

View File

@ -128,4 +128,4 @@ async function verifyManifestFiles(manifest) {
} }
} }
export { downloadManifestFiles, verifyManifestFiles }; export { calculateMD5, downloadManifestFiles, verifyManifestFiles };