This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 392
/
file-patch-controller.test.js
823 lines (660 loc) · 37.2 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import React from 'react';
import {shallow, mount} from 'enzyme';
import until from 'test-until';
import fs from 'fs';
import path from 'path';
import {cloneRepository, buildRepository} from '../helpers';
import FilePatch from '../../lib/models/file-patch';
import FilePatchController from '../../lib/controllers/file-patch-controller';
import Hunk from '../../lib/models/hunk';
import HunkLine from '../../lib/models/hunk-line';
import ResolutionProgress from '../../lib/models/conflicts/resolution-progress';
import Switchboard from '../../lib/switchboard';
function createFilePatch(oldFilePath, newFilePath, status, hunks) {
const oldFile = new FilePatch.File({path: oldFilePath});
const newFile = new FilePatch.File({path: newFilePath});
const patch = new FilePatch.Patch({status, hunks});
return new FilePatch(oldFile, newFile, patch);
}
let atomEnv, commandRegistry, tooltips, deserializers;
let switchboard, getFilePatchForPath;
let discardLines, didSurfaceFile, didDiveIntoFilePath, quietlySelectItem, undoLastDiscard, openFiles, getRepositoryForWorkdir;
let getSelectedStagingViewItems, resolutionProgress;
function createComponent(repository, filePath) {
atomEnv = global.buildAtomEnvironment();
commandRegistry = atomEnv.commands;
deserializers = atomEnv.deserializers;
tooltips = atomEnv.tooltips;
switchboard = new Switchboard();
discardLines = sinon.spy();
didSurfaceFile = sinon.spy();
didDiveIntoFilePath = sinon.spy();
quietlySelectItem = sinon.spy();
undoLastDiscard = sinon.spy();
openFiles = sinon.spy();
getSelectedStagingViewItems = sinon.spy();
getRepositoryForWorkdir = () => repository;
resolutionProgress = new ResolutionProgress();
FilePatchController.resetConfirmedLargeFilePatches();
return (
<FilePatchController
commandRegistry={commandRegistry}
deserializers={deserializers}
tooltips={tooltips}
resolutionProgress={resolutionProgress}
filePath={filePath}
initialStagingStatus="unstaged"
isPartiallyStaged={false}
switchboard={switchboard}
discardLines={discardLines}
didSurfaceFile={didSurfaceFile}
didDiveIntoFilePath={didDiveIntoFilePath}
quietlySelectItem={quietlySelectItem}
undoLastDiscard={undoLastDiscard}
openFiles={openFiles}
getRepositoryForWorkdir={getRepositoryForWorkdir}
workingDirectoryPath={repository.getWorkingDirectoryPath()}
getSelectedStagingViewItems={getSelectedStagingViewItems}
uri={'some/uri'}
/>
);
}
async function refreshRepository(wrapper) {
const workDir = wrapper.prop('workingDirectoryPath');
const repository = wrapper.prop('getRepositoryForWorkdir')(workDir);
const promise = wrapper.prop('switchboard').getFinishRepositoryRefreshPromise();
repository.refresh();
await promise;
wrapper.update();
}
describe('FilePatchController', function() {
afterEach(function() {
atomEnv.destroy();
});
describe('unit tests', function() {
let workdirPath, repository, filePath, component;
beforeEach(async function() {
workdirPath = await cloneRepository('multi-line-file');
repository = await buildRepository(workdirPath);
filePath = 'sample.js';
component = createComponent(repository, filePath);
getFilePatchForPath = sinon.stub(repository, 'getFilePatchForPath');
});
describe('buildURI', function() {
it('correctly uri encodes all components', function() {
const filePathWithSpecialChars = '???.txt';
const stagingStatus = 'staged';
const URI = FilePatchController.buildURI(filePathWithSpecialChars, workdirPath, stagingStatus);
assert.isTrue(URI.includes(encodeURIComponent(filePathWithSpecialChars)));
assert.isTrue(URI.includes(encodeURIComponent(workdirPath)));
assert.isTrue(URI.includes(encodeURIComponent(stagingStatus)));
});
});
describe('when the FilePatch is too large', function() {
it('renders a confirmation widget', async function() {
const hunk1 = new Hunk(0, 0, 1, 1, '', [
new HunkLine('line-1', 'added', 1, 1),
new HunkLine('line-2', 'added', 2, 2),
new HunkLine('line-3', 'added', 3, 3),
new HunkLine('line-4', 'added', 4, 4),
new HunkLine('line-5', 'added', 5, 5),
new HunkLine('line-6', 'added', 6, 6),
]);
const filePatch = createFilePatch(filePath, filePath, 'modified', [hunk1]);
getFilePatchForPath.returns(filePatch);
const wrapper = mount(React.cloneElement(component, {largeDiffByteThreshold: 5}));
await assert.async.match(wrapper.text(), /large .+ diff/);
});
it('renders the full diff when the confirmation is clicked', async function() {
const hunk = new Hunk(0, 0, 1, 1, '', [
new HunkLine('line-1', 'added', 1, 1),
new HunkLine('line-2', 'added', 2, 2),
new HunkLine('line-3', 'added', 3, 3),
new HunkLine('line-4', 'added', 4, 4),
new HunkLine('line-5', 'added', 5, 5),
new HunkLine('line-6', 'added', 6, 6),
]);
const filePatch = createFilePatch(filePath, filePath, 'modified', [hunk]);
getFilePatchForPath.returns(filePatch);
const wrapper = mount(React.cloneElement(component, {largeDiffByteThreshold: 5}));
await assert.async.isTrue(wrapper.update().find('.large-file-patch').exists());
wrapper.find('.large-file-patch').find('button').simulate('click');
assert.isTrue(wrapper.find('HunkView').exists());
});
it('renders the full diff if the file has been confirmed before', async function() {
const hunk = new Hunk(0, 0, 1, 1, '', [
new HunkLine('line-1', 'added', 1, 1),
new HunkLine('line-2', 'added', 2, 2),
new HunkLine('line-3', 'added', 3, 3),
new HunkLine('line-4', 'added', 4, 4),
new HunkLine('line-5', 'added', 5, 5),
new HunkLine('line-6', 'added', 6, 6),
]);
const filePatch1 = createFilePatch(filePath, filePath, 'modified', [hunk]);
const filePatch2 = createFilePatch('b.txt', 'b.txt', 'modified', [hunk]);
getFilePatchForPath.returns(filePatch1);
const wrapper = mount(React.cloneElement(component, {
filePath: filePatch1.getPath(), largeDiffByteThreshold: 5,
}));
await assert.async.isTrue(wrapper.update().find('.large-file-patch').exists());
wrapper.find('.large-file-patch').find('button').simulate('click');
assert.isTrue(wrapper.find('HunkView').exists());
getFilePatchForPath.returns(filePatch2);
wrapper.setProps({filePath: filePatch2.getPath()});
await assert.async.isTrue(wrapper.update().find('.large-file-patch').exists());
getFilePatchForPath.returns(filePatch1);
wrapper.setProps({filePath: filePatch1.getPath()});
assert.isTrue(wrapper.update().find('HunkView').exists());
});
});
describe('onRepoRefresh', function() {
it('sets the correct FilePatch as state', async function() {
repository.getFilePatchForPath.restore();
fs.writeFileSync(path.join(workdirPath, filePath), 'change', 'utf8');
const wrapper = mount(component);
await assert.async.isNotNull(wrapper.state('filePatch'));
const originalFilePatch = wrapper.state('filePatch');
assert.equal(wrapper.state('stagingStatus'), 'unstaged');
fs.writeFileSync(path.join(workdirPath, 'file.txt'), 'change\nand again!', 'utf8');
await refreshRepository(wrapper);
assert.notEqual(originalFilePatch, wrapper.state('filePatch'));
assert.equal(wrapper.state('stagingStatus'), 'unstaged');
});
});
it('renders FilePatchView only if FilePatch has hunks', async function() {
const emptyFilePatch = createFilePatch(filePath, filePath, 'modified', []);
getFilePatchForPath.returns(emptyFilePatch);
const wrapper = mount(component);
assert.isTrue(wrapper.find('FilePatchView').exists());
assert.isTrue(wrapper.find('FilePatchView').text().includes('File has no contents'));
const hunk1 = new Hunk(0, 0, 1, 1, '', [new HunkLine('line-1', 'added', 1, 1)]);
const filePatch = createFilePatch(filePath, filePath, 'modified', [hunk1]);
getFilePatchForPath.returns(filePatch);
wrapper.instance().onRepoRefresh(repository);
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
assert.isTrue(wrapper.find('HunkView').text().includes('@@ -0,1 +0,1 @@'));
});
it('updates the FilePatch after a repo update', async function() {
const hunk1 = new Hunk(5, 5, 2, 1, '', [new HunkLine('line-1', 'added', -1, 5)]);
const hunk2 = new Hunk(8, 8, 1, 1, '', [new HunkLine('line-5', 'deleted', 8, -1)]);
const filePatch0 = createFilePatch(filePath, filePath, 'modified', [hunk1, hunk2]);
getFilePatchForPath.returns(filePatch0);
const wrapper = shallow(component);
let view0;
await until(() => {
view0 = wrapper.update().find('FilePatchView').shallow();
return view0.find({hunk: hunk1}).exists();
});
assert.isTrue(view0.find({hunk: hunk2}).exists());
const hunk3 = new Hunk(8, 8, 1, 1, '', [new HunkLine('line-10', 'modified', 10, 10)]);
const filePatch1 = createFilePatch(filePath, filePath, 'modified', [hunk1, hunk3]);
getFilePatchForPath.returns(filePatch1);
wrapper.instance().onRepoRefresh(repository);
let view1;
await until(() => {
view1 = wrapper.update().find('FilePatchView').shallow();
return view1.find({hunk: hunk3}).exists();
});
assert.isTrue(view1.find({hunk: hunk1}).exists());
assert.isFalse(view1.find({hunk: hunk2}).exists());
});
it('invokes a didSurfaceFile callback with the current file path', async function() {
const filePatch = createFilePatch(filePath, filePath, 'modified', [new Hunk(1, 1, 1, 3, '', [])]);
getFilePatchForPath.returns(filePatch);
const wrapper = mount(component);
await assert.async.isTrue(wrapper.update().find('Commands').exists());
commandRegistry.dispatch(wrapper.find('FilePatchView').getDOMNode(), 'core:move-right');
assert.isTrue(didSurfaceFile.calledWith(filePath, 'unstaged'));
});
describe('openCurrentFile({lineNumber})', () => {
it('sets the cursor on the correct line of the opened text editor', async function() {
const editorSpy = {
relativePath: null,
scrollToBufferPosition: sinon.spy(),
setCursorBufferPosition: sinon.spy(),
};
const openFilesStub = relativePaths => {
assert.lengthOf(relativePaths, 1);
editorSpy.relativePath = relativePaths[0];
return Promise.resolve([editorSpy]);
};
const hunk = new Hunk(5, 5, 2, 1, '', [new HunkLine('line-1', 'added', -1, 5)]);
const filePatch = createFilePatch(filePath, filePath, 'modified', [hunk]);
getFilePatchForPath.returns(filePatch);
const wrapper = mount(React.cloneElement(component, {openFiles: openFilesStub}));
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
wrapper.find('LineView').simulate('mousedown', {button: 0, detail: 1});
window.dispatchEvent(new MouseEvent('mouseup'));
commandRegistry.dispatch(wrapper.find('FilePatchView').getDOMNode(), 'github:open-file');
wrapper.update();
await assert.async.isTrue(editorSpy.setCursorBufferPosition.called);
assert.isTrue(editorSpy.relativePath === filePath);
const scrollCall = editorSpy.scrollToBufferPosition.firstCall;
assert.isTrue(scrollCall.args[0].isEqual([4, 0]));
assert.deepEqual(scrollCall.args[1], {center: true});
const cursorCall = editorSpy.setCursorBufferPosition.firstCall;
assert.isTrue(cursorCall.args[0].isEqual([4, 0]));
});
});
});
describe('integration tests', function() {
describe('handling symlink files', function() {
async function indexModeAndOid(repository, filename) {
const output = await repository.git.exec(['ls-files', '-s', '--', filename]);
if (output) {
const parts = output.split(' ');
return {mode: parts[0], oid: parts[1]};
} else {
return null;
}
}
it('unstages added lines that don\'t require symlink change', async function() {
const workingDirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workingDirPath);
// correctly handle symlinks on Windows
await repository.git.exec(['config', 'core.symlinks', 'true']);
const deletedSymlinkAddedFilePath = 'symlink.txt';
fs.unlinkSync(path.join(workingDirPath, deletedSymlinkAddedFilePath));
fs.writeFileSync(path.join(workingDirPath, deletedSymlinkAddedFilePath), 'qux\nfoo\nbar\nbaz\nzoo\n', 'utf8');
// Stage whole file
await repository.stageFiles([deletedSymlinkAddedFilePath]);
const component = createComponent(repository, deletedSymlinkAddedFilePath);
const wrapper = mount(React.cloneElement(component, {filePath: deletedSymlinkAddedFilePath, initialStagingStatus: 'staged'}));
// index shows symlink deltion and added lines
assert.autocrlfEqual(await repository.readFileFromIndex(deletedSymlinkAddedFilePath), 'qux\nfoo\nbar\nbaz\nzoo\n');
assert.equal((await indexModeAndOid(repository, deletedSymlinkAddedFilePath)).mode, '100644');
// Unstage a couple added lines, but not all
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('LineView').at(1).find('.github-HunkView-line').simulate('mousedown', {button: 0, detail: 1});
hunkView0.find('LineView').at(2).find('.github-HunkView-line').simulate('mousemove', {});
window.dispatchEvent(new MouseEvent('mouseup'));
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
// index shows symlink deletions still staged, only a couple of lines have been unstaged
assert.equal((await indexModeAndOid(repository, deletedSymlinkAddedFilePath)).mode, '100644');
assert.autocrlfEqual(await repository.readFileFromIndex(deletedSymlinkAddedFilePath), 'qux\nbaz\nzoo\n');
});
it('stages deleted lines that don\'t require symlink change', async function() {
const workingDirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workingDirPath);
const deletedFileAddedSymlinkPath = 'a.txt';
fs.unlinkSync(path.join(workingDirPath, deletedFileAddedSymlinkPath));
fs.symlinkSync(path.join(workingDirPath, 'regular-file.txt'), path.join(workingDirPath, deletedFileAddedSymlinkPath));
const component = createComponent(repository, deletedFileAddedSymlinkPath);
const wrapper = mount(React.cloneElement(component, {filePath: deletedFileAddedSymlinkPath, initialStagingStatus: 'unstaged'}));
// index shows file is not a symlink, no deleted lines
assert.equal((await indexModeAndOid(repository, deletedFileAddedSymlinkPath)).mode, '100644');
assert.autocrlfEqual(await repository.readFileFromIndex(deletedFileAddedSymlinkPath), 'foo\nbar\nbaz\n\n');
// stage a couple of lines, but not all
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('LineView').at(1).find('.github-HunkView-line').simulate('mousedown', {button: 0, detail: 1});
hunkView0.find('LineView').at(2).find('.github-HunkView-line').simulate('mousemove', {});
window.dispatchEvent(new MouseEvent('mouseup'));
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
// index shows symlink change has not been staged, a couple of lines have been deleted
assert.equal((await indexModeAndOid(repository, deletedFileAddedSymlinkPath)).mode, '100644');
assert.autocrlfEqual(await repository.readFileFromIndex(deletedFileAddedSymlinkPath), 'foo\n\n');
});
it('stages symlink change when staging added lines that depend on change', async function() {
const workingDirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workingDirPath);
// correctly handle symlinks on Windows
await repository.git.exec(['config', 'core.symlinks', 'true']);
const deletedSymlinkAddedFilePath = 'symlink.txt';
fs.unlinkSync(path.join(workingDirPath, deletedSymlinkAddedFilePath));
fs.writeFileSync(path.join(workingDirPath, deletedSymlinkAddedFilePath), 'qux\nfoo\nbar\nbaz\nzoo\n', 'utf8');
const component = createComponent(repository, deletedSymlinkAddedFilePath);
const wrapper = mount(React.cloneElement(component, {filePath: deletedSymlinkAddedFilePath}));
// index shows file is symlink
assert.equal((await indexModeAndOid(repository, deletedSymlinkAddedFilePath)).mode, '120000');
// Stage a couple added lines, but not all
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('LineView').at(1).find('.github-HunkView-line').simulate('mousedown', {button: 0, detail: 1});
hunkView0.find('LineView').at(2).find('.github-HunkView-line').simulate('mousemove', {});
window.dispatchEvent(new MouseEvent('mouseup'));
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
// index no longer shows file is symlink (symlink has been deleted), now a regular file with contents
assert.equal((await indexModeAndOid(repository, deletedSymlinkAddedFilePath)).mode, '100644');
assert.autocrlfEqual(await repository.readFileFromIndex(deletedSymlinkAddedFilePath), 'foo\nbar\n');
});
it('unstages symlink change when unstaging deleted lines that depend on change', async function() {
const workingDirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workingDirPath);
const deletedFileAddedSymlinkPath = 'a.txt';
fs.unlinkSync(path.join(workingDirPath, deletedFileAddedSymlinkPath));
fs.symlinkSync(path.join(workingDirPath, 'regular-file.txt'), path.join(workingDirPath, deletedFileAddedSymlinkPath));
await repository.stageFiles([deletedFileAddedSymlinkPath]);
const component = createComponent(repository, deletedFileAddedSymlinkPath);
const wrapper = mount(React.cloneElement(component, {filePath: deletedFileAddedSymlinkPath, initialStagingStatus: 'staged'}));
// index shows file is symlink
assert.equal((await indexModeAndOid(repository, deletedFileAddedSymlinkPath)).mode, '120000');
// unstage a couple of lines, but not all
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('LineView').at(1).find('.github-HunkView-line').simulate('mousedown', {button: 0, detail: 1});
hunkView0.find('LineView').at(2).find('.github-HunkView-line').simulate('mousemove', {});
window.dispatchEvent(new MouseEvent('mouseup'));
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
// index no longer shows file is symlink (symlink creation has been unstaged), shows contents of file that existed prior to symlink
assert.equal((await indexModeAndOid(repository, deletedFileAddedSymlinkPath)).mode, '100644');
assert.autocrlfEqual(await repository.readFileFromIndex(deletedFileAddedSymlinkPath), 'bar\nbaz\n');
});
it('stages file deletion when all deleted lines are staged', async function() {
const workingDirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workingDirPath);
await repository.getLoadPromise();
const deletedFileAddedSymlinkPath = 'a.txt';
fs.unlinkSync(path.join(workingDirPath, deletedFileAddedSymlinkPath));
fs.symlinkSync(path.join(workingDirPath, 'regular-file.txt'), path.join(workingDirPath, deletedFileAddedSymlinkPath));
const component = createComponent(repository, deletedFileAddedSymlinkPath);
const wrapper = mount(React.cloneElement(component, {filePath: deletedFileAddedSymlinkPath}));
assert.equal((await indexModeAndOid(repository, deletedFileAddedSymlinkPath)).mode, '100644');
// stage all deleted lines
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('.github-HunkView-title').simulate('click');
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
// File is not on index, file deletion has been staged
assert.isNull(await indexModeAndOid(repository, deletedFileAddedSymlinkPath));
const {stagedFiles, unstagedFiles} = await repository.getStatusesForChangedFiles();
assert.equal(unstagedFiles[deletedFileAddedSymlinkPath], 'added');
assert.equal(stagedFiles[deletedFileAddedSymlinkPath], 'deleted');
});
it('unstages file creation when all added lines are unstaged', async function() {
const workingDirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workingDirPath);
await repository.git.exec(['config', 'core.symlinks', 'true']);
const deletedSymlinkAddedFilePath = 'symlink.txt';
fs.unlinkSync(path.join(workingDirPath, deletedSymlinkAddedFilePath));
fs.writeFileSync(path.join(workingDirPath, deletedSymlinkAddedFilePath), 'qux\nfoo\nbar\nbaz\nzoo\n', 'utf8');
await repository.stageFiles([deletedSymlinkAddedFilePath]);
const component = createComponent(repository, deletedSymlinkAddedFilePath);
const wrapper = mount(React.cloneElement(component, {filePath: deletedSymlinkAddedFilePath, initialStagingStatus: 'staged'}));
assert.equal((await indexModeAndOid(repository, deletedSymlinkAddedFilePath)).mode, '100644');
// unstage all added lines
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('.github-HunkView-title').simulate('click');
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
// File is not on index, file creation has been unstaged
assert.isNull(await indexModeAndOid(repository, deletedSymlinkAddedFilePath));
const {stagedFiles, unstagedFiles} = await repository.getStatusesForChangedFiles();
assert.equal(unstagedFiles[deletedSymlinkAddedFilePath], 'added');
assert.equal(stagedFiles[deletedSymlinkAddedFilePath], 'deleted');
});
});
describe('handling non-symlink changes', function() {
let workdirPath, repository, filePath, component;
beforeEach(async function() {
workdirPath = await cloneRepository('multi-line-file');
repository = await buildRepository(workdirPath);
filePath = 'sample.js';
component = createComponent(repository, filePath);
});
it('stages and unstages hunks when the stage button is clicked on hunk views with no individual lines selected', async function() {
const absFilePath = path.join(workdirPath, filePath);
const originalLines = fs.readFileSync(absFilePath, 'utf8').split('\n');
const unstagedLines = originalLines.slice();
unstagedLines.splice(1, 1,
'this is a modified line',
'this is a new line',
'this is another new line',
);
unstagedLines.splice(11, 2, 'this is a modified line');
fs.writeFileSync(absFilePath, unstagedLines.join('\n'));
const wrapper = mount(component);
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
commandRegistry.dispatch(wrapper.find('FilePatchView').getDOMNode(), 'core:move-down');
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const hunkView0 = wrapper.find('HunkView').at(0);
assert.isFalse(hunkView0.prop('isSelected'));
const opPromise0 = switchboard.getFinishStageOperationPromise();
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
const expectedStagedLines = originalLines.slice();
expectedStagedLines.splice(1, 1,
'this is a modified line',
'this is a new line',
'this is another new line',
);
assert.autocrlfEqual(await repository.readFileFromIndex('sample.js'), expectedStagedLines.join('\n'));
const updatePromise0 = switchboard.getChangePatchPromise();
const stagedFilePatch = await repository.getFilePatchForPath('sample.js', {staged: true});
wrapper.setState({
stagingStatus: 'staged',
filePatch: stagedFilePatch,
});
await updatePromise0;
const hunkView1 = wrapper.find('HunkView').at(0);
const opPromise1 = switchboard.getFinishStageOperationPromise();
hunkView1.find('button.github-HunkView-stageButton').simulate('click');
await opPromise1;
assert.autocrlfEqual(await repository.readFileFromIndex('sample.js'), originalLines.join('\n'));
});
it('stages and unstages individual lines when the stage button is clicked on a hunk with selected lines', async function() {
const absFilePath = path.join(workdirPath, filePath);
const originalLines = fs.readFileSync(absFilePath, 'utf8').split('\n');
// write some unstaged changes
const unstagedLines = originalLines.slice();
unstagedLines.splice(1, 1,
'this is a modified line',
'this is a new line',
'this is another new line',
);
unstagedLines.splice(11, 2, 'this is a modified line');
fs.writeFileSync(absFilePath, unstagedLines.join('\n'));
// stage a subset of lines from first hunk
const wrapper = mount(component);
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise0 = switchboard.getFinishStageOperationPromise();
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('LineView').at(1).find('.github-HunkView-line').simulate('mousedown', {button: 0, detail: 1});
hunkView0.find('LineView').at(3).find('.github-HunkView-line').simulate('mousemove', {});
window.dispatchEvent(new MouseEvent('mouseup'));
hunkView0.find('button.github-HunkView-stageButton').simulate('click');
await opPromise0;
await refreshRepository(wrapper);
const expectedLines0 = originalLines.slice();
expectedLines0.splice(1, 1,
'this is a modified line',
'this is a new line',
);
assert.autocrlfEqual(await repository.readFileFromIndex('sample.js'), expectedLines0.join('\n'));
// stage remaining lines in hunk
const opPromise1 = switchboard.getFinishStageOperationPromise();
wrapper.find('HunkView').at(0).find('button.github-HunkView-stageButton').simulate('click');
await opPromise1;
await refreshRepository(wrapper);
const expectedLines1 = originalLines.slice();
expectedLines1.splice(1, 1,
'this is a modified line',
'this is a new line',
'this is another new line',
);
assert.autocrlfEqual(await repository.readFileFromIndex('sample.js'), expectedLines1.join('\n'));
// unstage a subset of lines from the first hunk
wrapper.setState({stagingStatus: 'staged'});
await refreshRepository(wrapper);
const hunkView2 = wrapper.find('HunkView').at(0);
hunkView2.find('LineView').at(1).find('.github-HunkView-line')
.simulate('mousedown', {button: 0, detail: 1});
window.dispatchEvent(new MouseEvent('mouseup'));
hunkView2.find('LineView').at(2).find('.github-HunkView-line')
.simulate('mousedown', {button: 0, detail: 1, metaKey: true});
window.dispatchEvent(new MouseEvent('mouseup'));
const opPromise2 = switchboard.getFinishStageOperationPromise();
hunkView2.find('button.github-HunkView-stageButton').simulate('click');
await opPromise2;
await refreshRepository(wrapper);
const expectedLines2 = originalLines.slice();
expectedLines2.splice(2, 0,
'this is a new line',
'this is another new line',
);
assert.autocrlfEqual(await repository.readFileFromIndex('sample.js'), expectedLines2.join('\n'));
// unstage the rest of the hunk
commandRegistry.dispatch(wrapper.find('FilePatchView').getDOMNode(), 'github:toggle-patch-selection-mode');
const opPromise3 = switchboard.getFinishStageOperationPromise();
wrapper.find('HunkView').at(0).find('button.github-HunkView-stageButton').simulate('click');
await opPromise3;
assert.autocrlfEqual(await repository.readFileFromIndex('sample.js'), originalLines.join('\n'));
});
// https://github.com/atom/github/issues/417
describe('when unstaging the last lines/hunks from a file', function() {
it('removes added files from index when last hunk is unstaged', async function() {
const absFilePath = path.join(workdirPath, 'new-file.txt');
fs.writeFileSync(absFilePath, 'foo\n');
await repository.stageFiles(['new-file.txt']);
const wrapper = mount(React.cloneElement(component, {
filePath: 'new-file.txt',
initialStagingStatus: 'staged',
}));
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const opPromise = switchboard.getFinishStageOperationPromise();
wrapper.find('HunkView').at(0).find('button.github-HunkView-stageButton').simulate('click');
await opPromise;
const stagedChanges = await repository.getStagedChanges();
assert.equal(stagedChanges.length, 0);
});
it('removes added files from index when last lines are unstaged', async function() {
const absFilePath = path.join(workdirPath, 'new-file.txt');
fs.writeFileSync(absFilePath, 'foo\n');
await repository.stageFiles(['new-file.txt']);
const wrapper = mount(React.cloneElement(component, {
filePath: 'new-file.txt',
initialStagingStatus: 'staged',
}));
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const viewNode = wrapper.find('FilePatchView').getDOMNode();
commandRegistry.dispatch(viewNode, 'github:toggle-patch-selection-mode');
commandRegistry.dispatch(viewNode, 'core:select-all');
const opPromise = switchboard.getFinishStageOperationPromise();
wrapper.find('HunkView').at(0).find('button.github-HunkView-stageButton').simulate('click');
await opPromise;
const stagedChanges = await repository.getStagedChanges();
assert.lengthOf(stagedChanges, 0);
});
});
// https://github.com/atom/github/issues/341
describe('when duplicate staging occurs', function() {
it('avoids patch conflicts with pending line staging operations', async function() {
const absFilePath = path.join(workdirPath, filePath);
const originalLines = fs.readFileSync(absFilePath, 'utf8').split('\n');
// write some unstaged changes
const unstagedLines = originalLines.slice();
unstagedLines.splice(1, 0,
'this is a modified line',
'this is a new line',
'this is another new line',
);
unstagedLines.splice(11, 2, 'this is a modified line');
fs.writeFileSync(absFilePath, unstagedLines.join('\n'));
const wrapper = mount(component);
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
const hunkView0 = wrapper.find('HunkView').at(0);
hunkView0.find('LineView').at(1).find('.github-HunkView-line')
.simulate('mousedown', {button: 0, detail: 1});
window.dispatchEvent(new MouseEvent('mouseup'));
// stage lines in rapid succession
// second stage action is a no-op since the first staging operation is in flight
const line1StagingPromise = switchboard.getFinishStageOperationPromise();
hunkView0.find('.github-HunkView-stageButton').simulate('click');
hunkView0.find('.github-HunkView-stageButton').simulate('click');
await line1StagingPromise;
const changePatchPromise = switchboard.getChangePatchPromise();
// assert that only line 1 has been staged
await refreshRepository(wrapper); // clear the cached file patches
let expectedLines = originalLines.slice();
expectedLines.splice(1, 0,
'this is a modified line',
);
let actualLines = await repository.readFileFromIndex(filePath);
assert.autocrlfEqual(actualLines, expectedLines.join('\n'));
await changePatchPromise;
wrapper.update();
const hunkView1 = wrapper.find('HunkView').at(0);
hunkView1.find('LineView').at(2).find('.github-HunkView-line')
.simulate('mousedown', {button: 0, detail: 1});
window.dispatchEvent(new MouseEvent('mouseup'));
const line2StagingPromise = switchboard.getFinishStageOperationPromise();
hunkView1.find('.github-HunkView-stageButton').simulate('click');
await line2StagingPromise;
// assert that line 2 has now been staged
expectedLines = originalLines.slice();
expectedLines.splice(1, 0,
'this is a modified line',
'this is a new line',
);
actualLines = await repository.readFileFromIndex(filePath);
assert.autocrlfEqual(actualLines, expectedLines.join('\n'));
});
it('avoids patch conflicts with pending hunk staging operations', async function() {
const absFilePath = path.join(workdirPath, filePath);
const originalLines = fs.readFileSync(absFilePath, 'utf8').split('\n');
// write some unstaged changes
const unstagedLines = originalLines.slice();
unstagedLines.splice(1, 0,
'this is a modified line',
'this is a new line',
'this is another new line',
);
unstagedLines.splice(11, 2, 'this is a modified line');
fs.writeFileSync(absFilePath, unstagedLines.join('\n'));
const wrapper = mount(component);
await assert.async.isTrue(wrapper.update().find('HunkView').exists());
// ensure staging the same hunk twice does not cause issues
// second stage action is a no-op since the first staging operation is in flight
const hunk1StagingPromise = switchboard.getFinishStageOperationPromise();
wrapper.find('HunkView').at(0).find('.github-HunkView-stageButton').simulate('click');
wrapper.find('HunkView').at(0).find('.github-HunkView-stageButton').simulate('click');
await hunk1StagingPromise;
const patchPromise0 = switchboard.getChangePatchPromise();
await refreshRepository(wrapper); // clear the cached file patches
const modifiedFilePatch = await repository.getFilePatchForPath(filePath);
wrapper.setState({filePatch: modifiedFilePatch});
await patchPromise0;
let expectedLines = originalLines.slice();
expectedLines.splice(1, 0,
'this is a modified line',
'this is a new line',
'this is another new line',
);
let actualLines = await repository.readFileFromIndex(filePath);
assert.autocrlfEqual(actualLines, expectedLines.join('\n'));
const hunk2StagingPromise = switchboard.getFinishStageOperationPromise();
wrapper.find('HunkView').at(0).find('.github-HunkView-stageButton').simulate('click');
await hunk2StagingPromise;
expectedLines = originalLines.slice();
expectedLines.splice(1, 0,
'this is a modified line',
'this is a new line',
'this is another new line',
);
expectedLines.splice(11, 2, 'this is a modified line');
actualLines = await repository.readFileFromIndex(filePath);
assert.autocrlfEqual(actualLines, expectedLines.join('\n'));
});
});
});
});
});