/
command.test.js
539 lines (419 loc) · 14.5 KB
/
command.test.js
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
/* eslint-disable class-methods-use-this */
"use strict";
const fs = require("fs-extra");
const log = require("npmlog");
const path = require("path");
const tempy = require("tempy");
// partially mocked
const childProcess = require("@lerna/child-process");
const os = require("os");
// normalize concurrency across different environments (localhost, CI, etc)
jest.spyOn(os, "cpus").mockImplementation(() => new Array(42));
// helpers
const initFixture = require("@lerna-test/helpers").initFixtureFactory(__dirname);
const { loggingOutput } = require("@lerna-test/helpers/logging-output");
const { updateLernaConfig } = require("@lerna-test/helpers");
// file under test
const { Command } = require("..");
describe("core-command", () => {
let testDir;
beforeAll(async () => {
testDir = await initFixture("basic");
});
afterEach(() => {
// ensure common CWD is restored when individual tests
// initialize their own fixture (which changes CWD)
if (process.cwd() !== testDir) {
process.chdir(testDir);
}
});
childProcess.getChildProcessCount = jest.fn(() => 0);
// swallow errors when passed in argv
const onRejected = () => {};
class OkCommand extends Command {
initialize() {
return true;
}
execute() {
return "ok";
}
}
// convenience to avoid silly "not implemented errors"
const testFactory = (argv = {}) => new OkCommand(Object.assign({ cwd: testDir }, argv));
describe(".logger", () => {
it("should be added to the instance", async () => {
const command = testFactory();
await command;
expect(command.logger).toBeDefined();
});
});
describe(".concurrency", () => {
it("should be added to the instance", async () => {
const command = testFactory({ concurrency: 6 });
await command;
expect(command.concurrency).toBe(6);
});
it("should fall back to default if concurrency given is NaN", async () => {
const command = testFactory({ concurrency: "bla" });
await command;
expect(command.concurrency).toBe(42);
});
it("should fall back to default if concurrency given is 0", async () => {
const command = testFactory({ concurrency: 0 });
await command;
expect(command.concurrency).toBe(42);
});
it("should fall back to 1 if concurrency given is smaller than 1", async () => {
const command = testFactory({ concurrency: -1 });
await command;
expect(command.concurrency).toBe(1);
});
});
describe(".toposort", () => {
it("is enabled by default", async () => {
const command = testFactory();
await command;
expect(command.toposort).toBe(true);
});
it("is disabled when sort config is explicitly false (--no-sort)", async () => {
const command = testFactory({ sort: false });
await command;
expect(command.toposort).toBe(false);
});
});
describe(".execOpts", () => {
const ONE_HUNDRED_MEGABYTES = 1000 * 1000 * 100;
it("has maxBuffer", async () => {
const command = testFactory({ maxBuffer: ONE_HUNDRED_MEGABYTES });
await command;
expect(command.execOpts.maxBuffer).toBe(ONE_HUNDRED_MEGABYTES);
});
it("has repo path", async () => {
const command = testFactory();
await command;
expect(command.execOpts.cwd).toBe(testDir);
});
});
it("returns a Promise", async () => {
const result = await testFactory();
expect(result).toBe("ok");
});
describe("when finished", () => {
it("resolves immediately when no child processes active", async () => {
await testFactory();
const logMessages = loggingOutput("warn");
expect(logMessages).toHaveLength(0);
});
it("waits to resolve when 1 child process active", async () => {
childProcess.getChildProcessCount.mockReturnValueOnce(1);
await testFactory();
const [logMessage] = loggingOutput("warn");
expect(logMessage).toMatch("Waiting for 1 child process to exit.");
});
it("waits to resolve when 2 child processes active", async () => {
childProcess.getChildProcessCount.mockReturnValueOnce(2);
await testFactory();
const [logMessage] = loggingOutput("warn");
expect(logMessage).toMatch("Waiting for 2 child processes to exit.");
});
});
describe("with package error", () => {
const originalConsoleError = console.error;
beforeEach(() => {
console.error = jest.fn();
});
afterEach(() => {
console.error = originalConsoleError;
});
it("logs stdout and stderr of error from package", async () => {
class PkgErrorCommand extends Command {
initialize() {
return true;
}
execute() {
const err = new Error("message");
err.command = "test-pkg-err";
err.stdout = "pkg-err-stdout";
err.stderr = "pkg-err-stderr";
err.pkg = {
name: "pkg-err-name",
};
throw err;
}
}
const command = new PkgErrorCommand({ cwd: testDir });
await expect(command).rejects.toThrow(
expect.objectContaining({
command: "test-pkg-err",
stdout: "pkg-err-stdout",
stderr: "pkg-err-stderr",
pkg: expect.objectContaining({
name: "pkg-err-name",
}),
})
);
expect(console.error).toHaveBeenCalledTimes(2);
expect(console.error).toHaveBeenNthCalledWith(1, "pkg-err-stdout");
expect(console.error).toHaveBeenNthCalledWith(2, "pkg-err-stderr");
});
it("does not log stdout/stderr after streaming ends", async () => {
class PkgErrorCommand extends Command {
initialize() {
return true;
}
execute() {
const err = new Error("message");
err.command = "test-pkg-err";
err.stdout = "pkg-err-stdout";
err.stderr = "pkg-err-stderr";
err.pkg = {
name: "pkg-err-name",
};
throw err;
}
}
const command = new PkgErrorCommand({ cwd: testDir, stream: true });
await expect(command).rejects.toThrow("message");
expect(console.error).not.toHaveBeenCalled();
});
});
describe("loglevel", () => {
afterEach(() => {
log.level = "silent";
});
it("is set from lerna.json config", async () => {
const cwd = await initFixture("basic");
await updateLernaConfig(cwd, { loglevel: "warn" });
await testFactory({ cwd, onRejected });
expect(log.level).toBe("warn");
});
});
describe(".packageGraph", () => {
it("returns the graph of packages", async () => {
const command = testFactory();
await command;
expect(command.packageGraph).toBeInstanceOf(Map);
});
});
describe(".options", () => {
class TestACommand extends Command {}
class TestBCommand extends Command {}
class TestCCommand extends Command {
get otherCommandConfigs() {
return ["testb"];
}
}
it("does not mutate argv parameter", async () => {
const argv = { cwd: testDir, onRejected };
const instance = new TestACommand(argv);
await instance;
expect(argv).toEqual({ cwd: testDir, onRejected });
expect(instance.argv).not.toEqual(argv);
});
it("should pick up global options", async () => {
const instance = new TestACommand({ cwd: testDir, onRejected });
await instance;
expect(instance.options.testOption).toBe("default");
});
it("should override global options with command-level options", async () => {
const instance = new TestBCommand({ cwd: testDir, onRejected });
await instance;
expect(instance.options.testOption).toBe("b");
});
it("should override global options with inherited command-level options", async () => {
const instance = new TestCCommand({ cwd: testDir, onRejected });
await instance;
expect(instance.options.testOption).toBe("b");
});
it("should override inherited command-level options with local command-level options", async () => {
const instance = new TestCCommand({ cwd: testDir, onRejected });
await instance;
expect(instance.options.testOption2).toBe("c");
});
it("should override everything with a CLI flag", async () => {
const instance = new TestCCommand({
cwd: testDir,
onRejected,
testOption2: "f",
});
await instance;
expect(instance.options.testOption2).toBe("f");
});
it("should inherit durable options when a CLI flag is undefined", async () => {
const instance = new TestCCommand({
cwd: testDir,
onRejected,
testOption: undefined, // yargs does this when --test-option is not passed
});
await instance;
expect(instance.options.testOption).toBe("b");
});
});
describe("subclass implementation", () => {
["initialize", "execute"].forEach((method) => {
it(`throws if ${method}() is not overridden`, () => {
const command = new Command({ cwd: testDir, onRejected });
expect(() => command[method]()).toThrow();
});
});
});
describe("validations", () => {
it("throws ENOGIT when repository is not initialized", async () => {
const cwd = tempy.directory();
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "ENOGIT",
})
);
});
it("throws ENOPKG when root package.json is not found", async () => {
const cwd = await initFixture("basic");
await fs.remove(path.join(cwd, "package.json"));
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "ENOPKG",
})
);
});
it("throws JSONError when root package.json has syntax error", async () => {
const cwd = await initFixture("basic");
await fs.writeFile(
path.join(cwd, "package.json"), // trailing comma ...v
'{ "name": "invalid", "lerna": { "version": "1.0.0" }, }'
);
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "JSONError",
})
);
});
it("throws ENOLERNA when lerna.json is not found", async () => {
const cwd = await initFixture("basic");
await fs.remove(path.join(cwd, "lerna.json"));
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "ENOLERNA",
})
);
});
it("throws ENOVERSION when lerna.json is empty", async () => {
const cwd = await initFixture("basic");
const lernaConfigPath = path.join(cwd, "lerna.json");
await fs.writeJson(lernaConfigPath, {});
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "ENOVERSION",
})
);
});
it("throws ENOVERSION when no version property exists in lerna.json", async () => {
const cwd = await initFixture("basic");
const lernaConfigPath = path.join(cwd, "lerna.json");
const lernaConfig = await fs.readJson(lernaConfigPath);
delete lernaConfig.version;
await fs.writeJson(lernaConfigPath, {
...lernaConfig,
});
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "ENOVERSION",
})
);
});
it("throws ENOWORKSPACES when npm client is pnpm and useWorkspaces is not true", async () => {
const cwd = await initFixture("pnpm");
const lernaConfigPath = path.join(cwd, "lerna.json");
const lernaConfig = await fs.readJson(lernaConfigPath);
await fs.writeJson(lernaConfigPath, {
...lernaConfig,
useWorkspaces: false,
});
await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: "ENOWORKSPACES",
message:
"Usage of pnpm without workspaces is not supported. To use pnpm with lerna, set useWorkspaces to true in lerna.json and configure pnpm to use workspaces: https://pnpm.io/workspaces.",
})
);
});
});
describe("loglevel with verbose option true", () => {
it("should be set to verbose if loglevel is error", async () => {
const command = testFactory({
loglevel: "error",
verbose: true,
});
await command;
expect(command.options.loglevel).toEqual("verbose");
});
it("should be set to verbose if loglevel is warn", async () => {
const command = testFactory({
loglevel: "warn",
verbose: true,
});
await command;
expect(command.options.loglevel).toEqual("verbose");
});
it("should be set to verbose if loglevel is info", async () => {
const command = testFactory({
loglevel: "info",
verbose: true,
});
await command;
expect(command.options.loglevel).toEqual("verbose");
});
it("should remain set to verbose if loglevel is verbose", async () => {
const command = testFactory({
loglevel: "verbose",
verbose: true,
});
await command;
expect(command.options.loglevel).toEqual("verbose");
});
it("should not be set to verbose if loglevel is silly", async () => {
const command = testFactory({
loglevel: "silly",
verbose: true,
});
await command;
expect(command.options.loglevel).toEqual("silly");
});
});
describe("loglevel without verbose option", () => {
it("should remain set to error if loglevel is error", async () => {
const command = testFactory({
loglevel: "error",
});
await command;
expect(command.options.loglevel).toEqual("error");
});
it("should remain set to warn if loglevel is warn", async () => {
const command = testFactory({
loglevel: "warn",
});
await command;
expect(command.options.loglevel).toEqual("warn");
});
it("should remain set to info if loglevel is info", async () => {
const command = testFactory({
loglevel: "info",
});
await command;
expect(command.options.loglevel).toEqual("info");
});
it("should remain set to verbose if loglevel is verbose", async () => {
const command = testFactory({
loglevel: "verbose",
});
await command;
expect(command.options.loglevel).toEqual("verbose");
});
it("should remain set to silly if loglevel is silly", async () => {
const command = testFactory({
loglevel: "silly",
});
await command;
expect(command.options.loglevel).toEqual("silly");
});
});
});