Skip to content

Commit d8ab857

Browse files
committed
Add some config listener tests
This also renames the config listeners for more consistency.
1 parent 1a5deab commit d8ab857

File tree

10 files changed

+125
-35
lines changed

10 files changed

+125
-35
lines changed

extensions/ql-vscode/src/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ export class CodeQLCliServer implements Disposable {
137137
this.restartCliServer();
138138
});
139139
}
140-
if (this.cliConfig.onDidChangeCliConfiguration) {
141-
this.cliConfig.onDidChangeCliConfiguration(() => {
140+
if (this.cliConfig.onDidChangeConfiguration) {
141+
this.cliConfig.onDidChangeConfiguration(() => {
142142
this.restartCliServer();
143143
});
144144
}

extensions/ql-vscode/src/config.ts

+9-21
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface DistributionConfig {
5757
personalAccessToken?: string;
5858
ownerName?: string;
5959
repositoryName?: string;
60-
onDidChangeDistributionConfiguration?: Event<void>;
60+
onDidChangeConfiguration?: Event<void>;
6161
}
6262

6363
// Query server configuration
@@ -82,22 +82,22 @@ export interface QueryServerConfig {
8282
numThreads: number;
8383
queryMemoryMb?: number;
8484
timeoutSecs: number;
85-
onDidChangeQueryServerConfiguration?: Event<void>;
85+
onDidChangeConfiguration?: Event<void>;
8686
}
8787

8888
/** When these settings change, the query history should be refreshed. */
8989
const QUERY_HISTORY_SETTINGS = [QUERY_HISTORY_FORMAT_SETTING];
9090

9191
export interface QueryHistoryConfig {
9292
format: string;
93-
onDidChangeQueryHistoryConfiguration: Event<void>;
93+
onDidChangeConfiguration: Event<void>;
9494
}
9595

9696
const CLI_SETTINGS = [NUMBER_OF_TEST_THREADS_SETTING];
9797

9898
export interface CliConfig {
9999
numberTestThreads: number;
100-
onDidChangeCliConfiguration?: Event<void>;
100+
onDidChangeConfiguration?: Event<void>;
101101
}
102102

103103

@@ -128,6 +128,10 @@ abstract class ConfigListener extends DisposableObject {
128128
private updateConfiguration(): void {
129129
this._onDidChangeConfiguration.fire();
130130
}
131+
132+
public get onDidChangeConfiguration(): Event<void> {
133+
return this._onDidChangeConfiguration.event;
134+
}
131135
}
132136

133137
export class DistributionConfigListener extends ConfigListener implements DistributionConfig {
@@ -143,17 +147,13 @@ export class DistributionConfigListener extends ConfigListener implements Distri
143147
return PERSONAL_ACCESS_TOKEN_SETTING.getValue() || undefined;
144148
}
145149

146-
public get onDidChangeDistributionConfiguration(): Event<void> {
147-
return this._onDidChangeConfiguration.event;
148-
}
149-
150150
protected handleDidChangeConfiguration(e: ConfigurationChangeEvent): void {
151151
this.handleDidChangeConfigurationForRelevantSettings(DISTRIBUTION_CHANGE_SETTINGS, e);
152152
}
153153
}
154154

155155
export class QueryServerConfigListener extends ConfigListener implements QueryServerConfig {
156-
private constructor(private _codeQlPath: string) {
156+
public constructor(private _codeQlPath = '') {
157157
super();
158158
}
159159

@@ -199,10 +199,6 @@ export class QueryServerConfigListener extends ConfigListener implements QuerySe
199199
return DEBUG_SETTING.getValue<boolean>();
200200
}
201201

202-
public get onDidChangeQueryServerConfiguration(): Event<void> {
203-
return this._onDidChangeConfiguration.event;
204-
}
205-
206202
protected handleDidChangeConfiguration(e: ConfigurationChangeEvent): void {
207203
this.handleDidChangeConfigurationForRelevantSettings(QUERY_SERVER_RESTARTING_SETTINGS, e);
208204
}
@@ -213,10 +209,6 @@ export class QueryHistoryConfigListener extends ConfigListener implements QueryH
213209
this.handleDidChangeConfigurationForRelevantSettings(QUERY_HISTORY_SETTINGS, e);
214210
}
215211

216-
public get onDidChangeQueryHistoryConfiguration(): Event<void> {
217-
return this._onDidChangeConfiguration.event;
218-
}
219-
220212
public get format(): string {
221213
return QUERY_HISTORY_FORMAT_SETTING.getValue<string>();
222214
}
@@ -228,10 +220,6 @@ export class CliConfigListener extends ConfigListener implements CliConfig {
228220
return NUMBER_OF_TEST_THREADS_SETTING.getValue();
229221
}
230222

231-
public get onDidChangeCliConfiguration(): Event<void> {
232-
return this._onDidChangeConfiguration.event;
233-
}
234-
235223
protected handleDidChangeConfiguration(e: ConfigurationChangeEvent): void {
236224
this.handleDidChangeConfigurationForRelevantSettings(CLI_SETTINGS, e);
237225
}

extensions/ql-vscode/src/distribution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class DistributionManager implements DistributionProvider {
5151
constructor(extensionContext: ExtensionContext, config: DistributionConfig, versionRange: semver.Range) {
5252
this._config = config;
5353
this._extensionSpecificDistributionManager = new ExtensionSpecificDistributionManager(extensionContext, config, versionRange);
54-
this._onDidChangeDistribution = config.onDidChangeDistributionConfiguration;
54+
this._onDidChangeDistribution = config.onDidChangeConfiguration;
5555
this._updateCheckRateLimiter = new InvocationRateLimiter(
5656
extensionContext,
5757
'extensionSpecificDistributionUpdateCheck',

extensions/ql-vscode/src/extension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
278278
}
279279
}
280280

281-
ctx.subscriptions.push(distributionConfigListener.onDidChangeDistributionConfiguration(() => installOrUpdateThenTryActivate({
281+
ctx.subscriptions.push(distributionConfigListener.onDidChangeConfiguration(() => installOrUpdateThenTryActivate({
282282
isUserInitiated: true,
283283
shouldDisplayMessageWhenNoUpdates: false,
284284
allowAutoUpdating: true

extensions/ql-vscode/src/query-history.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export class QueryHistoryManager extends DisposableObject {
279279
}
280280
)
281281
);
282-
queryHistoryConfigListener.onDidChangeQueryHistoryConfiguration(() => {
282+
queryHistoryConfigListener.onDidChangeConfiguration(() => {
283283
this.treeDataProvider.refresh();
284284
});
285285

extensions/ql-vscode/src/queryserver-client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ export class QueryServerClient extends DisposableObject {
5858
constructor(readonly config: QueryServerConfig, readonly cliServer: cli.CodeQLCliServer, readonly opts: ServerOpts, withProgressReporting: WithProgressReporting) {
5959
super();
6060
// When the query server configuration changes, restart the query server.
61-
if (config.onDidChangeQueryServerConfiguration !== undefined) {
62-
this.push(config.onDidChangeQueryServerConfiguration(async () => {
61+
if (config.onDidChangeConfiguration !== undefined) {
62+
this.push(config.onDidChangeConfiguration(async () => {
6363
this.logger.log('Restarting query server due to configuration changes...');
6464
await this.restartQueryServer();
6565
}, this));

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ describe('launching with a minimal workspace', async () => {
1313
it('should install the extension', () => {
1414
assert(ext);
1515
});
16+
1617
it('should not activate the extension at first', () => {
1718
assert(ext!.isActive === false);
1819
});
20+
1921
it('should activate the extension when a .ql file is opened', async function() {
2022
const folders = vscode.workspace.workspaceFolders;
2123
assert(folders && folders.length === 1);
@@ -24,10 +26,9 @@ describe('launching with a minimal workspace', async () => {
2426
const document = await vscode.workspace.openTextDocument(documentPath);
2527
assert(document.languageId === 'ql');
2628
// Delay slightly so that the extension has time to activate.
27-
this.timeout(3000);
28-
setTimeout(() => {
29-
assert(ext!.isActive);
30-
}, 1000);
29+
this.timeout(4000);
30+
await new Promise(resolve => setTimeout(resolve, 2000));
31+
assert(ext!.isActive);
3132
});
3233
});
3334

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'vscode-test';
2+
import 'mocha';
3+
import * as chaiAsPromised from 'chai-as-promised';
4+
import 'sinon-chai';
5+
import * as Sinon from 'sinon';
6+
import * as chai from 'chai';
7+
import { workspace } from 'vscode';
8+
9+
import {
10+
CliConfigListener,
11+
QueryHistoryConfigListener,
12+
QueryServerConfigListener
13+
} from '../../config';
14+
15+
chai.use(chaiAsPromised);
16+
const expect = chai.expect;
17+
18+
describe('config listeners', () => {
19+
let sandbox: Sinon.SinonSandbox;
20+
beforeEach(() => {
21+
sandbox = Sinon.createSandbox();
22+
});
23+
24+
afterEach(() => {
25+
sandbox.restore();
26+
});
27+
28+
interface TestConfig<T> {
29+
clazz: new() => {};
30+
settings: {
31+
name: string;
32+
property: string;
33+
values: [T, T];
34+
}[];
35+
}
36+
37+
const testConfig: TestConfig<string | number | boolean>[] = [
38+
{
39+
clazz: CliConfigListener,
40+
settings: [{
41+
name: 'codeQL.runningTests.numberOfThreads',
42+
property: 'numberTestThreads',
43+
values: [1, 0]
44+
}]
45+
},
46+
{
47+
clazz: QueryHistoryConfigListener,
48+
settings: [{
49+
name: 'codeQL.queryHistory.format',
50+
property: 'format',
51+
values: ['abc', 'def']
52+
}]
53+
},
54+
{
55+
clazz: QueryServerConfigListener,
56+
settings: [{
57+
name: 'codeQL.runningQueries.numberOfThreads',
58+
property: 'numThreads',
59+
values: [0, 1]
60+
}, {
61+
name: 'codeQL.runningQueries.memory',
62+
property: 'queryMemoryMb',
63+
values: [0, 1]
64+
}, {
65+
name: 'codeQL.runningQueries.debug',
66+
property: 'debug',
67+
values: [true, false]
68+
}]
69+
}
70+
];
71+
72+
testConfig.forEach(config => {
73+
describe(config.clazz.name, () => {
74+
let listener: any;
75+
let spy: Sinon.SinonSpy;
76+
beforeEach(() => {
77+
listener = new config.clazz();
78+
spy = Sinon.spy();
79+
listener.onDidChangeConfiguration(spy);
80+
});
81+
82+
config.settings.forEach(setting => {
83+
let origValue: any;
84+
beforeEach(async () => {
85+
origValue = workspace.getConfiguration().get(setting.name);
86+
await workspace.getConfiguration().update(setting.name, setting.values[0]);
87+
spy.resetHistory();
88+
});
89+
90+
afterEach(async () => {
91+
await workspace.getConfiguration().update(setting.name, origValue);
92+
});
93+
94+
it(`should listen for changes to '${setting.name}'`, async () => {
95+
await workspace.getConfiguration().update(setting.name, setting.values[1]);
96+
expect(spy.calledOnce).to.be.true;
97+
expect(listener[setting.property]).to.eq(setting.values[1]);
98+
});
99+
});
100+
});
101+
});
102+
});

extensions/ql-vscode/src/vscode-tests/no-workspace/query-results.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe('CompletedQuery', () => {
238238

239239
function mockQueryHistoryConfig(): QueryHistoryConfig {
240240
return {
241-
onDidChangeQueryHistoryConfiguration: onDidChangeQueryHistoryConfigurationSpy,
241+
onDidChangeConfiguration: onDidChangeQueryHistoryConfigurationSpy,
242242
format: 'pqr'
243243
};
244244
}

extensions/ql-vscode/test/pure-tests/query-test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ describe('using the query server', function() {
116116
},
117117
},
118118
{
119-
numberTestThreads: 2,
120-
onDidChangeCliConfiguration: (() => {}) as any
119+
numberTestThreads: 2
121120
},
122121
logger
123122
);

0 commit comments

Comments
 (0)