Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function pickExtensionPack(
// If the setting is not set, automatically pick a suitable directory
const extensionsDirectory = userExtensionsDirectory
? Uri.file(userExtensionsDirectory)
: await autoPickExtensionsDirectory();
: await autoPickExtensionsDirectory(logger);

if (!extensionsDirectory) {
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FileType, Uri, workspace, WorkspaceFolder } from "vscode";
import { getOnDiskWorkspaceFoldersObjects } from "../common/vscode/workspace-folders";
import { extLogger } from "../common/logging/vscode";
import { tmpdir } from "../common/files";
import { NotificationLogger, showAndLogErrorMessage } from "../common/logging";

/**
* Returns the ancestors of this path in order from furthest to closest (i.e. root of filesystem to parent directory)
Expand Down Expand Up @@ -143,9 +144,20 @@ async function findGitFolder(
* for which the .git directory is closest to a workspace folder
* 6. If none of the above apply, return `undefined`
*/
export async function autoPickExtensionsDirectory(): Promise<Uri | undefined> {
export async function autoPickExtensionsDirectory(
logger: NotificationLogger,
): Promise<Uri | undefined> {
const workspaceFolders = getOnDiskWorkspaceFoldersObjects();

// If there are no on-disk workspace folders, we can't do anything
if (workspaceFolders.length === 0) {
void showAndLogErrorMessage(
logger,
`Could not find any on-disk workspace folders. Please ensure that you have opened a folder or workspace.`,
);
return undefined;
}

// If there's only 1 workspace folder, use the `.github/codeql/extensions` directory in that folder
if (workspaceFolders.length === 1) {
return Uri.joinPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { join } from "path";
import { autoPickExtensionsDirectory } from "../../../../src/model-editor/extensions-workspace-folder";
import * as files from "../../../../src/common/files";
import { mkdirp } from "fs-extra";
import { NotificationLogger } from "../../../../src/common/logging";
import { createMockLogger } from "../../../__mocks__/loggerMock";

describe("autoPickExtensionsDirectory", () => {
let tmpDir: DirectoryResult;
Expand All @@ -19,6 +21,7 @@ describe("autoPickExtensionsDirectory", () => {
typeof workspace.updateWorkspaceFolders
>;
let mockedTmpDirUri: Uri;
let logger: NotificationLogger;

beforeEach(async () => {
tmpDir = await dir({
Expand Down Expand Up @@ -47,6 +50,8 @@ describe("autoPickExtensionsDirectory", () => {
.mockReturnValue(true);

jest.spyOn(files, "tmpdir").mockReturnValue(mockedTmpDir);

logger = createMockLogger();
});

afterEach(async () => {
Expand All @@ -72,7 +77,9 @@ describe("autoPickExtensionsDirectory", () => {
},
]);

expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
expect(await autoPickExtensionsDirectory(logger)).toEqual(
extensionsDirectory,
);
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
});

Expand All @@ -94,7 +101,9 @@ describe("autoPickExtensionsDirectory", () => {
Uri.joinPath(rootDirectory, "workspace.code-workspace"),
);

expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
expect(await autoPickExtensionsDirectory(logger)).toEqual(
extensionsDirectory,
);
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
name: "CodeQL Extension Packs",
uri: extensionsDirectory,
Expand All @@ -121,7 +130,7 @@ describe("autoPickExtensionsDirectory", () => {
Uri.joinPath(rootDirectory, "workspace.code-workspace"),
);

expect(await autoPickExtensionsDirectory()).toEqual(undefined);
expect(await autoPickExtensionsDirectory(logger)).toEqual(undefined);
});

it("when a workspace file does not exist and there is a common root directory", async () => {
Expand All @@ -138,7 +147,9 @@ describe("autoPickExtensionsDirectory", () => {
},
]);

expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
expect(await autoPickExtensionsDirectory(logger)).toEqual(
extensionsDirectory,
);
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
name: "CodeQL Extension Packs",
uri: extensionsDirectory,
Expand All @@ -164,7 +175,9 @@ describe("autoPickExtensionsDirectory", () => {
},
]);

expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
expect(await autoPickExtensionsDirectory(logger)).toEqual(
extensionsDirectory,
);
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(3, 0, {
name: "CodeQL Extension Packs",
uri: extensionsDirectory,
Expand All @@ -185,7 +198,7 @@ describe("autoPickExtensionsDirectory", () => {
},
]);

expect(await autoPickExtensionsDirectory()).toEqual(undefined);
expect(await autoPickExtensionsDirectory(logger)).toEqual(undefined);
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
});

Expand All @@ -205,10 +218,28 @@ describe("autoPickExtensionsDirectory", () => {
},
]);

expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
expect(await autoPickExtensionsDirectory(logger)).toEqual(
extensionsDirectory,
);
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
name: "CodeQL Extension Packs",
uri: extensionsDirectory,
});
});

it("when there is no on-disk workspace folder", async () => {
workspaceFoldersSpy.mockReturnValue([
{
uri: Uri.parse("codeql-zip-archive://codeql_db"),
name: "my-db",
index: 0,
},
]);

expect(await autoPickExtensionsDirectory(logger)).toEqual(undefined);
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
expect(logger.showErrorMessage).toHaveBeenCalledWith(
"Could not find any on-disk workspace folders. Please ensure that you have opened a folder or workspace.",
);
});
});