|
| 1 | +/// <reference path=".d.ts" /> |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +import {assert} from "chai"; |
| 5 | +import * as ConfigLib from "../lib/config"; |
| 6 | +import * as ErrorsLib from "../lib/common/errors"; |
| 7 | +import * as FsLib from "../lib/common/file-system"; |
| 8 | +import * as HostInfoLib from "../lib/common/host-info"; |
| 9 | +import * as LoggerLib from "../lib/common/logger"; |
| 10 | +import * as NpmInstallationManagerLib from "../lib/npm-installation-manager"; |
| 11 | +import * as OptionsLib from "../lib/options"; |
| 12 | +import * as StaticConfigLib from "../lib/config"; |
| 13 | + |
| 14 | +import Future = require("fibers/future"); |
| 15 | +import yok = require("../lib/common/yok"); |
| 16 | + |
| 17 | +function createTestInjector(): IInjector { |
| 18 | + let testInjector = new yok.Yok(); |
| 19 | + |
| 20 | + testInjector.register("config", ConfigLib.Configuration); |
| 21 | + testInjector.register("logger", LoggerLib.Logger); |
| 22 | + testInjector.register("lockfile", { }); |
| 23 | + testInjector.register("errors", ErrorsLib.Errors); |
| 24 | + testInjector.register("options", OptionsLib.Options); |
| 25 | + testInjector.register("fs", FsLib.FileSystem); |
| 26 | + testInjector.register("hostInfo", HostInfoLib.HostInfo); |
| 27 | + testInjector.register("staticConfig", StaticConfigLib.StaticConfig); |
| 28 | + |
| 29 | + testInjector.register("npmInstallationManager", NpmInstallationManagerLib.NpmInstallationManager); |
| 30 | + |
| 31 | + return testInjector; |
| 32 | +} |
| 33 | + |
| 34 | +function mockNpm(testInjector: IInjector, versions: string[], latestVersion: string) { |
| 35 | + testInjector.register("npm", { |
| 36 | + view: (packageName: string, propertyName: string) => { |
| 37 | + return (() => { |
| 38 | + if(propertyName === "versions") { |
| 39 | + let result = Object.create(null); |
| 40 | + result[latestVersion] = { |
| 41 | + "versions": versions |
| 42 | + }; |
| 43 | + |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + throw new Error(`Unable to find propertyName ${propertyName}.`); |
| 48 | + }).future<any>()(); |
| 49 | + }, |
| 50 | + load: () => Future.fromResult() |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +describe("Npm installation manager tests", () => { |
| 55 | + it("returns correct latest compatible version when only one exists", () => { |
| 56 | + let testInjector = createTestInjector(); |
| 57 | + |
| 58 | + let versions = ["1.4.0"]; |
| 59 | + let latestVersion = "1.4.0"; |
| 60 | + |
| 61 | + mockNpm(testInjector, versions, latestVersion); |
| 62 | + |
| 63 | + // Mock staticConfig.version |
| 64 | + let staticConfig = testInjector.resolve("staticConfig"); |
| 65 | + staticConfig.version = "1.4.0"; |
| 66 | + |
| 67 | + // Mock npmInstallationManager.getLatestVersion |
| 68 | + let npmInstallationManager = testInjector.resolve("npmInstallationManager"); |
| 69 | + npmInstallationManager.getLatestVersion = (packageName: string) => Future.fromResult(latestVersion); |
| 70 | + |
| 71 | + let actualLatestCompatibleVersion = npmInstallationManager.getLatestCompatibleVersion("").wait(); |
| 72 | + let expectedLatestCompatibleVersion = "1.4.0"; |
| 73 | + assert.equal(actualLatestCompatibleVersion, expectedLatestCompatibleVersion); |
| 74 | + }); |
| 75 | + it("returns correct latest compatible version", () => { |
| 76 | + let testInjector = createTestInjector(); |
| 77 | + |
| 78 | + let versions = ["1.2.0", "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.4.0"]; |
| 79 | + let latestVersion = "1.3.3"; |
| 80 | + |
| 81 | + mockNpm(testInjector, versions, latestVersion); |
| 82 | + |
| 83 | + // Mock staticConfig.version |
| 84 | + let staticConfig = testInjector.resolve("staticConfig"); |
| 85 | + staticConfig.version = "1.3.0"; |
| 86 | + |
| 87 | + // Mock npmInstallationManager.getLatestVersion |
| 88 | + let npmInstallationManager = testInjector.resolve("npmInstallationManager"); |
| 89 | + npmInstallationManager.getLatestVersion = (packageName: string) => Future.fromResult(latestVersion); |
| 90 | + |
| 91 | + let actualLatestCompatibleVersion = npmInstallationManager.getLatestCompatibleVersion("").wait(); |
| 92 | + let expectedLatestCompatibleVersion = "1.3.3"; |
| 93 | + assert.equal(actualLatestCompatibleVersion, expectedLatestCompatibleVersion); |
| 94 | + }); |
| 95 | +}); |
0 commit comments