forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.test.ts
More file actions
317 lines (279 loc) · 11.1 KB
/
helpers.test.ts
File metadata and controls
317 lines (279 loc) · 11.1 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
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
import { expect } from 'chai';
import 'mocha';
import { ExtensionContext, Memento, window } from 'vscode';
import * as yaml from 'js-yaml';
import * as tmp from 'tmp';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as sinon from 'sinon';
import { getInitialQueryContents, InvocationRateLimiter, isLikelyDbLanguageFolder, showBinaryChoiceDialog, showBinaryChoiceWithUrlDialog, showInformationMessageWithAction } from '../../helpers';
import { reportStreamProgress } from '../../commandRunner';
import Sinon = require('sinon');
import { fail } from 'assert';
describe('helpers', () => {
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe('Invocation rate limiter', () => {
// 1 January 2020
let currentUnixTime = 1577836800;
function createDate(dateString?: string): Date {
if (dateString) {
return new Date(dateString);
}
const numMillisecondsPerSecond = 1000;
return new Date(currentUnixTime * numMillisecondsPerSecond);
}
function createInvocationRateLimiter<T>(funcIdentifier: string, func: () => Promise<T>): InvocationRateLimiter<T> {
return new InvocationRateLimiter(new MockExtensionContext(), funcIdentifier, func, s => createDate(s));
}
it('initially invokes function', async () => {
let numTimesFuncCalled = 0;
const invocationRateLimiter = createInvocationRateLimiter('funcid', async () => {
numTimesFuncCalled++;
});
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100);
expect(numTimesFuncCalled).to.equal(1);
});
it('doesn\'t invoke function again if no time has passed', async () => {
let numTimesFuncCalled = 0;
const invocationRateLimiter = createInvocationRateLimiter('funcid', async () => {
numTimesFuncCalled++;
});
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100);
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100);
expect(numTimesFuncCalled).to.equal(1);
});
it('doesn\'t invoke function again if requested time since last invocation hasn\'t passed', async () => {
let numTimesFuncCalled = 0;
const invocationRateLimiter = createInvocationRateLimiter('funcid', async () => {
numTimesFuncCalled++;
});
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100);
currentUnixTime += 1;
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(2);
expect(numTimesFuncCalled).to.equal(1);
});
it('invokes function again immediately if requested time since last invocation is 0 seconds', async () => {
let numTimesFuncCalled = 0;
const invocationRateLimiter = createInvocationRateLimiter('funcid', async () => {
numTimesFuncCalled++;
});
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(0);
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(0);
expect(numTimesFuncCalled).to.equal(2);
});
it('invokes function again after requested time since last invocation has elapsed', async () => {
let numTimesFuncCalled = 0;
const invocationRateLimiter = createInvocationRateLimiter('funcid', async () => {
numTimesFuncCalled++;
});
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(1);
currentUnixTime += 1;
await invocationRateLimiter.invokeFunctionIfIntervalElapsed(1);
expect(numTimesFuncCalled).to.equal(2);
});
it('invokes functions with different rate limiters', async () => {
let numTimesFuncACalled = 0;
const invocationRateLimiterA = createInvocationRateLimiter('funcid', async () => {
numTimesFuncACalled++;
});
let numTimesFuncBCalled = 0;
const invocationRateLimiterB = createInvocationRateLimiter('funcid', async () => {
numTimesFuncBCalled++;
});
await invocationRateLimiterA.invokeFunctionIfIntervalElapsed(100);
await invocationRateLimiterB.invokeFunctionIfIntervalElapsed(100);
expect(numTimesFuncACalled).to.equal(1);
expect(numTimesFuncBCalled).to.equal(1);
});
});
describe('codeql-database.yml tests', () => {
let dir: tmp.DirResult;
beforeEach(() => {
dir = tmp.dirSync();
const contents = yaml.safeDump({
primaryLanguage: 'cpp'
});
fs.writeFileSync(path.join(dir.name, 'codeql-database.yml'), contents, 'utf8');
});
afterEach(() => {
dir.removeCallback();
});
it('should get initial query contents when language is known', () => {
expect(getInitialQueryContents('cpp', 'hucairz')).to.eq('import cpp\n\nselect ""');
});
it('should get initial query contents when dbscheme is known', () => {
expect(getInitialQueryContents('', 'semmlecode.cpp.dbscheme')).to.eq('import cpp\n\nselect ""');
});
it('should get initial query contents when nothing is known', () => {
expect(getInitialQueryContents('', 'hucairz')).to.eq('select ""');
});
});
it('should find likely db language folders', () => {
expect(isLikelyDbLanguageFolder('db-javascript')).to.be.true;
expect(isLikelyDbLanguageFolder('dbnot-a-db')).to.be.false;
});
class MockExtensionContext implements ExtensionContext {
subscriptions: { dispose(): unknown }[] = [];
workspaceState: Memento = new MockMemento();
globalState: Memento = new MockMemento();
extensionPath = '';
asAbsolutePath(_relativePath: string): string {
throw new Error('Method not implemented.');
}
storagePath = '';
globalStoragePath = '';
logPath = '';
}
class MockMemento implements Memento {
map = new Map<any, any>();
/**
* Return a value.
*
* @param key A string.
* @param defaultValue A value that should be returned when there is no
* value (`undefined`) with the given key.
* @return The stored value or the defaultValue.
*/
get<T>(key: string, defaultValue?: T): T {
return this.map.has(key) ? this.map.get(key) : defaultValue;
}
/**
* Store a value. The value must be JSON-stringifyable.
*
* @param key A string.
* @param value A value. MUST not contain cyclic references.
*/
async update(key: string, value: any): Promise<void> {
this.map.set(key, value);
}
}
it('should report stream progress', () => {
const spy = sandbox.spy();
const mockReadable = {
on: sandbox.spy()
};
const max = 1024 * 1024 * 4;
const firstStep = (1024 * 1024) + (1024 * 600);
const secondStep = 1024 * 1024 * 2;
(reportStreamProgress as any)(mockReadable, 'My prefix', max, spy);
// now pretend that we have received some messages
mockReadable.on.getCall(0).args[1]({ length: firstStep });
mockReadable.on.getCall(0).args[1]({ length: secondStep });
expect(spy).to.have.callCount(3);
expect(spy).to.have.been.calledWith({
step: 0,
maxStep: max,
message: 'My prefix [0.0 MB of 4.0 MB]',
});
expect(spy).to.have.been.calledWith({
step: firstStep,
maxStep: max,
message: 'My prefix [1.6 MB of 4.0 MB]',
});
expect(spy).to.have.been.calledWith({
step: firstStep + secondStep,
maxStep: max,
message: 'My prefix [3.6 MB of 4.0 MB]',
});
});
it('should report stream progress when total bytes unknown', () => {
const spy = sandbox.spy();
const mockReadable = {
on: sandbox.spy()
};
(reportStreamProgress as any)(mockReadable, 'My prefix', undefined, spy);
// There are no listeners registered to this readable
expect(mockReadable.on).not.to.have.been.called;
expect(spy).to.have.callCount(1);
expect(spy).to.have.been.calledWith({
step: 1,
maxStep: 2,
message: 'My prefix (Size unknown)',
});
});
describe('open dialog', () => {
let showInformationMessageSpy: Sinon.SinonStub;
beforeEach(() => {
showInformationMessageSpy = sandbox.stub(window, 'showInformationMessage');
});
it('should show a binary choice dialog and return `yes`', (done) => {
// pretend user chooses 'yes'
showInformationMessageSpy.onCall(0).resolvesArg(2);
const res = showBinaryChoiceDialog('xxx');
res.then((val) => {
expect(val).to.eq(true);
done();
}).catch(e => fail(e));
});
it('should show a binary choice dialog and return `no`', (done) => {
// pretend user chooses 'no'
showInformationMessageSpy.onCall(0).resolvesArg(3);
const res = showBinaryChoiceDialog('xxx');
res.then((val) => {
expect(val).to.eq(false);
done();
}).catch(e => fail(e));
});
it('should show an info dialog and confirm the action', (done) => {
// pretend user chooses to run action
showInformationMessageSpy.onCall(0).resolvesArg(1);
const res = showInformationMessageWithAction('xxx', 'yyy');
res.then((val) => {
expect(val).to.eq(true);
done();
}).catch(e => fail(e));
});
it('should show an action dialog and avoid choosing the action', (done) => {
// pretend user does not choose to run action
showInformationMessageSpy.onCall(0).resolves(undefined);
const res = showInformationMessageWithAction('xxx', 'yyy');
res.then((val) => {
expect(val).to.eq(false);
done();
}).catch(e => fail(e));
});
it('should show a binary choice dialog with a url and return `yes`', (done) => {
// pretend user clicks on the url twice and then clicks 'yes'
showInformationMessageSpy.onCall(0).resolvesArg(2);
showInformationMessageSpy.onCall(1).resolvesArg(2);
showInformationMessageSpy.onCall(2).resolvesArg(3);
const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url');
res.then((val) => {
expect(val).to.eq(true);
done();
}).catch(e => fail(e));
});
it('should show a binary choice dialog with a url and return `no`', (done) => {
// pretend user clicks on the url twice and then clicks 'no'
showInformationMessageSpy.onCall(0).resolvesArg(2);
showInformationMessageSpy.onCall(1).resolvesArg(2);
showInformationMessageSpy.onCall(2).resolvesArg(4);
const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url');
res.then((val) => {
expect(val).to.eq(false);
done();
}).catch(e => fail(e));
});
it('should show a binary choice dialog and exit after clcking `more info` 5 times', (done) => {
// pretend user clicks on the url twice and then clicks 'no'
showInformationMessageSpy.onCall(0).resolvesArg(2);
showInformationMessageSpy.onCall(1).resolvesArg(2);
showInformationMessageSpy.onCall(2).resolvesArg(2);
showInformationMessageSpy.onCall(3).resolvesArg(2);
showInformationMessageSpy.onCall(4).resolvesArg(2);
const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url');
res.then((val) => {
// No choie was made
expect(val).to.eq(undefined);
expect(showInformationMessageSpy.getCalls().length).to.eq(5);
done();
}).catch(e => fail(e));
});
});
});