1
mirror of https://github.com/jakejarvis/careful-downloader.git synced 2026-01-14 06:42:56 -05:00

BREAKING: allow hashes provided as a URL to a text file or a string

(closes #1)
This commit is contained in:
2021-10-16 06:10:36 -04:00
parent bcd2e89f7d
commit 4d3134afb8
8 changed files with 336 additions and 183 deletions

View File

@@ -4,74 +4,116 @@ import path from "path";
import { fileURLToPath } from "url";
import { expect } from "chai";
import downloader from "../index.js";
import download from "../index.js";
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#what-do-i-use-instead-of-__dirname-and-__filename
const __dirname = path.dirname(fileURLToPath(import.meta.url));
it("verified checksum, hugo.exe was extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
describe("checksum via downloaded text file", function () {
it("verified checksum, hugo.exe was extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
await downloader(
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_extended_0.88.1_Windows-64bit.zip",
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
{
destDir: path.join(__dirname, "temp"),
algorithm: "sha256",
encoding: "binary",
extract: true,
},
);
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.true;
// clean up
fs.removeSync(path.join(__dirname, "temp"));
});
it("incorrect checksum, not extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
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",
await download(
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_extended_0.88.1_Windows-64bit.zip",
{
checksumUrl: "https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
destDir: path.join(__dirname, "temp"),
algorithm: "sha256",
extract: true,
},
);
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.true;
// clean up
fs.removeSync(path.join(__dirname, "temp"));
});
it("incorrect checksum file, not extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
expect(async () => download(
// download mismatching versions to trigger error
"https://github.com/gohugoio/hugo/releases/download/v0.88.0/hugo_0.88.0_Windows-64bit.zip",
{
checksumUrl: "https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
destDir: path.join(__dirname, "temp"),
algorithm: "sha256",
encoding: "binary",
extract: false,
},
)).to.throw;
)).to.throw;
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.false;
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.false;
// clean up
fs.removeSync(path.join(__dirname, "temp"));
});
// clean up
fs.removeSync(path.join(__dirname, "temp"));
});
it("destDir located outside of module, throw error", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
it("destDir located outside of module, throw error", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
expect(async () => downloader(
expect(async () => download(
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_Windows-64bit.zip",
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
{
checksumUrl: "https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
destDir: "../vendor", // invalid path
},
)).to.throw;
)).to.throw;
});
it("zero options, download zip and leave it alone", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
await download(
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_extended_0.88.1_Windows-64bit.zip",
{
checksumUrl: "https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
},
);
expect(fs.existsSync(path.join(__dirname, "../downloads", "hugo_extended_0.88.1_Windows-64bit.zip"))).to.be.true;
// clean up
fs.removeSync(path.join(__dirname, "../downloads"));
});
});
it("zero options, download zip and leave it alone", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
describe("checksum via string", function () {
it("verified checksum, hugo.exe was extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
await downloader(
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_extended_0.88.1_Windows-64bit.zip",
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_checksums.txt",
);
await download(
"https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_extended_0.88.1_Windows-64bit.zip",
{
checksumHash: "aaa20e258cd668cff66400d365d73ddc375e44487692d49a5285b56330f6e6b2",
destDir: path.join(__dirname, "temp"),
algorithm: "sha256",
extract: true,
},
);
expect(fs.existsSync(path.join(__dirname, "../downloads", "hugo_extended_0.88.1_Windows-64bit.zip"))).to.be.true;
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.true;
// clean up
fs.removeSync(path.join(__dirname, "../downloads"));
// clean up
fs.removeSync(path.join(__dirname, "temp"));
});
it("incorrect checksum string, not extracted", async function () {
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
expect(async () => download(
// download mismatching versions to trigger error
"https://github.com/gohugoio/hugo/releases/download/v0.88.0/hugo_0.88.0_Windows-64bit.zip",
{
checksumHash: "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234",
destDir: path.join(__dirname, "temp"),
algorithm: "sha256",
extract: false,
},
)).to.throw;
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.false;
// clean up
fs.removeSync(path.join(__dirname, "temp"));
});
});