mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-25 08:27:19 +02:00
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:
commit
e10f11bd3a
@ -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,53 +22,31 @@ 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");
|
|
||||||
const randomLine = getRandomArbitrary(10, contents.length - 2);
|
|
||||||
contents.splice(
|
|
||||||
randomLine,
|
|
||||||
0,
|
|
||||||
"!" + file.substring(file.lastIndexOf("/") + 1)
|
|
||||||
);
|
|
||||||
fs.writeFileSync("test/pdfs/.gitignore", contents.join("\n"));
|
|
||||||
|
|
||||||
contents = fs.readFileSync(testManifest, "utf8");
|
let contents = fs.readFileSync(gitIgnore, "utf8").split("\n");
|
||||||
const pdf = file.substring(file.lastIndexOf("/") + 1, file.length - 4);
|
const randomLine = getRandomArbitrary(10, contents.length - 2);
|
||||||
const randomPoint = getRandomArbitrary(100, contents.length - 20);
|
contents.splice(randomLine, 0, "!" + file.substring(file.lastIndexOf("/") + 1));
|
||||||
const bracket = contents.indexOf("},\n", randomPoint);
|
fs.writeFileSync("test/pdfs/.gitignore", contents.join("\n"));
|
||||||
const out =
|
|
||||||
contents.substring(0, bracket) +
|
contents = fs.readFileSync(testManifest, "utf8");
|
||||||
"},\n" +
|
const pdf = file.substring(file.lastIndexOf("/") + 1, file.length - 4);
|
||||||
` { "id": "${pdf}",\n` +
|
const randomPoint = getRandomArbitrary(100, contents.length - 20);
|
||||||
` "file": "pdfs/${pdf}.pdf",\n` +
|
const bracket = contents.indexOf("},\n", randomPoint);
|
||||||
` "md5": "${md5}",\n` +
|
const out =
|
||||||
' "rounds": 1,\n' +
|
contents.substring(0, bracket) +
|
||||||
' "type": "eq"\n' +
|
"},\n" +
|
||||||
" " +
|
` { "id": "${pdf}",\n` +
|
||||||
contents.substring(bracket);
|
` "file": "pdfs/${pdf}.pdf",\n` +
|
||||||
fs.writeFileSync("test/test_manifest.json", out);
|
` "md5": "${md5}",\n` +
|
||||||
execSync(`git add ${testManifest} ${gitIgnore}`);
|
' "rounds": 1,\n' +
|
||||||
execSync(`git add ${file}`);
|
' "type": "eq"\n' +
|
||||||
});
|
" " +
|
||||||
|
contents.substring(bracket);
|
||||||
|
fs.writeFileSync("test/test_manifest.json", out);
|
||||||
|
execSync(`git add ${testManifest} ${gitIgnore}`);
|
||||||
|
execSync(`git add ${file}`);
|
||||||
|
|||||||
@ -128,4 +128,4 @@ async function verifyManifestFiles(manifest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { downloadManifestFiles, verifyManifestFiles };
|
export { calculateMD5, downloadManifestFiles, verifyManifestFiles };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user