|
| 1 | +import { Yok } from "../../lib/common/yok"; |
| 2 | +import { assert } from "chai"; |
| 3 | +import { PacoteService } from '../../lib/services/pacote-service'; |
| 4 | +import { LoggerStub } from "../stubs"; |
| 5 | +import { sandbox, SinonSandbox, SinonStub } from "sinon"; |
| 6 | +import { EventEmitter } from "events"; |
| 7 | +const pacote = require("pacote"); |
| 8 | +const tar = require("tar"); |
| 9 | +const path = require("path"); |
| 10 | + |
| 11 | +const npmCachePath = "npmCachePath"; |
| 12 | +const packageName = "testPackage"; |
| 13 | +const fullPath = `/Users/username/${packageName}`; |
| 14 | +const destinationDir = "destinationDir"; |
| 15 | +const defaultPacoteOpts: IPacoteBaseOptions = { cache: npmCachePath }; |
| 16 | +const proxySettings: IProxySettings = { |
| 17 | + hostname: "hostname", |
| 18 | + proxy: "proxy", |
| 19 | + port: "8888", |
| 20 | + rejectUnauthorized: true, |
| 21 | + username: null, |
| 22 | + password: null |
| 23 | +}; |
| 24 | + |
| 25 | +interface ITestSetup { |
| 26 | + isLocalPackage?: boolean; |
| 27 | + useProxySettings?: boolean; |
| 28 | + npmGetCachePathError?: Error; |
| 29 | +} |
| 30 | + |
| 31 | +interface ITestCase extends ITestSetup { |
| 32 | + manifestOptions?: IPacoteManifestOptions; |
| 33 | + additionalExtractOpts?: IPacoteExtractOptions; |
| 34 | + name: string; |
| 35 | + expectedArgs: any[]; |
| 36 | +} |
| 37 | + |
| 38 | +const createTestInjector = (opts?: ITestSetup): IInjector => { |
| 39 | + opts = opts || {}; |
| 40 | + |
| 41 | + const testInjector = new Yok(); |
| 42 | + testInjector.register("logger", LoggerStub); |
| 43 | + testInjector.register("pacoteService", PacoteService); |
| 44 | + testInjector.register("fs", { |
| 45 | + exists: (p: string): boolean => opts.isLocalPackage |
| 46 | + }); |
| 47 | + |
| 48 | + testInjector.register("proxyService", { |
| 49 | + getCache: async (): Promise<IProxySettings> => opts.useProxySettings ? proxySettings : null |
| 50 | + }); |
| 51 | + |
| 52 | + testInjector.register("npm", { |
| 53 | + getCachePath: async (): Promise<string> => { |
| 54 | + if (opts.npmGetCachePathError) { |
| 55 | + throw opts.npmGetCachePathError; |
| 56 | + } |
| 57 | + |
| 58 | + return npmCachePath; |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + return testInjector; |
| 63 | +}; |
| 64 | + |
| 65 | +class MockStream extends EventEmitter { |
| 66 | + public pipe(destination: any, options?: { end?: boolean; }): any { |
| 67 | + // Nothing to do here, just mock the method. |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +describe("pacoteService", () => { |
| 72 | + const manifestResult: any = {}; |
| 73 | + const manifestOptions: IPacoteManifestOptions = { fullMetadata: true }; |
| 74 | + let sandboxInstance: SinonSandbox = null; |
| 75 | + let manifestStub: SinonStub = null; |
| 76 | + let tarballStreamStub: SinonStub = null; |
| 77 | + let tarXStub: SinonStub = null; |
| 78 | + let tarballSourceStream: MockStream = null; |
| 79 | + let tarExtractDestinationStream: MockStream = null; |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + sandboxInstance = sandbox.create(); |
| 83 | + manifestStub = sandboxInstance.stub(pacote, "manifest").returns(Promise.resolve(manifestResult)); |
| 84 | + tarballSourceStream = new MockStream(); |
| 85 | + tarballStreamStub = sandboxInstance.stub(pacote.tarball, "stream").returns(tarballSourceStream); |
| 86 | + tarExtractDestinationStream = new MockStream(); |
| 87 | + tarXStub = sandboxInstance.stub(tar, "x").returns(tarExtractDestinationStream); |
| 88 | + }); |
| 89 | + |
| 90 | + afterEach(() => { |
| 91 | + sandboxInstance.restore(); |
| 92 | + }); |
| 93 | + |
| 94 | + const setupTest = (opts?: ITestSetup): IPacoteService => { |
| 95 | + opts = opts || {}; |
| 96 | + const testInjector = createTestInjector(opts); |
| 97 | + if (opts.isLocalPackage) { |
| 98 | + sandboxInstance.stub(path, "resolve").withArgs(packageName).returns(fullPath); |
| 99 | + } |
| 100 | + |
| 101 | + return testInjector.resolve<IPacoteService>("pacoteService"); |
| 102 | + }; |
| 103 | + |
| 104 | + describe("manifest", () => { |
| 105 | + describe("calls pacote.manifest", () => { |
| 106 | + |
| 107 | + const testData: ITestCase[] = [ |
| 108 | + { |
| 109 | + name: "with 'cache' only when no opts are passed", |
| 110 | + expectedArgs: [packageName, defaultPacoteOpts] |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "with 'cache' and passed options", |
| 114 | + manifestOptions, |
| 115 | + expectedArgs: [packageName, _.extend({}, defaultPacoteOpts, manifestOptions)] |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "with 'cache' and proxy settings", |
| 119 | + useProxySettings: true, |
| 120 | + expectedArgs: [packageName, _.extend({}, defaultPacoteOpts, proxySettings)] |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "with 'cache', passed options and proxy settings when proxy is configured", |
| 124 | + manifestOptions, |
| 125 | + useProxySettings: true, |
| 126 | + expectedArgs: [packageName, _.extend({}, defaultPacoteOpts, manifestOptions, proxySettings)] |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "with full path to file when it is local one", |
| 130 | + isLocalPackage: true, |
| 131 | + expectedArgs: [fullPath, defaultPacoteOpts] |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "with full path to file, 'cache' and passed options when local path is passed", |
| 135 | + manifestOptions, |
| 136 | + isLocalPackage: true, |
| 137 | + expectedArgs: [fullPath, _.extend({}, defaultPacoteOpts, manifestOptions)] |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "with full path to file, 'cache' and proxy settings when proxy is configured", |
| 141 | + manifestOptions, |
| 142 | + isLocalPackage: true, |
| 143 | + useProxySettings: true, |
| 144 | + expectedArgs: [fullPath, _.extend({}, defaultPacoteOpts, manifestOptions, proxySettings)] |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "with full path to file, 'cache', passed options and proxy settings when proxy is configured and local path is passed", |
| 148 | + manifestOptions, |
| 149 | + useProxySettings: true, |
| 150 | + isLocalPackage: true, |
| 151 | + expectedArgs: [fullPath, _.extend({}, defaultPacoteOpts, manifestOptions, proxySettings)] |
| 152 | + }, |
| 153 | + ]; |
| 154 | + |
| 155 | + testData.forEach(testCase => { |
| 156 | + it(testCase.name, async () => { |
| 157 | + const pacoteService = setupTest(testCase); |
| 158 | + const result = await pacoteService.manifest(packageName, testCase.manifestOptions); |
| 159 | + |
| 160 | + assert.equal(result, manifestResult); |
| 161 | + assert.deepEqual(manifestStub.firstCall.args, testCase.expectedArgs); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it("fails with npm error when unable to get npm cache", async () => { |
| 167 | + const npmGetCachePathError = new Error("npm error"); |
| 168 | + const pacoteService = setupTest({ npmGetCachePathError }); |
| 169 | + await assert.isRejected(pacoteService.manifest(packageName, null), npmGetCachePathError.message); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe("extractPackage", () => { |
| 174 | + it("fails with correct error when pacote.tarball.stream raises error event", async () => { |
| 175 | + const pacoteService = setupTest(); |
| 176 | + |
| 177 | + const pacoteExtractPackagePromise = pacoteService.extractPackage(packageName, destinationDir); |
| 178 | + setImmediate(() => { |
| 179 | + tarballSourceStream.emit("error", new Error("my error")); |
| 180 | + }); |
| 181 | + |
| 182 | + await assert.isRejected(pacoteExtractPackagePromise, "my error"); |
| 183 | + }); |
| 184 | + |
| 185 | + it("fails with correct error when the destination stream raises error event", async () => { |
| 186 | + const pacoteService = setupTest(); |
| 187 | + |
| 188 | + const pacoteExtractPackagePromise = pacoteService.extractPackage(packageName, destinationDir); |
| 189 | + setImmediate(() => { |
| 190 | + tarExtractDestinationStream.emit("error", new Error("my error")); |
| 191 | + }); |
| 192 | + |
| 193 | + await assert.isRejected(pacoteExtractPackagePromise, "my error"); |
| 194 | + }); |
| 195 | + |
| 196 | + it("resolves when the destination stream emits finish event", async () => { |
| 197 | + const pacoteService = setupTest(); |
| 198 | + |
| 199 | + const pacoteExtractPackagePromise = pacoteService.extractPackage(packageName, destinationDir); |
| 200 | + setImmediate(() => { |
| 201 | + tarExtractDestinationStream.emit("finish"); |
| 202 | + }); |
| 203 | + |
| 204 | + await assert.isFulfilled(pacoteExtractPackagePromise); |
| 205 | + }); |
| 206 | + |
| 207 | + describe("passes correct options to tar.x", () => { |
| 208 | + const defaultExtractOpts = { strip: 1, C: destinationDir }; |
| 209 | + const additionalExtractOpts: IPacoteExtractOptions = { |
| 210 | + filter: (p: string, stat: any) => true |
| 211 | + }; |
| 212 | + |
| 213 | + const testData: ITestCase[] = [ |
| 214 | + { |
| 215 | + name: "when only default options should be passed", |
| 216 | + expectedArgs: [defaultExtractOpts], |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "when additional options are passed", |
| 220 | + expectedArgs: [_.extend({}, defaultExtractOpts, additionalExtractOpts)], |
| 221 | + additionalExtractOpts |
| 222 | + }, |
| 223 | + ]; |
| 224 | + |
| 225 | + testData.forEach(testCase => { |
| 226 | + it(testCase.name, async () => { |
| 227 | + const pacoteService = setupTest(); |
| 228 | + |
| 229 | + const pacoteExtractPackagePromise = pacoteService.extractPackage(packageName, destinationDir, testCase.additionalExtractOpts); |
| 230 | + setImmediate(() => { |
| 231 | + tarExtractDestinationStream.emit("finish"); |
| 232 | + }); |
| 233 | + |
| 234 | + await assert.isFulfilled(pacoteExtractPackagePromise); |
| 235 | + |
| 236 | + assert.deepEqual(tarXStub.firstCall.args, testCase.expectedArgs); |
| 237 | + }); |
| 238 | + }); |
| 239 | + }); |
| 240 | + |
| 241 | + describe("passes correct options to pacote.tarball.stream", () => { |
| 242 | + const testData: ITestCase[] = [ |
| 243 | + { |
| 244 | + name: "when proxy is not set", |
| 245 | + expectedArgs: [packageName, defaultPacoteOpts] |
| 246 | + }, |
| 247 | + { |
| 248 | + name: "when proxy is not set and a local path is passed", |
| 249 | + isLocalPackage: true, |
| 250 | + expectedArgs: [fullPath, defaultPacoteOpts] |
| 251 | + }, |
| 252 | + { |
| 253 | + name: "when proxy is set", |
| 254 | + useProxySettings: true, |
| 255 | + expectedArgs: [packageName, _.extend({}, defaultPacoteOpts, proxySettings)] |
| 256 | + }, |
| 257 | + { |
| 258 | + name: "when proxy is set and a local path is passed", |
| 259 | + useProxySettings: true, |
| 260 | + isLocalPackage: true, |
| 261 | + expectedArgs: [fullPath, _.extend({}, defaultPacoteOpts, proxySettings)] |
| 262 | + }, |
| 263 | + |
| 264 | + ]; |
| 265 | + |
| 266 | + testData.forEach(testCase => { |
| 267 | + it(testCase.name, async () => { |
| 268 | + const pacoteService = setupTest(testCase); |
| 269 | + |
| 270 | + const pacoteExtractPackagePromise = pacoteService.extractPackage(packageName, destinationDir); |
| 271 | + setImmediate(() => { |
| 272 | + tarExtractDestinationStream.emit("finish"); |
| 273 | + }); |
| 274 | + |
| 275 | + await assert.isFulfilled(pacoteExtractPackagePromise); |
| 276 | + assert.deepEqual(tarballStreamStub.firstCall.args, testCase.expectedArgs); |
| 277 | + }); |
| 278 | + }); |
| 279 | + }); |
| 280 | + }); |
| 281 | +}); |
0 commit comments