forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
280 lines (221 loc) · 11.6 KB
/
Copy pathutils.test.ts
File metadata and controls
280 lines (221 loc) · 11.6 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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import * as utils from '../../common/utils';
import { EventEmitter } from 'vscode';
import * as timers from 'timers';
describe('utils', () => {
class HookError extends Error {
public errors: any[];
constructor(message: string, errors: any[]) {
super(message);
this.errors = errors;
}
}
describe('formatError', () => {
it('should format a normal error', () => {
const error = new Error('No!');
assert.strictEqual(utils.formatError(error), 'No!');
});
it('should format an error with submessages', () => {
const error = new HookError('Validation Failed', [
{ message: 'user_id can only have one pending review per pull request' },
]);
assert.strictEqual(utils.formatError(error), 'user_id can only have one pending review per pull request');
});
it('should not format when error message contains all information', () => {
const error = new HookError('Validation Failed: Some Validation error', []);
assert.strictEqual(utils.formatError(error), 'Validation Failed: Some Validation error');
});
it('should format an error with submessages that are strings', () => {
const error = new HookError('Validation Failed', ['Can not approve your own pull request']);
assert.strictEqual(utils.formatError(error), 'Can not approve your own pull request');
});
it('should format an error with field errors', () => {
const error = new HookError('Validation Failed', [{ field: 'title', value: 'garbage', status: 'custom' }]);
assert.strictEqual(utils.formatError(error), 'Validation Failed: Value "garbage" cannot be set for field title (code: custom)');
});
it('should format an error with custom ', () => {
const error = new HookError('Validation Failed', [{ message: 'Cannot push to this repo', status: 'custom' }]);
assert.strictEqual(utils.formatError(error), 'Cannot push to this repo');
});
});
describe('processPermalinks', () => {
const repoName = 'vscode';
const authority = 'github.com';
const sha = 'a'.repeat(40);
function makePermalink(filePath: string, startLine: number, endLine?: number): string {
const lineRef = endLine ? `#L${startLine}-L${endLine}` : `#L${startLine}`;
return `<a href="https://github.com/microsoft/vscode/blob/${sha}/${filePath}${lineRef}">link text</a>`;
}
it('should add data attributes when file exists locally', async () => {
const html = makePermalink('src/file.ts', 10);
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert(result.includes('data-local-file="src/file.ts"'));
assert(result.includes('data-start-line="10"'));
assert(result.includes('data-end-line="10"'));
assert(result.includes('data-link-type="blob"'));
assert(result.includes('data-permalink-processed="true"'));
assert(result.includes('view on GitHub'));
});
it('should set end line when range is specified', async () => {
const html = makePermalink('src/file.ts', 10, 20);
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert(result.includes('data-start-line="10"'));
assert(result.includes('data-end-line="20"'));
});
it('should not modify links when file does not exist locally', async () => {
const html = makePermalink('src/file.ts', 10);
const result = await utils.processPermalinks(html, repoName, authority, async () => false);
assert.strictEqual(result, html);
});
it('should not modify non-permalink links', async () => {
const html = '<a href="https://example.com">example</a>';
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert.strictEqual(result, html);
});
it('should not modify links to a different repo', async () => {
const html = `<a href="https://github.com/other/repo/blob/${sha}/src/file.ts#L10">link</a>`;
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert.strictEqual(result, html);
});
it('should skip already processed links', async () => {
const html = `<a data-permalink-processed="true" href="https://github.com/microsoft/vscode/blob/${sha}/src/file.ts#L10">link</a>`;
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert.strictEqual(result, html);
});
it('should process multiple links independently', async () => {
const html = makePermalink('src/exists.ts', 1) + makePermalink('src/missing.ts', 2);
const result = await utils.processPermalinks(html, repoName, authority, async (path) => path === 'src/exists.ts');
assert(result.includes('data-local-file="src/exists.ts"'));
assert(!result.includes('data-local-file="src/missing.ts"'));
});
it('should return original HTML when fileExistsCheck throws', async () => {
const html = makePermalink('src/file.ts', 10);
const result = await utils.processPermalinks(html, repoName, authority, async () => { throw new Error('fail'); });
assert.strictEqual(result, html);
});
it('should handle links without surrounding text', async () => {
const html = makePermalink('src/file.ts', 5);
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert(result.includes('link text'));
assert(result.includes('data-local-file="src/file.ts"'));
});
it('should escape HTML special characters in file paths', async () => {
const html = makePermalink('src/file&name-test.ts', 10);
const result = await utils.processPermalinks(html, repoName, authority, async () => true);
assert(result.includes('data-local-file="src/file&name-test.ts"'));
assert(!result.includes('data-local-file="src/file&name-test.ts"'));
});
});
describe('processDiffLinks', () => {
const repoOwner = 'microsoft';
const repoName = 'vscode';
const authority = 'github.com';
const prNumber = 123;
const diffHash = 'a'.repeat(64);
function makeDiffLink(hash: string, startLine?: number, endLine?: number, variant: 'files' | 'changes' = 'files'): string {
let fragment = `diff-${hash}`;
if (startLine !== undefined) {
fragment += `R${startLine}`;
if (endLine !== undefined) {
fragment += `-R${endLine}`;
}
}
return `<a href="https://github.com/microsoft/vscode/pull/${prNumber}/${variant}#${fragment}">link text</a>`;
}
it('should add data attributes when hash maps to a file', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = makeDiffLink(diffHash, 10);
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert(result.includes('data-local-file="src/file.ts"'));
assert(result.includes('data-start-line="10"'));
assert(result.includes('data-end-line="10"'));
assert(result.includes('data-link-type="diff"'));
assert(result.includes('data-permalink-processed="true"'));
assert(result.includes('view on GitHub'));
});
it('should set end line when range is specified', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = makeDiffLink(diffHash, 10, 20);
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert(result.includes('data-start-line="10"'));
assert(result.includes('data-end-line="20"'));
});
it('should default start line to 1 when no line is specified', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = makeDiffLink(diffHash);
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert(result.includes('data-start-line="1"'));
assert(result.includes('data-end-line="1"'));
});
it('should not modify links when hash is not in the map', async () => {
const hashMap: Record<string, string> = {};
const html = makeDiffLink(diffHash, 10);
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert.strictEqual(result, html);
});
it('should not modify non-diff links', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = '<a href="https://example.com">example</a>';
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert.strictEqual(result, html);
});
it('should not modify links to a different repo', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = `<a href="https://github.com/other/repo/pull/${prNumber}/files#diff-${diffHash}R10">link</a>`;
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert.strictEqual(result, html);
});
it('should skip already processed links', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = `<a data-permalink-processed="true" href="https://github.com/microsoft/vscode/pull/${prNumber}/files#diff-${diffHash}R10">link</a>`;
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert.strictEqual(result, html);
});
it('should match links using changes variant', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file.ts' };
const html = makeDiffLink(diffHash, 5, undefined, 'changes');
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert(result.includes('data-local-file="src/file.ts"'));
assert(result.includes('data-start-line="5"'));
});
it('should process multiple links independently', async () => {
const otherHash = 'b'.repeat(64);
const hashMap: Record<string, string> = { [diffHash]: 'src/found.ts' };
const html = makeDiffLink(diffHash, 1) + makeDiffLink(otherHash, 2);
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert(result.includes('data-local-file="src/found.ts"'));
assert(!result.includes('data-local-file="src/other.ts"'));
});
it('should escape HTML special characters in file names', async () => {
const hashMap: Record<string, string> = { [diffHash]: 'src/file&name"test.ts' };
const html = makeDiffLink(diffHash, 10);
const result = await utils.processDiffLinks(html, repoOwner, repoName, authority, hashMap, prNumber);
assert(result.includes('data-local-file="src/file&name"test.ts"'));
assert(!result.includes('data-local-file="src/file&name"test.ts"'));
});
});
describe('escapeHtmlAttr', () => {
it('should escape ampersands', () => {
assert.strictEqual(utils.escapeHtmlAttr('foo&bar'), 'foo&bar');
});
it('should escape double quotes', () => {
assert.strictEqual(utils.escapeHtmlAttr('foo"bar'), 'foo"bar');
});
it('should escape single quotes', () => {
assert.strictEqual(utils.escapeHtmlAttr('foo\'bar'), 'foo'bar');
});
it('should escape angle brackets', () => {
assert.strictEqual(utils.escapeHtmlAttr('<script>'), '<script>');
});
it('should escape all special characters together', () => {
assert.strictEqual(utils.escapeHtmlAttr('a&b"c\'d<e>f'), 'a&b"c'd<e>f');
});
it('should return the same string when no special characters', () => {
assert.strictEqual(utils.escapeHtmlAttr('normal-file.ts'), 'normal-file.ts');
});
});
});