1
mirror of https://github.com/jakejarvis/careful-downloader.git synced 2025-09-18 14:55:30 -04:00

fix incorrect checksum test

This commit is contained in:
2021-10-06 12:16:01 -04:00
parent f2a6a429e0
commit 609d4ed5ea
3 changed files with 37 additions and 35 deletions

1
.gitignore vendored
View File

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

View File

@@ -9,6 +9,7 @@ import decompress from "decompress";
import urlParse from "url-parse"; import urlParse from "url-parse";
export default async function downloader(downloadUrl, checksumUrl, options) { export default async function downloader(downloadUrl, checksumUrl, options) {
// intialize options if none are set
options = options || {}; options = options || {};
// don't delete the temp dir if set manually and dir exists // don't delete the temp dir if set manually and dir exists
@@ -36,8 +37,7 @@ export default async function downloader(downloadUrl, checksumUrl, options) {
]); ]);
// validate the checksum of the download // 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 // optionally clear the target directory of existing files
if (options.cleanDestDir) { if (options.cleanDestDir) {
await fs.remove(options.destDir); await fs.remove(options.destDir);
@@ -55,6 +55,9 @@ export default async function downloader(downloadUrl, checksumUrl, options) {
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename)); await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename));
return path.join(options.destDir, options.filename); return path.join(options.destDir, options.filename);
} }
} else {
throw new Error(`Invalid checksum for ${options.filename}.`);
}
} finally { } finally {
// delete temporary directory (except for edge cases above) // delete temporary directory (except for edge cases above)
if (deleteTempDir) { if (deleteTempDir) {
@@ -67,7 +70,7 @@ export default async function downloader(downloadUrl, checksumUrl, options) {
async function downloadFile(url, dest) { async function downloadFile(url, dest) {
const pipeline = promisify(stream.pipeline); const pipeline = promisify(stream.pipeline);
return await pipeline( return pipeline(
got.stream(url, { followRedirect: true }), // GitHub releases redirect to unpredictable URLs got.stream(url, { followRedirect: true }), // GitHub releases redirect to unpredictable URLs
fs.createWriteStream(dest), fs.createWriteStream(dest),
); );
@@ -79,5 +82,5 @@ async function checkChecksum(baseDir, downloadFile, checksumFile, algorithm, enc
defaultTextEncoding: encoding, 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"; 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 this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
const outDir = path.join(tempy.directory()); const outDir = path.join(tempy.directory());
@@ -27,13 +27,14 @@ it("hugo.exe was downloaded and extracted", async function () {
fs.removeSync(outDir); fs.removeSync(outDir);
}); });
// TODO: FIX THIS it("incorrect checksum, not extracted", async function () {
/* this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
it("incorrect checksum", async () => {
const outDir = path.join(tempy.directory()); const outDir = path.join(tempy.directory());
expect(await downloader( expect(async () => downloader(
"https://github.com/gohugoio/hugo/releases/download/v0.88.0/hugo_0.88.0_NetBSD-ARM.tar.gz", // 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", "https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
{ {
destDir: outDir, destDir: outDir,
@@ -41,10 +42,9 @@ it("incorrect checksum", async () => {
encoding: "binary", encoding: "binary",
extract: false, extract: false,
}, },
)).to.throw(/No checksum/); )).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);
}); });
*/