forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-adapter.test.ts
More file actions
167 lines (154 loc) · 5.87 KB
/
test-adapter.test.ts
File metadata and controls
167 lines (154 loc) · 5.87 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
import 'vscode-test';
import 'mocha';
import * as sinon from 'sinon';
import * as fs from 'fs-extra';
import { Uri, WorkspaceFolder } from 'vscode';
import { expect } from 'chai';
import { QLTestAdapter } from '../../test-adapter';
import { CodeQLCliServer } from '../../cli';
import { DatabaseItem, DatabaseItemImpl, DatabaseManager, FullDatabaseOptions } from '../../databases';
describe('test-adapter', () => {
let adapter: QLTestAdapter;
let fakeDatabaseManager: DatabaseManager;
let currentDatabaseItem: DatabaseItem | undefined;
let databaseItems: DatabaseItem[] = [];
let openDatabaseSpy: sinon.SinonStub;
let removeDatabaseItemSpy: sinon.SinonStub;
let renameDatabaseItemSpy: sinon.SinonStub;
let setCurrentDatabaseItemSpy: sinon.SinonStub;
let runTestsSpy: sinon.SinonStub;
let resolveTestsSpy: sinon.SinonStub;
let resolveQlpacksSpy: sinon.SinonStub;
let sandox: sinon.SinonSandbox;
const preTestDatabaseItem = new DatabaseItemImpl(
Uri.file('/path/to/test/dir/dir.testproj'),
undefined,
{ displayName: 'custom display name' } as unknown as FullDatabaseOptions,
(_) => { /* no change event listener */ }
);
const postTestDatabaseItem = new DatabaseItemImpl(
Uri.file('/path/to/test/dir/dir.testproj'),
undefined,
{ displayName: 'default name' } as unknown as FullDatabaseOptions,
(_) => { /* no change event listener */ }
);
beforeEach(() => {
sandox = sinon.createSandbox();
mockRunTests();
openDatabaseSpy = sandox.stub().resolves(postTestDatabaseItem);
removeDatabaseItemSpy = sandox.stub().resolves();
renameDatabaseItemSpy = sandox.stub().resolves();
setCurrentDatabaseItemSpy = sandox.stub().resolves();
resolveQlpacksSpy = sandox.stub().resolves({});
resolveTestsSpy = sandox.stub().resolves([]);
fakeDatabaseManager = {
currentDatabaseItem: undefined,
databaseItems: undefined,
openDatabase: openDatabaseSpy,
removeDatabaseItem: removeDatabaseItemSpy,
renameDatabaseItem: renameDatabaseItemSpy,
setCurrentDatabaseItem: setCurrentDatabaseItemSpy,
} as unknown as DatabaseManager;
sandox.stub(fakeDatabaseManager, 'currentDatabaseItem').get(() => currentDatabaseItem);
sandox.stub(fakeDatabaseManager, 'databaseItems').get(() => databaseItems);
sandox.stub(preTestDatabaseItem, 'isAffectedByTest').resolves(true);
adapter = new QLTestAdapter({
name: 'ABC',
uri: Uri.parse('file:/ab/c')
} as WorkspaceFolder, {
runTests: runTestsSpy,
resolveQlpacks: resolveQlpacksSpy,
resolveTests: resolveTestsSpy
} as unknown as CodeQLCliServer,
fakeDatabaseManager);
});
afterEach(() => {
sandox.restore();
});
it('should run some tests', async () => {
const listenerSpy = sandox.spy();
adapter.testStates(listenerSpy);
const testsPath = Uri.parse('file:/ab/c').fsPath;
const dPath = Uri.parse('file:/ab/c/d.ql').fsPath;
const gPath = Uri.parse('file:/ab/c/e/f/g.ql').fsPath;
const hPath = Uri.parse('file:/ab/c/e/f/h.ql').fsPath;
await adapter.run([testsPath]);
expect(listenerSpy.getCall(0).args).to.deep.eq([
{ type: 'started', tests: [testsPath] }
]);
expect(listenerSpy.getCall(1).args).to.deep.eq([{
type: 'test',
state: 'passed',
test: dPath,
message: undefined,
decorations: []
}]);
expect(listenerSpy.getCall(2).args).to.deep.eq([{
type: 'test',
state: 'errored',
test: gPath,
message: `\ncompilation error: ${gPath}\nERROR: abc\n`,
decorations: [
{ line: 1, message: 'abc' }
]
}]);
expect(listenerSpy.getCall(3).args).to.deep.eq([{
type: 'test',
state: 'failed',
test: hPath,
message: `\nfailed: ${hPath}\njkh\ntuv\n`,
decorations: []
}]);
expect(listenerSpy.getCall(4).args).to.deep.eq([{ type: 'finished' }]);
expect(listenerSpy).to.have.callCount(5);
});
it('should reregister testproj databases around test run', async () => {
sandox.stub(fs, 'access').resolves();
currentDatabaseItem = preTestDatabaseItem;
databaseItems = [preTestDatabaseItem];
await adapter.run(['/path/to/test/dir']);
removeDatabaseItemSpy.getCall(0).calledBefore(runTestsSpy.getCall(0));
openDatabaseSpy.getCall(0).calledAfter(runTestsSpy.getCall(0));
renameDatabaseItemSpy.getCall(0).calledAfter(openDatabaseSpy.getCall(0));
setCurrentDatabaseItemSpy.getCall(0).calledAfter(openDatabaseSpy.getCall(0));
sinon.assert.calledOnceWithExactly(
removeDatabaseItemSpy, sinon.match.any, sinon.match.any, preTestDatabaseItem);
sinon.assert.calledOnceWithExactly(
openDatabaseSpy, sinon.match.any, sinon.match.any, preTestDatabaseItem.databaseUri);
sinon.assert.calledOnceWithExactly(
renameDatabaseItemSpy, postTestDatabaseItem, preTestDatabaseItem.name);
sinon.assert.calledOnceWithExactly(
setCurrentDatabaseItemSpy, postTestDatabaseItem, true);
});
function mockRunTests() {
// runTests is an async generator function. This is not directly supported in sinon
// However, we can pretend the same thing by just returning an async array.
runTestsSpy = sandox.stub();
runTestsSpy.returns(
(async function* () {
yield Promise.resolve({
test: Uri.parse('file:/ab/c/d.ql').fsPath,
pass: true,
messages: []
});
yield Promise.resolve({
test: Uri.parse('file:/ab/c/e/f/g.ql').fsPath,
pass: false,
diff: ['pqr', 'xyz'],
// a compile error
failureStage: 'COMPILATION',
messages: [
{ position: { line: 1 }, message: 'abc', severity: 'ERROR' }
]
});
yield Promise.resolve({
test: Uri.parse('file:/ab/c/e/f/h.ql').fsPath,
pass: false,
diff: ['jkh', 'tuv'],
failureStage: 'RESULT',
messages: []
});
})()
);
}
});