Skip to content

Commit 7dfa6f0

Browse files
authored
Merge pull request #2408 from github/koesie10/replace-deprecated-storage-path
Replace deprecated `storagePath` by `storageUri`
2 parents 3ad006d + 6e5188f commit 7dfa6f0

File tree

5 files changed

+128
-20
lines changed

5 files changed

+128
-20
lines changed

extensions/ql-vscode/src/codeql-cli/distribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ class ExtensionSpecificDistributionManager {
505505
0,
506506
) || "";
507507
return join(
508-
this.extensionContext.globalStoragePath,
508+
this.extensionContext.globalStorageUri.fsPath,
509509
ExtensionSpecificDistributionManager._currentDistributionFolderBaseName +
510510
distributionFolderIndex,
511511
);

extensions/ql-vscode/src/databases/local-databases.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { DisposableObject } from "../pure/disposable-object";
2424
import { Logger, extLogger } from "../common";
2525
import { asError, getErrorMessage } from "../pure/helpers-pure";
2626
import { QueryRunner } from "../query-server";
27-
import { pathsEqual } from "../pure/files";
27+
import { containsPath, pathsEqual } from "../pure/files";
2828
import { redactableError } from "../pure/errors";
2929
import {
3030
getAutogenerateQlPacks,
@@ -1152,12 +1152,9 @@ export class DatabaseManager extends DisposableObject {
11521152
}
11531153

11541154
private isExtensionControlledLocation(uri: vscode.Uri) {
1155-
const storagePath = this.ctx.storagePath || this.ctx.globalStoragePath;
1156-
// the uri.fsPath function on windows returns a lowercase drive letter,
1157-
// but storagePath will have an uppercase drive letter. Be sure to compare
1158-
// URIs to URIs only
1159-
if (storagePath) {
1160-
return uri.fsPath.startsWith(vscode.Uri.file(storagePath).fsPath);
1155+
const storageUri = this.ctx.storageUri || this.ctx.globalStorageUri;
1156+
if (storageUri) {
1157+
return containsPath(storageUri.fsPath, uri.fsPath, process.platform);
11611158
}
11621159
return false;
11631160
}

extensions/ql-vscode/src/pure/files.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,37 @@ export async function getDirectoryNamesInsidePath(
5151
return dirNames;
5252
}
5353

54-
export function pathsEqual(
55-
path1: string,
56-
path2: string,
57-
platform: NodeJS.Platform,
58-
): boolean {
54+
function normalizePath(path: string, platform: NodeJS.Platform): string {
5955
// On Windows, "C:/", "C:\", and "c:/" are all equivalent. We need
6056
// to normalize the paths to ensure they all get resolved to the
6157
// same format. On Windows, we also need to do the comparison
6258
// case-insensitively.
63-
path1 = resolve(path1);
64-
path2 = resolve(path2);
59+
path = resolve(path);
6560
if (platform === "win32") {
66-
return path1.toLowerCase() === path2.toLowerCase();
61+
path = path.toLowerCase();
6762
}
68-
return path1 === path2;
63+
return path;
64+
}
65+
66+
export function pathsEqual(
67+
path1: string,
68+
path2: string,
69+
platform: NodeJS.Platform,
70+
): boolean {
71+
return normalizePath(path1, platform) === normalizePath(path2, platform);
72+
}
73+
74+
/**
75+
* Returns true if path1 contains path2.
76+
*/
77+
export function containsPath(
78+
path1: string,
79+
path2: string,
80+
platform: NodeJS.Platform,
81+
): boolean {
82+
return normalizePath(path2, platform).startsWith(
83+
normalizePath(path1, platform),
84+
);
6985
}
7086

7187
export async function readDirFullPaths(path: string): Promise<string[]> {

extensions/ql-vscode/test/unit-tests/pure/files.test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { join } from "path";
22

33
import {
4+
containsPath,
45
gatherQlFiles,
56
getDirectoryNamesInsidePath,
67
pathsEqual,
@@ -203,3 +204,98 @@ describe("pathsEqual", () => {
203204
},
204205
);
205206
});
207+
208+
describe("containsPath", () => {
209+
const testCases: Array<{
210+
path1: string;
211+
path2: string;
212+
platform: NodeJS.Platform;
213+
expected: boolean;
214+
}> = [
215+
{
216+
path1:
217+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
218+
path2:
219+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
220+
platform: "linux",
221+
expected: true,
222+
},
223+
{
224+
path1:
225+
"/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
226+
path2:
227+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
228+
platform: "linux",
229+
expected: false,
230+
},
231+
{
232+
path1:
233+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
234+
path2:
235+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
236+
platform: "linux",
237+
expected: false,
238+
},
239+
{
240+
path1:
241+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
242+
path2:
243+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
244+
platform: "win32",
245+
expected: true,
246+
},
247+
{
248+
path1:
249+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
250+
path2:
251+
"c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
252+
platform: "win32",
253+
expected: true,
254+
},
255+
{
256+
path1:
257+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
258+
path2:
259+
"D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
260+
platform: "win32",
261+
expected: false,
262+
},
263+
{
264+
path1:
265+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
266+
path2:
267+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
268+
platform: "win32",
269+
expected: false,
270+
},
271+
{
272+
path1:
273+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
274+
path2:
275+
"C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
276+
platform: "win32",
277+
expected: true,
278+
},
279+
{
280+
path1:
281+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
282+
path2:
283+
"D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
284+
platform: "win32",
285+
expected: false,
286+
},
287+
];
288+
289+
test.each(testCases)(
290+
"$path1 contains $path2 on $platform = $expected",
291+
({ path1, path2, platform, expected }) => {
292+
if (platform !== process.platform) {
293+
// We're using the platform-specific path.resolve, so we can't really run
294+
// these tests on all platforms.
295+
return;
296+
}
297+
298+
expect(containsPath(path1, path2, platform)).toEqual(expected);
299+
},
300+
);
301+
});

extensions/ql-vscode/test/vscode-tests/minimal-workspace/local-databases.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ describe("local databases", () => {
8080
dynamicProperties: {
8181
// pretend like databases added in the temp dir are controlled by the extension
8282
// so that they are deleted upon removal
83-
storagePath: () => extensionContextStoragePath,
84-
storageUri: () => Uri.parse(extensionContextStoragePath),
83+
storageUri: () => Uri.file(extensionContextStoragePath),
8584
},
8685
},
8786
);
@@ -277,7 +276,7 @@ describe("local databases", () => {
277276
updateSpy.mockClear();
278277

279278
// pretend that the database location is not controlled by the extension
280-
(databaseManager as any).ctx.storagePath = "hucairz";
279+
(databaseManager as any).ctx.storageUri = Uri.file("hucairz");
281280
extensionContextStoragePath = "hucairz";
282281

283282
await databaseManager.removeDatabaseItem(

0 commit comments

Comments
 (0)