Introduce helper method to detect folder in workspace

This commit is contained in:
Elena Tanasoiu
2023-02-05 21:56:14 +00:00
parent 41256ec9fd
commit 99f3d28640
2 changed files with 30 additions and 0 deletions

View File

@@ -255,6 +255,15 @@ export function getOnDiskWorkspaceFolders() {
return diskWorkspaceFolders; return diskWorkspaceFolders;
} }
/** Check if folder is already present in workspace */
export function isFolderAlreadyInWorkspace(folderName: string) {
const workspaceFolders = workspace.workspaceFolders || [];
return !!workspaceFolders.find(
(workspaceFolder) => workspaceFolder.name == folderName,
);
}
/** /**
* Provides a utility method to invoke a function only if a minimum time interval has elapsed since * Provides a utility method to invoke a function only if a minimum time interval has elapsed since
* the last invocation of that function. * the last invocation of that function.

View File

@@ -9,6 +9,8 @@ import {
SecretStorageChangeEvent, SecretStorageChangeEvent,
Uri, Uri,
window, window,
workspace,
WorkspaceFolder,
} from "vscode"; } from "vscode";
import { dump } from "js-yaml"; import { dump } from "js-yaml";
import * as tmp from "tmp"; import * as tmp from "tmp";
@@ -19,6 +21,7 @@ import { DirResult } from "tmp";
import { import {
getInitialQueryContents, getInitialQueryContents,
InvocationRateLimiter, InvocationRateLimiter,
isFolderAlreadyInWorkspace,
isLikelyDatabaseRoot, isLikelyDatabaseRoot,
isLikelyDbLanguageFolder, isLikelyDbLanguageFolder,
showBinaryChoiceDialog, showBinaryChoiceDialog,
@@ -533,3 +536,21 @@ describe("walkDirectory", () => {
expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]); expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]);
}); });
}); });
describe("isFolderAlreadyInWorkspace", () => {
beforeEach(() => {
const folders = [
{ name: "/first/path" },
{ name: "/second/path" },
] as WorkspaceFolder[];
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue(folders);
});
it("should return true if the folder is already in the workspace", () => {
expect(isFolderAlreadyInWorkspace("/first/path")).toBe(true);
});
it("should return false if the folder is not in the workspace", () => {
expect(isFolderAlreadyInWorkspace("/third/path")).toBe(false);
});
});