Skip to content

Commit d30eb27

Browse files
robertbrignullkoesie10
authored andcommitted
Move inspect to config.ts to reduce duplication of knowledge
1 parent ae2bd81 commit d30eb27

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

extensions/ql-vscode/src/config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ export class Setting {
3535
}
3636
return workspace.getConfiguration(this.parent.qualifiedName).update(this.name, value, target);
3737
}
38+
39+
inspect<T>(): InspectionResult<T> | undefined {
40+
if (this.parent === undefined) {
41+
throw new Error('Cannot update the value of a root setting.');
42+
}
43+
return workspace.getConfiguration(this.parent.qualifiedName).inspect(this.name);
44+
}
45+
}
46+
47+
export interface InspectionResult<T> {
48+
globalValue?: T;
49+
workspaceValue?: T,
50+
workspaceFolderValue?: T,
3851
}
3952

4053
const ROOT_SETTING = new Setting('codeQL');

extensions/ql-vscode/src/vscode-tests/test-config.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
import { ConfigurationTarget, workspace } from 'vscode';
2-
import { CUSTOM_CODEQL_PATH_SETTING, REMOTE_CONTROLLER_REPO, REMOTE_REPO_LISTS, Setting } from '../config';
1+
import { ConfigurationTarget } from 'vscode';
2+
import { CUSTOM_CODEQL_PATH_SETTING, InspectionResult, REMOTE_CONTROLLER_REPO, REMOTE_REPO_LISTS, Setting } from '../config';
33

44
class TestSetting<T> {
5-
private settingState: {
6-
globalValue?: T;
7-
workspaceValue?: T,
8-
workspaceFolderValue?: T,
9-
} | undefined;
5+
private settingState: InspectionResult<T> | undefined;
106

117
constructor(
128
public readonly setting: Setting,
139
private initialTestValue: T | undefined = undefined
1410
) { }
1511

1612
public async get(): Promise<T | undefined> {
17-
return this.section.get(this.setting.name);
13+
return this.setting.getValue();
1814
}
1915

2016
public async set(value: T | undefined, target: ConfigurationTarget = ConfigurationTarget.Global): Promise<void> {
21-
await this.section.update(this.setting.name, value, target);
17+
await this.setting.updateValue(value, target);
2218
}
2319

2420
public async setInitialTestValue(value: T | undefined) {
2521
this.initialTestValue = value;
2622
}
2723

2824
public async initialSetup() {
29-
this.settingState = this.section.inspect(this.setting.name);
25+
this.settingState = this.setting.inspect();
3026

3127
// Unfortunately it's not well-documented how to check whether we can write to a workspace
3228
// configuration. This is the best I could come up with. It only fails for initial test values
@@ -46,7 +42,7 @@ class TestSetting<T> {
4642
}
4743

4844
public async restoreToInitialValues() {
49-
const state = this.section.inspect(this.setting.name);
45+
const state = this.setting.inspect();
5046

5147
// We need to check the state of the setting before we restore it. This is less important for the global
5248
// configuration target, but the workspace/workspace folder configuration might not even exist. If they
@@ -61,13 +57,6 @@ class TestSetting<T> {
6157
await this.set(this.settingState?.workspaceFolderValue, ConfigurationTarget.WorkspaceFolder);
6258
}
6359
}
64-
65-
private get section() {
66-
if (this.setting.parent === undefined) {
67-
throw new Error('Cannot get the value of a root setting.');
68-
}
69-
return workspace.getConfiguration(this.setting.parent.qualifiedName);
70-
}
7160
}
7261

7362
export const testConfig = {

0 commit comments

Comments
 (0)