This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
file-patch.test.js
422 lines (384 loc) · 16.5 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
import {cloneRepository, buildRepository} from '../helpers';
import {toGitPathSep} from '../../lib/helpers';
import path from 'path';
import fs from 'fs';
import dedent from 'dedent-js';
import FilePatch from '../../lib/models/file-patch';
import Hunk from '../../lib/models/hunk';
import HunkLine from '../../lib/models/hunk-line';
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);
}
describe('FilePatch', function() {
it('detects executable mode changes', function() {
const of0 = new FilePatch.File({path: 'a.txt', mode: '100644'});
const nf0 = new FilePatch.File({path: 'a.txt', mode: '100755'});
const p0 = new FilePatch.Patch({status: 'modified', hunks: []});
const fp0 = new FilePatch(of0, nf0, p0);
assert.isTrue(fp0.didChangeExecutableMode());
const of1 = new FilePatch.File({path: 'a.txt', mode: '100755'});
const nf1 = new FilePatch.File({path: 'a.txt', mode: '100644'});
const p1 = new FilePatch.Patch({status: 'modified', hunks: []});
const fp1 = new FilePatch(of1, nf1, p1);
assert.isTrue(fp1.didChangeExecutableMode());
const of2 = new FilePatch.File({path: 'a.txt', mode: '100755'});
const nf2 = new FilePatch.File({path: 'a.txt', mode: '100755'});
const p2 = new FilePatch.Patch({status: 'modified', hunks: []});
const fp2 = new FilePatch(of2, nf2, p2);
assert.isFalse(fp2.didChangeExecutableMode());
const of3 = FilePatch.File.empty();
const nf3 = new FilePatch.File({path: 'a.txt', mode: '100755'});
const p3 = new FilePatch.Patch({status: 'modified', hunks: []});
const fp3 = new FilePatch(of3, nf3, p3);
assert.isFalse(fp3.didChangeExecutableMode());
const of4 = FilePatch.File.empty();
const nf4 = new FilePatch.File({path: 'a.txt', mode: '100755'});
const p4 = new FilePatch.Patch({status: 'modified', hunks: []});
const fp4 = new FilePatch(of4, nf4, p4);
assert.isFalse(fp4.didChangeExecutableMode());
});
describe('getStagePatchForLines()', function() {
it('returns a new FilePatch that applies only the specified lines', function() {
const filePatch = createFilePatch('a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 1, 3, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-2', 'added', -1, 2),
new HunkLine('line-3', 'unchanged', 1, 3),
]),
new Hunk(5, 7, 5, 4, '', [
new HunkLine('line-4', 'unchanged', 5, 7),
new HunkLine('line-5', 'deleted', 6, -1),
new HunkLine('line-6', 'deleted', 7, -1),
new HunkLine('line-7', 'added', -1, 8),
new HunkLine('line-8', 'added', -1, 9),
new HunkLine('line-9', 'added', -1, 10),
new HunkLine('line-10', 'deleted', 8, -1),
new HunkLine('line-11', 'deleted', 9, -1),
]),
new Hunk(20, 19, 2, 2, '', [
new HunkLine('line-12', 'deleted', 20, -1),
new HunkLine('line-13', 'added', -1, 19),
new HunkLine('line-14', 'unchanged', 21, 20),
new HunkLine('No newline at end of file', 'nonewline', -1, -1),
]),
]);
const linesFromHunk2 = filePatch.getHunks()[1].getLines().slice(1, 4);
assert.deepEqual(filePatch.getStagePatchForLines(new Set(linesFromHunk2)), createFilePatch(
'a.txt', 'a.txt', 'modified', [
new Hunk(5, 5, 5, 4, '', [
new HunkLine('line-4', 'unchanged', 5, 5),
new HunkLine('line-5', 'deleted', 6, -1),
new HunkLine('line-6', 'deleted', 7, -1),
new HunkLine('line-7', 'added', -1, 6),
new HunkLine('line-10', 'unchanged', 8, 7),
new HunkLine('line-11', 'unchanged', 9, 8),
]),
],
));
// add lines from other hunks
const linesFromHunk1 = filePatch.getHunks()[0].getLines().slice(0, 1);
const linesFromHunk3 = filePatch.getHunks()[2].getLines().slice(1, 2);
const selectedLines = linesFromHunk2.concat(linesFromHunk1, linesFromHunk3);
assert.deepEqual(filePatch.getStagePatchForLines(new Set(selectedLines)), createFilePatch(
'a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 1, 2, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-3', 'unchanged', 1, 2),
]),
new Hunk(5, 6, 5, 4, '', [
new HunkLine('line-4', 'unchanged', 5, 6),
new HunkLine('line-5', 'deleted', 6, -1),
new HunkLine('line-6', 'deleted', 7, -1),
new HunkLine('line-7', 'added', -1, 7),
new HunkLine('line-10', 'unchanged', 8, 8),
new HunkLine('line-11', 'unchanged', 9, 9),
]),
new Hunk(20, 18, 2, 3, '', [
new HunkLine('line-12', 'unchanged', 20, 18),
new HunkLine('line-13', 'added', -1, 19),
new HunkLine('line-14', 'unchanged', 21, 20),
new HunkLine('No newline at end of file', 'nonewline', -1, -1),
]),
],
));
});
describe('staging lines from deleted files', function() {
it('handles staging part of the file', function() {
const filePatch = createFilePatch('a.txt', null, 'deleted', [
new Hunk(1, 0, 3, 0, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'deleted', 3, -1),
]),
]);
const linesFromHunk = filePatch.getHunks()[0].getLines().slice(0, 2);
assert.deepEqual(filePatch.getStagePatchForLines(new Set(linesFromHunk)), createFilePatch(
'a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 3, 1, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'unchanged', 3, 1),
]),
],
));
});
it('handles staging all lines, leaving nothing unstaged', function() {
const filePatch = createFilePatch('a.txt', null, 'deleted', [
new Hunk(1, 0, 3, 0, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'deleted', 3, -1),
]),
]);
const linesFromHunk = filePatch.getHunks()[0].getLines();
assert.deepEqual(filePatch.getStagePatchForLines(new Set(linesFromHunk)), createFilePatch(
'a.txt', null, 'deleted', [
new Hunk(1, 0, 3, 0, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'deleted', 3, -1),
]),
],
));
});
});
});
describe('getUnstagePatchForLines()', function() {
it('returns a new FilePatch that applies only the specified lines', function() {
const filePatch = createFilePatch('a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 1, 3, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-2', 'added', -1, 2),
new HunkLine('line-3', 'unchanged', 1, 3),
]),
new Hunk(5, 7, 5, 4, '', [
new HunkLine('line-4', 'unchanged', 5, 7),
new HunkLine('line-5', 'deleted', 6, -1),
new HunkLine('line-6', 'deleted', 7, -1),
new HunkLine('line-7', 'added', -1, 8),
new HunkLine('line-8', 'added', -1, 9),
new HunkLine('line-9', 'added', -1, 10),
new HunkLine('line-10', 'deleted', 8, -1),
new HunkLine('line-11', 'deleted', 9, -1),
]),
new Hunk(20, 19, 2, 2, '', [
new HunkLine('line-12', 'deleted', 20, -1),
new HunkLine('line-13', 'added', -1, 19),
new HunkLine('line-14', 'unchanged', 21, 20),
new HunkLine('No newline at end of file', 'nonewline', -1, -1),
]),
]);
const lines = new Set(filePatch.getHunks()[1].getLines().slice(1, 5));
filePatch.getHunks()[2].getLines().forEach(line => lines.add(line));
assert.deepEqual(filePatch.getUnstagePatchForLines(lines), createFilePatch(
'a.txt', 'a.txt', 'modified', [
new Hunk(7, 7, 4, 4, '', [
new HunkLine('line-4', 'unchanged', 7, 7),
new HunkLine('line-7', 'deleted', 8, -1),
new HunkLine('line-8', 'deleted', 9, -1),
new HunkLine('line-5', 'added', -1, 8),
new HunkLine('line-6', 'added', -1, 9),
new HunkLine('line-9', 'unchanged', 10, 10),
]),
new Hunk(19, 21, 2, 2, '', [
new HunkLine('line-13', 'deleted', 19, -1),
new HunkLine('line-12', 'added', -1, 21),
new HunkLine('line-14', 'unchanged', 20, 22),
new HunkLine('No newline at end of file', 'nonewline', -1, -1),
]),
],
));
});
describe('unstaging lines from an added file', function() {
it('handles unstaging part of the file', function() {
const filePatch = createFilePatch(null, 'a.txt', 'added', [
new Hunk(0, 1, 0, 3, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-2', 'added', -1, 2),
new HunkLine('line-3', 'added', -1, 3),
]),
]);
const linesFromHunk = filePatch.getHunks()[0].getLines().slice(0, 2);
assert.deepEqual(filePatch.getUnstagePatchForLines(new Set(linesFromHunk)), createFilePatch(
'a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 3, 1, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'unchanged', 3, 1),
]),
],
));
});
it('handles unstaging all lines, leaving nothign staged', function() {
const filePatch = createFilePatch(null, 'a.txt', 'added', [
new Hunk(0, 1, 0, 3, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-2', 'added', -1, 2),
new HunkLine('line-3', 'added', -1, 3),
]),
]);
const linesFromHunk = filePatch.getHunks()[0].getLines();
assert.deepEqual(filePatch.getUnstagePatchForLines(new Set(linesFromHunk)), createFilePatch(
'a.txt', null, 'deleted', [
new Hunk(1, 0, 3, 0, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'deleted', 3, -1),
]),
],
));
});
});
});
it('handles newly added files', function() {
const filePatch = createFilePatch(null, 'a.txt', 'added', [
new Hunk(0, 1, 0, 3, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-2', 'added', -1, 2),
new HunkLine('line-3', 'added', -1, 3),
]),
]);
const linesFromHunk = filePatch.getHunks()[0].getLines().slice(0, 2);
assert.deepEqual(filePatch.getUnstagePatchForLines(new Set(linesFromHunk)), createFilePatch(
'a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 3, 1, '', [
new HunkLine('line-1', 'deleted', 1, -1),
new HunkLine('line-2', 'deleted', 2, -1),
new HunkLine('line-3', 'unchanged', 3, 1),
]),
],
));
});
describe('toString()', function() {
it('converts the patch to the standard textual format', async function() {
const workdirPath = await cloneRepository('multi-line-file');
const repository = await buildRepository(workdirPath);
const lines = fs.readFileSync(path.join(workdirPath, 'sample.js'), 'utf8').split('\n');
lines[0] = 'this is a modified line';
lines.splice(1, 0, 'this is a new line');
lines[11] = 'this is a modified line';
lines.splice(12, 1);
fs.writeFileSync(path.join(workdirPath, 'sample.js'), lines.join('\n'));
const patch = await repository.getFilePatchForPath('sample.js');
assert.equal(patch.toString(), dedent`
diff --git a/sample.js b/sample.js
--- a/sample.js
+++ b/sample.js
@@ -1,4 +1,5 @@
-var quicksort = function () {
+this is a modified line
+this is a new line
var sort = function(items) {
if (items.length <= 1) return items;
var pivot = items.shift(), current, left = [], right = [];
@@ -8,6 +9,5 @@
}
return sort(left).concat(pivot).concat(sort(right));
};
-
- return sort(Array.apply(this, arguments));
+this is a modified line
};
`);
});
it('correctly formats new files with no newline at the end', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = await buildRepository(workingDirPath);
fs.writeFileSync(path.join(workingDirPath, 'e.txt'), 'qux', 'utf8');
const patch = await repo.getFilePatchForPath('e.txt');
assert.equal(patch.toString(), dedent`
diff --git a/e.txt b/e.txt
new file mode 100644
--- /dev/null
+++ b/e.txt
@@ -0,0 +1,1 @@
+qux
\\ No newline at end of file
`);
});
describe('typechange file patches', function() {
it('handles typechange patches for a symlink replaced with a file', async function() {
const workdirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workdirPath);
await repository.git.exec(['config', 'core.symlinks', 'true']);
const deletedSymlinkAddedFilePath = 'symlink.txt';
fs.unlinkSync(path.join(workdirPath, deletedSymlinkAddedFilePath));
fs.writeFileSync(path.join(workdirPath, deletedSymlinkAddedFilePath), 'qux\nfoo\nbar\n', 'utf8');
const patch = await repository.getFilePatchForPath(deletedSymlinkAddedFilePath);
assert.equal(patch.toString(), dedent`
diff --git a/symlink.txt b/symlink.txt
deleted file mode 120000
--- a/symlink.txt
+++ /dev/null
@@ -1 +0,0 @@
-./regular-file.txt
\\ No newline at end of file
diff --git a/symlink.txt b/symlink.txt
new file mode 100644
--- /dev/null
+++ b/symlink.txt
@@ -0,0 +1,3 @@
+qux
+foo
+bar
`);
});
it('handles typechange patches for a file replaced with a symlink', async function() {
const workdirPath = await cloneRepository('symlinks');
const repository = await buildRepository(workdirPath);
const deletedFileAddedSymlinkPath = 'a.txt';
fs.unlinkSync(path.join(workdirPath, deletedFileAddedSymlinkPath));
fs.symlinkSync(path.join(workdirPath, 'regular-file.txt'), path.join(workdirPath, deletedFileAddedSymlinkPath));
const patch = await repository.getFilePatchForPath(deletedFileAddedSymlinkPath);
assert.equal(patch.toString(), dedent`
diff --git a/a.txt b/a.txt
deleted file mode 100644
--- a/a.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-foo
-bar
-baz
-
diff --git a/a.txt b/a.txt
new file mode 120000
--- /dev/null
+++ b/a.txt
@@ -0,0 +1 @@
+${toGitPathSep(path.join(workdirPath, 'regular-file.txt'))}
\\ No newline at end of file
`);
});
});
});
describe('getHeaderString()', function() {
it('formats paths with git path separators', function() {
const oldPath = path.join('foo', 'bar', 'old.js');
const newPath = path.join('baz', 'qux', 'new.js');
const patch = createFilePatch(oldPath, newPath, 'modified', []);
assert.equal(patch.getHeaderString(), dedent`
diff --git a/foo/bar/old.js b/baz/qux/new.js
--- a/foo/bar/old.js
+++ b/baz/qux/new.js
`);
});
});
it('returns the size in bytes from getByteSize()', function() {
const filePatch = createFilePatch('a.txt', 'a.txt', 'modified', [
new Hunk(1, 1, 1, 3, '', [
new HunkLine('line-1', 'added', -1, 1),
new HunkLine('line-2', 'added', -1, 2),
new HunkLine('line-3', 'unchanged', 1, 3),
]),
new Hunk(5, 7, 5, 4, '', [
new HunkLine('line-4', 'unchanged', 5, 7),
new HunkLine('line-5', 'deleted', 6, -1),
new HunkLine('line-6', 'deleted', 7, -1),
]),
]);
assert.strictEqual(filePatch.getByteSize(), 36);
});
});