Skip to content

Commit 9a2de39

Browse files
committed
Move getFirstStoragePath method into helpers
So that we can re-use it in the QLPackGenerator.
1 parent 8c0d0d8 commit 9a2de39

4 files changed

Lines changed: 67 additions & 85 deletions

File tree

extensions/ql-vscode/src/helpers.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "fs-extra";
99
import { glob } from "glob";
1010
import { load } from "js-yaml";
11-
import { join, basename } from "path";
11+
import { join, basename, dirname } from "path";
1212
import { dirSync } from "tmp-promise";
1313
import {
1414
ExtensionContext,
@@ -785,3 +785,24 @@ export async function* walkDirectory(
785785
}
786786
}
787787
}
788+
789+
export function getFirstStoragePath() {
790+
const workspaceFolders = workspace.workspaceFolders;
791+
792+
if (!workspaceFolders || workspaceFolders.length === 0) {
793+
throw new Error("No workspace folders found");
794+
}
795+
796+
const firstFolder = workspaceFolders[0];
797+
const firstFolderFsPath = firstFolder.uri.fsPath;
798+
799+
// For the vscode-codeql-starter repo, the first folder will be a ql pack
800+
// so we need to get the parent folder
801+
if (firstFolderFsPath.includes("codeql-custom-queries")) {
802+
// return the parent folder
803+
return dirname(firstFolderFsPath);
804+
} else {
805+
// if the first folder is not a ql pack, then we are in a normal workspace
806+
return firstFolderFsPath;
807+
}
808+
}

extensions/ql-vscode/src/skeleton-query-wizard.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { join, dirname } from "path";
1+
import { join } from "path";
22
import { CancellationToken, Uri, workspace, window as Window } from "vscode";
33
import { CodeQLCliServer } from "./cli";
44
import { OutputChannelLogger } from "./common";
55
import { Credentials } from "./common/authentication";
66
import { QueryLanguage } from "./common/query-language";
7-
import { askForLanguage, isFolderAlreadyInWorkspace } from "./helpers";
7+
import {
8+
askForLanguage,
9+
getFirstStoragePath,
10+
isFolderAlreadyInWorkspace,
11+
} from "./helpers";
812
import { getErrorMessage } from "./pure/helpers-pure";
913
import { QlPackGenerator } from "./qlpack-generator";
1014
import { DatabaseItem, DatabaseManager } from "./local-databases";
@@ -50,7 +54,7 @@ export class SkeletonQueryWizard {
5054
return;
5155
}
5256

53-
this.qlPackStoragePath = this.getFirstStoragePath();
57+
this.qlPackStoragePath = getFirstStoragePath();
5458

5559
const skeletonPackAlreadyExists = isFolderAlreadyInWorkspace(
5660
this.folderName,
@@ -93,27 +97,6 @@ export class SkeletonQueryWizard {
9397
});
9498
}
9599

96-
public getFirstStoragePath() {
97-
const workspaceFolders = workspace.workspaceFolders;
98-
99-
if (!workspaceFolders || workspaceFolders.length === 0) {
100-
throw new Error("No workspace folders found");
101-
}
102-
103-
const firstFolder = workspaceFolders[0];
104-
const firstFolderFsPath = firstFolder.uri.fsPath;
105-
106-
// For the vscode-codeql-starter repo, the first folder will be a ql pack
107-
// so we need to get the parent folder
108-
if (firstFolderFsPath.includes("codeql-custom-queries")) {
109-
// return the parent folder
110-
return dirname(firstFolderFsPath);
111-
} else {
112-
// if the first folder is not a ql pack, then we are in a normal workspace
113-
return firstFolderFsPath;
114-
}
115-
}
116-
117100
private async chooseLanguage() {
118101
this.progress({
119102
message: "Choose language",

extensions/ql-vscode/test/vscode-tests/cli-integration/skeleton-query-wizard.test.ts

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -303,66 +303,6 @@ describe("SkeletonQueryWizard", () => {
303303
});
304304
});
305305

306-
describe("getFirstStoragePath", () => {
307-
it("should return the first workspace folder", async () => {
308-
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
309-
{
310-
name: "codespaces-codeql",
311-
uri: { fsPath: "codespaces-codeql" },
312-
},
313-
] as WorkspaceFolder[]);
314-
315-
wizard = new SkeletonQueryWizard(
316-
mockCli,
317-
jest.fn(),
318-
credentials,
319-
extLogger,
320-
mockDatabaseManager,
321-
token,
322-
storagePath,
323-
);
324-
325-
expect(wizard.getFirstStoragePath()).toEqual("codespaces-codeql");
326-
});
327-
328-
describe("if user is in vscode-codeql-starter workspace", () => {
329-
it("should set storage path to parent folder", async () => {
330-
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
331-
{
332-
name: "codeql-custom-queries-cpp",
333-
uri: {
334-
fsPath: join(
335-
"vscode-codeql-starter",
336-
"codeql-custom-queries-cpp",
337-
),
338-
},
339-
},
340-
{
341-
name: "codeql-custom-queries-csharp",
342-
uri: {
343-
fsPath: join(
344-
"vscode-codeql-starter",
345-
"codeql-custom-queries-csharp",
346-
),
347-
},
348-
},
349-
] as WorkspaceFolder[]);
350-
351-
wizard = new SkeletonQueryWizard(
352-
mockCli,
353-
jest.fn(),
354-
credentials,
355-
extLogger,
356-
mockDatabaseManager,
357-
token,
358-
storagePath,
359-
);
360-
361-
expect(wizard.getFirstStoragePath()).toEqual("vscode-codeql-starter");
362-
});
363-
});
364-
});
365-
366306
describe("findDatabaseItemByNwo", () => {
367307
describe("when the item exists", () => {
368308
it("should return the database item", async () => {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { DirResult } from "tmp";
2727

2828
import {
29+
getFirstStoragePath,
2930
getInitialQueryContents,
3031
InvocationRateLimiter,
3132
isFolderAlreadyInWorkspace,
@@ -671,3 +672,40 @@ describe("prepareCodeTour", () => {
671672
});
672673
});
673674
});
675+
676+
describe("getFirstStoragePath", () => {
677+
it("should return the first workspace folder", async () => {
678+
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
679+
{
680+
name: "codespaces-codeql",
681+
uri: { fsPath: "codespaces-codeql" },
682+
},
683+
] as WorkspaceFolder[]);
684+
685+
expect(getFirstStoragePath()).toEqual("codespaces-codeql");
686+
});
687+
688+
describe("if user is in vscode-codeql-starter workspace", () => {
689+
it("should set storage path to parent folder", async () => {
690+
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
691+
{
692+
name: "codeql-custom-queries-cpp",
693+
uri: {
694+
fsPath: join("vscode-codeql-starter", "codeql-custom-queries-cpp"),
695+
},
696+
},
697+
{
698+
name: "codeql-custom-queries-csharp",
699+
uri: {
700+
fsPath: join(
701+
"vscode-codeql-starter",
702+
"codeql-custom-queries-csharp",
703+
),
704+
},
705+
},
706+
] as WorkspaceFolder[]);
707+
708+
expect(getFirstStoragePath()).toEqual("vscode-codeql-starter");
709+
});
710+
});
711+
});

0 commit comments

Comments
 (0)