-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathgetChangedPackages.test.ts
More file actions
378 lines (326 loc) · 15.4 KB
/
getChangedPackages.test.ts
File metadata and controls
378 lines (326 loc) · 15.4 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
import { describe, expect, it, afterEach, beforeAll, afterAll, jest } from '@jest/globals';
import { defaultBranchName, defaultRemoteBranchName } from '../__fixtures__/gitDefaults';
import { RepositoryFactory } from '../__fixtures__/repositoryFactory';
import { getPackageInfos } from '../monorepo/getPackageInfos';
import type { BeachballOptions, RepoOptions } from '../types/BeachballOptions';
import { getChangedPackages } from '../changefile/getChangedPackages';
import { initMockLogs } from '../__fixtures__/mockLogs';
import { generateChangeFiles } from '../__fixtures__/changeFiles';
import type { Repository } from '../__fixtures__/repository';
import { getParsedOptions } from '../options/getOptions';
import { getScopedPackages } from '../monorepo/getScopedPackages';
import { addGitObserver, clearGitObservers } from 'workspace-tools';
// These were formerly the isChangeFileNeeded tests.
// They still cover some relevant cases and have a simpler/cheaper setup.
describe('getChangedPackages (basic)', () => {
/** Factory reused for all the tests */
let repositoryFactory: RepositoryFactory;
/**
* Clone from the factory reused for multiple tests where it's safe.
* DO NOT:
* - push changes
* - create uncommitted files or directories
*/
let reusedRepo: Repository;
const gitObserver = jest.fn();
initMockLogs();
/** Get options/context, clear `gitObserver` mock, and call `getChangedPackages` */
function getChangedPackagesWrapper(options?: Partial<BeachballOptions>) {
const parsedOptions = getParsedOptions({
cwd: reusedRepo.rootPath,
argv: ['node', 'beachball', 'change'],
testRepoOptions: {
fetch: false,
branch: defaultRemoteBranchName,
...options,
},
});
const packageInfos = getPackageInfos(parsedOptions);
const scopedPackages = getScopedPackages(parsedOptions.options, packageInfos);
gitObserver.mockClear();
return getChangedPackages(parsedOptions.options, packageInfos, scopedPackages);
}
/** In `reusedRepo`, check out a branch with a unique name based on master */
function checkOutTestBranch() {
const branchName = expect.getState().currentTestName!.replace(/\W+/g, '-');
reusedRepo.checkout('-b', branchName, defaultBranchName);
}
beforeAll(() => {
repositoryFactory = new RepositoryFactory('monorepo');
reusedRepo = repositoryFactory.cloneRepository();
addGitObserver(gitObserver);
});
afterEach(() => {
gitObserver.mockClear();
});
afterAll(() => {
// clean up the factory and all clones
repositoryFactory.cleanUp();
clearGitObservers();
});
it('returns empty list when no changes have been made', () => {
expect(getChangedPackagesWrapper()).toEqual([]);
expect(gitObserver).toHaveBeenCalled();
});
it('returns package name when changes exist in a new branch', () => {
checkOutTestBranch();
reusedRepo.commitChange('packages/foo/myFilename');
expect(getChangedPackagesWrapper()).toEqual(['foo']);
expect(gitObserver).toHaveBeenCalled();
});
it('returns empty list when changes are CHANGELOG files', () => {
checkOutTestBranch();
reusedRepo.commitChange('packages/foo/CHANGELOG.md');
expect(getChangedPackagesWrapper()).toEqual([]);
expect(gitObserver).toHaveBeenCalled();
});
it('returns the given package name(s) as-is', () => {
expect(getChangedPackagesWrapper({ package: 'foo' })).toEqual(['foo']);
expect(gitObserver).not.toHaveBeenCalled();
// Currently it doesn't even check validity
expect(getChangedPackagesWrapper({ package: ['foo', 'bar', 'nope'] })).toEqual(['foo', 'bar', 'nope']);
expect(gitObserver).not.toHaveBeenCalled();
});
it('returns all packages with all: true', () => {
expect(getChangedPackagesWrapper({ all: true }).sort()).toEqual(['a', 'b', 'bar', 'baz', 'foo']);
expect(gitObserver).not.toHaveBeenCalled();
});
it('throws if the remote is invalid', () => {
const customRemote = 'foo';
reusedRepo.git(['remote', 'add', customRemote, 'file:///__nonexistent']);
checkOutTestBranch();
reusedRepo.commitChange('fake.js');
expect(() => {
getChangedPackagesWrapper({ fetch: true, branch: `${customRemote}/${defaultBranchName}` });
}).toThrow(`Fetching branch "${defaultBranchName}" from remote "${customRemote}" failed`);
expect(gitObserver).toHaveBeenCalled();
});
});
describe('getChangedPackages', () => {
// These tests reuse factories since they don't push changes
let singleFactory: RepositoryFactory;
let monorepoFactory: RepositoryFactory;
let multiFactory: RepositoryFactory;
const extraFactories: RepositoryFactory[] = [];
let repo: Repository | undefined;
const logs = initMockLogs();
function getOptionsAndPackages(
params: { repoOptions?: Partial<RepoOptions>; extraArgv?: string[]; cwd?: string } = {}
) {
const { repoOptions, extraArgv = [], cwd = repo!.rootPath } = params;
const parsedOptions = getParsedOptions({
cwd,
argv: ['node', 'beachball', 'change', ...extraArgv],
testRepoOptions: {
branch: defaultRemoteBranchName,
fetch: false,
...repoOptions,
},
});
const packageInfos = getPackageInfos(parsedOptions);
const scopedPackages = getScopedPackages(parsedOptions.options, packageInfos);
return { packageInfos, options: parsedOptions.options, parsedOptions, scopedPackages };
}
beforeAll(() => {
singleFactory = new RepositoryFactory('single');
monorepoFactory = new RepositoryFactory('monorepo');
multiFactory = new RepositoryFactory('multi-project');
});
afterEach(() => {
repo = undefined;
});
afterAll(() => {
singleFactory.cleanUp();
monorepoFactory.cleanUp();
multiFactory.cleanUp();
extraFactories.forEach(factory => factory.cleanUp());
});
it('detects changed files in single-package repo', () => {
repo = singleFactory.cloneRepository();
const { packageInfos, scopedPackages, options } = getOptionsAndPackages();
expect(getChangedPackages(options, packageInfos, scopedPackages)).toStrictEqual([]);
repo.stageChange('foo.js');
expect(getChangedPackages(options, packageInfos, scopedPackages)).toStrictEqual(['foo']);
});
it('respects ignorePatterns option', () => {
repo = singleFactory.cloneRepository();
const { packageInfos, scopedPackages, options } = getOptionsAndPackages({
repoOptions: { ignorePatterns: ['*.test.js', 'tests/**', 'yarn.lock'] },
extraArgv: ['--verbose'],
});
repo.writeFile('src/foo.test.js');
repo.writeFile('src/foo.test.js');
repo.writeFile('tests/stuff.js');
repo.writeFile('yarn.lock');
repo.git(['add', '-A']); // stage in one git operation
expect(getChangedPackages(options, packageInfos, scopedPackages)).toStrictEqual([]);
const logLines = logs.getMockLines('all');
expect(logLines).toMatch('ignored by pattern');
expect(logLines).toMatchInlineSnapshot(`
"[log] Checking for changes against "origin/master"
[log] Found 2 changed files in current branch (before filtering)
[log] - ~~src/foo.test.js~~ (ignored by pattern "*.test.js")
[log] - ~~tests/stuff.js~~ (ignored by pattern "tests/**")
[log] All files were ignored"
`);
});
it('detects changed files in monorepo', () => {
repo = monorepoFactory.cloneRepository();
const { packageInfos, scopedPackages, options } = getOptionsAndPackages();
expect(getChangedPackages(options, packageInfos, scopedPackages)).toStrictEqual([]);
repo.stageChange('packages/foo/test.js');
expect(getChangedPackages(options, packageInfos, scopedPackages)).toStrictEqual(['foo']);
});
it('excludes packages that already have change files', () => {
repo = monorepoFactory.cloneRepository();
const { packageInfos, scopedPackages, options } = getOptionsAndPackages({ extraArgv: ['--verbose'] });
// setup: create branch, change foo, create a change file, commit
repo.checkout('-b', 'test');
repo.commitChange('packages/foo/test.js');
generateChangeFiles(['foo'], options);
logs.clear();
// foo is not included in changed packages
let changedPackages = getChangedPackages(options, packageInfos, scopedPackages);
const logLines = logs.getMockLines('all', { sanitize: true });
expect(logLines).toMatch(/Your local repository already has change files for these packages:\s+• foo/);
expect(logLines).toMatchInlineSnapshot(`
"[log] Checking for changes against "origin/master"
[log] Found 2 changed files in current branch (before filtering)
[log] - ~~change/foo-<guid>.json~~ (ignored by pattern "change/*.json")
[log] - packages/foo/test.js
[log] Found 1 file in 1 package that should be published
[log] Your local repository already has change files for these packages:
• foo"
`);
expect(changedPackages).toStrictEqual([]);
logs.clear();
// change bar => bar is the only changed package returned
repo.stageChange('packages/bar/test.js');
changedPackages = getChangedPackages(options, packageInfos, scopedPackages);
expect(logs.getMockLines('all', { sanitize: true })).toMatchInlineSnapshot(`
"[log] Checking for changes against "origin/master"
[log] Found 3 changed files in current branch (before filtering)
[log] - ~~change/foo-<guid>.json~~ (ignored by pattern "change/*.json")
[log] - packages/foo/test.js
[log] - packages/bar/test.js
[log] Found 2 files in 2 packages that should be published
[log] Your local repository already has change files for these packages:
• foo"
`);
expect(changedPackages).toStrictEqual(['bar']);
});
it('excludes packages with staged (not committed) change files', () => {
repo = monorepoFactory.cloneRepository();
const { packageInfos, scopedPackages, options } = getOptionsAndPackages({ extraArgv: ['--verbose'] });
// setup: create branch, change foo
repo.checkout('-b', 'test-staged');
repo.commitChange('packages/foo/test.js');
// generate change files but only stage them (don't commit)
generateChangeFiles(['foo'], { ...options, commit: false });
logs.clear();
// foo is not included in changed packages because its staged change file is found
const changedPackages = getChangedPackages(options, packageInfos, scopedPackages);
expect(logs.getMockLines('all')).toMatch(
/Your local repository already has change files for these packages:\s+• foo/
);
expect(changedPackages).toStrictEqual([]);
});
it('ignores change files that exist in target remote branch', () => {
// This needs a separate factory since it pushes changes
const repositoryFactory = new RepositoryFactory('single');
extraFactories.push(repositoryFactory);
repo = repositoryFactory.cloneRepository();
const { packageInfos, scopedPackages, options } = getOptionsAndPackages({ extraArgv: ['--verbose'] });
expect(options.commit).toBe(true);
// create and push a change file in master
generateChangeFiles(['foo'], options);
repo.push();
// create a new branch and stage a new file + changes to existing file
repo.checkout('-b', 'test');
repo.writeFile('test.js');
repo.writeFile('yarn.lock', 'hi'); // this should already exist
repo.git(['add', '-A']);
logs.clear();
const changedPackages = getChangedPackages(options, packageInfos, scopedPackages);
expect(changedPackages).toStrictEqual(['foo']);
expect(logs.getMockLines('all', { sanitize: true })).toMatchInlineSnapshot(`
"[log] Checking for changes against "origin/master"
[log] Found 2 changed files in current branch (before filtering)
[log] - test.js
[log] - yarn.lock
[log] Found 2 files in 1 package that should be published"
`);
});
it('ignores package changes as appropriate', () => {
// Due to cost of fixtures, test various ignore scenarios together
const customFactory = new RepositoryFactory({
folders: {
packages: {
'private-pkg': { version: '1.0.0', private: true },
'no-publish': { version: '1.0.0', beachball: { shouldPublish: false } as BeachballOptions },
'out-of-scope': { version: '1.0.0' },
'ignore-pkg': { version: '1.0.0' },
'publish-me': { version: '1.0.0' },
},
},
});
extraFactories.push(customFactory);
repo = customFactory.cloneRepository();
repo.stageChange('packages/private-pkg/test.js');
repo.stageChange('packages/no-publish/test.js');
repo.stageChange('packages/out-of-scope/test.js');
repo.stageChange('packages/ignore-pkg/jest.config.js');
repo.stageChange('packages/ignore-pkg/CHANGELOG.md');
repo.stageChange('packages/publish-me/test.js');
const { packageInfos, scopedPackages, options } = getOptionsAndPackages({
repoOptions: {
scope: ['!packages/out-of-scope'],
ignorePatterns: ['**/jest.config.js'],
},
extraArgv: ['--verbose'],
});
const changedPackages = getChangedPackages(options, packageInfos, scopedPackages);
const logLines = logs.getMockLines('all');
// check individual cases
expect(logLines).toMatch('private-pkg is private');
expect(logLines).toMatch('no-publish has beachball.shouldPublish=false');
expect(logLines).toMatch('out-of-scope is out of scope');
expect(logLines).toMatch('ignored by pattern "**/jest.config.js"');
expect(logLines).toMatch('ignored by pattern "CHANGELOG.{md,json}"');
// and overall output
expect(logLines).toMatchInlineSnapshot(`
"[log] Checking for changes against "origin/master"
[log] Found 6 changed files in current branch (before filtering)
[log] - ~~packages/ignore-pkg/CHANGELOG.md~~ (ignored by pattern "CHANGELOG.{md,json}")
[log] - ~~packages/ignore-pkg/jest.config.js~~ (ignored by pattern "**/jest.config.js")
[log] - ~~packages/no-publish/test.js~~ (no-publish has beachball.shouldPublish=false)
[log] - ~~packages/out-of-scope/test.js~~ (out-of-scope is out of scope)
[log] - ~~packages/private-pkg/test.js~~ (private-pkg is private)
[log] - packages/publish-me/test.js
[log] Found 1 file in 1 package that should be published"
`);
// and return value
expect(changedPackages).toStrictEqual(['publish-me']);
});
it('detects changed files in multi-root monorepo repo', () => {
repo = multiFactory.cloneRepository();
const {
options: rootOptions,
packageInfos: rootPackageInfos,
scopedPackages: rootScopedPackages,
} = getOptionsAndPackages();
expect(Object.keys(multiFactory.fixtures)).toEqual(['project-a', 'project-b']);
const projectARoot = repo.pathTo('project-a');
const projectBRoot = repo.pathTo('project-b');
expect(getChangedPackages(rootOptions, rootPackageInfos, rootScopedPackages)).toStrictEqual([]);
repo.stageChange('project-a/packages/foo/test.js');
const infoA = getOptionsAndPackages({ cwd: projectARoot });
const infoB = getOptionsAndPackages({ cwd: projectBRoot });
const changedPackagesA = getChangedPackages(infoA.options, infoA.packageInfos, infoA.scopedPackages);
const changedPackagesB = getChangedPackages(infoB.options, infoB.packageInfos, infoB.scopedPackages);
const changedPackagesRoot = getChangedPackages(rootOptions, rootPackageInfos, rootScopedPackages);
expect(changedPackagesA).toStrictEqual(['@project-a/foo']);
expect(changedPackagesB).toStrictEqual([]);
expect(changedPackagesRoot).toStrictEqual(['@project-a/foo']);
});
});