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