-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
run-command.spec.ts
429 lines (337 loc) · 15.3 KB
/
run-command.spec.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
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
jest.mock('../lib/npm-run-script');
jest.mock('@lerna-lite/core', () => ({
...(jest.requireActual('@lerna-lite/core') as any), // return the other real methods, below we'll mock only 2 of the methods
logOutput: jest.requireActual('../../../core/src/__mocks__/output').logOutput,
}));
// also point to the local run command so that all mocks are properly used even by the command-runner
jest.mock('@lerna-lite/run', () => jest.requireActual('../run-command'));
import fs from 'fs-extra';
import globby from 'globby';
import { afterEach, afterAll } from 'jest-circus';
import yargParser from 'yargs-parser';
// make sure to import the output mock
import { logOutput, RunCommandOption } from '@lerna-lite/core';
// mocked modules
const { npmRunScript, npmRunScriptStreaming } = require('../lib/npm-run-script');
// helpers
const initFixture = require('@lerna-test/init-fixture')(__dirname);
const { loggingOutput } = require('@lerna-test/logging-output');
const { normalizeRelativeDir } = require('@lerna-test/normalize-relative-dir');
import { factory, RunCommand } from '../run-command';
const lernaRun = require('@lerna-test/command-runner')(require('../../../cli/src/cli-commands/cli-run-commands'));
// assertion helpers
const ranInPackagesStreaming = (testDir: string) =>
npmRunScriptStreaming.mock.calls.reduce((arr, [script, { args, npmClient, pkg, prefix }]) => {
const dir = normalizeRelativeDir(testDir, pkg.location);
const record = [dir, npmClient, 'run', script, `(prefixed: ${prefix})`].concat(args);
arr.push(record.join(' '));
return arr;
}, []);
const createArgv = (cwd: string, script?: string, ...args: any[]) => {
args.unshift('run');
const parserArgs = args.join(' ');
const argv = yargParser(parserArgs);
argv['$0'] = cwd;
if (script) {
argv.script = script;
}
args['logLevel'] = 'silent';
return argv as unknown as RunCommandOption;
};
describe('RunCommand', () => {
npmRunScript.mockImplementation((script, { pkg }) => Promise.resolve({ exitCode: 0, stdout: pkg.name }));
npmRunScriptStreaming.mockImplementation(() => Promise.resolve({ exitCode: 0 }));
afterEach(() => {
process.exitCode = undefined;
});
describe('in a basic repo', () => {
// working dir is never mutated
let testDir;
beforeAll(async () => {
testDir = await initFixture('basic');
});
it('should complain if invoked with an empty script', async () => {
const command = lernaRun(testDir)('');
await expect(command).rejects.toThrow('You must specify a lifecycle script to run');
});
it('should complain if invoked with an empty script using factory', async () => {
const command = factory(createArgv(testDir, ''));
await expect(command).rejects.toThrow('You must specify a lifecycle script to run');
});
it('should complain if invoked with an empty script using RunCommand class', async () => {
const command = new RunCommand(createArgv(testDir, ''));
await expect(command).rejects.toThrow('You must specify a lifecycle script to run');
});
it('runs a script in packages', async () => {
await lernaRun(testDir)('my-script');
const logLines = (logOutput as any).logged().split('\n');
expect(logLines).toContain('package-1');
expect(logLines).toContain('package-3');
});
it('runs a script in packages with --stream', async () => {
await lernaRun(testDir)('my-script', '--stream');
expect(ranInPackagesStreaming(testDir)).toMatchSnapshot();
});
it('omits package prefix with --stream --no-prefix', async () => {
// await lernaRun(testDir)('my-script', '--stream', '--no-prefix');
await new RunCommand(createArgv(testDir, 'my-script', '--stream', '--no-prefix'));
expect(ranInPackagesStreaming(testDir)).toMatchSnapshot();
});
it('always runs env script', async () => {
await factory(createArgv(testDir, 'env'));
// await lernaRun(testDir)('env');
expect((logOutput as any).logged().split('\n')).toEqual(['package-1', 'package-4', 'package-2', 'package-3']);
});
it('runs a script only in scoped packages', async () => {
// await lernaRun(testDir)('my-script', '--scope', 'package-1');
await new RunCommand(createArgv(testDir, 'my-script', '--scope', 'package-1'));
expect((logOutput as any).logged()).toBe('package-1');
});
it('does not run a script in ignored packages', async () => {
await new RunCommand(createArgv(testDir, 'my-script', '--ignore', 'package-@(2|3|4)'));
expect((logOutput as any).logged()).toBe('package-1');
});
it('does not error when no packages match', async () => {
await new RunCommand(createArgv(testDir, 'missing-script'));
expect(loggingOutput('info')).toContain('No packages found with the lifecycle script "missing-script"');
});
it('runs a script in all packages with --parallel', async () => {
await new RunCommand(createArgv(testDir, 'env', '--parallel'));
expect(ranInPackagesStreaming(testDir)).toMatchSnapshot();
});
it('omits package prefix with --parallel --no-prefix', async () => {
await new RunCommand(createArgv(testDir, 'env', '--parallel', '--no-prefix'));
expect(ranInPackagesStreaming(testDir)).toMatchSnapshot();
});
it('supports alternate npmClient configuration', async () => {
await new RunCommand(createArgv(testDir, 'env', '--npm-client', 'yarn'));
expect((logOutput as any).logged().split('\n')).toEqual(['package-1', 'package-4', 'package-2', 'package-3']);
});
it('reports script errors with early exit', async () => {
npmRunScript.mockImplementationOnce((script, { pkg }) => {
const err: any = new Error(pkg.name);
err.failed = true;
err.exitCode = 123;
return Promise.reject(err);
});
const command = new RunCommand(createArgv(testDir, 'fail'));
await expect(command).rejects.toThrow('package-1');
expect(process.exitCode).toBe(123);
});
it('propagates non-zero exit codes with --no-bail', async () => {
npmRunScript.mockImplementationOnce((script, { pkg }) => {
const err: any = new Error(pkg.name);
err.failed = true;
err.exitCode = 456;
err.stdout = pkg.name;
return Promise.resolve(err);
});
await new RunCommand(createArgv(testDir, 'my-script', '--no-bail'));
expect(process.exitCode).toBe(456);
expect((logOutput as any).logged().split('\n')).toEqual(['package-1', 'package-3']);
});
});
describe('with --include-filtered-dependencies', () => {
it('runs scoped command including filtered deps', async () => {
const testDir = await initFixture('include-filtered-dependencies');
await lernaRun(testDir)(
'my-script',
'--scope',
'@test/package-2',
'--include-filtered-dependencies',
'--',
'--silent'
);
const logLines = (logOutput as any).logged().split('\n');
expect(logLines).toContain('@test/package-1');
expect(logLines).toContain('@test/package-2');
});
});
describe('with --profile', () => {
it('executes a profiled command in all packages', async () => {
const cwd = await initFixture('basic');
await lernaRun(cwd)('my-script', '--profile');
const [profileLocation] = await globby('Lerna-Profile-*.json', { cwd, absolute: true });
const json = await fs.readJson(profileLocation);
expect(json).toMatchObject([
{
name: 'package-1',
ph: 'X',
ts: expect.any(Number),
pid: 1,
tid: expect.any(Number),
dur: expect.any(Number),
},
{
name: 'package-3',
},
]);
});
it('accepts --profile-location', async () => {
const cwd = await initFixture('basic');
await new RunCommand(createArgv(cwd, 'my-script', '--profile', '--profile-location', 'foo/bar'));
const [profileLocation] = await globby('foo/bar/Lerna-Profile-*.json', { cwd, absolute: true });
const exists = await fs.exists(profileLocation, null as any);
expect(exists).toBe(true);
});
});
describe('with --no-sort', () => {
it('runs scripts in lexical (not topological) order', async () => {
const testDir = await initFixture('toposort');
await new RunCommand(createArgv(testDir, 'env', '--concurrency', '1', '--no-sort'));
expect((logOutput as any).logged().split('\n')).toEqual([
'package-cycle-1',
'package-cycle-2',
'package-cycle-extraneous-1',
'package-cycle-extraneous-2',
'package-dag-1',
'package-dag-2a',
'package-dag-2b',
'package-dag-3',
'package-standalone',
]);
});
it('optionally streams output', async () => {
const testDir = await initFixture('toposort');
await new RunCommand(createArgv(testDir, 'env', '--concurrency', '1', '--no-sort', '--stream'));
expect(ranInPackagesStreaming(testDir)).toMatchInlineSnapshot(`
Array [
"packages/package-cycle-1 npm run env (prefixed: true)",
"packages/package-cycle-2 npm run env (prefixed: true)",
"packages/package-cycle-extraneous-1 npm run env (prefixed: true)",
"packages/package-cycle-extraneous-2 npm run env (prefixed: true)",
"packages/package-dag-1 npm run env (prefixed: true)",
"packages/package-dag-2a npm run env (prefixed: true)",
"packages/package-dag-2b npm run env (prefixed: true)",
"packages/package-dag-3 npm run env (prefixed: true)",
"packages/package-standalone npm run env (prefixed: true)",
]
`);
});
it('optionally streams output in cmd-dry-run mode and expect them all to be logged', async () => {
const testDir = await initFixture('toposort');
await new RunCommand(createArgv(testDir, 'env', '--concurrency', '1', '--no-sort', '--stream', '--cmd-dry-run'));
const logLines = (logOutput as any).logged().split('\n');
expect(logLines).toEqual([
'dry-run> package-cycle-1',
'dry-run> package-cycle-2',
'dry-run> package-cycle-extraneous-1',
'dry-run> package-cycle-extraneous-2',
'dry-run> package-dag-1',
'dry-run> package-dag-2a',
'dry-run> package-dag-2b',
'dry-run> package-dag-3',
'dry-run> package-standalone',
]);
});
});
describe('in a cyclical repo', () => {
it('warns when cycles are encountered', async () => {
const testDir = await initFixture('toposort');
await new RunCommand(createArgv(testDir, 'env', '--concurrency', '1'));
const [logMessage] = loggingOutput('warn');
expect(logMessage).toMatch('Dependency cycles detected, you should fix these!');
expect(logMessage).toMatch('package-cycle-1 -> package-cycle-2 -> package-cycle-1');
expect((logOutput as any).logged().split('\n')).toEqual([
'package-dag-1',
'package-standalone',
'package-dag-2a',
'package-dag-2b',
'package-cycle-1',
'package-cycle-2',
'package-dag-3',
'package-cycle-extraneous-1',
'package-cycle-extraneous-2',
]);
});
it('works with intersected cycles', async () => {
const testDir = await initFixture('cycle-intersection');
await new RunCommand(createArgv(testDir, 'env', '--concurrency', '1'));
const [logMessage] = loggingOutput('warn');
expect(logMessage).toMatch('Dependency cycles detected, you should fix these!');
expect(logMessage).toMatch('b -> c -> d -> e -> b');
expect(logMessage).toMatch('f -> g -> (nested cycle: b -> c -> d -> e -> b) -> f');
expect((logOutput as any).logged().split('\n')).toEqual(['f', 'b', 'e', 'd', 'c', 'g', 'a']);
});
it('works with separate cycles', async () => {
const testDir = await initFixture('cycle-separate');
await new RunCommand(createArgv(testDir, 'env', '--concurrency', '1'));
const [logMessage] = loggingOutput('warn');
expect(logMessage).toMatch('Dependency cycles detected, you should fix these!');
expect(logMessage).toMatch('b -> c -> d -> b');
expect(logMessage).toMatch('e -> f -> g -> e');
expect((logOutput as any).logged().split('\n')).toEqual(['e', 'g', 'f', 'h', 'b', 'd', 'c', 'a']);
});
it('should throw an error with --reject-cycles', async () => {
const testDir = await initFixture('toposort');
const command = new RunCommand(createArgv(testDir, 'env', '--reject-cycles'));
await expect(command).rejects.toThrow('Dependency cycles detected, you should fix these!');
});
});
// this is a temporary set of tests, which will be replaced by verdacio-driven tests
// once the required setup is fully set up
describe('in a repo powered by Nx', () => {
let testDir;
let collectedOutput = '';
let originalStdout;
beforeAll(async () => {
testDir = await initFixture('powered-by-nx');
process.env.NX_WORKSPACE_ROOT_PATH = testDir;
// @ts-ignore
jest.spyOn(process, 'exit').mockImplementation((code: any) => {
if (code !== 0) {
throw new Error();
}
});
originalStdout = process.stdout.write;
(process.stdout as any).write = (v) => {
collectedOutput = `${collectedOutput}\n${v}`;
};
});
afterAll(() => {
process.stdout.write = originalStdout;
});
it('runs a script in packages', async () => {
collectedOutput = '';
await lernaRun(testDir)('my-script');
expect(collectedOutput).toContain('package-1');
expect(collectedOutput).toContain('package-3');
expect(collectedOutput).toContain('Successfully ran target');
});
it('runs a script with a colon in the script name', async () => {
collectedOutput = '';
await lernaRun(testDir)('another-script:but-with-colons');
expect(collectedOutput).toContain('package-1-script-with-colons');
expect(collectedOutput).toContain('Successfully ran target');
});
it('runs a script only in scoped packages', async () => {
collectedOutput = '';
await lernaRun(testDir)('my-script', '--scope', 'package-1');
expect(collectedOutput).toContain('package-1');
expect(collectedOutput).not.toContain('package-3');
});
it('does not run a script in ignored packages', async () => {
collectedOutput = '';
await lernaRun(testDir)('my-script', '--ignore', 'package-@(2|3|4)');
expect(collectedOutput).toContain('package-1');
expect(collectedOutput).not.toContain('package-3');
});
it('runs a script in packages with --stream', async () => {
collectedOutput = '';
await lernaRun(testDir)('my-script', '--stream');
expect(collectedOutput).toContain('[package-1 ]');
expect(collectedOutput).toContain('[package-3 ]');
});
it('runs a cacheable script', async () => {
collectedOutput = '';
await lernaRun(testDir)('my-cacheable-script');
expect(collectedOutput).not.toContain('Nx read the output from the cache');
collectedOutput = '';
await lernaRun(testDir)('my-cacheable-script');
expect(collectedOutput).toContain('Nx read the output from the cache');
collectedOutput = '';
await lernaRun(testDir)('my-cacheable-script', '--skip-nx-cache');
expect(collectedOutput).not.toContain('Nx read the output from the cache');
});
});
});