Skip to content

Commit cac0add

Browse files
committed
Mock config instead of writing settings files
In our tests, we were writing settings files to disk because we were using the VSCode configuration API which writes settings to files. This results in flaky tests because concurrency can cause the VSCode API to misbehave. This will switch the tests to use a mocked API by default. For some tests the real implementation is used, but the large majority of tests is now using a mocked version which only keeps track of the configuration in memory. This makes it easier to reset the state between tests since we can just empty out the in-memory configuration.
1 parent 9f240c8 commit cac0add

11 files changed

Lines changed: 241 additions & 213 deletions

File tree

extensions/ql-vscode/src/config.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ export class Setting {
5656
.getConfiguration(this.parent.qualifiedName)
5757
.update(this.name, value, target);
5858
}
59-
60-
inspect<T>(): InspectionResult<T> | undefined {
61-
if (this.parent === undefined) {
62-
throw new Error("Cannot update the value of a root setting.");
63-
}
64-
return workspace
65-
.getConfiguration(this.parent.qualifiedName)
66-
.inspect(this.name);
67-
}
6859
}
6960

7061
export interface InspectionResult<T> {

extensions/ql-vscode/test/vscode-tests/cli-integration/jest.setup.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ import { dirname } from "path";
88
import fetch from "node-fetch";
99
import { DB_URL, dbLoc, setStoragePath, storagePath } from "./global.helper";
1010
import * as tmp from "tmp";
11-
import { getTestSetting } from "../test-config";
1211
import { CUSTOM_CODEQL_PATH_SETTING } from "../../../src/config";
13-
import { extensions, workspace } from "vscode";
12+
import { ConfigurationTarget, env, extensions, workspace } from "vscode";
13+
import { beforeEachAction } from "../test-config";
1414

15-
import baseJestSetup from "../jest.setup";
16-
17-
export default baseJestSetup;
15+
(env as any).openExternal = () => {
16+
/**/
17+
};
1818

1919
// create an extension storage location
2020
let removeStorage: tmp.DirResult["removeCallback"] | undefined;
2121

2222
beforeAll(async () => {
2323
// Set the CLI version here before activation to ensure we don't accidentally try to download a cli
24-
await getTestSetting(CUSTOM_CODEQL_PATH_SETTING)?.setInitialTestValue(
24+
await beforeEachAction();
25+
await CUSTOM_CODEQL_PATH_SETTING.updateValue(
2526
process.env.CLI_PATH,
27+
ConfigurationTarget.Workspace,
2628
);
27-
await getTestSetting(CUSTOM_CODEQL_PATH_SETTING)?.setup();
2829

2930
// ensure the test database is downloaded
3031
mkdirpSync(dirname(dbLoc));
@@ -78,6 +79,15 @@ beforeAll(async () => {
7879
await extensions.getExtension("GitHub.vscode-codeql")?.activate();
7980
});
8081

82+
beforeEach(async () => {
83+
await beforeEachAction();
84+
85+
await CUSTOM_CODEQL_PATH_SETTING.updateValue(
86+
process.env.CLI_PATH,
87+
ConfigurationTarget.Global,
88+
);
89+
});
90+
8191
// ensure extension is cleaned up.
8292
afterAll(async () => {
8393
// ensure temp directory is cleaned up.

extensions/ql-vscode/test/vscode-tests/cli-integration/remote-queries/variant-analysis-submission-integration.test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { resolve } from "path";
33
import {
44
authentication,
55
commands,
6+
ConfigurationTarget,
67
extensions,
78
QuickPickItem,
89
TextDocument,
@@ -12,7 +13,10 @@ import {
1213

1314
import { CodeQLExtensionInterface } from "../../../../src/extension";
1415
import { MockGitHubApiServer } from "../../../../src/mocks/mock-gh-api-server";
15-
import { mockConfiguration } from "../../utils/configuration-helpers";
16+
import {
17+
CANARY_FEATURES,
18+
setRemoteControllerRepo,
19+
} from "../../../../src/config";
1620

1721
jest.setTimeout(30_000);
1822

@@ -36,17 +40,8 @@ describe("Variant Analysis Submission Integration", () => {
3640
let showErrorMessageSpy: jest.SpiedFunction<typeof window.showErrorMessage>;
3741

3842
beforeEach(async () => {
39-
mockConfiguration({
40-
values: {
41-
codeQL: {
42-
canary: true,
43-
},
44-
"codeQL.variantAnalysis": {
45-
liveResults: true,
46-
controllerRepo: "github/vscode-codeql",
47-
},
48-
},
49-
});
43+
await CANARY_FEATURES.updateValue(true, ConfigurationTarget.Global);
44+
await setRemoteControllerRepo("github/vscode-codeql");
5045

5146
jest.spyOn(authentication, "getSession").mockResolvedValue({
5247
id: "test",
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { env } from "vscode";
2-
import { jestTestConfigHelper } from "./test-config";
2+
import { beforeEachAction } from "./test-config";
33

44
(env as any).openExternal = () => {
55
/**/
66
};
77

8-
export default async function setupEnv() {
9-
await jestTestConfigHelper();
10-
}
8+
beforeEach(async () => {
9+
await beforeEachAction();
10+
});

extensions/ql-vscode/test/vscode-tests/minimal-workspace/config.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import {
66
QueryHistoryConfigListener,
77
QueryServerConfigListener,
88
} from "../../../src/config";
9+
import { vscodeGetConfigurationMock } from "../test-config";
910

1011
describe("config listeners", () => {
12+
beforeEach(() => {
13+
vscodeGetConfigurationMock.mockRestore();
14+
});
15+
1116
interface TestConfig<T> {
1217
clazz: new () => ConfigListener;
1318
settings: Array<{

extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel-rendering.test.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { DbTreeViewItem } from "../../../../src/databases/ui/db-tree-view-item";
1313
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
1414
import { createMockExtensionContext } from "../../../factories/extension-context";
1515
import { createDbConfig } from "../../../factories/db-config-factories";
16-
import { mockConfiguration } from "../../utils/configuration-helpers";
16+
import { setRemoteControllerRepo } from "../../../../src/config";
1717

1818
describe("db panel rendering nodes", () => {
1919
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
@@ -50,12 +50,8 @@ describe("db panel rendering nodes", () => {
5050
});
5151

5252
describe("when controller repo is not set", () => {
53-
mockConfiguration({
54-
values: {
55-
"codeQL.variantAnalysis": {
56-
controllerRepo: undefined,
57-
},
58-
},
53+
beforeEach(async () => {
54+
await setRemoteControllerRepo(undefined);
5955
});
6056

6157
it("should not have any items", async () => {
@@ -81,14 +77,8 @@ describe("db panel rendering nodes", () => {
8177
});
8278

8379
describe("when controller repo is set", () => {
84-
beforeEach(() => {
85-
mockConfiguration({
86-
values: {
87-
"codeQL.variantAnalysis": {
88-
controllerRepo: "github/codeql",
89-
},
90-
},
91-
});
80+
beforeEach(async () => {
81+
await setRemoteControllerRepo("github/codeql");
9282
});
9383

9484
it("should render default remote nodes when the config is empty", async () => {

extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DbTreeViewItem } from "../../../../src/databases/ui/db-tree-view-item";
99
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
1010
import { createMockExtensionContext } from "../../../factories/extension-context";
1111
import { createDbConfig } from "../../../factories/db-config-factories";
12-
import { mockConfiguration } from "../../utils/configuration-helpers";
12+
import { setRemoteControllerRepo } from "../../../../src/config";
1313

1414
describe("db panel", () => {
1515
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
@@ -40,13 +40,7 @@ describe("db panel", () => {
4040
beforeEach(async () => {
4141
await ensureDir(workspaceStoragePath);
4242

43-
mockConfiguration({
44-
values: {
45-
"codeQL.variantAnalysis": {
46-
controllerRepo: "github/codeql",
47-
},
48-
},
49-
});
43+
await setRemoteControllerRepo("github/codeql");
5044
});
5145

5246
afterEach(async () => {

extensions/ql-vscode/test/vscode-tests/no-workspace/databases/db-panel-selection.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
1616
import { createMockExtensionContext } from "../../../factories/extension-context";
1717
import { createDbConfig } from "../../../factories/db-config-factories";
18-
import { mockConfiguration } from "../../utils/configuration-helpers";
18+
import { setRemoteControllerRepo } from "../../../../src/config";
1919

2020
describe("db panel selection", () => {
2121
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
@@ -46,13 +46,7 @@ describe("db panel selection", () => {
4646
beforeEach(async () => {
4747
await ensureDir(workspaceStoragePath);
4848

49-
mockConfiguration({
50-
values: {
51-
"codeQL.variantAnalysis": {
52-
controllerRepo: "github/codeql",
53-
},
54-
},
55-
});
49+
await setRemoteControllerRepo("github/codeql");
5650
});
5751

5852
afterEach(async () => {

extensions/ql-vscode/test/vscode-tests/no-workspace/telemetry.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { UserCancellationException } from "../../../src/commandRunner";
1313
import { ENABLE_TELEMETRY } from "../../../src/config";
1414
import * as Config from "../../../src/config";
1515
import { createMockExtensionContext } from "./index";
16+
import { vscodeGetConfigurationMock } from "../test-config";
1617

1718
// setting preferences can trigger lots of background activity
1819
// so need to bump up the timeout of this test.
@@ -40,6 +41,8 @@ describe("telemetry reporting", () => {
4041
>;
4142

4243
beforeEach(async () => {
44+
vscodeGetConfigurationMock.mockRestore();
45+
4346
try {
4447
// in case a previous test has accidentally activated this extension,
4548
// need to disable it first.

0 commit comments

Comments
 (0)