From 11bc465fca20984e46c2d8888b9cdc2e8ebad52a Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Mon, 11 Nov 2024 15:54:17 +0100 Subject: [PATCH] Fix and re-enable variant analysis submission integration tests --- extensions/ql-vscode/src/common/commands.ts | 4 +- .../vscode/vscode-mock-gh-api-server.ts | 50 ++++++++++++------- ...nt-analysis-submission-integration.test.ts | 27 ++++++---- 3 files changed, 52 insertions(+), 29 deletions(-) diff --git a/extensions/ql-vscode/src/common/commands.ts b/extensions/ql-vscode/src/common/commands.ts index 7b1cadcda7b..bdb8e792281 100644 --- a/extensions/ql-vscode/src/common/commands.ts +++ b/extensions/ql-vscode/src/common/commands.ts @@ -348,7 +348,9 @@ export type MockGitHubApiServerCommands = { "codeQL.mockGitHubApiServer.startRecording": () => Promise; "codeQL.mockGitHubApiServer.saveScenario": () => Promise; "codeQL.mockGitHubApiServer.cancelRecording": () => Promise; - "codeQL.mockGitHubApiServer.loadScenario": () => Promise; + "codeQL.mockGitHubApiServer.loadScenario": ( + scenario?: string, + ) => Promise; "codeQL.mockGitHubApiServer.unloadScenario": () => Promise; }; diff --git a/extensions/ql-vscode/src/common/mock-gh-api/vscode/vscode-mock-gh-api-server.ts b/extensions/ql-vscode/src/common/mock-gh-api/vscode/vscode-mock-gh-api-server.ts index b194bc360ad..8e1e04e9daa 100644 --- a/extensions/ql-vscode/src/common/mock-gh-api/vscode/vscode-mock-gh-api-server.ts +++ b/extensions/ql-vscode/src/common/mock-gh-api/vscode/vscode-mock-gh-api-server.ts @@ -63,26 +63,33 @@ export class VSCodeMockGitHubApiServer extends DisposableObject { ); } - public async loadScenario(): Promise { + public async loadScenario(scenario?: string): Promise { const scenariosPath = await this.getScenariosPath(); if (!scenariosPath) { return; } - const scenarioNames = await this.server.getScenarioNames(scenariosPath); - const scenarioQuickPickItems = scenarioNames.map((s) => ({ label: s })); - const quickPickOptions = { - placeHolder: "Select a scenario to load", - }; - const selectedScenario = await window.showQuickPick( - scenarioQuickPickItems, - quickPickOptions, - ); - if (!selectedScenario) { - return; + let scenarioName = scenario; + if (!scenarioName) { + const scenarioNames = await this.server.getScenarioNames(scenariosPath); + const scenarioQuickPickItems = scenarioNames.map((s) => ({ label: s })); + const quickPickOptions = { + placeHolder: "Select a scenario to load", + }; + const selectedScenario = await window.showQuickPick( + scenarioQuickPickItems, + quickPickOptions, + ); + if (!selectedScenario) { + return; + } + + scenarioName = selectedScenario.label; } - const scenarioName = selectedScenario.label; + if (!this.server.isListening && this.app.mode === AppMode.Test) { + await this.startServer(); + } await this.server.loadScenario(scenarioName, scenariosPath); @@ -94,12 +101,12 @@ export class VSCodeMockGitHubApiServer extends DisposableObject { true, ); - await window.showInformationMessage(`Loaded scenario '${scenarioName}'`); + void window.showInformationMessage(`Loaded scenario '${scenarioName}'`); } public async unloadScenario(): Promise { if (!this.server.isScenarioLoaded) { - await window.showInformationMessage("No scenario currently loaded"); + void window.showInformationMessage("No scenario currently loaded"); } else { await this.server.unloadScenario(); await this.app.commands.execute( @@ -107,7 +114,11 @@ export class VSCodeMockGitHubApiServer extends DisposableObject { "codeQL.mockGitHubApiServer.scenarioLoaded", false, ); - await window.showInformationMessage("Unloaded scenario"); + void window.showInformationMessage("Unloaded scenario"); + } + + if (this.server.isListening && this.app.mode === AppMode.Test) { + await this.stopServer(); } } @@ -139,7 +150,7 @@ export class VSCodeMockGitHubApiServer extends DisposableObject { true, ); - await window.showInformationMessage( + void window.showInformationMessage( 'Recording scenario. To save the scenario, use the "CodeQL Mock GitHub API Server: Save Scenario" command.', ); } @@ -221,7 +232,10 @@ export class VSCodeMockGitHubApiServer extends DisposableObject { return scenariosPath; } - if (this.app.mode === AppMode.Development) { + if ( + this.app.mode === AppMode.Development || + this.app.mode === AppMode.Test + ) { const developmentScenariosPath = path.join( this.app.extensionPath, "src/common/mock-gh-api/scenarios", diff --git a/extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-submission-integration.test.ts b/extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-submission-integration.test.ts index 41771a18540..182433637a7 100644 --- a/extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-submission-integration.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-submission-integration.test.ts @@ -3,18 +3,12 @@ import { resolve } from "path"; import type { TextDocument } from "vscode"; import { authentication, commands, window, workspace } from "vscode"; -import { MockGitHubApiServer } from "../../../../src/common/mock-gh-api/mock-gh-api-server"; import { mockedQuickPickItem } from "../../utils/mocking.helpers"; import { setRemoteControllerRepo } from "../../../../src/config"; import { getActivatedExtension } from "../../global.helper"; import { createVSCodeCommandManager } from "../../../../src/common/vscode/commands"; import type { AllCommands } from "../../../../src/common/commands"; -const mockServer = new MockGitHubApiServer(); -beforeAll(() => mockServer.startServer("bypass")); -afterEach(() => mockServer.unloadScenario()); -afterAll(() => mockServer.stopServer()); - async function showQlDocument(name: string): Promise { const folderPath = workspace.workspaceFolders![0].uri.fsPath; const documentPath = resolve(folderPath, name); @@ -24,7 +18,7 @@ async function showQlDocument(name: string): Promise { } // MSW can't intercept fetch requests made in VS Code, so we are skipping these tests for now -describe.skip("Variant Analysis Submission Integration", () => { +describe("Variant Analysis Submission Integration", () => { const commandManager = createVSCodeCommandManager(); let quickPickSpy: jest.SpiedFunction; let executeCommandSpy: jest.SpiedFunction; @@ -54,9 +48,16 @@ describe.skip("Variant Analysis Submission Integration", () => { await getActivatedExtension(); }); + afterAll(async () => { + await commandManager.execute("codeQL.mockGitHubApiServer.unloadScenario"); + }); + describe("Successful scenario", () => { beforeEach(async () => { - await mockServer.loadScenario("mrva-problem-query-success"); + await commandManager.execute( + "codeQL.mockGitHubApiServer.loadScenario", + "mrva-problem-query-success", + ); }); it("opens the variant analysis view", async () => { @@ -81,7 +82,10 @@ describe.skip("Variant Analysis Submission Integration", () => { describe("Missing controller repo", () => { beforeEach(async () => { - await mockServer.loadScenario("mrva-missing-controller-repo"); + await commandManager.execute( + "codeQL.mockGitHubApiServer.loadScenario", + "mrva-missing-controller-repo", + ); }); it("shows the error message", async () => { @@ -108,7 +112,10 @@ describe.skip("Variant Analysis Submission Integration", () => { describe("Submission failure", () => { beforeEach(async () => { - await mockServer.loadScenario("mrva-submission-failure"); + await commandManager.execute( + "codeQL.mockGitHubApiServer.loadScenario", + "mrva-submission-failure", + ); }); it("shows the error message", async () => {