Skip to content

Commit a4e4c67

Browse files
committed
Move getInitialQueryContents to separate file
1 parent 754fa67 commit a4e4c67

File tree

5 files changed

+68
-62
lines changed

5 files changed

+68
-62
lines changed

extensions/ql-vscode/src/helpers.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
writeFile,
66
opendir,
77
} from "fs-extra";
8-
import { join, basename, dirname } from "path";
8+
import { join, dirname } from "path";
99
import { dirSync } from "tmp-promise";
1010
import { Uri, window as Window, workspace, env, WorkspaceFolder } from "vscode";
1111
import { CodeQLCliServer } from "./codeql-cli/cli";
@@ -14,7 +14,7 @@ import { extLogger, OutputChannelLogger } from "./common";
1414
import { QueryMetadata } from "./pure/interface-types";
1515
import { telemetryListener } from "./telemetry";
1616
import { RedactableError } from "./pure/errors";
17-
import { dbSchemeToLanguage, QueryLanguage } from "./common/query-language";
17+
import { QueryLanguage } from "./common/query-language";
1818
import { isCodespacesTemplate } from "./config";
1919
import { AppCommandManager } from "./common/commands";
2020

@@ -352,27 +352,6 @@ export async function prepareCodeTour(
352352
}
353353
}
354354

355-
/**
356-
* Returns the initial contents for an empty query, based on the language of the selected
357-
* databse.
358-
*
359-
* First try to use the given language name. If that doesn't exist, try to infer it based on
360-
* dbscheme. Otherwise return no import statement.
361-
*
362-
* @param language the database language or empty string if unknown
363-
* @param dbscheme path to the dbscheme file
364-
*
365-
* @returns an import and empty select statement appropriate for the selected language
366-
*/
367-
export function getInitialQueryContents(language: string, dbscheme: string) {
368-
if (!language) {
369-
const dbschemeBase = basename(dbscheme) as keyof typeof dbSchemeToLanguage;
370-
language = dbSchemeToLanguage[dbschemeBase];
371-
}
372-
373-
return language ? `import ${language}\n\nselect ""` : 'select ""';
374-
}
375-
376355
export function isQueryLanguage(language: string): language is QueryLanguage {
377356
return Object.values(QueryLanguage).includes(language as QueryLanguage);
378357
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { basename } from "path";
2+
import { dbSchemeToLanguage } from "../common/query-language";
3+
4+
/**
5+
* Returns the initial contents for an empty query, based on the language of the selected
6+
* databse.
7+
*
8+
* First try to use the given language name. If that doesn't exist, try to infer it based on
9+
* dbscheme. Otherwise return no import statement.
10+
*
11+
* @param language the database language or empty string if unknown
12+
* @param dbscheme path to the dbscheme file
13+
*
14+
* @returns an import and empty select statement appropriate for the selected language
15+
*/
16+
export function getInitialQueryContents(language: string, dbscheme: string) {
17+
if (!language) {
18+
const dbschemeBase = basename(dbscheme) as keyof typeof dbSchemeToLanguage;
19+
language = dbSchemeToLanguage[dbschemeBase];
20+
}
21+
22+
return language ? `import ${language}\n\nselect ""` : 'select ""';
23+
}

extensions/ql-vscode/src/local-queries/quick-query.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { CancellationToken, window as Window, workspace, Uri } from "vscode";
55
import { LSPErrorCodes, ResponseError } from "vscode-languageclient";
66
import { CodeQLCliServer } from "../codeql-cli/cli";
77
import { DatabaseUI } from "../databases/local-databases-ui";
8-
import { getInitialQueryContents, showBinaryChoiceDialog } from "../helpers";
8+
import { showBinaryChoiceDialog } from "../helpers";
9+
import { getInitialQueryContents } from "./query-contents";
910
import { getPrimaryDbscheme, getQlPackForDbscheme } from "../databases/qlpack";
1011
import {
1112
ProgressCallback,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as tmp from "tmp";
2+
import { dump } from "js-yaml";
3+
import { writeFileSync } from "fs-extra";
4+
import { join } from "path";
5+
import { QueryLanguage } from "../../../src/common/query-language";
6+
import { getInitialQueryContents } from "../../../src/local-queries/query-contents";
7+
8+
describe("getInitialQueryContents", () => {
9+
let dir: tmp.DirResult;
10+
let language: QueryLanguage;
11+
12+
beforeEach(() => {
13+
dir = tmp.dirSync();
14+
language = QueryLanguage.Cpp;
15+
16+
const contents = dump({
17+
primaryLanguage: language,
18+
});
19+
writeFileSync(join(dir.name, "codeql-database.yml"), contents, "utf8");
20+
});
21+
22+
afterEach(() => {
23+
dir.removeCallback();
24+
});
25+
26+
it("should get initial query contents when language is known", () => {
27+
expect(getInitialQueryContents(language, "hucairz")).toBe(
28+
'import cpp\n\nselect ""',
29+
);
30+
});
31+
32+
it("should get initial query contents when dbscheme is known", () => {
33+
expect(getInitialQueryContents("", "semmlecode.cpp.dbscheme")).toBe(
34+
'import cpp\n\nselect ""',
35+
);
36+
});
37+
38+
it("should get initial query contents when nothing is known", () => {
39+
expect(getInitialQueryContents("", "hucairz")).toBe('select ""');
40+
});
41+
});

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Uri, window, workspace, WorkspaceFolder } from "vscode";
2-
import { dump } from "js-yaml";
32
import * as tmp from "tmp";
43
import { join } from "path";
54
import {
@@ -13,7 +12,6 @@ import { DirResult } from "tmp";
1312

1413
import {
1514
getFirstWorkspaceFolder,
16-
getInitialQueryContents,
1715
isFolderAlreadyInWorkspace,
1816
prepareCodeTour,
1917
showBinaryChoiceDialog,
@@ -23,46 +21,10 @@ import {
2321
walkDirectory,
2422
} from "../../../src/helpers";
2523
import { reportStreamProgress } from "../../../src/common/vscode/progress";
26-
import { QueryLanguage } from "../../../src/common/query-language";
2724
import { Setting } from "../../../src/config";
2825
import { createMockCommandManager } from "../../__mocks__/commandsMock";
2926

3027
describe("helpers", () => {
31-
describe("codeql-database.yml tests", () => {
32-
let dir: tmp.DirResult;
33-
let language: QueryLanguage;
34-
35-
beforeEach(() => {
36-
dir = tmp.dirSync();
37-
language = QueryLanguage.Cpp;
38-
39-
const contents = dump({
40-
primaryLanguage: language,
41-
});
42-
writeFileSync(join(dir.name, "codeql-database.yml"), contents, "utf8");
43-
});
44-
45-
afterEach(() => {
46-
dir.removeCallback();
47-
});
48-
49-
it("should get initial query contents when language is known", () => {
50-
expect(getInitialQueryContents(language, "hucairz")).toBe(
51-
'import cpp\n\nselect ""',
52-
);
53-
});
54-
55-
it("should get initial query contents when dbscheme is known", () => {
56-
expect(getInitialQueryContents("", "semmlecode.cpp.dbscheme")).toBe(
57-
'import cpp\n\nselect ""',
58-
);
59-
});
60-
61-
it("should get initial query contents when nothing is known", () => {
62-
expect(getInitialQueryContents("", "hucairz")).toBe('select ""');
63-
});
64-
});
65-
6628
it("should report stream progress", () => {
6729
const progressSpy = jest.fn();
6830
const mockReadable = {

0 commit comments

Comments
 (0)