Merge pull request #2408 from github/koesie10/replace-deprecated-storage-path

Replace deprecated `storagePath` by `storageUri`
This commit is contained in:
Koen Vlaswinkel
2023-05-10 11:08:52 +02:00
committed by GitHub
5 changed files with 129 additions and 21 deletions

View File

@@ -505,7 +505,7 @@ class ExtensionSpecificDistributionManager {
0,
) || "";
return join(
this.extensionContext.globalStoragePath,
this.extensionContext.globalStorageUri.fsPath,
ExtensionSpecificDistributionManager._currentDistributionFolderBaseName +
distributionFolderIndex,
);

View File

@@ -24,7 +24,7 @@ import { DisposableObject } from "../pure/disposable-object";
import { Logger, extLogger } from "../common";
import { asError, getErrorMessage } from "../pure/helpers-pure";
import { QueryRunner } from "../query-server";
import { pathsEqual } from "../pure/files";
import { containsPath, pathsEqual } from "../pure/files";
import { redactableError } from "../pure/errors";
import {
getAutogenerateQlPacks,
@@ -1152,12 +1152,9 @@ export class DatabaseManager extends DisposableObject {
}
private isExtensionControlledLocation(uri: vscode.Uri) {
const storagePath = this.ctx.storagePath || this.ctx.globalStoragePath;
// the uri.fsPath function on windows returns a lowercase drive letter,
// but storagePath will have an uppercase drive letter. Be sure to compare
// URIs to URIs only
if (storagePath) {
return uri.fsPath.startsWith(vscode.Uri.file(storagePath).fsPath);
const storageUri = this.ctx.storageUri || this.ctx.globalStorageUri;
if (storageUri) {
return containsPath(storageUri.fsPath, uri.fsPath, process.platform);
}
return false;
}

View File

@@ -51,21 +51,37 @@ export async function getDirectoryNamesInsidePath(
return dirNames;
}
function normalizePath(path: string, platform: NodeJS.Platform): string {
// On Windows, "C:/", "C:\", and "c:/" are all equivalent. We need
// to normalize the paths to ensure they all get resolved to the
// same format. On Windows, we also need to do the comparison
// case-insensitively.
path = resolve(path);
if (platform === "win32") {
path = path.toLowerCase();
}
return path;
}
export function pathsEqual(
path1: string,
path2: string,
platform: NodeJS.Platform,
): boolean {
// On Windows, "C:/", "C:\", and "c:/" are all equivalent. We need
// to normalize the paths to ensure they all get resolved to the
// same format. On Windows, we also need to do the comparison
// case-insensitively.
path1 = resolve(path1);
path2 = resolve(path2);
if (platform === "win32") {
return path1.toLowerCase() === path2.toLowerCase();
}
return path1 === path2;
return normalizePath(path1, platform) === normalizePath(path2, platform);
}
/**
* Returns true if path1 contains path2.
*/
export function containsPath(
path1: string,
path2: string,
platform: NodeJS.Platform,
): boolean {
return normalizePath(path2, platform).startsWith(
normalizePath(path1, platform),
);
}
export async function readDirFullPaths(path: string): Promise<string[]> {

View File

@@ -1,6 +1,7 @@
import { join } from "path";
import {
containsPath,
gatherQlFiles,
getDirectoryNamesInsidePath,
pathsEqual,
@@ -203,3 +204,98 @@ describe("pathsEqual", () => {
},
);
});
describe("containsPath", () => {
const testCases: Array<{
path1: string;
path2: string;
platform: NodeJS.Platform;
expected: boolean;
}> = [
{
path1:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: true,
},
{
path1:
"/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: false,
},
{
path1:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "linux",
expected: false,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: false,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "win32",
expected: false,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: true,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
"D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: false,
},
];
test.each(testCases)(
"$path1 contains $path2 on $platform = $expected",
({ path1, path2, platform, expected }) => {
if (platform !== process.platform) {
// We're using the platform-specific path.resolve, so we can't really run
// these tests on all platforms.
return;
}
expect(containsPath(path1, path2, platform)).toEqual(expected);
},
);
});

View File

@@ -80,8 +80,7 @@ describe("local databases", () => {
dynamicProperties: {
// pretend like databases added in the temp dir are controlled by the extension
// so that they are deleted upon removal
storagePath: () => extensionContextStoragePath,
storageUri: () => Uri.parse(extensionContextStoragePath),
storageUri: () => Uri.file(extensionContextStoragePath),
},
},
);
@@ -277,7 +276,7 @@ describe("local databases", () => {
updateSpy.mockClear();
// pretend that the database location is not controlled by the extension
(databaseManager as any).ctx.storagePath = "hucairz";
(databaseManager as any).ctx.storageUri = Uri.file("hucairz");
extensionContextStoragePath = "hucairz";
await databaseManager.removeDatabaseItem(