Move getInitialQueryContents to separate file
This commit is contained in:
@@ -5,7 +5,7 @@ import {
|
||||
writeFile,
|
||||
opendir,
|
||||
} from "fs-extra";
|
||||
import { join, basename, dirname } from "path";
|
||||
import { join, dirname } from "path";
|
||||
import { dirSync } from "tmp-promise";
|
||||
import { Uri, window as Window, workspace, env, WorkspaceFolder } from "vscode";
|
||||
import { CodeQLCliServer } from "./codeql-cli/cli";
|
||||
@@ -14,7 +14,7 @@ import { extLogger, OutputChannelLogger } from "./common";
|
||||
import { QueryMetadata } from "./pure/interface-types";
|
||||
import { telemetryListener } from "./telemetry";
|
||||
import { RedactableError } from "./pure/errors";
|
||||
import { dbSchemeToLanguage, QueryLanguage } from "./common/query-language";
|
||||
import { QueryLanguage } from "./common/query-language";
|
||||
import { isCodespacesTemplate } from "./config";
|
||||
import { AppCommandManager } from "./common/commands";
|
||||
|
||||
@@ -352,27 +352,6 @@ export async function prepareCodeTour(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the initial contents for an empty query, based on the language of the selected
|
||||
* databse.
|
||||
*
|
||||
* First try to use the given language name. If that doesn't exist, try to infer it based on
|
||||
* dbscheme. Otherwise return no import statement.
|
||||
*
|
||||
* @param language the database language or empty string if unknown
|
||||
* @param dbscheme path to the dbscheme file
|
||||
*
|
||||
* @returns an import and empty select statement appropriate for the selected language
|
||||
*/
|
||||
export function getInitialQueryContents(language: string, dbscheme: string) {
|
||||
if (!language) {
|
||||
const dbschemeBase = basename(dbscheme) as keyof typeof dbSchemeToLanguage;
|
||||
language = dbSchemeToLanguage[dbschemeBase];
|
||||
}
|
||||
|
||||
return language ? `import ${language}\n\nselect ""` : 'select ""';
|
||||
}
|
||||
|
||||
export function isQueryLanguage(language: string): language is QueryLanguage {
|
||||
return Object.values(QueryLanguage).includes(language as QueryLanguage);
|
||||
}
|
||||
|
||||
23
extensions/ql-vscode/src/local-queries/query-contents.ts
Normal file
23
extensions/ql-vscode/src/local-queries/query-contents.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { basename } from "path";
|
||||
import { dbSchemeToLanguage } from "../common/query-language";
|
||||
|
||||
/**
|
||||
* Returns the initial contents for an empty query, based on the language of the selected
|
||||
* databse.
|
||||
*
|
||||
* First try to use the given language name. If that doesn't exist, try to infer it based on
|
||||
* dbscheme. Otherwise return no import statement.
|
||||
*
|
||||
* @param language the database language or empty string if unknown
|
||||
* @param dbscheme path to the dbscheme file
|
||||
*
|
||||
* @returns an import and empty select statement appropriate for the selected language
|
||||
*/
|
||||
export function getInitialQueryContents(language: string, dbscheme: string) {
|
||||
if (!language) {
|
||||
const dbschemeBase = basename(dbscheme) as keyof typeof dbSchemeToLanguage;
|
||||
language = dbSchemeToLanguage[dbschemeBase];
|
||||
}
|
||||
|
||||
return language ? `import ${language}\n\nselect ""` : 'select ""';
|
||||
}
|
||||
@@ -5,7 +5,8 @@ import { CancellationToken, window as Window, workspace, Uri } from "vscode";
|
||||
import { LSPErrorCodes, ResponseError } from "vscode-languageclient";
|
||||
import { CodeQLCliServer } from "../codeql-cli/cli";
|
||||
import { DatabaseUI } from "../databases/local-databases-ui";
|
||||
import { getInitialQueryContents, showBinaryChoiceDialog } from "../helpers";
|
||||
import { showBinaryChoiceDialog } from "../helpers";
|
||||
import { getInitialQueryContents } from "./query-contents";
|
||||
import { getPrimaryDbscheme, getQlPackForDbscheme } from "../databases/qlpack";
|
||||
import {
|
||||
ProgressCallback,
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import * as tmp from "tmp";
|
||||
import { dump } from "js-yaml";
|
||||
import { writeFileSync } from "fs-extra";
|
||||
import { join } from "path";
|
||||
import { QueryLanguage } from "../../../src/common/query-language";
|
||||
import { getInitialQueryContents } from "../../../src/local-queries/query-contents";
|
||||
|
||||
describe("getInitialQueryContents", () => {
|
||||
let dir: tmp.DirResult;
|
||||
let language: QueryLanguage;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = tmp.dirSync();
|
||||
language = QueryLanguage.Cpp;
|
||||
|
||||
const contents = dump({
|
||||
primaryLanguage: language,
|
||||
});
|
||||
writeFileSync(join(dir.name, "codeql-database.yml"), contents, "utf8");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
dir.removeCallback();
|
||||
});
|
||||
|
||||
it("should get initial query contents when language is known", () => {
|
||||
expect(getInitialQueryContents(language, "hucairz")).toBe(
|
||||
'import cpp\n\nselect ""',
|
||||
);
|
||||
});
|
||||
|
||||
it("should get initial query contents when dbscheme is known", () => {
|
||||
expect(getInitialQueryContents("", "semmlecode.cpp.dbscheme")).toBe(
|
||||
'import cpp\n\nselect ""',
|
||||
);
|
||||
});
|
||||
|
||||
it("should get initial query contents when nothing is known", () => {
|
||||
expect(getInitialQueryContents("", "hucairz")).toBe('select ""');
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Uri, window, workspace, WorkspaceFolder } from "vscode";
|
||||
import { dump } from "js-yaml";
|
||||
import * as tmp from "tmp";
|
||||
import { join } from "path";
|
||||
import {
|
||||
@@ -13,7 +12,6 @@ import { DirResult } from "tmp";
|
||||
|
||||
import {
|
||||
getFirstWorkspaceFolder,
|
||||
getInitialQueryContents,
|
||||
isFolderAlreadyInWorkspace,
|
||||
prepareCodeTour,
|
||||
showBinaryChoiceDialog,
|
||||
@@ -23,46 +21,10 @@ import {
|
||||
walkDirectory,
|
||||
} from "../../../src/helpers";
|
||||
import { reportStreamProgress } from "../../../src/common/vscode/progress";
|
||||
import { QueryLanguage } from "../../../src/common/query-language";
|
||||
import { Setting } from "../../../src/config";
|
||||
import { createMockCommandManager } from "../../__mocks__/commandsMock";
|
||||
|
||||
describe("helpers", () => {
|
||||
describe("codeql-database.yml tests", () => {
|
||||
let dir: tmp.DirResult;
|
||||
let language: QueryLanguage;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = tmp.dirSync();
|
||||
language = QueryLanguage.Cpp;
|
||||
|
||||
const contents = dump({
|
||||
primaryLanguage: language,
|
||||
});
|
||||
writeFileSync(join(dir.name, "codeql-database.yml"), contents, "utf8");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
dir.removeCallback();
|
||||
});
|
||||
|
||||
it("should get initial query contents when language is known", () => {
|
||||
expect(getInitialQueryContents(language, "hucairz")).toBe(
|
||||
'import cpp\n\nselect ""',
|
||||
);
|
||||
});
|
||||
|
||||
it("should get initial query contents when dbscheme is known", () => {
|
||||
expect(getInitialQueryContents("", "semmlecode.cpp.dbscheme")).toBe(
|
||||
'import cpp\n\nselect ""',
|
||||
);
|
||||
});
|
||||
|
||||
it("should get initial query contents when nothing is known", () => {
|
||||
expect(getInitialQueryContents("", "hucairz")).toBe('select ""');
|
||||
});
|
||||
});
|
||||
|
||||
it("should report stream progress", () => {
|
||||
const progressSpy = jest.fn();
|
||||
const mockReadable = {
|
||||
|
||||
Reference in New Issue
Block a user