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:
parent
f2a6a429e0
commit
609d4ed5ea
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
test/out/
|
|
||||||
|
37
index.js
37
index.js
@ -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,24 +37,26 @@ 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
|
||||||
|
if (options.cleanDestDir) {
|
||||||
|
await fs.remove(options.destDir);
|
||||||
|
}
|
||||||
|
|
||||||
// optionally clear the target directory of existing files
|
// ensure the target directory exists
|
||||||
if (options.cleanDestDir) {
|
await fs.mkdirp(options.destDir);
|
||||||
await fs.remove(options.destDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ensure the target directory exists
|
if (options.extract) {
|
||||||
await fs.mkdirp(options.destDir);
|
// decompress download and move resulting files to final destination
|
||||||
|
await decompress(path.join(options.tempDir, options.filename), options.destDir);
|
||||||
if (options.extract) {
|
return options.destDir;
|
||||||
// decompress download and move resulting files to final destination
|
} else {
|
||||||
await decompress(path.join(options.tempDir, options.filename), options.destDir);
|
// move verified download to final destination as-is
|
||||||
return options.destDir;
|
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename));
|
||||||
|
return path.join(options.destDir, options.filename);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// move verified download to final destination as-is
|
throw new Error(`Invalid checksum for ${options.filename}.`);
|
||||||
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename));
|
|
||||||
return path.join(options.destDir, options.filename);
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// delete temporary directory (except for edge cases above)
|
// 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) {
|
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);
|
||||||
}
|
}
|
||||||
|
@ -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,24 +27,24 @@ 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.1/hugo_0.88.1_checksums.txt",
|
"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",
|
destDir: outDir,
|
||||||
encoding: "binary",
|
algorithm: "sha256",
|
||||||
extract: false,
|
encoding: "binary",
|
||||||
},
|
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);
|
||||||
});
|
});
|
||||||
*/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user