This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathfile-patch.test.js
930 lines (797 loc) · 36.1 KB
/
file-patch.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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
import fs from 'fs-extra';
import path from 'path';
import until from 'test-until';
import dedent from 'dedent-js';
import {setup, teardown} from './helpers';
import GitShellOutStrategy from '../../lib/git-shell-out-strategy';
describe('integration: file patches', function() {
// NOTE: This test does not pass on VSTS macOS builds. It will be re-enabled
// once the underlying problem is solved. See atom/github#2617 for details.
if (process.env.CI_PROVIDER === 'VSTS') { return; }
let context, wrapper, atomEnv;
let workspace;
let commands, workspaceElement;
let repoRoot, git;
let usesWorkspaceObserver;
this.timeout(Math.max(this.timeout(), 10000));
this.retries(5); // FLAKE
beforeEach(function() {
// These tests take a little longer because they rely on real filesystem events and git operations.
until.setDefaultTimeout(9000);
});
afterEach(async function() {
if (context) {
await teardown(context);
}
});
async function useFixture(fixtureName) {
context = await setup({
initialRoots: [fixtureName],
});
wrapper = context.wrapper;
atomEnv = context.atomEnv;
commands = atomEnv.commands;
workspace = atomEnv.workspace;
repoRoot = atomEnv.project.getPaths()[0];
git = new GitShellOutStrategy(repoRoot);
usesWorkspaceObserver = context.githubPackage.getContextPool().getContext(repoRoot).useWorkspaceChangeObserver();
workspaceElement = atomEnv.views.getView(workspace);
// Open the git tab
await commands.dispatch(workspaceElement, 'github:toggle-git-tab-focus');
wrapper.update();
}
function repoPath(...parts) {
return path.join(repoRoot, ...parts);
}
// Manually trigger a Repository update on Linux, which uses a WorkspaceChangeObserver.
function triggerChange() {
if (usesWorkspaceObserver) {
context.githubPackage.getActiveRepository().refresh();
}
}
async function clickFileInGitTab(stagingStatus, relativePath) {
let listItem = null;
await until(() => {
listItem = wrapper
.update()
.find(`.github-StagingView-${stagingStatus} .github-FilePatchListView-item`)
.filterWhere(w => w.find('.github-FilePatchListView-path').text() === relativePath);
return listItem.exists();
}, `the list item for path ${relativePath} (${stagingStatus}) appears`);
listItem.simulate('mousedown', {button: 0, persist() {}});
window.dispatchEvent(new MouseEvent('mouseup'));
const itemSelector = `ChangedFileItem[relPath="${relativePath}"][stagingStatus="${stagingStatus}"]`;
await until(
() => wrapper.update().find(itemSelector).find('.github-FilePatchView').exists(),
`the ChangedFileItem for ${relativePath} arrives and loads`,
);
}
function getPatchItem(stagingStatus, relativePath) {
return wrapper.update().find(`ChangedFileItem[relPath="${relativePath}"][stagingStatus="${stagingStatus}"]`);
}
function getPatchEditor(stagingStatus, relativePath) {
const component = getPatchItem(stagingStatus, relativePath).find('.github-FilePatchView').find('AtomTextEditor');
if (!component.exists()) {
return null;
}
return component.instance().getModel();
}
function patchContent(stagingStatus, relativePath, ...rows) {
const aliases = new Map([
['added', 'github-FilePatchView-line--added'],
['deleted', 'github-FilePatchView-line--deleted'],
['nonewline', 'github-FilePatchView-line--nonewline'],
['selected', 'github-FilePatchView-line--selected'],
]);
const knownClasses = new Set(aliases.values());
let actualRowText = [];
const differentRows = new Set();
const actualClassesByRow = new Map();
const missingClassesByRow = new Map();
const unexpectedClassesByRow = new Map();
return until(() => {
// Determine the CSS classes applied to each screen line within the patch editor. This is gnarly, but based on
// the logic that TextEditorComponent::queryDecorationsToRender() actually uses to determine what classes to
// apply when rendering line elements.
const editor = getPatchEditor(stagingStatus, relativePath);
if (editor === null) {
actualRowText = ['Unable to find patch item'];
return false;
}
editor.setSoftWrapped(false);
const decorationsByMarker = editor.decorationManager.decorationPropertiesByMarkerForScreenRowRange(0, Infinity);
actualClassesByRow.clear();
for (const [marker, decorations] of decorationsByMarker) {
const rowNumbers = marker.getScreenRange().getRows();
for (const decoration of decorations) {
if (decoration.type !== 'line') {
continue;
}
for (const row of rowNumbers) {
const classes = actualClassesByRow.get(row) || [];
classes.push(decoration.class);
actualClassesByRow.set(row, classes);
}
}
}
actualRowText = [];
differentRows.clear();
missingClassesByRow.clear();
unexpectedClassesByRow.clear();
let match = true;
for (let i = 0; i < Math.max(rows.length, editor.getLastScreenRow()); i++) {
const [expectedText, ...givenClasses] = rows[i] || [''];
const expectedClasses = givenClasses.map(givenClass => aliases.get(givenClass) || givenClass);
const actualText = editor.lineTextForScreenRow(i);
const actualClasses = new Set(actualClassesByRow.get(i) || []);
actualRowText[i] = actualText;
if (actualText !== expectedText) {
// The patch text for this screen row differs.
differentRows.add(i);
match = false;
}
const missingClasses = expectedClasses.filter(expectedClass => !actualClasses.delete(expectedClass));
if (missingClasses.length > 0) {
// An expected class was not present on this screen row.
missingClassesByRow.set(i, missingClasses);
match = false;
}
const unexpectedClasses = Array.from(actualClasses).filter(remainingClass => knownClasses.has(remainingClass));
if (unexpectedClasses.length > 0) {
// A known class that was not expected was present on this screen row.
unexpectedClassesByRow.set(i, unexpectedClasses);
match = false;
}
}
return match;
}, 'a matching updated file patch arrives').catch(e => {
let diagnosticOutput = '';
for (let i = 0; i < actualRowText.length; i++) {
diagnosticOutput += differentRows.has(i) ? '! ' : ' ';
diagnosticOutput += actualRowText[i];
const annotations = [];
annotations.push(...actualClassesByRow.get(i) || []);
for (const missingClass of (missingClassesByRow.get(i) || [])) {
annotations.push(`-"${missingClass}"`);
}
for (const unexpectedClass of (unexpectedClassesByRow.get(i) || [])) {
annotations.push(`x"${unexpectedClass}"`);
}
if (annotations.length > 0) {
diagnosticOutput += ' ';
diagnosticOutput += annotations.join(' ');
}
diagnosticOutput += '\n';
}
// eslint-disable-next-line no-console
console.log('Unexpected patch contents:\n', diagnosticOutput);
throw e;
});
}
describe('with an added file', function() {
beforeEach(async function() {
await useFixture('three-files');
await fs.writeFile(repoPath('added-file.txt'), '0000\n0001\n0002\n0003\n0004\n0005\n', {encoding: 'utf8'});
triggerChange();
await clickFileInGitTab('unstaged', 'added-file.txt');
});
describe('unstaged', function() {
it('may be partially staged', async function() {
// Stage lines two and three
getPatchEditor('unstaged', 'added-file.txt').setSelectedBufferRange([[2, 1], [3, 3]]);
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await patchContent(
'unstaged', 'added-file.txt',
['0000', 'added'],
['0001', 'added'],
['0002'],
['0003'],
['0004', 'added', 'selected'],
['0005', 'added'],
);
await clickFileInGitTab('staged', 'added-file.txt');
await patchContent(
'staged', 'added-file.txt',
['0002', 'added', 'selected'],
['0003', 'added', 'selected'],
);
});
it('may be completed staged', async function() {
getPatchEditor('unstaged', 'added-file.txt').selectAll();
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('staged', 'added-file.txt');
await patchContent(
'staged', 'added-file.txt',
['0000', 'added', 'selected'],
['0001', 'added', 'selected'],
['0002', 'added', 'selected'],
['0003', 'added', 'selected'],
['0004', 'added', 'selected'],
['0005', 'added', 'selected'],
);
});
it('may discard lines', async function() {
getPatchEditor('unstaged', 'added-file.txt').setSelectedBufferRange([[1, 0], [3, 3]]);
wrapper.find('.github-HunkHeaderView-discardButton').simulate('click');
await patchContent(
'unstaged', 'added-file.txt',
['0000', 'added'],
['0004', 'added', 'selected'],
['0005', 'added'],
);
const editor = await workspace.open(repoPath('added-file.txt'));
assert.strictEqual(editor.getText(), '0000\n0004\n0005\n');
});
});
describe('staged', function() {
beforeEach(async function() {
await git.stageFiles(['added-file.txt']);
await clickFileInGitTab('staged', 'added-file.txt');
});
it('may be partially unstaged', async function() {
this.retries(5); // FLAKE
getPatchEditor('staged', 'added-file.txt').setSelectedBufferRange([[3, 0], [4, 3]]);
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await patchContent(
'staged', 'added-file.txt',
['0000', 'added'],
['0001', 'added'],
['0002', 'added'],
['0005', 'added', 'selected'],
);
await clickFileInGitTab('unstaged', 'added-file.txt');
await patchContent(
'unstaged', 'added-file.txt',
['0000'],
['0001'],
['0002'],
['0003', 'added', 'selected'],
['0004', 'added', 'selected'],
['0005'],
);
});
it('may be completely unstaged', async function() {
this.retries(5); // FLAKE
getPatchEditor('staged', 'added-file.txt').selectAll();
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('unstaged', 'added-file.txt');
await patchContent(
'unstaged', 'added-file.txt',
['0000', 'added', 'selected'],
['0001', 'added', 'selected'],
['0002', 'added', 'selected'],
['0003', 'added', 'selected'],
['0004', 'added', 'selected'],
['0005', 'added', 'selected'],
);
});
});
});
describe('with a removed file', function() {
beforeEach(async function() {
await useFixture('multi-line-file');
await fs.remove(repoPath('sample.js'));
triggerChange();
});
describe('unstaged', function() {
beforeEach(async function() {
await clickFileInGitTab('unstaged', 'sample.js');
});
it('may be partially staged', async function() {
getPatchEditor('unstaged', 'sample.js').setSelectedBufferRange([[4, 0], [7, 5]]);
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {', 'deleted'],
[' const sort = function(items) {', 'deleted'],
[' if (items.length <= 1) { return items; }', 'deleted'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted'],
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
[' };', 'deleted'],
['', 'deleted'],
[' return sort(Array.apply(this, arguments));', 'deleted'],
['};', 'deleted'],
);
await clickFileInGitTab('staged', 'sample.js');
await patchContent(
'staged', 'sample.js',
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }'],
[' let pivot = items.shift(), current, left = [], right = [];'],
[' while (items.length > 0) {', 'deleted', 'selected'],
[' current = items.shift();', 'deleted', 'selected'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted', 'selected'],
[' }', 'deleted', 'selected'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' };'],
[''],
);
});
it('may be completely staged', async function() {
getPatchEditor('unstaged', 'sample.js').setSelectedBufferRange([[0, 0], [12, 2]]);
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('staged', 'sample.js');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {', 'deleted', 'selected'],
[' const sort = function(items) {', 'deleted', 'selected'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {', 'deleted', 'selected'],
[' current = items.shift();', 'deleted', 'selected'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted', 'selected'],
[' }', 'deleted', 'selected'],
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
[' };', 'deleted', 'selected'],
['', 'deleted', 'selected'],
[' return sort(Array.apply(this, arguments));', 'deleted', 'selected'],
['};', 'deleted', 'selected'],
);
});
it('may discard lines', async function() {
getPatchEditor('unstaged', 'sample.js').setSelectedBufferRanges([
[[1, 0], [2, 0]],
[[7, 0], [8, 1]],
]);
wrapper.find('.github-HunkHeaderView-discardButton').simulate('click');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {', 'deleted'],
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted'],
[' while (items.length > 0) {', 'deleted'],
[' current = items.shift();', 'deleted'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' };', 'deleted', 'selected'],
['', 'deleted'],
[' return sort(Array.apply(this, arguments));', 'deleted'],
['};', 'deleted'],
);
const editor = await workspace.open(repoPath('sample.js'));
assert.strictEqual(
editor.getText(),
' const sort = function(items) {\n' +
' if (items.length <= 1) { return items; }\n' +
' }\n' +
' return sort(left).concat(pivot).concat(sort(right));\n',
);
});
});
describe('staged', function() {
beforeEach(async function() {
await git.stageFiles(['sample.js']);
await clickFileInGitTab('staged', 'sample.js');
});
it('may be partially unstaged', async function() {
getPatchEditor('staged', 'sample.js').setSelectedBufferRange([[8, 0], [8, 5]]);
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {', 'deleted'],
[' const sort = function(items) {', 'deleted'],
[' if (items.length <= 1) { return items; }', 'deleted'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted'],
[' while (items.length > 0) {', 'deleted'],
[' current = items.shift();', 'deleted'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted'],
[' }', 'deleted'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' };', 'deleted', 'selected'],
['', 'deleted'],
[' return sort(Array.apply(this, arguments));', 'deleted'],
['};', 'deleted'],
);
await clickFileInGitTab('unstaged', 'sample.js');
await patchContent(
'unstaged', 'sample.js',
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
);
});
it('may be completely unstaged', async function() {
getPatchEditor('staged', 'sample.js').selectAll();
wrapper.find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('unstaged', 'sample.js');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {', 'deleted', 'selected'],
[' const sort = function(items) {', 'deleted', 'selected'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {', 'deleted', 'selected'],
[' current = items.shift();', 'deleted', 'selected'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted', 'selected'],
[' }', 'deleted', 'selected'],
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
[' };', 'deleted', 'selected'],
['', 'deleted', 'selected'],
[' return sort(Array.apply(this, arguments));', 'deleted', 'selected'],
['};', 'deleted', 'selected'],
);
});
});
});
describe('with a symlink that used to be a file', function() {
beforeEach(async function() {
await useFixture('multi-line-file');
await fs.remove(repoPath('sample.js'));
await fs.writeFile(repoPath('target.txt'), 'something to point the symlink to', {encoding: 'utf8'});
await fs.symlink(repoPath('target.txt'), repoPath('sample.js'));
triggerChange();
});
describe('unstaged', function() {
before(function() {
if (process.platform === 'win32') {
this.skip();
}
});
beforeEach(async function() {
await clickFileInGitTab('unstaged', 'sample.js');
});
it('may stage the content deletion without the symlink creation', async function() {
getPatchEditor('unstaged', 'sample.js').selectAll();
getPatchItem('unstaged', 'sample.js').find('.github-HunkHeaderView-stageButton').simulate('click');
await patchContent(
'unstaged', 'sample.js',
[repoPath('target.txt'), 'added', 'selected'],
[' No newline at end of file', 'nonewline'],
);
assert.isTrue(getPatchItem('unstaged', 'sample.js').find('.github-FilePatchView-metaTitle').exists());
await clickFileInGitTab('staged', 'sample.js');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {', 'deleted', 'selected'],
[' const sort = function(items) {', 'deleted', 'selected'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {', 'deleted', 'selected'],
[' current = items.shift();', 'deleted', 'selected'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted', 'selected'],
[' }', 'deleted', 'selected'],
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
[' };', 'deleted', 'selected'],
['', 'deleted', 'selected'],
[' return sort(Array.apply(this, arguments));', 'deleted', 'selected'],
['};', 'deleted', 'selected'],
);
assert.isFalse(getPatchItem('staged', 'sample.js').find('.github-FilePatchView-metaTitle').exists());
});
it('may stage the content deletion and the symlink creation', async function() {
getPatchItem('unstaged', 'sample.js').find('.github-FilePatchView-metaControls button').simulate('click');
await clickFileInGitTab('staged', 'sample.js');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {', 'deleted', 'selected'],
[' const sort = function(items) {', 'deleted', 'selected'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {', 'deleted', 'selected'],
[' current = items.shift();', 'deleted', 'selected'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted', 'selected'],
[' }', 'deleted', 'selected'],
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
[' };', 'deleted', 'selected'],
['', 'deleted', 'selected'],
[' return sort(Array.apply(this, arguments));', 'deleted', 'selected'],
['};', 'deleted', 'selected'],
);
assert.isTrue(getPatchItem('staged', 'sample.js').find('.github-FilePatchView-metaTitle').exists());
});
});
describe('staged', function() {
before(function() {
if (process.platform === 'win32') {
this.skip();
}
});
beforeEach(async function() {
await git.stageFiles(['sample.js']);
await clickFileInGitTab('staged', 'sample.js');
});
it.skip('may unstage the symlink creation but not the content deletion', async function() {
getPatchItem('staged', 'sample.js').find('.github-FilePatchView-metaControls button').simulate('click');
await clickFileInGitTab('unstaged', 'sample.js');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {', 'deleted', 'selected'],
[' const sort = function(items) {', 'deleted', 'selected'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {', 'deleted', 'selected'],
[' current = items.shift();', 'deleted', 'selected'],
[' current < pivot ? left.push(current) : right.push(current);', 'deleted', 'selected'],
[' }', 'deleted', 'selected'],
[' return sort(left).concat(pivot).concat(sort(right));', 'deleted', 'selected'],
[' };', 'deleted', 'selected'],
['', 'deleted', 'selected'],
[' return sort(Array.apply(this, arguments));', 'deleted', 'selected'],
['};', 'deleted', 'selected'],
);
assert.isTrue(getPatchItem('unstaged', 'sample.js').find('.github-FilePatchView-metaTitle').exists());
});
});
});
describe('with a file that used to be a symlink', function() {
beforeEach(async function() {
await useFixture('symlinks');
await fs.remove(repoPath('symlink.txt'));
await fs.writeFile(repoPath('symlink.txt'), "Guess what I'm a text file now suckers", {encoding: 'utf8'});
triggerChange();
});
describe.skip('unstaged', function() {
beforeEach(async function() {
await clickFileInGitTab('unstaged', 'symlink.txt');
});
it('may stage the symlink deletion without the content addition', async function() {
getPatchItem('unstaged', 'symlink.txt').find('.github-FilePatchView-metaControls button').simulate('click');
await assert.async.isFalse(
getPatchItem('unstaged', 'symlink.txt').find('.github-FilePatchView-metaTitle').exists(),
);
await patchContent(
'unstaged', 'symlink.txt',
["Guess what I'm a text file now suckers", 'added', 'selected'],
[' No newline at end of file', 'nonewline'],
);
await clickFileInGitTab('staged', 'symlink.txt');
await patchContent(
'staged', 'symlink.txt',
['./regular-file.txt', 'deleted', 'selected'],
[' No newline at end of file', 'nonewline'],
);
assert.isTrue(getPatchItem('staged', 'symlink.txt').find('.github-FilePatchView-metaTitle').exists());
});
it('may stage the content addition and the symlink deletion together', async function() {
getPatchEditor('unstaged', 'symlink.txt').selectAll();
getPatchItem('unstaged', 'symlink.txt').find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('staged', 'symlink.txt');
await patchContent(
'staged', 'symlink.txt',
["Guess what I'm a text file now suckers", 'added', 'selected'],
[' No newline at end of file', 'nonewline'],
);
assert.isTrue(getPatchItem('staged', 'symlink.txt').find('.github-FilePatchView-metaTitle').exists());
});
});
describe('staged', function() {
before(function() {
if (process.platform === 'win32') {
this.skip();
}
});
beforeEach(async function() {
await git.stageFiles(['symlink.txt']);
await clickFileInGitTab('staged', 'symlink.txt');
});
it('may unstage the content addition and the symlink deletion together', async function() {
getPatchItem('staged', 'symlink.txt').find('.github-FilePatchView-metaControls button').simulate('click');
await clickFileInGitTab('unstaged', 'symlink.txt');
assert.isTrue(getPatchItem('unstaged', 'symlink.txt').find('.github-FilePatchView-metaTitle').exists());
await patchContent(
'unstaged', 'symlink.txt',
["Guess what I'm a text file now suckers", 'added', 'selected'],
[' No newline at end of file', 'nonewline'],
);
});
it('may unstage the content addition without the symlink deletion', async function() {
getPatchEditor('staged', 'symlink.txt').selectAll();
getPatchItem('staged', 'symlink.txt').find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('unstaged', 'symlink.txt');
await patchContent(
'unstaged', 'symlink.txt',
["Guess what I'm a text file now suckers", 'added', 'selected'],
[' No newline at end of file', 'nonewline'],
);
assert.isFalse(getPatchItem('unstaged', 'symlink.txt').find('.github-FilePatchView-metaTitle').exists());
await clickFileInGitTab('staged', 'symlink.txt');
assert.isTrue(getPatchItem('staged', 'symlink.txt').find('.github-FilePatchView-metaTitle').exists());
await patchContent(
'staged', 'symlink.txt',
['./regular-file.txt', 'deleted', 'selected'],
[' No newline at end of file', 'nonewline'],
);
});
});
});
describe('with a modified file', function() {
beforeEach(async function() {
await useFixture('multi-line-file');
await fs.writeFile(
repoPath('sample.js'),
dedent`
const quicksort = function() {
const sort = function(items) {
while (items.length > 0) {
current = items.shift();
current < pivot ? left.push(current) : right.push(current);
}
return sort(left).concat(pivot).concat(sort(right));
// added 0
// added 1
};
return sort(Array.apply(this, arguments));
};\n
`,
{encoding: 'utf8'},
);
triggerChange();
});
describe('unstaged', function() {
beforeEach(async function() {
await clickFileInGitTab('unstaged', 'sample.js');
});
it('may be partially staged', async function() {
getPatchEditor('unstaged', 'sample.js').setSelectedBufferRanges([
[[2, 0], [2, 0]],
[[10, 0], [10, 0]],
]);
getPatchItem('unstaged', 'sample.js').find('.github-HunkHeaderView-stageButton').simulate('click');
// in the case of multiple selections, the next selection is calculated based on bottom most selection
// When the bottom most changed line in a diff is staged/unstaged, then the new bottom most changed
// line is selected.
// Essentially we want to keep the selection close to where it was, for ease of keyboard navigation.
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 0', 'added', 'selected'],
[' // added 1'],
[' };'],
[''],
);
await clickFileInGitTab('staged', 'sample.js');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 1', 'added', 'selected'],
[' };'],
[''],
[' return sort(Array.apply(this, arguments));'],
);
});
it('may be completely staged', async function() {
getPatchEditor('unstaged', 'sample.js').selectAll();
getPatchItem('unstaged', 'sample.js').find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('staged', 'sample.js');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 0', 'added', 'selected'],
[' // added 1', 'added', 'selected'],
[' };'],
[''],
[' return sort(Array.apply(this, arguments));'],
);
});
it('may discard lines', async function() {
getPatchEditor('unstaged', 'sample.js').setSelectedBufferRanges([
[[3, 0], [3, 0]],
[[9, 0], [9, 0]],
]);
getPatchItem('unstaged', 'sample.js').find('.github-HunkHeaderView-discardButton').simulate('click');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }', 'deleted'],
[' let pivot = items.shift(), current, left = [], right = [];'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 1', 'added', 'selected'],
[' };'],
[''],
[' return sort(Array.apply(this, arguments));'],
);
const editor = await workspace.open(repoPath('sample.js'));
assert.strictEqual(editor.getText(), dedent`
const quicksort = function() {
const sort = function(items) {
let pivot = items.shift(), current, left = [], right = [];
while (items.length > 0) {
current = items.shift();
current < pivot ? left.push(current) : right.push(current);
}
return sort(left).concat(pivot).concat(sort(right));
// added 1
};
return sort(Array.apply(this, arguments));
};\n
`);
});
});
describe('staged', function() {
beforeEach(async function() {
await git.stageFiles(['sample.js']);
await clickFileInGitTab('staged', 'sample.js');
});
it('may be partially unstaged', async function() {
getPatchEditor('staged', 'sample.js').setSelectedBufferRanges([
[[3, 0], [3, 0]],
[[10, 0], [10, 0]],
]);
getPatchItem('staged', 'sample.js').find('.github-HunkHeaderView-stageButton').simulate('click');
await patchContent(
'staged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }', 'deleted'],
[' let pivot = items.shift(), current, left = [], right = [];'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 0', 'added', 'selected'],
[' };'],
[''],
[' return sort(Array.apply(this, arguments));'],
);
await clickFileInGitTab('unstaged', 'sample.js');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 0'],
[' // added 1', 'added', 'selected'],
[' };'],
[''],
[' return sort(Array.apply(this, arguments));'],
);
});
it('may be fully unstaged', async function() {
getPatchEditor('staged', 'sample.js').selectAll();
getPatchItem('staged', 'sample.js').find('.github-HunkHeaderView-stageButton').simulate('click');
await clickFileInGitTab('unstaged', 'sample.js');
await patchContent(
'unstaged', 'sample.js',
['const quicksort = function() {'],
[' const sort = function(items) {'],
[' if (items.length <= 1) { return items; }', 'deleted', 'selected'],
[' let pivot = items.shift(), current, left = [], right = [];', 'deleted', 'selected'],
[' while (items.length > 0) {'],
[' current = items.shift();'],
[' current < pivot ? left.push(current) : right.push(current);'],
[' }'],
[' return sort(left).concat(pivot).concat(sort(right));'],
[' // added 0', 'added', 'selected'],
[' // added 1', 'added', 'selected'],
[' };'],
[''],
[' return sort(Array.apply(this, arguments));'],
);
});
});
});
});