-
Notifications
You must be signed in to change notification settings - Fork 369
/
helpers.ts
453 lines (419 loc) · 12.9 KB
/
helpers.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {readFileSync, readdirSync, statSync, existsSync} from 'fs';
import {resolve, posix} from 'path';
import * as crypto from 'crypto';
import * as sinon from 'sinon';
import * as snapshot from 'snap-shot-it';
import * as suggester from 'code-suggester';
import {CreatePullRequestUserOptions} from 'code-suggester/build/src/types';
import {Octokit} from '@octokit/rest';
import {
Commit,
ConventionalCommit,
parseConventionalCommits,
} from '../src/commit';
import {GitHub, GitHubTag, GitHubRelease} from '../src/github';
import {Update} from '../src/update';
import {expect} from 'chai';
import {CandidateReleasePullRequest} from '../src/manifest';
import {Version} from '../src/version';
import {PullRequestTitle} from '../src/util/pull-request-title';
import {PullRequestBody, ReleaseData} from '../src/util/pull-request-body';
import {BranchName} from '../src/util/branch-name';
import {ReleaseType} from '../src/factory';
import {
GitHubFileContents,
DEFAULT_FILE_MODE,
} from '@google-automations/git-file-utils';
import {CompositeUpdater} from '../src/updaters/composite';
import {PullRequestOverflowHandler} from '../src/util/pull-request-overflow-handler';
import {ReleasePullRequest} from '../src/release-pull-request';
import {PullRequest} from '../src/pull-request';
export function stubSuggesterWithSnapshot(
sandbox: sinon.SinonSandbox,
snapName: string
) {
sandbox.replace(
suggester,
'createPullRequest',
(
_octokit: Octokit,
changes: suggester.Changes | null | undefined,
options: CreatePullRequestUserOptions
): Promise<number> => {
snapshot(snapName + ': changes', stringifyExpectedChanges([...changes!]));
snapshot(snapName + ': options', stringifyExpectedOptions(options));
return Promise.resolve(22);
}
);
}
export function safeSnapshot(content: string) {
snapshot(dateSafe(newLine(content)));
}
export function dateSafe(content: string): string {
return content.replace(
/[0-9]{4}-[0-9]{2}-[0-9]{2}/g,
'1983-10-10' // use a fake date, so that we don't break daily.
);
}
function stringifyExpectedOptions(
expected: CreatePullRequestUserOptions
): string {
expected.description = newLine(expected.description);
let stringified = '';
for (const [option, value] of Object.entries(expected)) {
stringified = `${stringified}\n${option}: ${value}`;
}
return dateSafe(stringified);
}
function newLine(content: string): string {
return content.replace(/\r\n/g, '\n');
}
/*
* Given an object of changes expected to be made by code-suggester API,
* stringify content in such a way that it works well for snapshots:
*/
export function stringifyExpectedChanges(expected: [string, object][]): string {
let stringified = '';
for (const update of expected) {
stringified = `${stringified}\nfilename: ${update[0]}`;
const obj = update[1] as {[key: string]: string};
stringified = `${stringified}\n${newLine(obj.content)}`;
}
return dateSafe(stringified);
}
/*
* Reads a plain-old-JavaScript object, stored in fixtures directory.
* these are used to represent responses from the methods in the github.ts
* wrapper for GitHub API calls:
*/
export function readPOJO(name: string): object {
const content = readFileSync(
resolve('./test/fixtures/pojos', `${name}.json`),
'utf8'
);
return JSON.parse(content);
}
/**
* Reads a fixture file as a string or returns empty string if the fixture
* does not exist.
*/
export function readFixture(fixturesPath: string, fixture: string): string {
const file = resolve(fixturesPath, fixture);
if (!existsSync(file)) {
return '';
}
return readFileSync(resolve(fixturesPath, fixture), 'utf8');
}
export function buildMockConventionalCommit(
message: string,
files: string[] = []
): ConventionalCommit[] {
return parseConventionalCommits([
{
// Ensure SHA is same on Windows with replace:
sha: crypto
.createHash('md5')
.update(message.replace(/\r\n/g, '\n'))
.digest('hex'),
message,
files: files,
},
]);
}
export function buildMockCommit(message: string, files: string[] = []): Commit {
return {
// Ensure SHA is same on Windows with replace:
sha: crypto
.createHash('md5')
.update(message.replace(/\r\n/g, '\n'))
.digest('hex'),
message,
files: files,
};
}
export function buildGitHubFileContent(
fixturesPath: string,
fixture: string
): GitHubFileContents {
return buildGitHubFileRaw(
readFileSync(resolve(fixturesPath, fixture), 'utf8').replace(/\r\n/g, '\n')
);
}
export function buildGitHubFileRaw(content: string): GitHubFileContents {
return {
content: Buffer.from(content, 'utf8').toString('base64'),
parsedContent: content,
// fake a consistent sha
sha: crypto.createHash('md5').update(content).digest('hex'),
mode: DEFAULT_FILE_MODE,
};
}
export interface StubFiles {
sandbox: sinon.SinonSandbox;
github: GitHub;
// "master" TODO update all test code to use "main"
targetBranch?: string;
// Example1: test/updaters/fixtures/python
// Example2: test/fixtures/releaser/repo
fixturePath: string;
// list of files in the mocked repo relative to the repo root. These should
// have corresponding fixture files to use as stubbed content.
// Example1: ["setup.py, "src/foolib/version.py"]
// Example2: ["python/setup.py", "python/src/foolib/version.py"]
files: string[];
// If true, the fixture files are assumed to exist directly beneath
// Example (following Example1 above)
// - test/updaters/fixtures/python/setup.py
// - test/updaters/fixtures/python/version.py
//
// if false, the fixture files are assumed to exist under fixturePath *with*
// their relative path prefix.
// Example (following Example2 above)
// - test/fixtures/releaser/repo/python/setup.py
// - test/fixtures/releaser/python/src/foolib/version.py
flatten?: boolean;
// Inline content for files to stub.
// Example: [
// ['pkg1/package.json', '{"version":"1.2.3","name":"@foo/pkg1"}']
// ['py/setup.py', 'version = "3.2.1"\nname = "pylib"']
// ]
inlineFiles?: [string, string][];
}
export function stubFilesFromFixtures(options: StubFiles) {
const {fixturePath, sandbox, github, files} = options;
const inlineFiles = options.inlineFiles ?? [];
const overlap = inlineFiles.filter(f => files.includes(f[0]));
if (overlap.length > 0) {
throw new Error(
'Overlap between files and inlineFiles: ' + JSON.stringify(overlap)
);
}
const targetBranch = options.targetBranch ?? 'master';
const flatten = options.flatten ?? true;
const stub = sandbox.stub(github, 'getFileContentsOnBranch');
for (const file of files) {
let fixtureFile = file;
if (flatten) {
const parts = file.split('/');
fixtureFile = parts[parts.length - 1];
}
stub
.withArgs(file, targetBranch)
.resolves(buildGitHubFileContent(fixturePath, fixtureFile));
}
for (const [file, content] of inlineFiles) {
stub.withArgs(file, targetBranch).resolves(buildGitHubFileRaw(content));
}
stub.rejects(Object.assign(Error('not found'), {status: 404}));
}
// get list of files in a directory
export function getFilesInDir(
directory: string,
fileList: string[] = []
): string[] {
const items = readdirSync(directory);
for (const item of items) {
const stat = statSync(posix.join(directory, item));
if (stat.isDirectory())
fileList = getFilesInDir(posix.join(directory, item), fileList);
else fileList.push(posix.join(directory, item));
}
return fileList;
}
// get list of files with a particular prefix in a directory
export function getFilesInDirWithPrefix(directory: string, prefix: string) {
const allFiles = getFilesInDir(directory);
return allFiles
.filter(p => {
return posix.extname(p) === `.${prefix}`;
})
.map(p => posix.relative(directory, p));
}
/* eslint-disable @typescript-eslint/no-explicit-any */
export function assertHasUpdate(
updates: Update[],
path: string,
clazz?: any
): Update {
const found = updates.find(update => {
return update.path === path;
});
expect(found, `update for ${path}`).to.not.be.undefined;
if (clazz) {
expect(found?.updater).instanceof(
clazz,
`expected update to be of class ${clazz}`
);
}
return found!;
}
export function assertHasUpdates(
updates: Update[],
path: string,
...clazz: any
) {
if (clazz.length <= 1) {
return assertHasUpdate(updates, path, clazz[0]);
}
const composite = assertHasUpdate(updates, path, CompositeUpdater)
.updater as CompositeUpdater;
expect(composite.updaters).to.be.lengthOf(
clazz.length,
`expected to find exactly ${clazz.length} updaters`
);
for (let i = 0; i < clazz.length; i++) {
expect(composite.updaters[i]).to.be.instanceof(
clazz[i],
`expected updaters[${i}] to be of class ${clazz[i]}`
);
}
return composite;
}
export function assertNoHasUpdate(updates: Update[], path: string) {
const found = updates.find(update => {
return update.path === path;
});
expect(found, `update for ${path}`).to.be.undefined;
}
export function loadCommitFixtures(name: string): Commit[] {
const content = readFileSync(
resolve('./test/fixtures/commits', `${name}.json`),
'utf8'
);
return JSON.parse(content);
}
export function buildCommitFromFixture(name: string): Commit {
const message = readFileSync(
resolve('./test/fixtures/commit-messages', `${name}.txt`),
'utf8'
);
return buildMockCommit(message);
}
interface MockCandidatePullRequestOptions {
component?: string;
updates?: Update[];
notes?: string;
draft?: boolean;
labels?: string[];
group?: string;
}
export function buildMockCandidatePullRequest(
path: string,
releaseType: ReleaseType,
versionString: string,
options: MockCandidatePullRequestOptions = {}
): CandidateReleasePullRequest {
const version = Version.parse(versionString);
return {
path,
pullRequest: {
title: PullRequestTitle.ofTargetBranch('main'),
body: new PullRequestBody([
{
component: options.component,
version,
notes:
options.notes ??
`Release notes for path: ${path}, releaseType: ${releaseType}`,
},
]),
updates: options.updates ?? [],
labels: options.labels ?? [],
headRefName: BranchName.ofTargetBranch('main').toString(),
version,
draft: options.draft ?? false,
group: options.group,
},
config: {
releaseType,
},
};
}
export function mockCommits(
sandbox: sinon.SinonSandbox,
github: GitHub,
commits: Commit[]
): sinon.SinonStub {
async function* fakeGenerator() {
for (const commit of commits) {
yield commit;
}
}
return sandbox.stub(github, 'mergeCommitIterator').returns(fakeGenerator());
}
export function mockReleases(
sandbox: sinon.SinonSandbox,
github: GitHub,
releases: GitHubRelease[]
): sinon.SinonStub {
async function* fakeGenerator() {
for (const release of releases) {
yield release;
}
}
return sandbox.stub(github, 'releaseIterator').returns(fakeGenerator());
}
export function mockTags(
sandbox: sinon.SinonSandbox,
github: GitHub,
tags: GitHubTag[]
): sinon.SinonStub {
async function* fakeGenerator() {
for (const tag of tags) {
yield tag;
}
}
return sandbox.stub(github, 'tagIterator').returns(fakeGenerator());
}
export function mockPullRequests(
sandbox: sinon.SinonSandbox,
github: GitHub,
pullRequests: PullRequest[]
): sinon.SinonStub {
async function* fakeGenerator() {
for (const pullRequest of pullRequests) {
yield pullRequest;
}
}
return sandbox.stub(github, 'pullRequestIterator').returns(fakeGenerator());
}
export function mockReleaseData(count: number): ReleaseData[] {
const releaseData: ReleaseData[] = [];
const version = Version.parse('1.2.3');
for (let i = 0; i < count; i++) {
releaseData.push({
component: `component${i}`,
version,
notes: `release notes for component${i}`,
});
}
return releaseData;
}
export class MockPullRequestOverflowHandler
implements PullRequestOverflowHandler
{
async handleOverflow(
pullRequest: ReleasePullRequest,
_maxSize?: number | undefined
): Promise<string> {
return pullRequest.body.toString();
}
async parseOverflow(
pullRequest: PullRequest
): Promise<PullRequestBody | undefined> {
return PullRequestBody.parse(pullRequest.body);
}
}