forked from electron/trop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.spec.ts
171 lines (152 loc) · 4.84 KB
/
operations.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
import { spawnSync } from 'child_process';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import simpleGit from 'simple-git';
import { PRChange } from '../src/enums';
import { initRepo } from '../src/operations/init-repo';
import { setupRemotes } from '../src/operations/setup-remotes';
import { updateManualBackport } from '../src/operations/update-manual-backport';
import { tagBackportReviewers } from '../src/utils';
let dirObject: { dir?: string } | null = null;
const saveDir = (o: { dir: string }) => {
dirObject = o;
return o.dir;
};
const backportPRClosedEvent = require('./fixtures/backport_pull_request.closed.json');
const backportPRMergedEvent = require('./fixtures/backport_pull_request.merged.json');
const backportPROpenedEvent = require('./fixtures/backport_pull_request.opened.json');
jest.mock('../src/utils', () => ({
tagBackportReviewers: jest.fn().mockReturnValue(Promise.resolve()),
isSemverMinorPR: jest.fn().mockReturnValue(false),
}));
jest.mock('../src/utils/label-utils', () => ({
labelExistsOnPR: jest.fn().mockResolvedValue(true),
getSemverLabel: jest.fn().mockResolvedValue(false),
addLabels: jest.fn(),
removeLabel: jest.fn(),
}));
describe('runner', () => {
jest.setTimeout(30000);
console.error = jest.fn();
afterEach(async () => {
if (dirObject && dirObject.dir) {
await fs.remove(dirObject.dir);
}
});
describe('initRepo()', () => {
it('should clone a github repository', async () => {
const dir = saveDir(
await initRepo({
slug: 'electron/trop',
accessToken: '',
}),
);
expect(await fs.pathExists(dir)).toBe(true);
expect(await fs.pathExists(path.resolve(dir, '.git'))).toBe(true);
});
it('should fail if the github repository does not exist', async () => {
await expect(
initRepo({
slug: 'electron/this-is-not-trop',
accessToken: '',
}),
).rejects.toBeTruthy();
});
});
describe('setUpRemotes()', () => {
let dir: string;
beforeEach(async () => {
dir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'trop-spec-'));
await fs.mkdirp(dir);
spawnSync('git', ['init'], { cwd: dir });
});
afterEach(async () => {
if (await fs.pathExists(dir)) {
await fs.remove(dir);
}
});
it('should set new remotes correctly', async () => {
await setupRemotes({
dir,
remotes: [
{
name: 'origin',
value: 'https://github.com/electron/clerk.git',
},
{
name: 'secondary',
value: 'https://github.com/electron/trop.git',
},
],
});
const git = simpleGit(dir);
const remotes = await git.raw(['remote', '-v']);
const parsedRemotes = remotes
.trim()
.replace(/ +/g, ' ')
.replace(/\t/g, ' ')
.replace(/ \(fetch\)/g, '')
.replace(/ \(push\)/g, '')
.split(/\r?\n/g)
.map((line) => line.trim().split(' '));
expect(parsedRemotes.length).toBe(4);
for (const remote of parsedRemotes) {
expect(remote.length).toBe(2);
expect(['origin', 'secondary']).toContain(remote[0]);
if (remote[0] === 'origin') {
expect(
remote[1].endsWith('github.com/electron/clerk.git'),
).toBeTruthy();
} else {
expect(
remote[1].endsWith('github.com/electron/trop.git'),
).toBeTruthy();
}
}
});
});
describe('updateManualBackport()', () => {
let context: any;
const octokit = {
pulls: {
get: jest.fn().mockReturnValue(Promise.resolve({})),
},
issues: {
createComment: jest.fn().mockReturnValue(Promise.resolve({})),
listComments: jest.fn().mockReturnValue(Promise.resolve({ data: [] })),
},
};
it('tags reviewers on manual backport creation', async () => {
context = {
...backportPROpenedEvent,
octokit,
repo: jest.fn(),
};
await updateManualBackport(context, PRChange.OPEN, 1234);
expect(tagBackportReviewers).toHaveBeenCalled();
expect(tagBackportReviewers).toHaveBeenCalledWith({
context,
targetPrNumber: 7,
});
});
it('does not tag reviewers on merged PRs', async () => {
context = {
...backportPRMergedEvent,
octokit,
repo: jest.fn(),
};
await updateManualBackport(context, PRChange.MERGE, 1234);
expect(tagBackportReviewers).not.toHaveBeenCalled();
});
it('does not tag reviewers on closed PRs', async () => {
context = {
...backportPRClosedEvent,
octokit,
repo: jest.fn(),
};
await updateManualBackport(context, PRChange.CLOSE, 1234);
expect(tagBackportReviewers).not.toHaveBeenCalled();
});
});
});