-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpackageManager-spec.ts
More file actions
541 lines (515 loc) · 19.8 KB
/
packageManager-spec.ts
File metadata and controls
541 lines (515 loc) · 19.8 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import child_process from "child_process";
import path from "path";
import { App, Config, IFileSystem, PackageManager, ProjectConfig, Util } from "@igniteui/cli-core";
import { resetSpy } from "../helpers/utils";
describe("Unit - Package Manager", () => {
it("ensureIgniteUISource - Should run through properly when install now is set to true", async () => {
const mockRequire = {
dependencies: {
"ignite-ui": "20.1"
}
};
const mockTemplateMgr = jasmine.createSpyObj("mockTemplateMgr", {
getProjectLibrary: {
getProject() {
return {
upgradeIgniteUIPackages: () => Promise.resolve(false)
};
},
projectIds: ["empty"]
}
});
const mockProjectConfig = {
igPackageRegistry: "trial",
project: {
components: ["igGrid", "igExcel"],
igniteuiSource: `./node_modules/ignite-ui`,
isBundle: false
}
} as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
spyOn(ProjectConfig, "setConfig");
spyOn(PackageManager, "addPackage").and.returnValue(true);
spyOn(path, "join").and.returnValue("fakemodule.json");
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
// should ignore already installed
spyOn(App.container, "get").and.returnValue(mockFs);
const fakeSpawnSync = (cmd: any, args: string[], opts: any) => {
if (args.includes("whoami")) {
throw new Error("");
}
return {
status: 0,
pid: 0,
output: [],
stdout: "",
stderr: "",
signal: "SIGABRT"
};
};
spyOn(Util, 'spawnSync').and.callFake(fakeSpawnSync as any);
spyOn(Util, "log");
spyOn(PackageManager, "removePackage");
await PackageManager.ensureIgniteUISource(true, mockTemplateMgr, true);
expect(Util.log).toHaveBeenCalledTimes(4);
expect(Util.log).toHaveBeenCalledWith(
"The project you've created requires the full version of Ignite UI from Infragistics private feed.",
"gray"
);
expect(Util.log).toHaveBeenCalledWith(
"We are initiating the login process for you. This will be required only once per environment.",
"gray"
);
expect(Util.log).toHaveBeenCalledWith(
"Adding a registry user account for trial",
"yellow"
);
expect(Util.log).toHaveBeenCalledWith(
`Use your Infragistics account credentials. "@" is not supported, ` +
`use "!!", so "username@infragistics.com" should be entered as "username!!infragistics.com"`,
"yellow"
);
expect(path.join).toHaveBeenCalled();
expect(Util.spawnSync).toHaveBeenCalledWith(
/^win/.test(process.platform) ? "npm.cmd" : "npm",
["adduser", `--registry=trial`, `--scope=@infragistics`, `--auth-type=legacy`],
{
stdio: "inherit"
}
);
expect(Util.spawnSync).toHaveBeenCalledWith(
"npm",
['config', 'set', `@infragistics:registry`, mockProjectConfig.igPackageRegistry]
);
expect(PackageManager.removePackage).toHaveBeenCalled();
expect(PackageManager.addPackage).toHaveBeenCalledWith(`@infragistics/ignite-ui-full@"20.1"`, true);
});
it("ensureIgniteUISource - Should run through properly when install = true && package error", async () => {
class TestPackageManager extends PackageManager {
public static getPackageJSON(): any { }
}
const mockRequire = {
dependencies: {
"ignite-ui": "20.1"
}
};
spyOn(require("module"), "_load").and.returnValue(mockRequire);
const mockTemplateMgr = jasmine.createSpyObj("mockTemplateMgr", {
getProjectLibrary: {
getProject() {
return { upgradeIgniteUIPackages: () => Promise.resolve(false) };
},
projectIds: ["empty"]
}
});
const mockProjectConfig = {
igPackageRegistry: "trial",
project: {
components: ["igGrid", "igExcel"],
igniteuiSource: `./node_modules/ignite-ui`,
isBundle: false
}
} as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
spyOn(ProjectConfig, "setConfig");
spyOn(TestPackageManager, "addPackage").and.returnValue(true);
const fakeSpawnSync = (cmd: any, args: string[], opts: any) => {
if (args.includes("whoami")) {
throw new Error("no user");
}
return {
status: 1,
pid: 0,
output: [],
stdout: "",
stderr: "",
signal: "SIGABRT"
};
};
spyOn(Util, 'spawnSync').and.callFake(fakeSpawnSync as any);
spyOn(Util, "log");
spyOn(TestPackageManager, "removePackage");
spyOn(TestPackageManager, "getPackageJSON").and.callFake(() => mockRequire);
await TestPackageManager.ensureIgniteUISource(true, mockTemplateMgr, true);
expect(ProjectConfig.localConfig).toHaveBeenCalled();
expect(Util.log).toHaveBeenCalledTimes(12);
expect(Util.log).toHaveBeenCalledWith(
"The project you've created requires the full version of Ignite UI from Infragistics private feed.",
"gray"
); // x2
expect(Util.log).toHaveBeenCalledWith(
"We are initiating the login process for you. This will be required only once per environment.",
"gray"
); // x2
expect(Util.log).toHaveBeenCalledWith(
"Adding a registry user account for trial",
"yellow"
); // x2
expect(Util.log).toHaveBeenCalledWith(
`Use your Infragistics account credentials. "@" is not supported, ` +
`use "!!", so "username@infragistics.com" should be entered as "username!!infragistics.com"`,
"yellow"
); // x2
expect(Util.log).toHaveBeenCalledWith(
"Something went wrong, " +
"please follow the steps in this guide: https://www.igniteui.com/help/using-ignite-ui-npm-packages",
"red"
); // x2
expect(Util.log).toHaveBeenCalledWith(
"Something went wrong with upgrading Ignite UI to the full version. " +
`As a result only views using OSS components will run correctly.`,
"yellow"
); // x1
expect(Util.log).toHaveBeenCalledWith(
"Please visit https://www.igniteui.com/help/using-ignite-ui-npm-packages " +
`for instructions on how to install the full package.`,
"yellow"
); // x1
expect(Util.spawnSync).toHaveBeenCalledWith(
/^win/.test(process.platform) ? "npm.cmd" : "npm",
["adduser", `--registry=trial`, `--scope=@infragistics`, `--auth-type=legacy`],
{
stdio: "inherit"
}
);
expect(Util.spawnSync).toHaveBeenCalledWith(
"npm",
['whoami', `--registry=${mockProjectConfig.igPackageRegistry}`],
{
stdio: "pipe",
encoding: "utf8"
}
);
});
it("ensureIgniteUISource - Should run through properly when install now is set to false", async () => {
spyOn(Util, "log");
const mockTemplateMgr = jasmine.createSpyObj("mockTemplateMgr", {
getProjectLibrary: {
getProject() {
return {
upgradeIgniteUIPackages: () => Promise.resolve(false)
};
}
}
});
const mockProjectConfig = {
igPackageRegistry: "trial",
project: {
components: ["igGrid", "igExcel"],
igniteuiSource: `./node_modules/ignite-ui`,
isBundle: false
}
} as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
await PackageManager.ensureIgniteUISource(false, mockTemplateMgr, true);
expect(ProjectConfig.localConfig).toHaveBeenCalled();
expect(Util.log).toHaveBeenCalledWith(
"Template(s) that require the full version of Ignite UI found in the project. " +
"You might be prompted for credentials on build to install it.", "yellow");
});
it("ensureIgniteUISource - Should respect oss version when upgrading", async () => {
class TestPackageManager extends PackageManager {
public static ensureRegistryUser(config: Config): boolean { return true; }
public static getPackageJSON(): any { }
}
const mockDeps = {
dependencies: {
"ignite-ui": "~20.1"
}
};
const mockProjectConfig = {
project: {
components: ["igGrid", "igExcel"],
igniteuiSource: `./node_modules/ignite-ui`,
isBundle: false
}
} as unknown as Config;
const mockTemplateMgr = jasmine.createSpyObj("mockTemplateMgr", {
getProjectLibrary: {
getProject() {
return { upgradeIgniteUIPackages: () => Promise.resolve(true) };
},
projectIds: ["empty"]
},
generateConfig: jasmine.createSpy().and.returnValue(mockProjectConfig),
});
spyOn(ProjectConfig, "localConfig").and.callFake(() => mockProjectConfig);
spyOn(ProjectConfig, "setConfig");
spyOn(TestPackageManager, "addPackage").and.callThrough();
spyOn(Util, "spawnSync");
spyOn(Util, "log");
spyOn(TestPackageManager, "removePackage");
spyOn(TestPackageManager, "getPackageJSON").and.callFake(() => mockDeps);
await TestPackageManager.ensureIgniteUISource(true, mockTemplateMgr, true);
expect(TestPackageManager.addPackage).toHaveBeenCalledWith(`@infragistics/ignite-ui-full@"~20.1"`, true);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`,
['install', `@infragistics/ignite-ui-full@"~20.1"`, '--quiet', '--save'],
jasmine.any(Object)
);
expect(TestPackageManager.removePackage).toHaveBeenCalledWith("ignite-ui", true);
mockDeps.dependencies["ignite-ui"] = "^17.1";
mockProjectConfig.project.igniteuiSource = "./node_modules/ignite-ui";
mockTemplateMgr.generateConfig = mockProjectConfig;
await TestPackageManager.ensureIgniteUISource(true, mockTemplateMgr, true);
expect(TestPackageManager.addPackage).toHaveBeenCalledWith(`@infragistics/ignite-ui-full@"^17.1"`, true);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`,
['install', `@infragistics/ignite-ui-full@"^17.1"`, '--quiet', '--save'],
jasmine.any(Object)
);
mockDeps.dependencies["ignite-ui"] = ">=0.1.0 <0.2.0";
mockProjectConfig.project.igniteuiSource = "./node_modules/ignite-ui";
mockTemplateMgr.generateConfig = mockProjectConfig;
await TestPackageManager.ensureIgniteUISource(true, mockTemplateMgr, true);
expect(TestPackageManager.addPackage).toHaveBeenCalledWith(`@infragistics/ignite-ui-full@">=0.1.0 <0.2.0"`, true);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`,
['install', `@infragistics/ignite-ui-full@">=0.1.0 <0.2.0"`, '--quiet', '--save'],
jasmine.any(Object)
);
});
it("Should run installPackages properly with error code", async () => {
const mockProjectConfig = { packagesInstalled: false } as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
spyOn(Util, "log");
spyOn(Util, "execSync").and.callFake(() => {
const err = new Error("Example");
err["status"] = 1;
throw err;
});
spyOn(ProjectConfig, "setConfig");
await PackageManager.installPackages(true);
expect(ProjectConfig.localConfig).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledTimes(3);
expect(Util.log).toHaveBeenCalledWith(`Installing npm packages`);
expect(Util.log).toHaveBeenCalledWith(`Error installing npm packages.`);
expect(Util.log).toHaveBeenCalledWith(`Example`);
expect(Util.execSync).toHaveBeenCalledWith(`npm install --quiet`,
{ stdio: ["inherit"], killSignal: "SIGINT" });
mockProjectConfig.packagesInstalled = true;
expect(ProjectConfig.setConfig).toHaveBeenCalledWith(mockProjectConfig);
});
it("Should run installPackages properly without error code", async () => {
const mockProjectConfig = { packagesInstalled: false } as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
spyOn(Util, "log");
spyOn(Util, "execSync").and.returnValue("");
spyOn(ProjectConfig, "setConfig");
await PackageManager.installPackages(true);
expect(ProjectConfig.localConfig).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledTimes(2);
expect(Util.log).toHaveBeenCalledWith(`Installing npm packages`);
expect(Util.log).toHaveBeenCalledWith(`Packages installed successfully`);
expect(Util.execSync).toHaveBeenCalledWith(`npm install --quiet`,
{ stdio: ["inherit"], killSignal: "SIGINT" });
mockProjectConfig.packagesInstalled = true;
expect(ProjectConfig.setConfig).toHaveBeenCalledWith(mockProjectConfig);
});
it("Should exit on installPackages if child install is terminated", async () => {
const mockProjectConfig = { packagesInstalled: false, disableAnalytics: true } as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
spyOn(Util, "log");
spyOn(ProjectConfig, "setConfig");
spyOn(process, "exit");
spyOn(Util, "execSync").and.callFake(() => {
const err = new Error("Error");
err["status"] = 3221225786; // ctl + c while child install is running
throw err;
});
await PackageManager.installPackages(true);
expect(Util.log).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledWith(`Installing npm packages`);
expect(Util.execSync).toHaveBeenCalledWith(`npm install --quiet`,
{ stdio: ["inherit"], killSignal: "SIGINT" });
expect(process.exit).toHaveBeenCalled();
expect(ProjectConfig.setConfig).toHaveBeenCalledTimes(0);
});
it("Should run removePackage properly with error code", async () => {
spyOn(Util, "log");
const fakeSpawnSync = (cmd: any, args: string[], opts: any) => {
throw new Error("Error");
};
spyOn(Util, 'spawnSync').and.callFake(fakeSpawnSync as any);
PackageManager.removePackage("example-package", true);
expect(Util.log).toHaveBeenCalledTimes(2);
expect(Util.log).toHaveBeenCalledWith(`Error uninstalling package example-package with npm`);
expect(Util.log).toHaveBeenCalledWith(`Error`);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`,
['uninstall', "example-package", '--quiet', '--save'],
{ stdio: "pipe", encoding: "utf8" }
);
});
it("Should run removePackage properly without error code", async () => {
spyOn(Util, "log");
const fakeSpawnSync = (cmd: any, args: string[], opts: any) => {
return {
status: 0,
pid: 0,
output: [],
stdout: "",
stderr: "",
signal: "SIGABRT"
};
};
spyOn(Util, 'spawnSync').and.callFake(fakeSpawnSync as any);
PackageManager.removePackage("example-package");
expect(Util.log).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledWith(`Package example-package uninstalled successfully`);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`,
['uninstall', "example-package", '--quiet', '--save'],
{ stdio: "pipe", encoding: "utf8" }
);
});
it("Should run addPackage properly with error code", async () => {
spyOn(Util, "log");
spyOn(Util, "spawnSync").and.callFake(() => {
const err = new Error("Error");
err["status"] = 1;
throw err;
});
PackageManager.addPackage("example-package", true);
expect(Util.log).toHaveBeenCalledTimes(2);
expect(Util.log).toHaveBeenCalledWith(`Error installing package example-package with npm`);
expect(Util.log).toHaveBeenCalledWith(`Error`);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`, ['install', 'example-package', '--quiet', '--save'], { stdio: "pipe", encoding: "utf8" });
});
it("Should run addPackage properly without error code", async () => {
spyOn(Util, "log");
spyOn(Util, "spawnSync").and.returnValue({
status: 0,
pid: 0,
output: [],
stdout: "",
stderr: "",
signal: null
});
PackageManager.addPackage("example-package", true);
expect(Util.log).toHaveBeenCalledTimes(1);
expect(Util.log).toHaveBeenCalledWith(`Package example-package installed successfully`);
expect(Util.spawnSync).toHaveBeenCalledWith(
`npm`, ['install', 'example-package', '--quiet', '--save'], { stdio: "pipe", encoding: "utf8" });
});
it("queuePackage should start package install", async () => {
const mockRequire = {
dependencies: {}
};
const mockProjectConfig = { packagesInstalled: true } as unknown as Config;
spyOn(require("module"), "_load").and.returnValue(mockRequire);
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
spyOn(Util, "log");
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
// should ignore already installed
spyOn(App.container, "get").and.returnValue(mockFs);
const spawnSpy = spyOn(child_process, "spawn").and.returnValue({
stdout: { on: jasmine.createSpy() },
stderr: { on: jasmine.createSpy() },
on: jasmine.createSpy()
} as any);
PackageManager.queuePackage("test-pack");
expect(Util.log).toHaveBeenCalledTimes(0);
expect(child_process.spawn).toHaveBeenCalledTimes(1);
expect((child_process.spawn as any)).toHaveBeenCalledWith(
`npm`, ['install', 'test-pack', '--quiet', '--no-save']);
});
it("queuePackage should ignore existing package installs", async () => {
const mockRequire = {
dependencies: {
"test-pack": "20.1"
}
};
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
const mockProjectConfig = { packagesInstalled: true } as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
// should ignore already installed
spyOn(App.container, "get").and.returnValue(mockFs);
spyOn(Util, "log");
const spawnSpy = spyOn(child_process, "spawn").and.returnValue({
stdout: { on: jasmine.createSpy() },
stderr: { on: jasmine.createSpy() },
on: jasmine.createSpy()
} as any);
PackageManager.queuePackage("test-pack");
expect(Util.log).toHaveBeenCalledTimes(0);
expect(child_process.spawn).toHaveBeenCalledTimes(0);
// should ignore if already in queue
PackageManager.queuePackage("test-pack2");
PackageManager.queuePackage("test-pack2");
expect(child_process.spawn).toHaveBeenCalledTimes(1);
});
it("Should wait for and log queued package installs", async () => {
PackageManager["installQueue"] = []; //must reset from other tests
const mockRequire = {
dependencies: {}
};
// should ignore already installed
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
const mockProjectConfig = { packagesInstalled: true } as unknown as Config;
spyOn(ProjectConfig, "localConfig").and.returnValue(mockProjectConfig);
// spyOn(require("module"), "_load").and.returnValue(mockRequire);
spyOn(Util, "log");
spyOn(App.container, "get").and.returnValue(mockFs);
const createMockChild = (exitCode: number, stdoutData: string, stderrData: string) => {
const mockChild = {
stdout: { on: jasmine.createSpy() },
stderr: { on: jasmine.createSpy() },
on: jasmine.createSpy()
};
// Setup stdout data handler
mockChild.stdout.on.and.callFake((event: string, handler: any) => {
if (event === 'data') {
setTimeout(() => handler(Buffer.from(stdoutData)), 10);
}
});
// Setup stderr data handler
mockChild.stderr.on.and.callFake((event: string, handler: any) => {
if (event === 'data') {
setTimeout(() => handler(Buffer.from(stderrData)), 10);
}
});
// Setup close handler
mockChild.on.and.callFake((event: string, handler: any) => {
if (event === 'close') {
setTimeout(() => handler(exitCode), 20);
}
});
return mockChild;
};
const spawnSpy = spyOn(child_process, 'spawn').and.callFake(() => {
return createMockChild(0, 'stdout data', 'stderr data') as any;
});
PackageManager.queuePackage("test-pack");
PackageManager.queuePackage("test-pack2");
await PackageManager.flushQueue(true, true);
expect(Util.log).toHaveBeenCalledTimes(3);
expect(Util.log).toHaveBeenCalledWith(`Waiting for additional packages to install`);
expect(Util.log).toHaveBeenCalledWith("Package test-pack installed successfully");
expect(Util.log).toHaveBeenCalledWith("Package test-pack2 installed successfully");
resetSpy(Util.log);
// on error
spawnSpy.and.callFake(() => {
return createMockChild(1, '', 'stderr') as any;
});
PackageManager.queuePackage("test-pack3");
await PackageManager.flushQueue(true, true);
expect(Util.log).toHaveBeenCalledTimes(3);
expect(Util.log).toHaveBeenCalledWith(`Waiting for additional packages to install`);
expect(Util.log).toHaveBeenCalledWith("Error installing package test-pack3");
expect(Util.log).toHaveBeenCalledWith("stderr");
});
});