|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as path from 'path'; |
| 7 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 8 | +import * as chai from "chai"; |
| 9 | +import * as util from '../../../src/common'; |
| 10 | +import { CreateTmpDir, TmpAsset } from '../../../src/CreateTmpAsset'; |
| 11 | +import TestZip from '../testAssets/TestZip'; |
| 12 | +import { downloadAndInstallPackages } from '../../../src/packageManager/downloadAndInstallPackages'; |
| 13 | +import NetworkSettings from '../../../src/NetworkSettings'; |
| 14 | +import { EventStream } from '../../../src/EventStream'; |
| 15 | +import { DownloadStart, DownloadSizeObtained, DownloadProgress, DownloadSuccess, InstallationStart, PackageInstallStart } from '../../../src/omnisharp/loggingEvents'; |
| 16 | +import MockHttpsServer from '../testAssets/MockHttpsServer'; |
| 17 | +import { createTestFile } from '../testAssets/TestFile'; |
| 18 | +import TestEventBus from '../testAssets/TestEventBus'; |
| 19 | +import { AbsolutePathPackage } from '../../../src/packageManager/AbsolutePathPackage'; |
| 20 | +import { AbsolutePath } from '../../../src/packageManager/AbsolutePath'; |
| 21 | + |
| 22 | +chai.use(chaiAsPromised); |
| 23 | +let expect = chai.expect; |
| 24 | + |
| 25 | +suite(`${downloadAndInstallPackages.name}`, () => { |
| 26 | + let tmpInstallDir: TmpAsset; |
| 27 | + let server: MockHttpsServer; |
| 28 | + let testZip: TestZip; |
| 29 | + let tmpDirPath: string; |
| 30 | + let eventStream: EventStream; |
| 31 | + let eventBus: TestEventBus; |
| 32 | + let downloadablePackage: AbsolutePathPackage[]; |
| 33 | + let notDownloadablePackage: AbsolutePathPackage[]; |
| 34 | + |
| 35 | + const packageDescription = "Test Package"; |
| 36 | + const networkSettingsProvider = () => new NetworkSettings(undefined, false); |
| 37 | + |
| 38 | + setup(async () => { |
| 39 | + eventStream = new EventStream(); |
| 40 | + server = await MockHttpsServer.CreateMockHttpsServer(); |
| 41 | + eventBus = new TestEventBus(eventStream); |
| 42 | + tmpInstallDir = await CreateTmpDir(true); |
| 43 | + tmpDirPath = tmpInstallDir.name; |
| 44 | + downloadablePackage = <AbsolutePathPackage[]>[ |
| 45 | + { |
| 46 | + url: `${server.baseUrl}/downloadablePackage`, |
| 47 | + description: packageDescription, |
| 48 | + installPath: new AbsolutePath(tmpDirPath) |
| 49 | + }]; |
| 50 | + |
| 51 | + notDownloadablePackage = <AbsolutePathPackage[]>[ |
| 52 | + { |
| 53 | + url: `${server.baseUrl}/notDownloadablePackage`, |
| 54 | + description: packageDescription, |
| 55 | + installPath: new AbsolutePath(tmpDirPath) |
| 56 | + }]; |
| 57 | + |
| 58 | + testZip = await TestZip.createTestZipAsync(createTestFile("Foo", "foo.txt")); |
| 59 | + await server.start(); |
| 60 | + server.addRequestHandler('GET', '/downloadablePackage', 200, { |
| 61 | + "content-type": "application/zip", |
| 62 | + "content-length": testZip.size |
| 63 | + }, testZip.buffer); |
| 64 | + |
| 65 | + server.addRequestHandler('GET', '/notDownloadablePackage', 404); |
| 66 | + }); |
| 67 | + |
| 68 | + suite("If the download and install succeeds", () => { |
| 69 | + test("The expected files are installs at the specified path", async () => { |
| 70 | + await downloadAndInstallPackages(downloadablePackage, networkSettingsProvider, eventStream); |
| 71 | + for (let elem of testZip.files) { |
| 72 | + let filePath = path.join(tmpDirPath, elem.path); |
| 73 | + expect(await util.fileExists(filePath)).to.be.true; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + test("install.Lock is present", async () => { |
| 78 | + await downloadAndInstallPackages(downloadablePackage, networkSettingsProvider, eventStream); |
| 79 | + for (let elem of testZip.files) { |
| 80 | + let filePath = path.join(tmpDirPath, elem.path); |
| 81 | + expect(await util.fileExists(filePath)).to.be.true; |
| 82 | + } |
| 83 | + |
| 84 | + expect(await util.fileExists(path.join(tmpDirPath, "install.Lock"))).to.be.true; |
| 85 | + }); |
| 86 | + |
| 87 | + test("Events are created in the correct order", async () => { |
| 88 | + let eventsSequence = [ |
| 89 | + new PackageInstallStart(), |
| 90 | + new DownloadStart(packageDescription), |
| 91 | + new DownloadSizeObtained(testZip.size), |
| 92 | + new DownloadProgress(100, packageDescription), |
| 93 | + new DownloadSuccess(' Done!'), |
| 94 | + new InstallationStart(packageDescription) |
| 95 | + ]; |
| 96 | + |
| 97 | + await downloadAndInstallPackages(downloadablePackage, networkSettingsProvider, eventStream); |
| 98 | + expect(eventBus.getEvents()).to.be.deep.equal(eventsSequence); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + suite("If the download and install fails", () => { |
| 103 | + test("Throws an exception when the download fails", async () => { |
| 104 | + await downloadAndInstallPackages(notDownloadablePackage, networkSettingsProvider, eventStream).should.be.rejected; |
| 105 | + }); |
| 106 | + |
| 107 | + test("install.Lock is not present when the download fails", async () => { |
| 108 | + try { |
| 109 | + await downloadAndInstallPackages(notDownloadablePackage, networkSettingsProvider, eventStream); |
| 110 | + } |
| 111 | + catch (error) { |
| 112 | + expect(await util.fileExists(path.join(tmpDirPath, "install.Lock"))).to.be.false; |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + teardown(async () => { |
| 118 | + if (tmpInstallDir) { |
| 119 | + tmpInstallDir.dispose(); |
| 120 | + } |
| 121 | + |
| 122 | + await server.stop(); |
| 123 | + eventBus.dispose(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments