forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr-checkout-controller.test.js
299 lines (250 loc) · 10.1 KB
/
pr-checkout-controller.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
import React from 'react';
import {shallow} from 'enzyme';
import {BarePullRequestCheckoutController} from '../../lib/controllers/pr-checkout-controller';
import {cloneRepository, buildRepository} from '../helpers';
import BranchSet from '../../lib/models/branch-set';
import Branch, {nullBranch} from '../../lib/models/branch';
import RemoteSet from '../../lib/models/remote-set';
import Remote from '../../lib/models/remote';
import {GitError} from '../../lib/git-shell-out-strategy';
import {repositoryBuilder} from '../builder/graphql/repository';
import {pullRequestBuilder} from '../builder/graphql/pr';
import * as reporterProxy from '../../lib/reporter-proxy';
import repositoryQuery from '../../lib/controllers/__generated__/prCheckoutController_repository.graphql';
import pullRequestQuery from '../../lib/controllers/__generated__/prCheckoutController_pullRequest.graphql';
describe('PullRequestCheckoutController', function() {
let localRepository, children;
beforeEach(async function() {
localRepository = await buildRepository(await cloneRepository());
children = sinon.spy();
});
function buildApp(override = {}) {
const branches = new BranchSet([
new Branch('master', nullBranch, nullBranch, true),
]);
const remotes = new RemoteSet([
new Remote('origin', '[email protected]:atom/github.git'),
]);
const props = {
repository: repositoryBuilder(repositoryQuery).build(),
pullRequest: pullRequestBuilder(pullRequestQuery).build(),
localRepository,
isAbsent: false,
isLoading: false,
isPresent: true,
isMerging: false,
isRebasing: false,
branches,
remotes,
children,
...override,
};
return <BarePullRequestCheckoutController {...props} />;
}
it('is disabled if the repository is loading or absent', function() {
const wrapper = shallow(buildApp({isAbsent: true}));
const [op] = children.lastCall.args;
assert.isFalse(op.isEnabled());
assert.strictEqual(op.getMessage(), 'No repository found');
wrapper.setProps({isAbsent: false, isLoading: true});
const [op1] = children.lastCall.args;
assert.isFalse(op1.isEnabled());
assert.strictEqual(op1.getMessage(), 'Loading');
wrapper.setProps({isAbsent: false, isLoading: false, isPresent: false});
const [op2] = children.lastCall.args;
assert.isFalse(op2.isEnabled());
assert.strictEqual(op2.getMessage(), 'No repository found');
});
it('is disabled if the local repository is merging or rebasing', function() {
const wrapper = shallow(buildApp({isMerging: true}));
const [op0] = children.lastCall.args;
assert.isFalse(op0.isEnabled());
assert.strictEqual(op0.getMessage(), 'Merge in progress');
wrapper.setProps({isMerging: false, isRebasing: true});
const [op1] = children.lastCall.args;
assert.isFalse(op1.isEnabled());
assert.strictEqual(op1.getMessage(), 'Rebase in progress');
});
it('is disabled if the pullRequest has no headRepository', function() {
shallow(buildApp({
pullRequest: pullRequestBuilder(pullRequestQuery).nullHeadRepository().build(),
}));
const [op] = children.lastCall.args;
assert.isFalse(op.isEnabled());
assert.strictEqual(op.getMessage(), 'Pull request head repository does not exist');
});
it('is disabled if the current branch already corresponds to the pull request', function() {
const upstream = Branch.createRemoteTracking('remotes/origin/feature', 'origin', 'refs/heads/feature');
const branches = new BranchSet([
new Branch('current', upstream, upstream, true),
]);
const remotes = new RemoteSet([
new Remote('origin', '[email protected]:aaa/bbb.git'),
]);
const pullRequest = pullRequestBuilder(pullRequestQuery)
.headRefName('feature')
.headRepository(r => {
r.owner(o => o.login('aaa'));
r.name('bbb');
})
.build();
shallow(buildApp({
pullRequest,
branches,
remotes,
}));
const [op] = children.lastCall.args;
assert.isFalse(op.isEnabled());
assert.strictEqual(op.getMessage(), 'Current');
});
it('recognizes a current branch even if it was pulled from the refs/pull/... ref', function() {
const upstream = Branch.createRemoteTracking('remotes/origin/pull/123/head', 'origin', 'refs/pull/123/head');
const branches = new BranchSet([
new Branch('current', upstream, upstream, true),
]);
const remotes = new RemoteSet([
new Remote('origin', '[email protected]:aaa/bbb.git'),
]);
const repository = repositoryBuilder(repositoryQuery)
.owner(o => o.login('aaa'))
.name('bbb')
.build();
const pullRequest = pullRequestBuilder(pullRequestQuery)
.number(123)
.headRefName('feature')
.headRepository(r => {
r.owner(o => o.login('ccc'));
r.name('ddd');
})
.build();
shallow(buildApp({
repository,
pullRequest,
branches,
remotes,
}));
const [op] = children.lastCall.args;
assert.isFalse(op.isEnabled());
assert.strictEqual(op.getMessage(), 'Current');
});
it('creates a new remote, fetches a PR branch, and checks it out into a new local branch', async function() {
const upstream = Branch.createRemoteTracking('remotes/origin/current', 'origin', 'refs/heads/current');
const branches = new BranchSet([
new Branch('current', upstream, upstream, true),
]);
const remotes = new RemoteSet([
new Remote('origin', '[email protected]:aaa/bbb.git'),
]);
sinon.stub(localRepository, 'addRemote').resolves(new Remote('ccc', '[email protected]:ccc/ddd.git'));
sinon.stub(localRepository, 'fetch').resolves();
sinon.stub(localRepository, 'checkout').resolves();
const pullRequest = pullRequestBuilder(pullRequestQuery)
.number(456)
.headRefName('feature')
.headRepository(r => {
r.owner(o => o.login('ccc'));
r.name('ddd');
})
.build();
shallow(buildApp({
pullRequest,
branches,
remotes,
}));
sinon.spy(reporterProxy, 'incrementCounter');
const [op] = children.lastCall.args;
await op.run();
assert.isTrue(localRepository.addRemote.calledWith('ccc', '[email protected]:ccc/ddd.git'));
assert.isTrue(localRepository.fetch.calledWith('refs/heads/feature', {remoteName: 'ccc'}));
assert.isTrue(localRepository.checkout.calledWith('pr-456/ccc/feature', {
createNew: true,
track: true,
startPoint: 'refs/remotes/ccc/feature',
}));
assert.isTrue(reporterProxy.incrementCounter.calledWith('checkout-pr'));
});
it('fetches a PR branch from an existing remote and checks it out into a new local branch', async function() {
sinon.stub(localRepository, 'fetch').resolves();
sinon.stub(localRepository, 'checkout').resolves();
const branches = new BranchSet([
new Branch('current', nullBranch, nullBranch, true),
]);
const remotes = new RemoteSet([
new Remote('origin', '[email protected]:aaa/bbb.git'),
new Remote('existing', '[email protected]:ccc/ddd.git'),
]);
const pullRequest = pullRequestBuilder(pullRequestQuery)
.number(789)
.headRefName('clever-name')
.headRepository(r => {
r.owner(o => o.login('ccc'));
r.name('ddd');
})
.build();
shallow(buildApp({
pullRequest,
branches,
remotes,
}));
sinon.spy(reporterProxy, 'incrementCounter');
const [op] = children.lastCall.args;
await op.run();
assert.isTrue(localRepository.fetch.calledWith('refs/heads/clever-name', {remoteName: 'existing'}));
assert.isTrue(localRepository.checkout.calledWith('pr-789/ccc/clever-name', {
createNew: true,
track: true,
startPoint: 'refs/remotes/existing/clever-name',
}));
assert.isTrue(reporterProxy.incrementCounter.calledWith('checkout-pr'));
});
it('checks out an existing local branch that corresponds to the pull request', async function() {
sinon.stub(localRepository, 'pull').resolves();
sinon.stub(localRepository, 'checkout').resolves();
const currentUpstream = Branch.createRemoteTracking('remotes/origin/current', 'origin', 'refs/heads/current');
const branches = new BranchSet([
new Branch('current', currentUpstream, currentUpstream, true),
new Branch('existing', Branch.createRemoteTracking('remotes/upstream/pull/123', 'upstream', 'refs/heads/yes')),
new Branch('wrong/remote', Branch.createRemoteTracking('remotes/wrong/pull/123', 'wrong', 'refs/heads/yes')),
new Branch('wrong/ref', Branch.createRemoteTracking('remotes/upstream/pull/123', 'upstream', 'refs/heads/no')),
]);
const remotes = new RemoteSet([
new Remote('origin', '[email protected]:aaa/bbb.git'),
new Remote('upstream', '[email protected]:ccc/ddd.git'),
new Remote('wrong', '[email protected]:eee/fff.git'),
]);
const pullRequest = pullRequestBuilder(pullRequestQuery)
.number(456)
.headRefName('yes')
.headRepository(r => {
r.owner(o => o.login('ccc'));
r.name('ddd');
})
.build();
shallow(buildApp({
pullRequest,
branches,
remotes,
}));
sinon.spy(reporterProxy, 'incrementCounter');
const [op] = children.lastCall.args;
await op.run();
assert.isTrue(localRepository.checkout.calledWith('existing'));
assert.isTrue(localRepository.pull.calledWith('refs/heads/yes', {remoteName: 'upstream', ffOnly: true}));
assert.isTrue(reporterProxy.incrementCounter.calledWith('checkout-pr'));
});
it('squelches git errors', async function() {
sinon.stub(localRepository, 'addRemote').rejects(new GitError('handled by the pipeline'));
shallow(buildApp({}));
// Should not throw
const [op] = children.lastCall.args;
await op.run();
assert.isTrue(localRepository.addRemote.called);
});
it('propagates non-git errors', async function() {
sinon.stub(localRepository, 'addRemote').rejects(new Error('not handled by the pipeline'));
shallow(buildApp({}));
const [op] = children.lastCall.args;
await assert.isRejected(op.run(), /not handled by the pipeline/);
assert.isTrue(localRepository.addRemote.called);
});
});