-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathconfig.test.ts
More file actions
173 lines (159 loc) · 5.08 KB
/
config.test.ts
File metadata and controls
173 lines (159 loc) · 5.08 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
import { ConfigurationTarget, workspace } from "vscode";
import type { ConfigListener } from "../../../src/config";
import {
CliConfigListener,
QueryHistoryConfigListener,
QueryServerConfigListener,
VSCODE_GITHUB_ENTERPRISE_URI_SETTING,
getEnterpriseUri,
hasEnterpriseUri,
hasGhecDrUri,
} from "../../../src/config";
import { vscodeGetConfigurationMock } from "../test-config";
describe("config listeners", () => {
beforeEach(() => {
vscodeGetConfigurationMock.mockRestore();
});
interface TestConfig<T> {
clazz: new () => ConfigListener;
settings: Array<{
name: string;
property: string;
values: [T, T];
}>;
}
const testConfig: Array<TestConfig<string | number | boolean>> = [
{
clazz: CliConfigListener,
settings: [
{
name: "codeQL.runningQueries.numberOfThreads",
property: "numberThreads",
values: [0, 1],
},
{
name: "codeQL.runningTests.numberOfThreads",
property: "numberTestThreads",
values: [1, 0],
},
{
name: "codeQL.runningQueries.maxPaths",
property: "maxPaths",
values: [0, 1],
},
],
},
{
clazz: QueryHistoryConfigListener,
settings: [
{
name: "codeQL.queryHistory.format",
property: "format",
values: ["abc", "def"],
},
],
},
{
clazz: QueryServerConfigListener,
settings: [
{
name: "codeQL.runningQueries.numberOfThreads",
property: "numThreads",
values: [0, 1],
},
{
name: "codeQL.runningQueries.cacheSize",
property: "cacheSize",
values: [0, 1],
},
{
name: "codeQL.runningQueries.memory",
property: "queryMemoryMb",
values: [0, 1],
},
{
name: "codeQL.runningQueries.debug",
property: "debug",
values: [true, false],
},
],
},
];
testConfig.forEach((config) => {
describe(config.clazz.name, () => {
config.settings.forEach((setting) => {
let origValue: string | number | boolean | undefined;
beforeEach(async () => {
origValue = workspace.getConfiguration().get(setting.name);
await workspace
.getConfiguration()
.update(setting.name, setting.values[0]);
await wait();
});
afterEach(async () => {
await workspace.getConfiguration().update(setting.name, origValue);
await wait();
});
it(`should listen for changes to '${setting.name}'`, async () => {
const listener = new config.clazz();
const onDidChangeConfiguration = jest.fn();
listener.onDidChangeConfiguration(onDidChangeConfiguration);
await workspace
.getConfiguration()
.update(setting.name, setting.values[1]);
await wait();
const newValue = listener[setting.property as keyof typeof listener];
expect(newValue).toEqual(setting.values[1]);
expect(onDidChangeConfiguration).toHaveBeenCalled();
});
});
});
});
// Need to wait some time since the onDidChangeConfiguration listeners fire
// asynchronously and we sometimes need to wait for them to complete in
// order to have as successful test.
async function wait(ms = 50) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
});
describe("enterprise URI", () => {
it("detects no enterprise URI when config value is not set", async () => {
expect(getEnterpriseUri()).toBeUndefined();
expect(hasEnterpriseUri()).toBe(false);
expect(hasGhecDrUri()).toBe(false);
});
it("detects no enterprise URI when config value is set to an invalid value", async () => {
await VSCODE_GITHUB_ENTERPRISE_URI_SETTING.updateValue(
"invalid-uri",
ConfigurationTarget.Global,
);
expect(getEnterpriseUri()).toBeUndefined();
expect(hasEnterpriseUri()).toBe(false);
expect(hasGhecDrUri()).toBe(false);
});
it("detects an enterprise URI when config value is set to a GHES URI", async () => {
await VSCODE_GITHUB_ENTERPRISE_URI_SETTING.updateValue(
"https://github.example.com",
ConfigurationTarget.Global,
);
expect(getEnterpriseUri()?.toString()).toBe("https://github.example.com/");
expect(hasEnterpriseUri()).toBe(true);
expect(hasGhecDrUri()).toBe(false);
});
it("detects a GHEC-DR URI when config value is set to a GHEC-DR URI", async () => {
await VSCODE_GITHUB_ENTERPRISE_URI_SETTING.updateValue(
"https://example.ghe.com",
ConfigurationTarget.Global,
);
expect(getEnterpriseUri()?.toString()).toBe("https://example.ghe.com/");
expect(hasEnterpriseUri()).toBe(true);
expect(hasGhecDrUri()).toBe(true);
});
it("Upgrades HTTP URIs to HTTPS", async () => {
await VSCODE_GITHUB_ENTERPRISE_URI_SETTING.updateValue(
"http://example.ghe.com",
ConfigurationTarget.Global,
);
expect(getEnterpriseUri()?.toString()).toBe("https://example.ghe.com/");
});
});