forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.test.ts
More file actions
232 lines (216 loc) · 8.74 KB
/
Copy pathdiff.test.ts
File metadata and controls
232 lines (216 loc) · 8.74 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
import { default as assert } from 'assert';
import { parseDiffHunk, DiffHunk, getModifiedContentFromDiffHunk } from '../../common/diffHunk';
import { DiffLine, DiffChangeType } from '../../common/diffHunk';
import {
getDiffLineByPosition,
} from '../../common/diffPositionMapping';
const diff_hunk_0 = [
`@@ -1,5 +1,6 @@`,
` {`,
` "appService.zipIgnorePattern": [`,
` "node_modules{,/**}"`,
`- ]`,
`-}`,
`\\ No newline at end of file`,
`+ ],`,
`+ "editor.insertSpaces": false`,
`+}`,
].join('\n');
describe('diff hunk parsing', () => {
it('diffhunk iterator', () => {
const diffHunkReader = parseDiffHunk(diff_hunk_0);
const diffHunkIter = diffHunkReader.next();
const diffHunk = diffHunkIter.value;
assert.strictEqual(diffHunk.diffLines.length, 9);
assert.deepStrictEqual(diffHunk.diffLines[0], new DiffLine(DiffChangeType.Control, -1, -1, 0, `@@ -1,5 +1,6 @@`));
assert.deepStrictEqual(diffHunk.diffLines[1], new DiffLine(DiffChangeType.Context, 1, 1, 1, ` {`));
assert.deepStrictEqual(
diffHunk.diffLines[2],
new DiffLine(DiffChangeType.Context, 2, 2, 2, ` "appService.zipIgnorePattern": [`),
);
assert.deepStrictEqual(
diffHunk.diffLines[3],
new DiffLine(DiffChangeType.Context, 3, 3, 3, ` "node_modules{,/**}"`),
);
assert.deepStrictEqual(diffHunk.diffLines[4], new DiffLine(DiffChangeType.Delete, 4, -1, 4, `- ]`));
assert.deepStrictEqual(diffHunk.diffLines[5], new DiffLine(DiffChangeType.Delete, 5, -1, 5, `-}`, false));
assert.deepStrictEqual(diffHunk.diffLines[6], new DiffLine(DiffChangeType.Add, -1, 4, 7, `+ ],`));
assert.deepStrictEqual(
diffHunk.diffLines[7],
new DiffLine(DiffChangeType.Add, -1, 5, 8, `+ "editor.insertSpaces": false`),
);
assert.deepStrictEqual(diffHunk.diffLines[8], new DiffLine(DiffChangeType.Add, -1, 6, 9, `+}`));
});
// #GH-2000
it('should handle parsing diffs of diff patches', () => {
const diffDiffHunk = [
'@@ -4,9 +4,9 @@ https://bugs.python.org/issue24844',
" Compiling python fails in Xcode 4 (clang < 3.3) where existence of 'atomic'",
' is detected by configure, but it is not fully functional.',
' ',
'---- configure.orig 2019-12-21 15:43:09.000000000 -0500',
'-+++ configure 2019-12-21 15:45:31.000000000 -0500',
'-@@ -16791,6 +16791,24 @@',
'+--- configure.orig 2020-07-13 07:11:53.000000000 -0500',
'++++ configure 2020-07-15 10:20:09.000000000 -0500',
'+@@ -16837,6 +16837,24 @@',
' volatile int val = 1;',
' int main() {',
" __atomic_load_n(&val, __ATOMIC_SEQ_CST);'",
].join('\n');
const diffHunkReader = parseDiffHunk(diffDiffHunk);
const diffHunkIter = diffHunkReader.next();
const diffHunk = diffHunkIter.value;
assert.strictEqual(diffHunk.diffLines.length, 13);
assert.strictEqual(diffHunk.newLength, 9);
assert.strictEqual(diffHunk.newLineNumber, 4);
assert.strictEqual(diffHunk.oldLength, 9);
assert.strictEqual(diffHunk.oldLineNumber, 4);
assert.deepStrictEqual(
diffHunk.diffLines[0],
new DiffLine(DiffChangeType.Control, -1, -1, 0, '@@ -4,9 +4,9 @@ https://bugs.python.org/issue24844'),
);
assert.deepStrictEqual(
diffHunk.diffLines[1],
new DiffLine(
DiffChangeType.Context,
4,
4,
1,
" Compiling python fails in Xcode 4 (clang < 3.3) where existence of 'atomic'",
),
);
assert.deepStrictEqual(
diffHunk.diffLines[2],
new DiffLine(DiffChangeType.Context, 5, 5, 2, ' is detected by configure, but it is not fully functional.'),
);
assert.deepStrictEqual(diffHunk.diffLines[3], new DiffLine(DiffChangeType.Context, 6, 6, 3, ' '));
assert.deepStrictEqual(
diffHunk.diffLines[4],
new DiffLine(DiffChangeType.Delete, 7, -1, 4, '---- configure.orig\t2019-12-21 15:43:09.000000000 -0500'),
);
assert.deepStrictEqual(
diffHunk.diffLines[5],
new DiffLine(DiffChangeType.Delete, 8, -1, 5, '-+++ configure\t2019-12-21 15:45:31.000000000 -0500'),
);
assert.deepStrictEqual(
diffHunk.diffLines[6],
new DiffLine(DiffChangeType.Delete, 9, -1, 6, '-@@ -16791,6 +16791,24 @@'),
);
assert.deepStrictEqual(
diffHunk.diffLines[7],
new DiffLine(DiffChangeType.Add, -1, 7, 7, '+--- configure.orig\t2020-07-13 07:11:53.000000000 -0500'),
);
assert.deepStrictEqual(
diffHunk.diffLines[8],
new DiffLine(DiffChangeType.Add, -1, 8, 8, '++++ configure\t2020-07-15 10:20:09.000000000 -0500'),
);
assert.deepStrictEqual(
diffHunk.diffLines[9],
new DiffLine(DiffChangeType.Add, -1, 9, 9, '+@@ -16837,6 +16837,24 @@'),
);
assert.deepStrictEqual(
diffHunk.diffLines[10],
new DiffLine(DiffChangeType.Context, 10, 10, 10, ' volatile int val = 1;'),
);
assert.deepStrictEqual(
diffHunk.diffLines[11],
new DiffLine(DiffChangeType.Context, 11, 11, 11, ' int main() {'),
);
assert.deepStrictEqual(
diffHunk.diffLines[12],
new DiffLine(DiffChangeType.Context, 12, 12, 12, " __atomic_load_n(&val, __ATOMIC_SEQ_CST);'"),
);
});
it('getDiffLineByPosition', () => {
const diffHunkReader = parseDiffHunk(diff_hunk_0);
const diffHunkIter = diffHunkReader.next();
const diffHunk = diffHunkIter.value;
for (let i = 0; i < diffHunk.diffLines.length; i++) {
const diffLine = diffHunk.diffLines[i];
assert.deepStrictEqual(getDiffLineByPosition([diffHunk], diffLine.positionInHunk), diffLine, `diff line ${i}`);
}
});
it('#239. Diff hunk parsing fails when line count for added content is omitted', () => {
const diffHunkReader = parseDiffHunk('@@ -0,0 +1 @@');
const diffHunkIter = diffHunkReader.next();
const diffHunk = diffHunkIter.value;
assert.strictEqual(diffHunk.diffLines.length, 1);
});
it('', () => {
const diffHunkReader = parseDiffHunk(`@@ -1 +1,5 @@
# README
+
+This is my readme
+
+Another line"`);
const diffHunkIter = diffHunkReader.next();
const diffHunk = diffHunkIter.value;
assert.strictEqual(diffHunk.diffLines.length, 5);
});
describe('getModifiedContentFromDiffHunk', () => {
const originalContent = [
`/*---------------------------------------------------------------------------------------------`,
`* Copyright (c) Microsoft Corporation. All rights reserved.`,
`* Licensed under the MIT License. See License.txt in the project root for license information.`,
`*--------------------------------------------------------------------------------------------*/`,
``,
`'use strict';`,
``,
`import { window, commands, ExtensionContext } from 'vscode';`,
`import { showQuickPick, showInputBox } from './basicInput';`,
`import { multiStepInput } from './multiStepInput';`,
`import { quickOpen } from './quickOpen';`,
``,
`export function activate(context: ExtensionContext) {`,
` context.subscriptions.push(commands.registerCommand('samples.quickInput', async () => {`,
` const options: { [key: string]: (context: ExtensionContext) => Promise<void> } = {`,
` showQuickPick,`,
` showInputBox,`,
` multiStepInput,`,
` quickOpen,`,
` };`,
` const quickPick = window.createQuickPick();`,
` quickPick.items = Object.keys(options).map(label => ({ label }));`,
` quickPick.onDidChangeSelection(selection => {`,
` if (selection[0]) {`,
` options[selection[0].label](context)`,
` .catch(console.error);`,
` }`,
` });`,
` quickPick.onDidHide(() => quickPick.dispose());`,
` quickPick.show();`,
` }));`,
`}`,
].join('\n');
it('returns the original file when there is no patch', () => {
assert.strictEqual(getModifiedContentFromDiffHunk(originalContent, ''), originalContent);
});
it('returns modified content for patch with multiple additions', () => {
const patch = [
`@@ -9,6 +9,7 @@ import { window, commands, ExtensionContext } from 'vscode';`,
` import { showQuickPick, showInputBox } from './basicInput';`,
` import { multiStepInput } from './multiStepInput';`,
` import { quickOpen } from './quickOpen';`,
`+import { promptCommand } from './promptCommandWithHistory';`,
` `,
` export function activate(context: ExtensionContext) {`,
` context.subscriptions.push(commands.registerCommand('samples.quickInput', async () => {`,
`@@ -17,6 +18,7 @@ export function activate(context: ExtensionContext) {`,
` showInputBox,`,
` multiStepInput,`,
` quickOpen,`,
`+ promptCommand`,
` };`,
` const quickPick = window.createQuickPick();`,
` quickPick.items = Object.keys(options).map(label => ({ label }));`,
].join('\n');
const lines = originalContent.split('\n');
lines.splice(11, 0, `import { promptCommand } from './promptCommandWithHistory';`);
lines.splice(20, 0, ` promptCommand`);
const expectedModifiedContent = lines.join('\n');
const modifiedContent = getModifiedContentFromDiffHunk(originalContent, patch);
assert.strictEqual(modifiedContent, expectedModifiedContent);
});
});
});