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 patheditor-conflict-controller.test.js
528 lines (398 loc) · 20.5 KB
/
editor-conflict-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
import fs from 'fs-extra';
import temp from 'temp';
import path from 'path';
import React from 'react';
import {mount} from 'enzyme';
import {Point} from 'atom';
import ResolutionProgress from '../../lib/models/conflicts/resolution-progress';
import {OURS, BASE, THEIRS} from '../../lib/models/conflicts/source';
import EditorConflictController from '../../lib/controllers/editor-conflict-controller';
import ConflictController from '../../lib/controllers/conflict-controller';
const onlyTwoMarkers = `This is some text before the marking.
More text.
<<<<<<< HEAD
My changes
Multi-line even
=======
Your changes
>>>>>>> other-branch
In between.
<<<<<<< HEAD
More of my changes
=======
More of your changes
>>>>>>> other-branch
Stuff at the very end.`;
describe('EditorConflictController', function() {
let atomEnv, workspace, commands, app, wrapper, editor, editorView;
let resolutionProgress, refreshResolutionProgress;
let fixtureFile;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
commands = atomEnv.commands;
refreshResolutionProgress = sinon.spy();
resolutionProgress = new ResolutionProgress();
});
afterEach(function() {
atomEnv.destroy();
});
const useFixture = async function(fixtureName, {isRebase, withEditor} = {isRebase: false}) {
const fixturePath = path.join(
path.dirname(__filename), '..', 'fixtures', 'conflict-marker-examples', fixtureName);
const tempDir = temp.mkdirSync('conflict-fixture-');
fixtureFile = path.join(tempDir, fixtureName);
fs.copySync(fixturePath, fixtureFile);
editor = await workspace.open(fixtureFile);
if (withEditor) {
withEditor(editor);
}
editorView = atomEnv.views.getView(editor);
app = (
<EditorConflictController
workspace={workspace}
commands={commands}
editor={editor}
resolutionProgress={resolutionProgress}
refreshResolutionProgress={refreshResolutionProgress}
isRebase={isRebase}
/>
);
wrapper = mount(app);
};
const textFromSide = function(side) {
return editor.getTextInBufferRange(side.marker.getBufferRange());
};
it('scrolls the first conflict into view', async function() {
await useFixture('triple-2way-diff.txt', {
withEditor(e) { sinon.stub(e, 'scrollToBufferPosition'); },
});
assert.isTrue(editor.scrollToBufferPosition.calledWith(new Point(4, 0), {center: true}));
});
describe('on a file with 2-way diff markers', function() {
let conflicts;
beforeEach(async function() {
await useFixture('triple-2way-diff.txt');
conflicts = Array.from(wrapper.state('conflicts'));
});
it('creates a Conflict from each conflict marker', function() {
assert.lengthOf(conflicts, 3);
assert.equal(textFromSide(conflicts[0].getSide(OURS)), 'My changes\nMulti-line even\n');
assert.equal(textFromSide(conflicts[0].separator), '=======\n');
assert.equal(textFromSide(conflicts[0].getSide(THEIRS)), 'Your changes\n');
assert.equal(textFromSide(conflicts[1].getSide(OURS)), 'My middle changes\n');
assert.equal(textFromSide(conflicts[1].separator), '=======\n');
assert.equal(textFromSide(conflicts[1].getSide(THEIRS)), 'Your middle changes\n');
assert.equal(textFromSide(conflicts[2].getSide(OURS)), 'More of my changes\n');
assert.equal(textFromSide(conflicts[2].separator), '=======\n');
assert.equal(textFromSide(conflicts[2].getSide(THEIRS)), '');
});
it('renders a ConflictController for each Conflict', function() {
const conflictControllers = wrapper.find(ConflictController);
assert.lengthOf(conflictControllers, conflicts.length);
conflicts.forEach(conflict => {
assert.isTrue(conflictControllers.someWhere(cc => cc.prop('conflict') === conflict));
});
});
it('reports the unresolved conflict count on render', function() {
assert.equal(resolutionProgress.getRemaining(editor.getPath()), 3);
});
it('resolves a conflict as "ours"', function() {
const conflict = conflicts[1];
assert.isFalse(conflict.isResolved());
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(THEIRS)]);
assert.include(editor.getText(), 'Text in between 0 and 1.\n\nMy middle changes\n\nText in between 1 and 2.');
});
it('resolves a conflict as "theirs"', function() {
const conflict = conflicts[1];
editor.setCursorBufferPosition([14, 1]); // On "My middle changes"
commands.dispatch(editorView, 'github:resolve-as-theirs');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(THEIRS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(OURS)]);
assert.include(editor.getText(), 'Text in between 0 and 1.\n\nYour middle changes\n\nText in between 1 and 2.');
});
it('resolves a conflict as current', function() {
const conflict = conflicts[1];
editor.setCursorBufferPosition([14, 1]); // On "My middle changes"
commands.dispatch(editorView, 'github:resolve-as-current');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(THEIRS)]);
assert.include(editor.getText(), 'Text in between 0 and 1.\n\nMy middle changes\n\nText in between 1 and 2.');
});
it('resolves multiple conflicts as current', function() {
editor.setCursorBufferPosition([14, 1]); // On "My middle changes"
editor.addCursorAtBufferPosition([24, 3]); // On "More of your changes"
commands.dispatch(editorView, 'github:resolve-as-current');
assert.isFalse(conflicts[0].isResolved());
assert.isTrue(conflicts[1].isResolved());
assert.strictEqual(conflicts[1].getChosenSide(), conflicts[1].getSide(OURS));
assert.isTrue(conflicts[2].isResolved());
assert.strictEqual(conflicts[2].getChosenSide(), conflicts[2].getSide(THEIRS));
});
it('resolves multiple conflicts as "ours"', function() {
assert.isFalse(conflicts[0].isResolved());
assert.isFalse(conflicts[1].isResolved());
assert.isFalse(conflicts[2].isResolved());
editor.setCursorBufferPosition([8, 3]); // On "Your changes"
editor.addCursorAtBufferPosition([11, 2]); // On "Text in between 0 and 1."
editor.addCursorAtBufferPosition([14, 5]); // On "My middle changes"
editor.addCursorAtBufferPosition([15, 0]); // On "======="
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.isTrue(conflicts[0].isResolved());
assert.strictEqual(conflicts[0].getChosenSide(), conflicts[0].getSide(OURS));
assert.deepEqual(conflicts[0].getUnchosenSides(), [conflicts[0].getSide(THEIRS)]);
assert.isTrue(conflicts[1].isResolved());
assert.strictEqual(conflicts[1].getChosenSide(), conflicts[1].getSide(OURS));
assert.deepEqual(conflicts[1].getUnchosenSides(), [conflicts[1].getSide(THEIRS)]);
assert.isFalse(conflicts[2].isResolved());
});
it('resolves multiple conflicts as "theirs"', function() {
assert.isFalse(conflicts[0].isResolved());
assert.isFalse(conflicts[1].isResolved());
assert.isFalse(conflicts[2].isResolved());
editor.setCursorBufferPosition([8, 3]); // On "Your changes"
editor.addCursorAtBufferPosition([11, 2]); // On "Text in between 0 and 1."
editor.addCursorAtBufferPosition([22, 5]); // On "More of my changes"
commands.dispatch(editorView, 'github:resolve-as-theirs');
assert.isTrue(conflicts[0].isResolved());
assert.strictEqual(conflicts[0].getChosenSide(), conflicts[0].getSide(THEIRS));
assert.deepEqual(conflicts[0].getUnchosenSides(), [conflicts[0].getSide(OURS)]);
assert.isFalse(conflicts[1].isResolved());
assert.isTrue(conflicts[2].isResolved());
assert.strictEqual(conflicts[2].getChosenSide(), conflicts[2].getSide(THEIRS));
assert.deepEqual(conflicts[2].getUnchosenSides(), [conflicts[2].getSide(OURS)]);
});
it('disregards conflicts with cursors on both sides', function() {
editor.setCursorBufferPosition([6, 3]); // On "Multi-line even"
editor.addCursorAtBufferPosition([14, 1]); // On "My middle changes"
editor.addCursorAtBufferPosition([16, 0]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-current');
assert.isTrue(conflicts[0].isResolved());
assert.strictEqual(conflicts[0].getChosenSide(), conflicts[0].getSide(OURS));
assert.isFalse(conflicts[1].isResolved());
assert.isFalse(conflicts[2].isResolved());
});
it('resolves a conflict as "ours then theirs"', function() {
const conflict = conflicts[1];
editor.setCursorBufferPosition([14, 1]); // On "My middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours-then-theirs');
assert.isTrue(conflict.isResolved());
assert.include(editor.getText(), 'Text in between 0 and 1.' +
'\n\nMy middle changes\nYour middle changes\n\nText in between 1 and 2.');
});
it('resolves a conflict as "theirs then ours"', function() {
const conflict = conflicts[1];
editor.setCursorBufferPosition([14, 1]); // On "My middle changes"
commands.dispatch(editorView, 'github:resolve-as-theirs-then-ours');
assert.isTrue(conflict.isResolved());
assert.include(editor.getText(), 'Text in between 0 and 1.' +
'\n\nYour middle changes\nMy middle changes\n\nText in between 1 and 2.');
});
it('resolves a conflict as custom text', function() {
const conflict = conflicts[1];
const range = conflict.getSide(OURS).getMarker().getBufferRange();
editor.setTextInBufferRange(range, 'Actually it should be this\n');
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
assert.include(editor.getText(), 'Text in between 0 and 1.\n\n' +
'Actually it should be this\n\nText in between 1 and 2.');
});
it('reverts modified text within a conflict', function() {
const conflict = conflicts[1];
const range = conflict.getSide(OURS).getMarker().getBufferRange();
editor.setTextInBufferRange(range, 'Actually it should be this\n');
editor.setCursorBufferPosition([14, 3]); // On "Actually it should be this"
commands.dispatch(editorView, 'github:revert-conflict-modifications');
assert.isFalse(conflict.getSide(OURS).isModified());
assert.include(editor.getText(), 'Text in between 0 and 1.\n\n' +
'<<<<<<< HEAD\n' +
'My middle changes\n' +
'=======\n' +
'Your middle changes\n' +
'>>>>>>> other-branch\n' +
'\nText in between 1 and 2.');
});
it('preserves a modified side banner', function() {
const conflict = conflicts[1];
const range = conflict.getSide(OURS).getBannerMarker().getBufferRange();
editor.setTextInBufferRange(range, '>>>>>>> Changed this myself\n');
assert.isTrue(conflict.getSide(OURS).isBannerModified());
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
assert.include(editor.getText(), 'Text in between 0 and 1.\n\n' +
'>>>>>>> Changed this myself\n' +
'My middle changes\n\n' +
'Text in between 1 and 2.');
});
it('preserves a modified separator', function() {
const conflict = conflicts[1];
const range = conflict.getSeparator().getMarker().getBufferRange();
editor.setTextInBufferRange(range, '==== hooray ====\n');
assert.isTrue(conflict.getSeparator().isModified());
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.include(editor.getText(), 'Text in between 0 and 1.\n\n' +
'My middle changes\n' +
'==== hooray ====\n\n' +
'Text in between 1 and 2.');
});
it('reports resolution progress', function() {
assert.equal(resolutionProgress.getRemaining(editor.getPath()), 3);
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.equal(resolutionProgress.getRemaining(editor.getPath()), 2);
});
it('dismisses a conflict for manual resolution', function() {
const dismissedConflict = conflicts[1];
assert.equal(resolutionProgress.getRemaining(editor.getPath()), 3);
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:dismiss-conflict');
wrapper.update();
assert.equal(resolutionProgress.getRemaining(editor.getPath()), 2);
assert.include(editor.getText(), 'Text in between 0 and 1.\n\n' +
'<<<<<<< HEAD\n' +
'My middle changes\n' +
'=======\n' +
'Your middle changes\n' +
'>>>>>>> other-branch\n' +
'\nText in between 1 and 2.');
assert.lengthOf(wrapper.find(ConflictController), 2);
assert.isFalse(wrapper.find(ConflictController).someWhere(cc => cc.prop('conflict') === dismissedConflict));
});
it('refreshes conflict markers on buffer reload', async function() {
fs.writeFileSync(fixtureFile, onlyTwoMarkers);
await assert.async.equal(wrapper.state('conflicts').size, 2);
wrapper.update();
assert.lengthOf(wrapper.find(ConflictController), 2);
assert.equal(resolutionProgress.getRemaining(fixtureFile), 2);
});
it('triggers an offline resolution progress refresh when the editor is closed', async function() {
editor.setCursorBufferPosition([16, 6]); // On "Your middle changes"
commands.dispatch(editorView, 'github:resolve-as-ours');
editor.destroy();
await assert.async.isTrue(refreshResolutionProgress.calledWith(fixtureFile));
});
it('performs a resolution from the context menu', function() {
const conflict = conflicts[1];
assert.isFalse(conflict.isResolved());
wrapper.find('ConflictController').at(1).prop('resolveAsSequence')([OURS]);
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
});
it('dismisses a conflict from the context menu', function() {
const conflict = conflicts[2];
wrapper.find('ConflictController').at(2).prop('dismiss')();
wrapper.update();
assert.lengthOf(wrapper.find(ConflictController), 2);
assert.isFalse(wrapper.find(ConflictController).someWhere(cc => cc.prop('conflict') === conflict));
});
});
describe('on a file with 3-way diff markers', function() {
let conflicts, conflict;
beforeEach(async function() {
await useFixture('single-3way-diff.txt');
conflicts = Array.from(wrapper.state('conflicts'));
conflict = conflicts[0];
});
it('creates a conflict controller for each conflict', function() {
assert.lengthOf(conflicts, 1);
assert.equal(textFromSide(conflicts[0].getSide(BASE)), 'These are original texts\n');
});
it('resolves a conflict as "ours"', function() {
assert.isFalse(conflict.isResolved());
editor.setCursorBufferPosition([3, 4]); // On "These are original texts"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(BASE), conflict.getSide(THEIRS)]);
assert.include(editor.getText(), 'These are my changes\n\nPast the end\n');
});
it('resolves a conflict as "theirs"', function() {
editor.setCursorBufferPosition([3, 4]); // On "These are original texts"
commands.dispatch(editorView, 'github:resolve-as-theirs');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(THEIRS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(OURS), conflict.getSide(BASE)]);
assert.include(editor.getText(), 'These are your changes\n\nPast the end\n');
});
it('resolves a conflict as "base"', function() {
editor.setCursorBufferPosition([1, 0]); // On "These are my changes"
commands.dispatch(editorView, 'github:resolve-as-base');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(BASE));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(OURS), conflict.getSide(THEIRS)]);
assert.include(editor.getText(), 'These are original texts\n\nPast the end\n');
});
it('resolves a conflict as "ours then theirs"', function() {
editor.setCursorBufferPosition([3, 4]); // On "These are original texts"
commands.dispatch(editorView, 'github:resolve-as-ours-then-theirs');
assert.isTrue(conflict.isResolved());
assert.include(editor.getText(), 'These are my changes\nThese are your changes\n\nPast the end\n');
});
it('resolves a conflict as "theirs then ours"', function() {
editor.setCursorBufferPosition([3, 4]); // On "These are original texts"
commands.dispatch(editorView, 'github:resolve-as-theirs-then-ours');
assert.isTrue(conflict.isResolved());
assert.include(editor.getText(), 'These are your changes\nThese are my changes\n\nPast the end\n');
});
});
describe('while rebasing', function() {
let conflict;
beforeEach(async function() {
await useFixture('single-3way-diff.txt', {isRebase: true});
[conflict] = Array.from(wrapper.state('conflicts'));
});
it('resolves a conflict as "ours"', function() {
editor.setCursorBufferPosition([3, 3]); // On "these are original texts"
commands.dispatch(editorView, 'github:resolve-as-ours');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(OURS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(BASE), conflict.getSide(THEIRS)]);
assert.equal(editor.getText(), 'These are your changes\n\nPast the end\n');
});
it('resolves a conflict as "theirs"', function() {
editor.setCursorBufferPosition([3, 3]); // On "these are original texts"
commands.dispatch(editorView, 'github:resolve-as-theirs');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(THEIRS));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(OURS), conflict.getSide(BASE)]);
assert.equal(editor.getText(), 'These are my changes\n\nPast the end\n');
});
it('resolves a conflict as "base"', function() {
editor.setCursorBufferPosition([3, 3]); // On "these are original texts"
commands.dispatch(editorView, 'github:resolve-as-base');
assert.isTrue(conflict.isResolved());
assert.strictEqual(conflict.getChosenSide(), conflict.getSide(BASE));
assert.deepEqual(conflict.getUnchosenSides(), [conflict.getSide(OURS), conflict.getSide(THEIRS)]);
assert.equal(editor.getText(), 'These are original texts\n\nPast the end\n');
});
it('resolves a conflict as "ours then theirs"', function() {
editor.setCursorBufferPosition([3, 3]); // On "these are original texts"
commands.dispatch(editorView, 'github:resolve-as-ours-then-theirs');
assert.isTrue(conflict.isResolved());
assert.equal(editor.getText(), 'These are your changes\nThese are my changes\n\nPast the end\n');
});
it('resolves a conflict as "theirs then ours"', function() {
editor.setCursorBufferPosition([3, 3]); // On "these are original texts"
commands.dispatch(editorView, 'github:resolve-as-theirs-then-ours');
assert.isTrue(conflict.isResolved());
assert.equal(editor.getText(), 'These are my changes\nThese are your changes\n\nPast the end\n');
});
});
it('cleans up its subscriptions when unmounting', async function() {
await useFixture('triple-2way-diff.txt');
wrapper.unmount();
editor.destroy();
assert.isFalse(refreshResolutionProgress.called);
});
});