Show error message for no workspace folders with model editor
It is possible to open the model editor without opening a folder, but this gave an unhelpful error message. This commit adds a more helpful error message.
This commit is contained in:
@@ -64,7 +64,7 @@ export async function pickExtensionPack(
|
|||||||
// If the setting is not set, automatically pick a suitable directory
|
// If the setting is not set, automatically pick a suitable directory
|
||||||
const extensionsDirectory = userExtensionsDirectory
|
const extensionsDirectory = userExtensionsDirectory
|
||||||
? Uri.file(userExtensionsDirectory)
|
? Uri.file(userExtensionsDirectory)
|
||||||
: await autoPickExtensionsDirectory();
|
: await autoPickExtensionsDirectory(logger);
|
||||||
|
|
||||||
if (!extensionsDirectory) {
|
if (!extensionsDirectory) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { FileType, Uri, workspace, WorkspaceFolder } from "vscode";
|
|||||||
import { getOnDiskWorkspaceFoldersObjects } from "../common/vscode/workspace-folders";
|
import { getOnDiskWorkspaceFoldersObjects } from "../common/vscode/workspace-folders";
|
||||||
import { extLogger } from "../common/logging/vscode";
|
import { extLogger } from "../common/logging/vscode";
|
||||||
import { tmpdir } from "../common/files";
|
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)
|
* 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
|
* for which the .git directory is closest to a workspace folder
|
||||||
* 6. If none of the above apply, return `undefined`
|
* 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();
|
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 there's only 1 workspace folder, use the `.github/codeql/extensions` directory in that folder
|
||||||
if (workspaceFolders.length === 1) {
|
if (workspaceFolders.length === 1) {
|
||||||
return Uri.joinPath(
|
return Uri.joinPath(
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { join } from "path";
|
|||||||
import { autoPickExtensionsDirectory } from "../../../../src/model-editor/extensions-workspace-folder";
|
import { autoPickExtensionsDirectory } from "../../../../src/model-editor/extensions-workspace-folder";
|
||||||
import * as files from "../../../../src/common/files";
|
import * as files from "../../../../src/common/files";
|
||||||
import { mkdirp } from "fs-extra";
|
import { mkdirp } from "fs-extra";
|
||||||
|
import { NotificationLogger } from "../../../../src/common/logging";
|
||||||
|
import { createMockLogger } from "../../../__mocks__/loggerMock";
|
||||||
|
|
||||||
describe("autoPickExtensionsDirectory", () => {
|
describe("autoPickExtensionsDirectory", () => {
|
||||||
let tmpDir: DirectoryResult;
|
let tmpDir: DirectoryResult;
|
||||||
@@ -19,6 +21,7 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
typeof workspace.updateWorkspaceFolders
|
typeof workspace.updateWorkspaceFolders
|
||||||
>;
|
>;
|
||||||
let mockedTmpDirUri: Uri;
|
let mockedTmpDirUri: Uri;
|
||||||
|
let logger: NotificationLogger;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
tmpDir = await dir({
|
tmpDir = await dir({
|
||||||
@@ -47,6 +50,8 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
.mockReturnValue(true);
|
.mockReturnValue(true);
|
||||||
|
|
||||||
jest.spyOn(files, "tmpdir").mockReturnValue(mockedTmpDir);
|
jest.spyOn(files, "tmpdir").mockReturnValue(mockedTmpDir);
|
||||||
|
|
||||||
|
logger = createMockLogger();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@@ -72,7 +77,9 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
|
expect(await autoPickExtensionsDirectory(logger)).toEqual(
|
||||||
|
extensionsDirectory,
|
||||||
|
);
|
||||||
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
|
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -94,7 +101,9 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
Uri.joinPath(rootDirectory, "workspace.code-workspace"),
|
Uri.joinPath(rootDirectory, "workspace.code-workspace"),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
|
expect(await autoPickExtensionsDirectory(logger)).toEqual(
|
||||||
|
extensionsDirectory,
|
||||||
|
);
|
||||||
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
|
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
|
||||||
name: "CodeQL Extension Packs",
|
name: "CodeQL Extension Packs",
|
||||||
uri: extensionsDirectory,
|
uri: extensionsDirectory,
|
||||||
@@ -121,7 +130,7 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
Uri.joinPath(rootDirectory, "workspace.code-workspace"),
|
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 () => {
|
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, {
|
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
|
||||||
name: "CodeQL Extension Packs",
|
name: "CodeQL Extension Packs",
|
||||||
uri: extensionsDirectory,
|
uri: extensionsDirectory,
|
||||||
@@ -164,7 +175,9 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
|
expect(await autoPickExtensionsDirectory(logger)).toEqual(
|
||||||
|
extensionsDirectory,
|
||||||
|
);
|
||||||
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(3, 0, {
|
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(3, 0, {
|
||||||
name: "CodeQL Extension Packs",
|
name: "CodeQL Extension Packs",
|
||||||
uri: extensionsDirectory,
|
uri: extensionsDirectory,
|
||||||
@@ -185,7 +198,7 @@ describe("autoPickExtensionsDirectory", () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(await autoPickExtensionsDirectory()).toEqual(undefined);
|
expect(await autoPickExtensionsDirectory(logger)).toEqual(undefined);
|
||||||
expect(updateWorkspaceFoldersSpy).not.toHaveBeenCalled();
|
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, {
|
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(2, 0, {
|
||||||
name: "CodeQL Extension Packs",
|
name: "CodeQL Extension Packs",
|
||||||
uri: extensionsDirectory,
|
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.",
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user