Skip to content

Commit 158a07c

Browse files
authored
Merge pull request #294 from jcreedcmu/jcreed/fix-quick-eval
Fix quick-eval in .qll error
2 parents 2fee4cc + 7ac5a8f commit 158a07c

File tree

6 files changed

+96
-11
lines changed

6 files changed

+96
-11
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 28 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ql-vscode/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@
444444
"webpack-cli": "^3.3.2",
445445
"eslint": "~6.8.0",
446446
"@typescript-eslint/eslint-plugin": "~2.23.0",
447-
"@typescript-eslint/parser": "~2.23.0"
447+
"@typescript-eslint/parser": "~2.23.0",
448+
"chai-as-promised": "~7.1.1",
449+
"@types/chai-as-promised": "~7.1.2"
448450
}
449451
}

extensions/ql-vscode/src/run-queries.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ type SelectedQuery = {
313313
* @param selectedResourceUri The selected resource when the command was run.
314314
* @param quickEval Whether the command being run is `Quick Evaluation`.
315315
*/
316-
async function determineSelectedQuery(selectedResourceUri: vscode.Uri | undefined, quickEval: boolean): Promise<SelectedQuery> {
316+
export async function determineSelectedQuery(selectedResourceUri: vscode.Uri | undefined, quickEval: boolean): Promise<SelectedQuery> {
317317
const editor = vscode.window.activeTextEditor;
318318

319319
// Choose which QL file to use.
@@ -336,8 +336,15 @@ async function determineSelectedQuery(selectedResourceUri: vscode.Uri | undefine
336336
}
337337
const queryPath = queryUri.fsPath || '';
338338

339-
if (!queryPath.endsWith('.ql')) {
340-
throw new Error('The selected resource is not a CodeQL query file; It should have the extension ".ql".');
339+
if (quickEval) {
340+
if (!(queryPath.endsWith('.ql') || queryPath.endsWith('.qll'))) {
341+
throw new Error('The selected resource is not a CodeQL file; It should have the extension ".ql" or ".qll".');
342+
}
343+
}
344+
else {
345+
if (!(queryPath.endsWith('.ql'))) {
346+
throw new Error('The selected resource is not a CodeQL query file; It should have the extension ".ql".');
347+
}
341348
}
342349

343350
// Whether we chose the file from the active editor or from a context menu,

extensions/ql-vscode/src/vscode-tests/minimal-workspace/activation.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as assert from 'assert';
2+
import * as chai from 'chai';
3+
import * as chaiAsPromised from 'chai-as-promised';
4+
import 'mocha';
25
import * as path from 'path';
36
import * as vscode from 'vscode';
7+
import * as determiningSelectedQueryTest from './determining-selected-query-test';
8+
9+
chai.use(chaiAsPromised);
410

511
describe('launching with a minimal workspace', async () => {
612
const ext = vscode.extensions.getExtension('GitHub.vscode-codeql');
@@ -24,3 +30,5 @@ describe('launching with a minimal workspace', async () => {
2430
}, 1000);
2531
});
2632
});
33+
34+
determiningSelectedQueryTest.run();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect } from 'chai';
2+
import * as path from 'path';
3+
import * as vscode from 'vscode';
4+
import { Uri } from 'vscode';
5+
import { determineSelectedQuery } from '../../run-queries';
6+
7+
async function showQlDocument(name: string): Promise<vscode.TextDocument> {
8+
const folderPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
9+
const documentPath = path.resolve(folderPath, name);
10+
const document = await vscode.workspace.openTextDocument(documentPath);
11+
await vscode.window.showTextDocument(document!);
12+
return document;
13+
}
14+
15+
export function run() {
16+
describe('Determining selected query', async () => {
17+
it('should allow ql files to be queried', async () => {
18+
const q = await determineSelectedQuery(Uri.parse('file:///tmp/queryname.ql'), false);
19+
expect(q.queryPath).to.equal(path.join('/', 'tmp', 'queryname.ql'));
20+
expect(q.quickEvalPosition).to.equal(undefined);
21+
});
22+
23+
it('should allow ql files to be quick-evaled', async () => {
24+
const doc = await showQlDocument('query.ql');
25+
const q = await determineSelectedQuery(doc.uri, true);
26+
expect(q.queryPath).to.satisfy((p: string) => p.endsWith(path.join('ql-vscode', 'test', 'data', 'query.ql')));
27+
});
28+
29+
it('should allow qll files to be quick-evaled', async () => {
30+
const doc = await showQlDocument('library.qll');
31+
const q = await determineSelectedQuery(doc.uri, true);
32+
expect(q.queryPath).to.satisfy((p: string) => p.endsWith(path.join('ql-vscode', 'test', 'data', 'library.qll')));
33+
});
34+
35+
it('should reject non-ql files when running a query', async () => {
36+
await expect(determineSelectedQuery(Uri.parse('file:///tmp/queryname.txt'), false)).to.be.rejectedWith(Error, 'The selected resource is not a CodeQL query file');
37+
await expect(determineSelectedQuery(Uri.parse('file:///tmp/queryname.qll'), false)).to.be.rejectedWith(Error, 'The selected resource is not a CodeQL query file');
38+
});
39+
40+
it('should reject non-ql[l] files when running a quick eval', async () => {
41+
await expect(determineSelectedQuery(Uri.parse('file:///tmp/queryname.txt'), true)).to.be.rejectedWith(Error, 'The selected resource is not a CodeQL file');
42+
});
43+
});
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
predicate foo() {
2+
1 == 1
3+
}

0 commit comments

Comments
 (0)