1
mirror of https://github.com/jakejarvis/careful-downloader.git synced 2025-04-26 07:45:23 -04:00

fix incorrect checksum test

This commit is contained in:
Jake Jarvis 2021-10-06 12:16:01 -04:00
parent f2a6a429e0
commit 609d4ed5ea
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
3 changed files with 37 additions and 35 deletions

1
.gitignore vendored
View File

@ -1,2 +1 @@
node_modules/
test/out/

View File

@ -9,6 +9,7 @@ import decompress from "decompress";
import urlParse from "url-parse";
export default async function downloader(downloadUrl, checksumUrl, options) {
// intialize options if none are set
options = options || {};
// don't delete the temp dir if set manually and dir exists
@ -36,24 +37,26 @@ export default async function downloader(downloadUrl, checksumUrl, options) {
]);
// validate the checksum of the download
await checkChecksum(options.tempDir, options.filename, "checksums.txt", options.algorithm, options.encoding);
if (await checkChecksum(options.tempDir, options.filename, "checksums.txt", options.algorithm, options.encoding)) {
// optionally clear the target directory of existing files
if (options.cleanDestDir) {
await fs.remove(options.destDir);
}
// optionally clear the target directory of existing files
if (options.cleanDestDir) {
await fs.remove(options.destDir);
}
// ensure the target directory exists
await fs.mkdirp(options.destDir);
// ensure the target directory exists
await fs.mkdirp(options.destDir);
if (options.extract) {
// decompress download and move resulting files to final destination
await decompress(path.join(options.tempDir, options.filename), options.destDir);
return options.destDir;
if (options.extract) {
// decompress download and move resulting files to final destination
await decompress(path.join(options.tempDir, options.filename), options.destDir);
return options.destDir;
} else {
// move verified download to final destination as-is
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename));
return path.join(options.destDir, options.filename);
}
} else {
// move verified download to final destination as-is
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename));
return path.join(options.destDir, options.filename);
throw new Error(`Invalid checksum for ${options.filename}.`);
}
} finally {
// delete temporary directory (except for edge cases above)
@ -67,7 +70,7 @@ export default async function downloader(downloadUrl, checksumUrl, options) {
async function downloadFile(url, dest) {
const pipeline = promisify(stream.pipeline);
return await pipeline(
return pipeline(
got.stream(url, { followRedirect: true }), // GitHub releases redirect to unpredictable URLs
fs.createWriteStream(dest),
);
@ -79,5 +82,5 @@ async function checkChecksum(baseDir, downloadFile, checksumFile, algorithm, enc
defaultTextEncoding: encoding,
});
return await checker.validate(baseDir, downloadFile);
return checker.validate(baseDir, downloadFile);
}

View File

@ -6,7 +6,7 @@ import { expect } from "chai";
import downloader from "../index.js";
it("hugo.exe was downloaded and extracted", async function () {
it("verified checksum, hugo.exe was extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
const outDir = path.join(tempy.directory());
@ -27,24 +27,24 @@ it("hugo.exe was downloaded and extracted", async function () {
fs.removeSync(outDir);
});
// TODO: FIX THIS
/*
it("incorrect checksum", async () => {
it("incorrect checksum, not extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
const outDir = path.join(tempy.directory());
expect(await downloader(
"https://github.com/gohugoio/hugo/releases/download/v0.88.0/hugo_0.88.0_NetBSD-ARM.tar.gz",
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
{
destDir: outDir,
algorithm: "sha256",
encoding: "binary",
extract: false,
},
)).to.throw(/No checksum/);
expect(async () => downloader(
// download mismatching versions to trigger error
"https://github.com/gohugoio/hugo/releases/download/v0.88.0/hugo_0.88.0_Windows-64bit.zip",
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
{
destDir: outDir,
algorithm: "sha256",
encoding: "binary",
extract: false,
},
)).to.throw;
// assert(fs.existsSync(path.join(outDir, "hugo.exe")));
expect(fs.existsSync(path.join(outDir, "hugo.exe"))).to.be.false;
// fs.removeSync(outDir);
fs.removeSync(outDir);
});
*/