Remove as unknown as QuickPickItem

This is definitely not a perfect solution since we're essentially just
moving the place where we're casting. However, because we have manually
made the types similar, this provides some type assurances where there
were none before. This also has the cast in only one place, which makes
it easier to find and fix in the future.
This commit is contained in:
Koen Vlaswinkel
2023-03-01 12:21:14 +01:00
parent 56d283f6d5
commit db0fea3af5
6 changed files with 45 additions and 33 deletions

View File

@@ -1,4 +1,4 @@
import { extensions, QuickPickItem, window } from "vscode";
import { extensions, window } from "vscode";
import { join } from "path";
import { CodeQLCliServer } from "../../../src/cli";
@@ -10,6 +10,7 @@ import {
handleDownloadPacks,
handleInstallPackDependencies,
} from "../../../src/packaging";
import { mockedQuickPickItem } from "../utils/mocking.helpers";
// up to 3 minutes per test
jest.setTimeout(3 * 60 * 1000);
@@ -56,7 +57,7 @@ describe("Packaging commands", () => {
it("should download all core query packs", async () => {
quickPickSpy.mockResolvedValue(
"Download all core query packs" as unknown as QuickPickItem,
mockedQuickPickItem("Download all core query packs"),
);
await handleDownloadPacks(cli, progress);
@@ -67,7 +68,7 @@ describe("Packaging commands", () => {
it("should download valid user-specified pack", async () => {
quickPickSpy.mockResolvedValue(
"Download custom specified pack" as unknown as QuickPickItem,
mockedQuickPickItem("Download custom specified pack"),
);
inputBoxSpy.mockResolvedValue("codeql/csharp-solorigate-queries");
@@ -79,7 +80,7 @@ describe("Packaging commands", () => {
it("should show error when downloading invalid user-specified pack", async () => {
quickPickSpy.mockResolvedValue(
"Download custom specified pack" as unknown as QuickPickItem,
mockedQuickPickItem("Download custom specified pack"),
);
inputBoxSpy.mockResolvedValue("foo/not-a-real-pack@0.0.1");
@@ -93,12 +94,14 @@ describe("Packaging commands", () => {
it("should install valid workspace pack", async () => {
const rootDir = join(__dirname, "./data");
quickPickSpy.mockResolvedValue([
{
label: "integration-test-queries-javascript",
packRootDir: [rootDir],
},
] as unknown as QuickPickItem);
quickPickSpy.mockResolvedValue(
mockedQuickPickItem([
{
label: "integration-test-queries-javascript",
packRootDir: [rootDir],
},
]),
);
await handleInstallPackDependencies(cli, progress);
expect(showAndLogInformationMessageSpy).toHaveBeenCalledWith(
@@ -108,12 +111,14 @@ describe("Packaging commands", () => {
it("should throw an error when installing invalid workspace pack", async () => {
const rootDir = join(__dirname, "../data-invalid-pack");
quickPickSpy.mockResolvedValue([
{
label: "foo/bar",
packRootDir: [rootDir],
},
] as unknown as QuickPickItem);
quickPickSpy.mockResolvedValue(
mockedQuickPickItem([
{
label: "foo/bar",
packRootDir: [rootDir],
},
]),
);
try {
// expect this to throw an error

View File

@@ -3,7 +3,6 @@ import {
commands,
env,
extensions,
QuickPickItem,
TextDocument,
TextEditor,
Uri,
@@ -59,7 +58,7 @@ import { DbManager } from "../../../../src/databases/db-manager";
import { App } from "../../../../src/common/app";
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
import { mockedObject } from "../../utils/mocking.helpers";
import { mockedObject, mockedQuickPickItem } from "../../utils/mocking.helpers";
// up to 3 minutes per test
jest.setTimeout(3 * 60 * 1000);
@@ -131,7 +130,7 @@ describe("Variant Analysis Manager", () => {
beforeEach(async () => {
jest
.spyOn(window, "showQuickPick")
.mockResolvedValueOnce("javascript" as unknown as QuickPickItem);
.mockResolvedValueOnce(mockedQuickPickItem("javascript"));
cancellationTokenSource = new CancellationTokenSource();

View File

@@ -4,7 +4,6 @@ import {
authentication,
commands,
extensions,
QuickPickItem,
TextDocument,
window,
workspace,
@@ -13,6 +12,7 @@ import {
import { CodeQLExtensionInterface } from "../../../../src/extension";
import { MockGitHubApiServer } from "../../../../src/mocks/mock-gh-api-server";
import { setRemoteControllerRepo } from "../../../../src/config";
import { mockedQuickPickItem } from "../../utils/mocking.helpers";
jest.setTimeout(30_000);
@@ -71,9 +71,7 @@ describe("Variant Analysis Submission Integration", () => {
await showQlDocument("query.ql");
// Select target language for your query
quickPickSpy.mockResolvedValueOnce(
"javascript" as unknown as QuickPickItem,
);
quickPickSpy.mockResolvedValueOnce(mockedQuickPickItem("javascript"));
await commands.executeCommand("codeQL.runVariantAnalysis");
@@ -112,9 +110,7 @@ describe("Variant Analysis Submission Integration", () => {
await showQlDocument("query.ql");
// Select target language for your query
quickPickSpy.mockResolvedValueOnce(
"javascript" as unknown as QuickPickItem,
);
quickPickSpy.mockResolvedValueOnce(mockedQuickPickItem("javascript"));
await commands.executeCommand("codeQL.runVariantAnalysis");

View File

@@ -1,13 +1,14 @@
import { join } from "path";
import { createFileSync, mkdirSync } from "fs-extra";
import * as tmp from "tmp";
import { QuickPickItem, window } from "vscode";
import { window } from "vscode";
import {
convertGithubNwoToDatabaseUrl,
findDirWithFile,
} from "../../../src/databaseFetcher";
import * as Octokit from "@octokit/rest";
import { mockedQuickPickItem } from "../utils/mocking.helpers";
// These tests make API calls and may need extra time to complete.
jest.setTimeout(10000);
@@ -71,7 +72,7 @@ describe("databaseFetcher", () => {
],
};
mockRequest.mockResolvedValue(mockApiResponse);
quickPickSpy.mockResolvedValue("javascript" as unknown as QuickPickItem);
quickPickSpy.mockResolvedValue(mockedQuickPickItem("javascript"));
const githubRepo = "github/codeql";
const result = await convertGithubNwoToDatabaseUrl(
githubRepo,

View File

@@ -22,10 +22,10 @@ import { createMockVariantAnalysisHistoryItem } from "../../../factories/query-h
import { VariantAnalysisHistoryItem } from "../../../../src/query-history/variant-analysis-history-item";
import { QueryStatus } from "../../../../src/query-status";
import { VariantAnalysisStatus } from "../../../../src/variant-analysis/shared/variant-analysis";
import { QuickPickItem, TextEditor } from "vscode";
import { TextEditor } from "vscode";
import { WebviewReveal } from "../../../../src/interface-utils";
import * as helpers from "../../../../src/helpers";
import { mockedObject } from "../../utils/mocking.helpers";
import { mockedObject, mockedQuickPickItem } from "../../utils/mocking.helpers";
describe("QueryHistoryManager", () => {
const mockExtensionLocation = join(tmpDir.name, "mock-extension-location");
@@ -977,9 +977,12 @@ describe("QueryHistoryManager", () => {
it("should find the second query to compare when one is selected", async () => {
const thisQuery = localQueryHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
showQuickPickSpy.mockResolvedValue({
query: localQueryHistory[0],
} as unknown as QuickPickItem);
showQuickPickSpy.mockResolvedValue(
mockedQuickPickItem({
label: "Query 1",
query: localQueryHistory[0],
}),
);
const otherQuery = await (
queryHistoryManager as any

View File

@@ -1,3 +1,5 @@
import { QuickPickItem, window } from "vscode";
export type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>;
@@ -41,3 +43,9 @@ export function mockedObject<T extends object>(
},
});
}
export function mockedQuickPickItem<T extends QuickPickItem | string>(
value: T | T[],
): Awaited<ReturnType<typeof window.showQuickPick>> {
return value as Awaited<ReturnType<typeof window.showQuickPick>>;
}