Fix problem with detecting storage folder on windows

The `getFirstStoragePath()` method would break on windows:

```
Path contains invalid characters: /c:/git-repo/codespaces-codeql (codeQL.createSkeletonQuery)
```

This makes sense, since we're looking to get the parent folder by splitting for `/`.

In windows, paths use `\` instead of `/`.

So let's detect the platform and add a test for this case.
This commit is contained in:
Elena Tanasoiu
2023-04-12 09:32:47 +00:00
parent a6ffb6b020
commit 3087886400
2 changed files with 48 additions and 2 deletions

View File

@@ -104,7 +104,11 @@ export class SkeletonQueryWizard {
// so we need to get the parent folder
if (firstFolder.uri.path.includes("codeql-custom-queries")) {
// slice off the last part of the path and return the parent folder
return firstFolder.uri.path.split("/").slice(0, -1).join("/");
if (process.platform === "win32") {
return firstFolder.uri.path.split("\\").slice(0, -1).join("\\");
} else {
return firstFolder.uri.path.split("/").slice(0, -1).join("/");
}
} else {
// if the first folder is not a ql pack, then we are in a normal workspace
return firstFolder.uri.path;

View File

@@ -305,7 +305,7 @@ describe("SkeletonQueryWizard", () => {
it("should return the first workspace folder", async () => {
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
{
name: "codeql-custom-queries-cpp",
name: "codespaces-codeql",
uri: { path: "codespaces-codeql" },
},
] as WorkspaceFolder[]);
@@ -347,6 +347,48 @@ describe("SkeletonQueryWizard", () => {
expect(wizard.getFirstStoragePath()).toEqual("vscode-codeql-starter");
});
});
describe("if user is on windows", () => {
let originalPlatform: string;
beforeEach(() => {
originalPlatform = process.platform;
Object.defineProperty(process, "platform", {
value: "win32",
});
});
afterEach(() => {
originalPlatform = process.platform;
Object.defineProperty(process, "platform", {
value: originalPlatform,
});
});
it("should return the first workspace folder", async () => {
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
{
name: "codespaces-codeql",
uri: { path: "codespaces-codeql\\codeql-custom-queries-cpp" },
},
] as WorkspaceFolder[]);
Object.defineProperty(process, "platform", {
value: "win32",
});
wizard = new SkeletonQueryWizard(
mockCli,
jest.fn(),
credentials,
extLogger,
mockDatabaseManager,
token,
);
expect(wizard.getFirstStoragePath()).toEqual("codespaces-codeql");
});
});
});
describe("findDatabaseItemByNwo", () => {