forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-patch.test.js
More file actions
233 lines (215 loc) · 9.33 KB
/
Copy pathfile-patch.test.js
File metadata and controls
233 lines (215 loc) · 9.33 KB
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
/** @babel */
import {copyRepositoryDir, buildRepository} from '../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'
describe('FilePatch', () => {
describe('getId()', () => {
it('returns a logical identifier for the FilePatch', () => {
assert.equal(new FilePatch('a.txt', 'b.txt', 1234, 1234, 'renamed').getId(), 'a/a.txt b/b.txt')
assert.equal(new FilePatch('a.txt', 'a.txt', 1234, 1234, 'modified').getId(), 'a/a.txt b/a.txt')
assert.equal(new FilePatch(null, 'a.txt', 0, 1234, 'added').getId(), 'a/null b/a.txt')
assert.equal(new FilePatch('a.txt', null, 1234, 0, 'removed').getId(), 'a/a.txt b/null')
})
})
describe('update(filePatch)', () => {
it('mutates the FilePatch to match the given FilePatch, preserving hunk and line instances where possible', () => {
const hunks = [
new Hunk(1, 1, 1, 3, [
new HunkLine('line-1\n', 'added', -1, 1),
new HunkLine('line-2\n', 'added', -1, 2),
new HunkLine('line-3\n', 'unchanged', 1, 3)
]),
new Hunk(5, 7, 5, 4, [
new HunkLine('line-4\n', 'unchanged', 5, 7),
new HunkLine('line-5\n', 'removed', 6, -1),
new HunkLine('line-6\n', 'removed', 7, -1),
new HunkLine('line-7\n', 'added', -1, 8),
new HunkLine('line-8\n', 'added', -1, 9),
new HunkLine('line-9\n', 'added', -1, 10),
new HunkLine('line-10\n', 'removed', 8, -1),
new HunkLine('line-11\n', 'removed', 9, -1)
]),
new Hunk(20, 19, 2, 2, [
new HunkLine('line-12\n', 'removed', 20, -1),
new HunkLine('line-13\n', 'added', -1, 19),
new HunkLine('line-14\n', 'unchanged', 21, 20)
])
]
const patch = new FilePatch('a.txt', 'b.txt', 1234, 1234, 'renamed', hunks)
const newPatch = new FilePatch('a.txt', 'b.txt', 1234, 1234, 'renamed', [
new Hunk(9, 9, 2, 1, [
new HunkLine('line-9\n', 'added', -1, 9),
new HunkLine('line-10\n', 'removed', 8, -1),
new HunkLine('line-11\n', 'removed', 9, -1)
]),
new Hunk(15, 14, 1, 1, [
new HunkLine('line-15\n', 'removed', 15, -1),
new HunkLine('line-16\n', 'added', -1, 14)
]),
new Hunk(21, 19, 2, 3, [
new HunkLine('line-13\n', 'added', -1, 19),
new HunkLine('line-14\n', 'unchanged', 21, 20),
new HunkLine('line-17\n', 'unchanged', 22, 21)
])
])
const originalHunks = hunks.slice()
const originalLines = originalHunks.map(h => h.getLines().slice())
patch.update(newPatch)
const newHunks = patch.getHunks()
assert.deepEqual(patch, newPatch)
assert.equal(newHunks.length, 3)
assert.equal(newHunks.indexOf(originalHunks[0]), -1)
assert.equal(newHunks.indexOf(originalHunks[1]), 0)
assert.equal(newHunks.indexOf(originalHunks[2]), 2)
assert.equal(newHunks[0].getLines().indexOf(originalLines[1][0]), -1)
assert.equal(newHunks[0].getLines().indexOf(originalLines[1][5]), 0)
assert.equal(newHunks[0].getLines().indexOf(originalLines[1][7]), 2)
assert.equal(newHunks[2].getLines().indexOf(originalLines[1][0]), -1)
assert.equal(newHunks[2].getLines().indexOf(originalLines[2][1]), 0)
assert.equal(newHunks[2].getLines().indexOf(originalLines[2][2]), 1)
})
it('throws an error when the supplied filePatch has a different id', () => {
const patch = new FilePatch('a.txt', 'b.txt', 1234, 1234, 'renamed')
assert.throws(() => patch.update(new FilePatch('c.txt', 'd.txt', 1234, 1234, 'renamed')))
})
})
describe('getStagePatchForLines()', () => {
it('returns a new FilePatch that applies only the specified lines', () => {
const filePatch = new FilePatch('a.txt', 'a.txt', 1234, 1234, 'modified', [
new Hunk(1, 1, 1, 3, [
new HunkLine('line-1\n', 'added', -1, 1),
new HunkLine('line-2\n', 'added', -1, 2),
new HunkLine('line-3\n', 'unchanged', 1, 3)
]),
new Hunk(5, 7, 5, 4, [
new HunkLine('line-4\n', 'unchanged', 5, 7),
new HunkLine('line-5\n', 'removed', 6, -1),
new HunkLine('line-6\n', 'removed', 7, -1),
new HunkLine('line-7\n', 'added', -1, 8),
new HunkLine('line-8\n', 'added', -1, 9),
new HunkLine('line-9\n', 'added', -1, 10),
new HunkLine('line-10\n', 'removed', 8, -1),
new HunkLine('line-11\n', 'removed', 9, -1)
]),
new Hunk(20, 19, 2, 2, [
new HunkLine('line-12\n', 'removed', 20, -1),
new HunkLine('line-13\n', 'added', -1, 19),
new HunkLine('line-14\n', 'unchanged', 21, 20)
])
])
const lines = new Set(filePatch.getHunks()[1].getLines().slice(1, 4))
assert.deepEqual(filePatch.getStagePatchForLines(lines), new FilePatch(
'a.txt', 'a.txt', 1234, 1234, 'modified', [
new Hunk(1, 1, 1, 1, [
new HunkLine('line-3\n', 'unchanged', 1, 1)
]),
new Hunk(5, 5, 5, 4, [
new HunkLine('line-4\n', 'unchanged', 5, 5),
new HunkLine('line-5\n', 'removed', 6, -1),
new HunkLine('line-6\n', 'removed', 7, -1),
new HunkLine('line-7\n', 'added', -1, 6),
new HunkLine('line-10\n', 'unchanged', 8, 7),
new HunkLine('line-11\n', 'unchanged', 9, 8)
]),
new Hunk(20, 17, 2, 2, [
new HunkLine('line-12\n', 'unchanged', 20, 17),
new HunkLine('line-14\n', 'unchanged', 21, 18)
])
]
))
})
})
describe('getUnstagePatchForLines()', () => {
it('returns a new FilePatch that applies only the specified lines', () => {
const filePatch = new FilePatch('a.txt', 'a.txt', 1234, 1234, 'modified', [
new Hunk(1, 1, 1, 3, [
new HunkLine('line-1\n', 'added', -1, 1),
new HunkLine('line-2\n', 'added', -1, 2),
new HunkLine('line-3\n', 'unchanged', 1, 3)
]),
new Hunk(5, 7, 5, 4, [
new HunkLine('line-4\n', 'unchanged', 5, 7),
new HunkLine('line-5\n', 'removed', 6, -1),
new HunkLine('line-6\n', 'removed', 7, -1),
new HunkLine('line-7\n', 'added', -1, 8),
new HunkLine('line-8\n', 'added', -1, 9),
new HunkLine('line-9\n', 'added', -1, 10),
new HunkLine('line-10\n', 'removed', 8, -1),
new HunkLine('line-11\n', 'removed', 9, -1)
]),
new Hunk(20, 19, 2, 2, [
new HunkLine('line-12\n', 'removed', 20, -1),
new HunkLine('line-13\n', 'added', -1, 19),
new HunkLine('line-14\n', 'unchanged', 21, 20)
])
])
const lines = new Set(filePatch.getHunks()[1].getLines().slice(1, 5))
assert.deepEqual(filePatch.getUnstagePatchForLines(lines), new FilePatch(
'a.txt', 'a.txt', 1234, 1234, 'modified', [
new Hunk(1, 1, 3, 3, [
new HunkLine('line-1\n', 'unchanged', 1, 1),
new HunkLine('line-2\n', 'unchanged', 2, 2),
new HunkLine('line-3\n', 'unchanged', 3, 3)
]),
new Hunk(7, 7, 4, 4, [
new HunkLine('line-4\n', 'unchanged', 7, 7),
new HunkLine('line-7\n', 'removed', 8, -1),
new HunkLine('line-8\n', 'removed', 9, -1),
new HunkLine('line-5\n', 'added', -1, 8),
new HunkLine('line-6\n', 'added', -1, 9),
new HunkLine('line-9\n', 'unchanged', 10, 10)
]),
new Hunk(19, 21, 2, 2, [
new HunkLine('line-13\n', 'unchanged', 19, 21),
new HunkLine('line-14\n', 'unchanged', 20, 22)
])
]
))
})
})
describe('toString()', () => {
it('converts the patch to the standard textual format', async () => {
const workdirPath = copyRepositoryDir(2)
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.getUnstagedChanges()
assert.equal(patch.toString(), dedent`
@@ -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 () => {
const workingDirPath = copyRepositoryDir(1)
const repo = await buildRepository(workingDirPath)
fs.writeFileSync(path.join(workingDirPath, 'e.txt'), 'qux', 'utf8')
const [patch] = await repo.getUnstagedChanges()
assert.equal(patch.toString(), dedent`
@@ -0,0 +1,1 @@
+qux
\\ No newline at end of file
`)
})
})
})