This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
file-patch-controller.test.js
460 lines (347 loc) · 18.4 KB
/
file-patch-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
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
454
455
456
457
458
459
460
import path from 'path';
import fs from 'fs-extra';
import React from 'react';
import {shallow} from 'enzyme';
import FilePatchController from '../../lib/controllers/file-patch-controller';
import FilePatch from '../../lib/models/patch/file-patch';
import * as reporterProxy from '../../lib/reporter-proxy';
import {cloneRepository, buildRepository} from '../helpers';
describe('FilePatchController', function() {
let atomEnv, repository, filePatch;
beforeEach(async function() {
atomEnv = global.buildAtomEnvironment();
const workdirPath = await cloneRepository();
repository = await buildRepository(workdirPath);
// a.txt: unstaged changes
await fs.writeFile(path.join(workdirPath, 'a.txt'), '00\n01\n02\n03\n04\n05\n06');
filePatch = await repository.getFilePatchForPath('a.txt', {staged: false});
});
afterEach(function() {
atomEnv.destroy();
});
function buildApp(overrideProps = {}) {
const props = {
repository,
stagingStatus: 'unstaged',
relPath: 'a.txt',
isPartiallyStaged: false,
filePatch,
hasUndoHistory: false,
workspace: atomEnv.workspace,
commands: atomEnv.commands,
keymaps: atomEnv.keymaps,
tooltips: atomEnv.tooltips,
config: atomEnv.config,
destroy: () => {},
discardLines: () => {},
undoLastDiscard: () => {},
surfaceFileAtPath: () => {},
...overrideProps,
};
return <FilePatchController {...props} />;
}
it('passes extra props to the FilePatchView', function() {
const extra = Symbol('extra');
const wrapper = shallow(buildApp({extra}));
assert.strictEqual(wrapper.find('FilePatchView').prop('extra'), extra);
});
it('calls undoLastDiscard through with set arguments', function() {
const undoLastDiscard = sinon.spy();
const wrapper = shallow(buildApp({relPath: 'b.txt', undoLastDiscard}));
wrapper.find('FilePatchView').prop('undoLastDiscard')();
assert.isTrue(undoLastDiscard.calledWith('b.txt', repository));
});
it('calls surfaceFileAtPath with set arguments', function() {
const surfaceFileAtPath = sinon.spy();
const wrapper = shallow(buildApp({relPath: 'c.txt', surfaceFileAtPath}));
wrapper.find('FilePatchView').prop('surfaceFile')();
assert.isTrue(surfaceFileAtPath.calledWith('c.txt', 'unstaged'));
});
describe('diveIntoMirrorPatch()', function() {
it('destroys the current pane and opens the staged changes', async function() {
const destroy = sinon.spy();
sinon.stub(atomEnv.workspace, 'open').resolves();
const wrapper = shallow(buildApp({relPath: 'c.txt', stagingStatus: 'unstaged', destroy}));
await wrapper.find('FilePatchView').prop('diveIntoMirrorPatch')();
assert.isTrue(destroy.called);
assert.isTrue(atomEnv.workspace.open.calledWith(
'atom-github://file-patch/c.txt' +
`?workdir=${encodeURIComponent(repository.getWorkingDirectoryPath())}&stagingStatus=staged`,
));
});
it('destroys the current pane and opens the unstaged changes', async function() {
const destroy = sinon.spy();
sinon.stub(atomEnv.workspace, 'open').resolves();
const wrapper = shallow(buildApp({relPath: 'd.txt', stagingStatus: 'staged', destroy}));
await wrapper.find('FilePatchView').prop('diveIntoMirrorPatch')();
assert.isTrue(destroy.called);
assert.isTrue(atomEnv.workspace.open.calledWith(
'atom-github://file-patch/d.txt' +
`?workdir=${encodeURIComponent(repository.getWorkingDirectoryPath())}&stagingStatus=unstaged`,
));
});
});
describe('openFile()', function() {
it('opens an editor on the current file', async function() {
const wrapper = shallow(buildApp({relPath: 'a.txt', stagingStatus: 'unstaged'}));
const editor = await wrapper.find('FilePatchView').prop('openFile')([]);
assert.strictEqual(editor.getPath(), path.join(repository.getWorkingDirectoryPath(), 'a.txt'));
});
it('sets the cursor to a single position', async function() {
const wrapper = shallow(buildApp({relPath: 'a.txt', stagingStatus: 'unstaged'}));
const editor = await wrapper.find('FilePatchView').prop('openFile')([[1, 1]]);
assert.deepEqual(editor.getCursorBufferPositions().map(p => p.serialize()), [[1, 1]]);
});
it('adds cursors at a set of positions', async function() {
const wrapper = shallow(buildApp({relPath: 'a.txt', stagingStatus: 'unstaged'}));
const editor = await wrapper.find('FilePatchView').prop('openFile')([[1, 1], [3, 1], [5, 0]]);
assert.deepEqual(editor.getCursorBufferPositions().map(p => p.serialize()), [[1, 1], [3, 1], [5, 0]]);
});
});
describe('toggleFile()', function() {
it('stages the current file if unstaged', async function() {
sinon.spy(repository, 'stageFiles');
const wrapper = shallow(buildApp({relPath: 'a.txt', stagingStatus: 'unstaged'}));
await wrapper.find('FilePatchView').prop('toggleFile')();
assert.isTrue(repository.stageFiles.calledWith(['a.txt']));
});
it('unstages the current file if staged', async function() {
sinon.spy(repository, 'unstageFiles');
const wrapper = shallow(buildApp({relPath: 'a.txt', stagingStatus: 'staged'}));
await wrapper.find('FilePatchView').prop('toggleFile')();
assert.isTrue(repository.unstageFiles.calledWith(['a.txt']));
});
it('is a no-op if a staging operation is already in progress', async function() {
sinon.stub(repository, 'stageFiles').resolves('staged');
sinon.stub(repository, 'unstageFiles').resolves('unstaged');
const wrapper = shallow(buildApp({relPath: 'a.txt', stagingStatus: 'unstaged'}));
assert.strictEqual(await wrapper.find('FilePatchView').prop('toggleFile')(), 'staged');
// No-op
assert.isNull(await wrapper.find('FilePatchView').prop('toggleFile')());
// Simulate an identical patch arriving too soon
wrapper.setProps({filePatch: filePatch.clone()});
// Still a no-op
assert.isNull(await wrapper.find('FilePatchView').prop('toggleFile')());
// Simulate updated patch arrival
const promise = wrapper.instance().patchChangePromise;
wrapper.setProps({filePatch: FilePatch.createNull()});
await promise;
// Performs an operation again
assert.strictEqual(await wrapper.find('FilePatchView').prop('toggleFile')(), 'staged');
});
});
describe('selected row and selection mode tracking', function() {
it('captures the selected row set', function() {
const wrapper = shallow(buildApp());
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), []);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'hunk');
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1, 2]), 'line');
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), [1, 2]);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'line');
});
it('does not re-render if the row set and selection mode are unchanged', function() {
const wrapper = shallow(buildApp());
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), []);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'hunk');
sinon.spy(wrapper.instance(), 'render');
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1, 2]), 'line');
assert.isTrue(wrapper.instance().render.called);
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), [1, 2]);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'line');
wrapper.instance().render.resetHistory();
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([2, 1]), 'line');
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), [1, 2]);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'line');
assert.isFalse(wrapper.instance().render.called);
wrapper.instance().render.resetHistory();
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1, 2]), 'hunk');
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), [1, 2]);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'hunk');
assert.isTrue(wrapper.instance().render.called);
});
describe('discardLines()', function() {
it('records an event', async function() {
const wrapper = shallow(buildApp());
sinon.stub(reporterProxy, 'addEvent');
await wrapper.find('FilePatchView').prop('discardRows')(new Set([1, 2]));
assert.isTrue(reporterProxy.addEvent.calledWith('discard-unstaged-changes', {
package: 'github',
component: 'FilePatchController',
lineCount: 2,
eventSource: undefined,
}));
});
});
describe('undoLastDiscard()', function() {
it('records an event', function() {
const wrapper = shallow(buildApp());
sinon.stub(reporterProxy, 'addEvent');
wrapper.find('FilePatchView').prop('undoLastDiscard')();
assert.isTrue(reporterProxy.addEvent.calledWith('undo-last-discard', {
package: 'github',
component: 'FilePatchController',
eventSource: undefined,
}));
});
});
});
describe('toggleRows()', function() {
it('is a no-op with no selected rows', async function() {
const wrapper = shallow(buildApp());
sinon.spy(repository, 'applyPatchToIndex');
await wrapper.find('FilePatchView').prop('toggleRows')();
assert.isFalse(repository.applyPatchToIndex.called);
});
it('applies a stage patch to the index', async function() {
const wrapper = shallow(buildApp());
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1]));
sinon.spy(filePatch, 'getStagePatchForLines');
sinon.spy(repository, 'applyPatchToIndex');
await wrapper.find('FilePatchView').prop('toggleRows')();
assert.sameMembers(Array.from(filePatch.getStagePatchForLines.lastCall.args[0]), [1]);
assert.isTrue(repository.applyPatchToIndex.calledWith(filePatch.getStagePatchForLines.returnValues[0]));
});
it('toggles a different row set if provided', async function() {
const wrapper = shallow(buildApp());
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1]), 'line');
sinon.spy(filePatch, 'getStagePatchForLines');
sinon.spy(repository, 'applyPatchToIndex');
await wrapper.find('FilePatchView').prop('toggleRows')(new Set([2]), 'hunk');
assert.sameMembers(Array.from(filePatch.getStagePatchForLines.lastCall.args[0]), [2]);
assert.isTrue(repository.applyPatchToIndex.calledWith(filePatch.getStagePatchForLines.returnValues[0]));
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), [2]);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'hunk');
});
it('applies an unstage patch to the index', async function() {
await repository.stageFiles(['a.txt']);
const otherPatch = await repository.getFilePatchForPath('a.txt', {staged: true});
const wrapper = shallow(buildApp({filePatch: otherPatch, stagingStatus: 'staged'}));
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([2]));
sinon.spy(otherPatch, 'getUnstagePatchForLines');
sinon.spy(repository, 'applyPatchToIndex');
await wrapper.find('FilePatchView').prop('toggleRows')();
assert.sameMembers(Array.from(otherPatch.getUnstagePatchForLines.lastCall.args[0]), [2]);
assert.isTrue(repository.applyPatchToIndex.calledWith(otherPatch.getUnstagePatchForLines.returnValues[0]));
});
});
if (process.platform !== 'win32') {
describe('toggleModeChange()', function() {
it("it stages an unstaged file's new mode", async function() {
const p = path.join(repository.getWorkingDirectoryPath(), 'a.txt');
await fs.chmod(p, 0o755);
repository.refresh();
const newFilePatch = await repository.getFilePatchForPath('a.txt', {staged: false});
const wrapper = shallow(buildApp({filePatch: newFilePatch, stagingStatus: 'unstaged'}));
sinon.spy(repository, 'stageFileModeChange');
await wrapper.find('FilePatchView').prop('toggleModeChange')();
assert.isTrue(repository.stageFileModeChange.calledWith('a.txt', '100755'));
});
it("it stages a staged file's old mode", async function() {
const p = path.join(repository.getWorkingDirectoryPath(), 'a.txt');
await fs.chmod(p, 0o755);
await repository.stageFiles(['a.txt']);
repository.refresh();
const newFilePatch = await repository.getFilePatchForPath('a.txt', {staged: true});
const wrapper = shallow(buildApp({filePatch: newFilePatch, stagingStatus: 'staged'}));
sinon.spy(repository, 'stageFileModeChange');
await wrapper.find('FilePatchView').prop('toggleModeChange')();
assert.isTrue(repository.stageFileModeChange.calledWith('a.txt', '100644'));
});
});
describe('toggleSymlinkChange', function() {
it('handles an addition and typechange with a special repository method', async function() {
if (process.env.ATOM_GITHUB_SKIP_SYMLINKS) {
this.skip();
return;
}
const p = path.join(repository.getWorkingDirectoryPath(), 'waslink.txt');
const dest = path.join(repository.getWorkingDirectoryPath(), 'destination');
await fs.writeFile(dest, 'asdf\n', 'utf8');
await fs.symlink(dest, p);
await repository.stageFiles(['waslink.txt', 'destination']);
await repository.commit('zero');
await fs.unlink(p);
await fs.writeFile(p, 'fdsa\n', 'utf8');
repository.refresh();
const symlinkPatch = await repository.getFilePatchForPath('waslink.txt', {staged: false});
const wrapper = shallow(buildApp({filePatch: symlinkPatch, relPath: 'waslink.txt', stagingStatus: 'unstaged'}));
sinon.spy(repository, 'stageFileSymlinkChange');
await wrapper.find('FilePatchView').prop('toggleSymlinkChange')();
assert.isTrue(repository.stageFileSymlinkChange.calledWith('waslink.txt'));
});
it('stages non-addition typechanges normally', async function() {
if (process.env.ATOM_GITHUB_SKIP_SYMLINKS) {
this.skip();
return;
}
const p = path.join(repository.getWorkingDirectoryPath(), 'waslink.txt');
const dest = path.join(repository.getWorkingDirectoryPath(), 'destination');
await fs.writeFile(dest, 'asdf\n', 'utf8');
await fs.symlink(dest, p);
await repository.stageFiles(['waslink.txt', 'destination']);
await repository.commit('zero');
await fs.unlink(p);
repository.refresh();
const symlinkPatch = await repository.getFilePatchForPath('waslink.txt', {staged: false});
const wrapper = shallow(buildApp({filePatch: symlinkPatch, relPath: 'waslink.txt', stagingStatus: 'unstaged'}));
sinon.spy(repository, 'stageFiles');
await wrapper.find('FilePatchView').prop('toggleSymlinkChange')();
assert.isTrue(repository.stageFiles.calledWith(['waslink.txt']));
});
it('handles a deletion and typechange with a special repository method', async function() {
const p = path.join(repository.getWorkingDirectoryPath(), 'waslink.txt');
const dest = path.join(repository.getWorkingDirectoryPath(), 'destination');
await fs.writeFile(dest, 'asdf\n', 'utf8');
await fs.writeFile(p, 'fdsa\n', 'utf8');
await repository.stageFiles(['waslink.txt', 'destination']);
await repository.commit('zero');
await fs.unlink(p);
await fs.symlink(dest, p);
await repository.stageFiles(['waslink.txt']);
repository.refresh();
const symlinkPatch = await repository.getFilePatchForPath('waslink.txt', {staged: true});
const wrapper = shallow(buildApp({filePatch: symlinkPatch, relPath: 'waslink.txt', stagingStatus: 'staged'}));
sinon.spy(repository, 'stageFileSymlinkChange');
await wrapper.find('FilePatchView').prop('toggleSymlinkChange')();
assert.isTrue(repository.stageFileSymlinkChange.calledWith('waslink.txt'));
});
it('unstages non-deletion typechanges normally', async function() {
const p = path.join(repository.getWorkingDirectoryPath(), 'waslink.txt');
const dest = path.join(repository.getWorkingDirectoryPath(), 'destination');
await fs.writeFile(dest, 'asdf\n', 'utf8');
await fs.symlink(dest, p);
await repository.stageFiles(['waslink.txt', 'destination']);
await repository.commit('zero');
await fs.unlink(p);
repository.refresh();
const symlinkPatch = await repository.getFilePatchForPath('waslink.txt', {staged: true});
const wrapper = shallow(buildApp({filePatch: symlinkPatch, relPath: 'waslink.txt', stagingStatus: 'staged'}));
sinon.spy(repository, 'unstageFiles');
await wrapper.find('FilePatchView').prop('toggleSymlinkChange')();
assert.isTrue(repository.unstageFiles.calledWith(['waslink.txt']));
});
});
}
it('calls discardLines with selected rows', async function() {
const discardLines = sinon.spy();
const wrapper = shallow(buildApp({discardLines}));
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1, 2]));
await wrapper.find('FilePatchView').prop('discardRows')();
const lastArgs = discardLines.lastCall.args;
assert.strictEqual(lastArgs[0], filePatch);
assert.sameMembers(Array.from(lastArgs[1]), [1, 2]);
assert.strictEqual(lastArgs[2], repository);
});
it('calls discardLines with explicitly provided rows', async function() {
const discardLines = sinon.spy();
const wrapper = shallow(buildApp({discardLines}));
wrapper.find('FilePatchView').prop('selectedRowsChanged')(new Set([1, 2]));
await wrapper.find('FilePatchView').prop('discardRows')(new Set([4, 5]), 'hunk');
const lastArgs = discardLines.lastCall.args;
assert.strictEqual(lastArgs[0], filePatch);
assert.sameMembers(Array.from(lastArgs[1]), [4, 5]);
assert.strictEqual(lastArgs[2], repository);
assert.sameMembers(Array.from(wrapper.find('FilePatchView').prop('selectedRows')), [4, 5]);
assert.strictEqual(wrapper.find('FilePatchView').prop('selectionMode'), 'hunk');
});
});