Merge pull request #3003 from github/koesie10/fix-error-no-workspace-folders

Show error message for no workspace folders with model editor
This commit is contained in:
Koen Vlaswinkel 2023-10-20 17:31:37 +02:00 коммит произвёл GitHub
Родитель 8c594239cd 89ccd70752
Коммит 48df8de2c2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 52 добавлений и 9 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -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)
@ -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(

Просмотреть файл

@ -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;
@ -19,6 +21,7 @@ describe("autoPickExtensionsDirectory", () => {
typeof workspace.updateWorkspaceFolders
>;
let mockedTmpDirUri: Uri;
let logger: NotificationLogger;
beforeEach(async () => {
tmpDir = await dir({
@ -47,6 +50,8 @@ describe("autoPickExtensionsDirectory", () => {
.mockReturnValue(true);
jest.spyOn(files, "tmpdir").mockReturnValue(mockedTmpDir);
logger = createMockLogger();
});
afterEach(async () => {
@ -72,7 +77,9 @@ describe("autoPickExtensionsDirectory", () => {
},
]);
expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
expect(await autoPickExtensionsDirectory(logger)).toEqual(
extensionsDirectory,
);
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
});
@ -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,
@ -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 () => {
@ -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,
@ -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,
@ -185,7 +198,7 @@ describe("autoPickExtensionsDirectory", () => {
},
]);
expect(await autoPickExtensionsDirectory()).toEqual(undefined);
expect(await autoPickExtensionsDirectory(logger)).toEqual(undefined);
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
});
@ -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.",
);
});
});