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
178 lines (159 loc) · 7.08 KB
/
Copy pathdiff.test.ts
File metadata and controls
178 lines (159 loc) · 7.08 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
import * as assert from 'assert';
import { parseDiffHunk, DiffHunk, getModifiedContentFromDiffHunk } from '../../common/diffHunk';
import { DiffLine, DiffChangeType } from '../../common/diffHunk';
import { getDiffLineByPosition, mapHeadLineToDiffHunkPosition, mapCommentsToHead } 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', () => {
let diffHunkReader = parseDiffHunk(diff_hunk_0);
let diffHunkIter = diffHunkReader.next();
let diffHunk = diffHunkIter.value;
assert.equal(diffHunk.diffLines.length, 9);
assert.deepEqual(diffHunk.diffLines[0], new DiffLine(DiffChangeType.Control, -1, -1, 0, `@@ -1,5 +1,6 @@`));
assert.deepEqual(diffHunk.diffLines[1], new DiffLine(DiffChangeType.Context, 1, 1, 1, ` {`));
assert.deepEqual(diffHunk.diffLines[2], new DiffLine(DiffChangeType.Context, 2, 2, 2, ` "appService.zipIgnorePattern": [`));
assert.deepEqual(diffHunk.diffLines[3], new DiffLine(DiffChangeType.Context, 3, 3, 3, ` "node_modules{,/**}"`));
assert.deepEqual(diffHunk.diffLines[4], new DiffLine(DiffChangeType.Delete, 4, -1, 4, `- ]`));
assert.deepEqual(diffHunk.diffLines[5], new DiffLine(DiffChangeType.Delete, 5, -1, 5, `-}`, false));
assert.deepEqual(diffHunk.diffLines[6], new DiffLine(DiffChangeType.Add, -1, 4, 7, `+ ],`));
assert.deepEqual(diffHunk.diffLines[7], new DiffLine(DiffChangeType.Add, -1, 5, 8, `+ "editor.insertSpaces": false`));
assert.deepEqual(diffHunk.diffLines[8], new DiffLine(DiffChangeType.Add, -1, 6, 9, `+}`));
});
it('getDiffLineByPosition', () => {
let diffHunkReader = parseDiffHunk(diff_hunk_0);
let diffHunkIter = diffHunkReader.next();
let diffHunk = diffHunkIter.value;
for (let i = 0; i < diffHunk.diffLines.length; i++) {
let diffLine = diffHunk.diffLines[i];
assert.deepEqual(getDiffLineByPosition([diffHunk], diffLine.positionInHunk), diffLine, `diff line ${i}`);
}
});
it('mapHeadLineToDiffHunkPosition', () => {
let diffHunkReader = parseDiffHunk(diff_hunk_0);
let diffHunkIter = diffHunkReader.next();
let diffHunk = diffHunkIter.value;
for (let i = 0; i < diffHunk.diffLines.length; i++) {
let diffLine = diffHunk.diffLines[i];
switch (diffLine.type) {
case DiffChangeType.Delete:
assert.equal(mapHeadLineToDiffHunkPosition([diffHunk], '', diffLine.oldLineNumber, true), diffLine.positionInHunk);
break;
case DiffChangeType.Add:
assert.equal(mapHeadLineToDiffHunkPosition([diffHunk], '', diffLine.newLineNumber, false), diffLine.positionInHunk);
break;
case DiffChangeType.Context:
assert.equal(mapHeadLineToDiffHunkPosition([diffHunk], '', diffLine.oldLineNumber, true), diffLine.positionInHunk);
assert.equal(mapHeadLineToDiffHunkPosition([diffHunk], '', diffLine.newLineNumber, false), diffLine.positionInHunk);
break;
default:
break;
}
}
});
it('#239. Diff hunk parsing fails when line count for added content is omitted', () => {
let diffHunkReader = parseDiffHunk('@@ -0,0 +1 @@');
let diffHunkIter = diffHunkReader.next();
let diffHunk = diffHunkIter.value;
assert.equal(diffHunk.diffLines.length, 1);
});
it('', () => {
let diffHunkReader = parseDiffHunk(`@@ -1 +1,5 @@
# README
+
+This is my readme
+
+Another line"`);
let diffHunkIter = diffHunkReader.next();
let diffHunk = diffHunkIter.value;
assert.equal(diffHunk.diffLines.length, 5);
});
describe('mapCommentsToHead', () => {
it('should handle comments that are on a deleted diff line', () => {
const comments = [{
position: 66
}];
const diffHunk = new DiffHunk(481, 16, 489, 10, 54);
diffHunk.diffLines.push(new DiffLine(DiffChangeType.Delete, 489, -1, 66, '- this.editorBlurTimeout.cancelAndSet(() => {'));
const mappedComments = mapCommentsToHead([diffHunk], '', comments as any);
assert(mappedComments.length === 1);
console.log(mappedComments[0].absolutePosition);
assert.equal(mappedComments[0].absolutePosition, 489);
});
});
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.equal(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`);
let expectedModifiedContent = lines.join('\n');
const modifiedContent = getModifiedContentFromDiffHunk(originalContent, patch);
assert.equal(modifiedContent, expectedModifiedContent);
});
});
});