|
| 1 | +import * as sinon from "sinon"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import * as fs from "fs-extra"; |
| 4 | +import * as path from "path"; |
| 5 | +import * as downloadUtils from "../downloadUtils"; |
| 6 | +import { getOrDownloadUniversalMaker } from "./universalMakerDownload"; |
| 7 | + |
| 8 | +describe("universalMakerDownload", () => { |
| 9 | + let existsSyncStub: sinon.SinonStub; |
| 10 | + let ensureDirSyncStub: sinon.SinonStub; |
| 11 | + let copySyncStub: sinon.SinonStub; |
| 12 | + let chmodSyncStub: sinon.SinonStub; |
| 13 | + let downloadToTmpStub: sinon.SinonStub; |
| 14 | + let validateSizeStub: sinon.SinonStub; |
| 15 | + let validateChecksumStub: sinon.SinonStub; |
| 16 | + |
| 17 | + let originalPlatform: string; |
| 18 | + let originalArch: string; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + originalPlatform = process.platform; |
| 22 | + originalArch = process.arch; |
| 23 | + Object.defineProperty(process, "platform", { value: "linux", configurable: true }); |
| 24 | + Object.defineProperty(process, "arch", { value: "x64", configurable: true }); |
| 25 | + |
| 26 | + existsSyncStub = sinon.stub(fs, "existsSync"); |
| 27 | + ensureDirSyncStub = sinon.stub(fs, "ensureDirSync"); |
| 28 | + copySyncStub = sinon.stub(fs, "copySync"); |
| 29 | + chmodSyncStub = sinon.stub(fs, "chmodSync"); |
| 30 | + downloadToTmpStub = sinon.stub(downloadUtils, "downloadToTmp"); |
| 31 | + validateSizeStub = sinon.stub(downloadUtils, "validateSize"); |
| 32 | + validateChecksumStub = sinon.stub(downloadUtils, "validateChecksum"); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + sinon.restore(); |
| 37 | + Object.defineProperty(process, "platform", { value: originalPlatform, configurable: true }); |
| 38 | + Object.defineProperty(process, "arch", { value: originalArch, configurable: true }); |
| 39 | + |
| 40 | + }); |
| 41 | + |
| 42 | + it("should return cached binary if valid", async () => { |
| 43 | + existsSyncStub.returns(true); |
| 44 | + validateSizeStub.resolves(); |
| 45 | + validateChecksumStub.resolves(); |
| 46 | + |
| 47 | + const result = await getOrDownloadUniversalMaker(); |
| 48 | + expect(existsSyncStub).to.have.been.calledOnce; |
| 49 | + expect(validateSizeStub).to.have.been.calledOnce; |
| 50 | + expect(validateChecksumStub).to.have.been.calledOnce; |
| 51 | + expect(downloadToTmpStub).to.not.have.been.called; |
| 52 | + expect(result).to.include("universal-maker-linux-x64"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should redownload if cached binary fails validation", async () => { |
| 56 | + existsSyncStub.returns(true); |
| 57 | + // Fail on first call (cache check), succeed on second (downloaded file) |
| 58 | + validateSizeStub.onFirstCall().rejects(new Error("Invalid size")); |
| 59 | + validateSizeStub.onSecondCall().resolves(); |
| 60 | + |
| 61 | + downloadToTmpStub.resolves("/tmp/downloaded_file"); |
| 62 | + validateChecksumStub.resolves(); // For the new file |
| 63 | + |
| 64 | + const result = await getOrDownloadUniversalMaker(); |
| 65 | + expect(existsSyncStub).to.have.been.calledOnce; |
| 66 | + expect(validateSizeStub).to.have.been.calledTwice; |
| 67 | + expect(downloadToTmpStub).to.have.been.calledOnce; |
| 68 | + expect(copySyncStub).to.have.been.calledOnce; |
| 69 | + expect(result).to.include("universal-maker-linux-x64"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should download if binary not in cache", async () => { |
| 73 | + existsSyncStub.returns(false); |
| 74 | + downloadToTmpStub.resolves("/tmp/downloaded_file"); |
| 75 | + validateSizeStub.resolves(); |
| 76 | + validateChecksumStub.resolves(); |
| 77 | + |
| 78 | + const result = await getOrDownloadUniversalMaker(); |
| 79 | + expect(existsSyncStub).to.have.been.calledOnce; |
| 80 | + expect(downloadToTmpStub).to.have.been.calledOnce; |
| 81 | + expect(copySyncStub).to.have.been.calledOnce; |
| 82 | + expect(result).to.include("universal-maker-linux-x64"); |
| 83 | + }); |
| 84 | +}); |
0 commit comments