-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathqueries.test.ts
More file actions
204 lines (178 loc) · 6.23 KB
/
queries.test.ts
File metadata and controls
204 lines (178 loc) · 6.23 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
import { fail } from 'assert';
import { CancellationToken, commands, ExtensionContext, extensions, Uri } from 'vscode';
import * as sinon from 'sinon';
import * as path from 'path';
import * as fs from 'fs-extra';
import { expect } from 'chai';
import * as yaml from 'js-yaml';
import { DatabaseItem, DatabaseManager } from '../../databases';
import { CodeQLExtensionInterface } from '../../extension';
import { cleanDatabases, dbLoc, storagePath } from './global.helper';
import { importArchiveDatabase } from '../../databaseFetcher';
import { compileAndRunQueryAgainstDatabase, createInitialQueryInfo } from '../../run-queries';
import { CodeQLCliServer } from '../../cli';
import { QueryServerClient } from '../../queryserver-client';
import { skipIfNoCodeQL } from '../ensureCli';
import { QueryResultType } from '../../pure/messages';
import { tmpDir } from '../../helpers';
/**
* Integration tests for queries
*/
describe('Queries', function() {
this.timeout(20000);
before(function() {
skipIfNoCodeQL(this);
});
let dbItem: DatabaseItem;
let databaseManager: DatabaseManager;
let cli: CodeQLCliServer;
let qs: QueryServerClient;
let sandbox: sinon.SinonSandbox;
let progress: sinon.SinonSpy;
let token: CancellationToken;
let ctx: ExtensionContext;
let qlpackFile: string;
let qlpackLockFile: string;
let oldQlpackLockFile: string; // codeql v2.6.3 and earlier
let qlFile: string;
beforeEach(async () => {
sandbox = sinon.createSandbox();
try {
const extension = await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate();
if ('databaseManager' in extension) {
databaseManager = extension.databaseManager;
cli = extension.cliServer;
qs = extension.qs;
cli.quiet = true;
ctx = extension.ctx;
qlpackFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.yml`;
qlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/codeql-pack.lock.yml`;
oldQlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.lock.yml`;
qlFile = `${ctx.storageUri?.fsPath}/quick-queries/quick-query.ql`;
} else {
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
}
// Ensure we are starting from a clean slate.
safeDel(qlFile);
safeDel(qlpackFile);
progress = sandbox.spy();
token = {} as CancellationToken;
// Add a database, but make sure the database manager is empty first
await cleanDatabases(databaseManager);
const uri = Uri.file(dbLoc);
const maybeDbItem = await importArchiveDatabase(
uri.toString(true),
databaseManager,
storagePath,
progress,
token,
cli
);
if (!maybeDbItem) {
throw new Error('Could not import database');
}
dbItem = maybeDbItem;
} catch (e) {
fail(e as Error);
}
});
afterEach(async () => {
try {
sandbox.restore();
safeDel(qlpackFile);
safeDel(qlFile);
await cleanDatabases(databaseManager);
} catch (e) {
fail(e as Error);
}
});
it('should run a query', async () => {
try {
const queryPath = path.join(__dirname, 'data', 'simple-query.ql');
const result = await compileAndRunQueryAgainstDatabase(
cli,
qs,
dbItem,
await mockInitialQueryInfo(queryPath),
path.join(tmpDir.name, 'mock-storage-path'),
progress,
token
);
// just check that the query was successful
expect(result.result.resultType).to.eq(QueryResultType.SUCCESS);
} catch (e) {
console.error('Test Failed');
fail(e as Error);
}
});
// Asserts a fix for bug https://github.com/github/vscode-codeql/issues/733
it('should restart the database and run a query', async () => {
try {
await commands.executeCommand('codeQL.restartQueryServer');
const queryPath = path.join(__dirname, 'data', 'simple-query.ql');
const result = await compileAndRunQueryAgainstDatabase(
cli,
qs,
dbItem,
await mockInitialQueryInfo(queryPath),
path.join(tmpDir.name, 'mock-storage-path'),
progress,
token
);
// this message would indicate that the databases were not properly reregistered
expect(result.result.message).not.to.eq('No result from server');
expect(result.result.resultType).to.eq(QueryResultType.SUCCESS);
} catch (e) {
console.error('Test Failed');
fail(e as Error);
}
});
it('should create a quick query', async () => {
await commands.executeCommand('codeQL.quickQuery');
// should have created the quick query file and query pack file
expect(fs.pathExistsSync(qlFile)).to.be.true;
expect(fs.pathExistsSync(qlpackFile)).to.be.true;
const qlpackContents: any = await yaml.load(
fs.readFileSync(qlpackFile, 'utf8')
);
// Should have chosen the js libraries
expect(qlpackContents.dependencies['codeql/javascript-all']).to.eq('*');
// Should also have a codeql-pack.lock.yml file
const packFileToUse = fs.pathExistsSync(qlpackLockFile) ? qlpackLockFile : oldQlpackLockFile;
const qlpackLock: any = await yaml.load(
fs.readFileSync(packFileToUse, 'utf8')
);
expect(!!qlpackLock.dependencies['codeql/javascript-all'].version).to.be.true;
});
it('should avoid creating a quick query', async () => {
fs.mkdirpSync(path.dirname(qlpackFile));
fs.writeFileSync(qlpackFile, yaml.dump({
name: 'quick-query',
version: '1.0.0',
dependencies: {
'codeql/javascript-all': '*'
}
}));
fs.writeFileSync(qlFile, 'xxx');
await commands.executeCommand('codeQL.quickQuery');
// should not have created the quick query file because database schema hasn't changed
expect(fs.readFileSync(qlFile, 'utf8')).to.eq('xxx');
});
function safeDel(file: string) {
try {
fs.unlinkSync(file);
} catch (e) {
// ignore
}
}
async function mockInitialQueryInfo(queryPath: string) {
return await createInitialQueryInfo(
Uri.file(queryPath),
{
name: dbItem.name,
databaseUri: dbItem.databaseUri.toString(),
},
false
);
}
});