Skip to content

Commit 0d1199b

Browse files
committed
Filters qltest-discovery
qlpack tests that are not contained within the current workspace folder will be filtered from the test runner view. This also fixes a test that should have been failing but wasn't.
1 parent 3edd8ec commit 0d1199b

File tree

5 files changed

+77
-25
lines changed

5 files changed

+77
-25
lines changed

extensions/ql-vscode/src/qlpack-discovery.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ export class QLPackDiscovery extends Discovery<QlpacksInfo> {
1616
private readonly watcher = this.push(new MultiFileSystemWatcher());
1717
private _qlPacks: readonly QLPack[] = [];
1818

19-
constructor(private readonly workspaceFolder: WorkspaceFolder,
20-
private readonly cliServer: CodeQLCliServer) {
21-
19+
constructor(
20+
private readonly workspaceFolder: WorkspaceFolder,
21+
private readonly cliServer: CodeQLCliServer
22+
) {
2223
super();
2324

2425
// Watch for any changes to `qlpack.yml` files in this workspace folder.
2526
// TODO: The CLI server should tell us what paths to watch for.
2627
this.watcher.addWatch(new RelativePattern(this.workspaceFolder, '**/qlpack.yml'));
2728
this.watcher.addWatch(new RelativePattern(this.workspaceFolder, '**/.codeqlmanifest.json'));
2829
this.push(this.watcher.onDidChange(this.handleQLPackFileChanged, this));
29-
30-
this.refresh();
3130
}
3231

3332
public get onDidChangeQLPacks(): Event<void> { return this._onDidChangeQLPacks.event; }

extensions/ql-vscode/src/qltest-discovery.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
2-
import { QLPackDiscovery } from './qlpack-discovery';
2+
import { QLPackDiscovery, QLPack } from './qlpack-discovery';
33
import { Discovery } from './discovery';
4-
import { EventEmitter, Event, Uri, RelativePattern, env } from 'vscode';
4+
import { EventEmitter, Event, Uri, RelativePattern, WorkspaceFolder, env } from 'vscode';
55
import { MultiFileSystemWatcher } from './vscode-utils/multi-file-system-watcher';
66
import { CodeQLCliServer } from './cli';
77

@@ -114,15 +114,15 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
114114
private readonly watcher: MultiFileSystemWatcher = this.push(new MultiFileSystemWatcher());
115115
private _testDirectories: QLTestDirectory[] = [];
116116

117-
constructor(private readonly qlPackDiscovery: QLPackDiscovery,
118-
private readonly cliServer: CodeQLCliServer) {
119-
117+
constructor(
118+
private readonly qlPackDiscovery: QLPackDiscovery,
119+
private readonly workspaceFolder: WorkspaceFolder,
120+
private readonly cliServer: CodeQLCliServer
121+
) {
120122
super();
121123

122124
this.push(this.qlPackDiscovery.onDidChangeQLPacks(this.handleDidChangeQLPacks, this));
123125
this.push(this.watcher.onDidChange(this.handleDidChange, this));
124-
125-
this.refresh();
126126
}
127127

128128
/**
@@ -151,7 +151,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
151151
const qlPacks = this.qlPackDiscovery.qlPacks;
152152
for (const qlPack of qlPacks) {
153153
//HACK: Assume that only QL packs whose name ends with '-tests' contain tests.
154-
if (qlPack.name.endsWith('-tests')) {
154+
if (this.isRelevantQlPack(qlPack)) {
155155
watchPaths.push(qlPack.uri.fsPath);
156156
const testPackage = await this.discoverTests(qlPack.uri.fsPath, qlPack.name);
157157
if (testPackage !== undefined) {
@@ -160,10 +160,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
160160
}
161161
}
162162

163-
return {
164-
testDirectories: testDirectories,
165-
watchPaths: watchPaths
166-
};
163+
return { testDirectories, watchPaths };
167164
}
168165

169166
protected update(results: QLTestDiscoveryResults): void {
@@ -177,6 +174,15 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
177174
this._onDidChangeTests.fire();
178175
}
179176

177+
/**
178+
* Only include qlpacks suffixed with '-tests' that are contained
179+
* within the provided workspace folder.
180+
*/
181+
private isRelevantQlPack(qlPack: QLPack): boolean {
182+
return qlPack.name.endsWith('-tests')
183+
&& qlPack.uri.fsPath.startsWith(this.workspaceFolder.uri.fsPath);
184+
}
185+
180186
/**
181187
* Discover all QL tests in the specified directory and its subdirectories.
182188
* @param fullPath The full path of the test directory.

extensions/ql-vscode/src/test-adapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export class QLTestAdapter extends DisposableObject implements TestAdapter {
9898
super();
9999

100100
this.qlPackDiscovery = this.push(new QLPackDiscovery(workspaceFolder, cliServer));
101-
this.qlTestDiscovery = this.push(new QLTestDiscovery(this.qlPackDiscovery, cliServer));
101+
this.qlTestDiscovery = this.push(new QLTestDiscovery(this.qlPackDiscovery, workspaceFolder, cliServer));
102+
this.qlPackDiscovery.refresh();
103+
this.qlTestDiscovery.refresh();
102104

103105
this.push(this.qlTestDiscovery.onDidChangeTests(this.discoverTests, this));
104106
}

extensions/ql-vscode/src/vscode-tests/no-workspace/contextual/queryResolver.test.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,20 @@ describe('queryResolver', () => {
4141
});
4242
});
4343

44-
it('should throw an error when there are no queries found', () => {
44+
it('should throw an error when there are no queries found', async () => {
4545
mockCli.resolveQueriesInSuite.returns([]);
4646

47-
expect(module.resolveQueries(
48-
mockCli, 'my-qlpack', KeyType.DefinitionQuery)
49-
).to.be.rejectedWith(
50-
'Couldn\'t find any queries tagged ide-contextual-queries/local-definitions for qlpack my-qlpack'
51-
);
47+
// TODO: Figure out why chai-as-promised isn't failing the test on an
48+
// unhandled rejection.
49+
try {
50+
await module.resolveQueries(mockCli, 'my-qlpack', KeyType.DefinitionQuery);
51+
// should reject
52+
expect(true).to.be.false;
53+
} catch (e) {
54+
expect(e.message).to.eq(
55+
'Couldn\'t find any queries tagged ide-contextual-queries/local-definitions for qlpack my-qlpack'
56+
);
57+
}
5258
});
5359
});
5460

@@ -78,7 +84,8 @@ describe('queryResolver', () => {
7884

7985
'../helpers': {
8086
resolveDatasetFolder: resolveDatasetFolderSpy,
81-
getOnDiskWorkspaceFolders: () => ({})
87+
getOnDiskWorkspaceFolders: () => ({}),
88+
showAndLogErrorMessage: () => ({})
8289
}
8390
});
8491
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'vscode-test';
2+
import 'mocha';
3+
import { Uri } from 'vscode';
4+
import { expect } from 'chai';
5+
6+
import { QLTestDiscovery } from '../../qltest-discovery';
7+
8+
describe('qltest-discovery', () => {
9+
describe('isRelevantQlPack', () => {
10+
it('should check if a qlpack is relevant', () => {
11+
const qlTestDiscover: any = new QLTestDiscovery(
12+
{ onDidChangeQLPacks: () => ({}) } as any,
13+
{ uri: Uri.parse('file:///a/b/c') } as any,
14+
{} as any
15+
);
16+
17+
expect(qlTestDiscover.isRelevantQlPack({
18+
name: '-hucairz',
19+
uri: Uri.parse('file:///a/b/c/d')
20+
})).to.be.false;
21+
22+
expect(qlTestDiscover.isRelevantQlPack({
23+
name: '-tests',
24+
uri: Uri.parse('file:///a/b/')
25+
})).to.be.false;
26+
27+
expect(qlTestDiscover.isRelevantQlPack({
28+
name: '-tests',
29+
uri: Uri.parse('file:///a/b/c')
30+
})).to.be.true;
31+
32+
expect(qlTestDiscover.isRelevantQlPack({
33+
name: '-tests',
34+
uri: Uri.parse('file:///a/b/c/d')
35+
})).to.be.true;
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)