Skip to content

Commit 6114b59

Browse files
committed
Fix tests when no controller repo is set
This also extracts the helper to do so into a separate function to avoid duplicating this logic.
1 parent b3c4979 commit 6114b59

File tree

5 files changed

+216
-127
lines changed

5 files changed

+216
-127
lines changed

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

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212

1313
import { CodeQLExtensionInterface } from "../../../../src/extension";
1414
import { MockGitHubApiServer } from "../../../../src/mocks/mock-gh-api-server";
15+
import { mockConfiguration } from "../../utils/configuration-helpers";
1516

1617
jest.setTimeout(30_000);
1718

@@ -35,50 +36,17 @@ describe("Variant Analysis Submission Integration", () => {
3536
let showErrorMessageSpy: jest.SpiedFunction<typeof window.showErrorMessage>;
3637

3738
beforeEach(async () => {
38-
const originalGetConfiguration = workspace.getConfiguration;
39-
40-
jest
41-
.spyOn(workspace, "getConfiguration")
42-
.mockImplementation((section, scope) => {
43-
const configuration = originalGetConfiguration(section, scope);
44-
45-
return {
46-
get(key: string, defaultValue?: unknown) {
47-
if (section === "codeQL.variantAnalysis" && key === "liveResults") {
48-
return true;
49-
}
50-
if (section === "codeQL" && key == "canary") {
51-
return true;
52-
}
53-
if (
54-
section === "codeQL.variantAnalysis" &&
55-
key === "controllerRepo"
56-
) {
57-
return "github/vscode-codeql";
58-
}
59-
return configuration.get(key, defaultValue);
60-
},
61-
has(key: string) {
62-
return configuration.has(key);
63-
},
64-
inspect(key: string) {
65-
return configuration.inspect(key);
66-
},
67-
update(
68-
key: string,
69-
value: unknown,
70-
configurationTarget?: boolean,
71-
overrideInLanguage?: boolean,
72-
) {
73-
return configuration.update(
74-
key,
75-
value,
76-
configurationTarget,
77-
overrideInLanguage,
78-
);
79-
},
80-
};
81-
});
39+
mockConfiguration({
40+
values: {
41+
codeQL: {
42+
canary: true,
43+
},
44+
"codeQL.variantAnalysis": {
45+
liveResults: true,
46+
controllerRepo: "github/vscode-codeql",
47+
},
48+
},
49+
});
8250

8351
jest.spyOn(authentication, "getSession").mockResolvedValue({
8452
id: "test",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +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";
1617

1718
describe("db panel rendering nodes", () => {
1819
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
@@ -42,6 +43,14 @@ describe("db panel rendering nodes", () => {
4243

4344
beforeEach(async () => {
4445
await ensureDir(workspaceStoragePath);
46+
47+
mockConfiguration({
48+
values: {
49+
"codeQL.variantAnalysis": {
50+
controllerRepo: "github/codeql",
51+
},
52+
},
53+
});
4554
});
4655

4756
afterEach(async () => {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +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";
1213

1314
describe("db panel", () => {
1415
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
@@ -38,6 +39,14 @@ describe("db panel", () => {
3839

3940
beforeEach(async () => {
4041
await ensureDir(workspaceStoragePath);
42+
43+
mockConfiguration({
44+
values: {
45+
"codeQL.variantAnalysis": {
46+
controllerRepo: "github/codeql",
47+
},
48+
},
49+
});
4150
});
4251

4352
afterEach(async () => {

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

Lines changed: 133 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +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";
1819

1920
describe("db panel selection", () => {
2021
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
@@ -50,101 +51,150 @@ describe("db panel selection", () => {
5051
await remove(workspaceStoragePath);
5152
});
5253

53-
it("should mark selected remote db list as selected", async () => {
54-
const dbConfig: DbConfig = createDbConfig({
55-
remoteLists: [
56-
{
57-
name: "my-list-1",
58-
repositories: ["owner1/repo1", "owner1/repo2"],
54+
describe("when controller repo is not set", () => {
55+
beforeEach(() => {
56+
mockConfiguration({
57+
values: {
58+
"codeQL.variantAnalysis": {
59+
controllerRepo: undefined,
60+
},
5961
},
60-
{
61-
name: "my-list-2",
62-
repositories: ["owner2/repo1", "owner2/repo2"],
63-
},
64-
],
65-
selected: {
66-
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
67-
listName: "my-list-2",
68-
},
62+
});
6963
});
7064

71-
await saveDbConfig(dbConfig);
72-
73-
const dbTreeItems = await dbTreeDataProvider.getChildren();
65+
it("should not have any items", async () => {
66+
const dbConfig: DbConfig = createDbConfig({
67+
remoteLists: [
68+
{
69+
name: "my-list-1",
70+
repositories: ["owner1/repo1", "owner1/repo2"],
71+
},
72+
{
73+
name: "my-list-2",
74+
repositories: ["owner2/repo1", "owner2/repo2"],
75+
},
76+
],
77+
selected: {
78+
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
79+
listName: "my-list-2",
80+
},
81+
});
7482

75-
expect(dbTreeItems).toBeTruthy();
76-
const items = dbTreeItems!;
83+
await saveDbConfig(dbConfig);
7784

78-
const list1 = items.find(
79-
(c) =>
80-
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
81-
c.dbItem?.listName === "my-list-1",
82-
);
83-
const list2 = items.find(
84-
(c) =>
85-
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
86-
c.dbItem?.listName === "my-list-2",
87-
);
85+
const dbTreeItems = await dbTreeDataProvider.getChildren();
8886

89-
expect(list1).toBeTruthy();
90-
expect(list2).toBeTruthy();
91-
expect(isTreeViewItemSelectable(list1!)).toBeTruthy();
92-
expect(isTreeViewItemSelected(list2!)).toBeTruthy();
87+
expect(dbTreeItems).toHaveLength(0);
88+
});
9389
});
9490

95-
it("should mark selected remote db inside list as selected", async () => {
96-
const dbConfig: DbConfig = createDbConfig({
97-
remoteLists: [
98-
{
99-
name: "my-list-1",
100-
repositories: ["owner1/repo1", "owner1/repo2"],
101-
},
102-
{
103-
name: "my-list-2",
104-
repositories: ["owner1/repo1", "owner2/repo2"],
91+
describe("when controller repo is set", () => {
92+
beforeEach(() => {
93+
mockConfiguration({
94+
values: {
95+
"codeQL.variantAnalysis": {
96+
controllerRepo: "github/codeql",
97+
},
10598
},
106-
],
107-
remoteRepos: ["owner1/repo1"],
108-
selected: {
109-
kind: SelectedDbItemKind.VariantAnalysisRepository,
110-
repositoryName: "owner1/repo1",
111-
listName: "my-list-2",
112-
},
99+
});
113100
});
114101

115-
await saveDbConfig(dbConfig);
116-
117-
const dbTreeItems = await dbTreeDataProvider.getChildren();
118-
119-
expect(dbTreeItems).toBeTruthy();
120-
const items = dbTreeItems!;
121-
122-
const list2 = items.find(
123-
(c) =>
124-
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
125-
c.dbItem?.listName === "my-list-2",
126-
);
127-
expect(list2).toBeTruthy();
128-
129-
const repo1Node = list2?.children.find(
130-
(c) =>
131-
c.dbItem?.kind === DbItemKind.RemoteRepo &&
132-
c.dbItem?.repoFullName === "owner1/repo1",
133-
);
134-
expect(repo1Node).toBeTruthy();
135-
expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy();
136-
137-
const repo2Node = list2?.children.find(
138-
(c) =>
139-
c.dbItem?.kind === DbItemKind.RemoteRepo &&
140-
c.dbItem?.repoFullName === "owner2/repo2",
141-
);
142-
expect(repo2Node).toBeTruthy();
143-
expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy();
102+
it("should mark selected remote db list as selected", async () => {
103+
const dbConfig: DbConfig = createDbConfig({
104+
remoteLists: [
105+
{
106+
name: "my-list-1",
107+
repositories: ["owner1/repo1", "owner1/repo2"],
108+
},
109+
{
110+
name: "my-list-2",
111+
repositories: ["owner2/repo1", "owner2/repo2"],
112+
},
113+
],
114+
selected: {
115+
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
116+
listName: "my-list-2",
117+
},
118+
});
119+
120+
await saveDbConfig(dbConfig);
121+
122+
const dbTreeItems = await dbTreeDataProvider.getChildren();
123+
124+
expect(dbTreeItems).toBeTruthy();
125+
const items = dbTreeItems!;
126+
127+
const list1 = items.find(
128+
(c) =>
129+
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
130+
c.dbItem?.listName === "my-list-1",
131+
);
132+
const list2 = items.find(
133+
(c) =>
134+
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
135+
c.dbItem?.listName === "my-list-2",
136+
);
137+
138+
expect(list1).toBeTruthy();
139+
expect(list2).toBeTruthy();
140+
expect(isTreeViewItemSelectable(list1!)).toBeTruthy();
141+
expect(isTreeViewItemSelected(list2!)).toBeTruthy();
142+
});
144143

145-
for (const item of items) {
146-
expect(isTreeViewItemSelectable(item)).toBeTruthy();
147-
}
144+
it("should mark selected remote db inside list as selected", async () => {
145+
const dbConfig: DbConfig = createDbConfig({
146+
remoteLists: [
147+
{
148+
name: "my-list-1",
149+
repositories: ["owner1/repo1", "owner1/repo2"],
150+
},
151+
{
152+
name: "my-list-2",
153+
repositories: ["owner1/repo1", "owner2/repo2"],
154+
},
155+
],
156+
remoteRepos: ["owner1/repo1"],
157+
selected: {
158+
kind: SelectedDbItemKind.VariantAnalysisRepository,
159+
repositoryName: "owner1/repo1",
160+
listName: "my-list-2",
161+
},
162+
});
163+
164+
await saveDbConfig(dbConfig);
165+
166+
const dbTreeItems = await dbTreeDataProvider.getChildren();
167+
168+
expect(dbTreeItems).toBeTruthy();
169+
const items = dbTreeItems!;
170+
171+
const list2 = items.find(
172+
(c) =>
173+
c.dbItem?.kind === DbItemKind.RemoteUserDefinedList &&
174+
c.dbItem?.listName === "my-list-2",
175+
);
176+
expect(list2).toBeTruthy();
177+
178+
const repo1Node = list2?.children.find(
179+
(c) =>
180+
c.dbItem?.kind === DbItemKind.RemoteRepo &&
181+
c.dbItem?.repoFullName === "owner1/repo1",
182+
);
183+
expect(repo1Node).toBeTruthy();
184+
expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy();
185+
186+
const repo2Node = list2?.children.find(
187+
(c) =>
188+
c.dbItem?.kind === DbItemKind.RemoteRepo &&
189+
c.dbItem?.repoFullName === "owner2/repo2",
190+
);
191+
expect(repo2Node).toBeTruthy();
192+
expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy();
193+
194+
for (const item of items) {
195+
expect(isTreeViewItemSelectable(item)).toBeTruthy();
196+
}
197+
});
148198
});
149199

150200
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {

0 commit comments

Comments
 (0)