Skip to content

Commit 99f3d28

Browse files
committed
Introduce helper method to detect folder in workspace
1 parent 41256ec commit 99f3d28

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

extensions/ql-vscode/src/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ export function getOnDiskWorkspaceFolders() {
255255
return diskWorkspaceFolders;
256256
}
257257

258+
/** Check if folder is already present in workspace */
259+
export function isFolderAlreadyInWorkspace(folderName: string) {
260+
const workspaceFolders = workspace.workspaceFolders || [];
261+
262+
return !!workspaceFolders.find(
263+
(workspaceFolder) => workspaceFolder.name == folderName,
264+
);
265+
}
266+
258267
/**
259268
* Provides a utility method to invoke a function only if a minimum time interval has elapsed since
260269
* the last invocation of that function.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
SecretStorageChangeEvent,
1010
Uri,
1111
window,
12+
workspace,
13+
WorkspaceFolder,
1214
} from "vscode";
1315
import { dump } from "js-yaml";
1416
import * as tmp from "tmp";
@@ -19,6 +21,7 @@ import { DirResult } from "tmp";
1921
import {
2022
getInitialQueryContents,
2123
InvocationRateLimiter,
24+
isFolderAlreadyInWorkspace,
2225
isLikelyDatabaseRoot,
2326
isLikelyDbLanguageFolder,
2427
showBinaryChoiceDialog,
@@ -533,3 +536,21 @@ describe("walkDirectory", () => {
533536
expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]);
534537
});
535538
});
539+
540+
describe("isFolderAlreadyInWorkspace", () => {
541+
beforeEach(() => {
542+
const folders = [
543+
{ name: "/first/path" },
544+
{ name: "/second/path" },
545+
] as WorkspaceFolder[];
546+
547+
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue(folders);
548+
});
549+
it("should return true if the folder is already in the workspace", () => {
550+
expect(isFolderAlreadyInWorkspace("/first/path")).toBe(true);
551+
});
552+
553+
it("should return false if the folder is not in the workspace", () => {
554+
expect(isFolderAlreadyInWorkspace("/third/path")).toBe(false);
555+
});
556+
});

0 commit comments

Comments
 (0)