Skip to content

Commit 400bde6

Browse files
Merge pull request #2509 from github/robertbrignull/contains_path
Fix containsPath for paths that share a prefix
2 parents 6dbbd22 + 3a9fa42 commit 400bde6

File tree

5 files changed

+79
-44
lines changed

5 files changed

+79
-44
lines changed

extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ export class DatabaseItemImpl implements DatabaseItem {
207207
return pathsEqual(
208208
databasePath,
209209
join(testdir, `${testdirbase}.testproj`),
210-
process.platform,
211210
);
212211
}
213212
} catch {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ export class DatabaseManager extends DisposableObject {
652652
private isExtensionControlledLocation(uri: vscode.Uri) {
653653
const storageUri = this.ctx.storageUri || this.ctx.globalStorageUri;
654654
if (storageUri) {
655-
return containsPath(storageUri.fsPath, uri.fsPath, process.platform);
655+
return containsPath(storageUri.fsPath, uri.fsPath);
656656
}
657657
return false;
658658
}

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { pathExists, stat, readdir, opendir } from "fs-extra";
2-
import { join, resolve } from "path";
2+
import { isAbsolute, join, relative, resolve } from "path";
33

44
/**
55
* Recursively finds all .ql files in this set of Uris.
@@ -51,36 +51,32 @@ export async function getDirectoryNamesInsidePath(
5151
return dirNames;
5252
}
5353

54-
function normalizePath(path: string, platform: NodeJS.Platform): string {
54+
function normalizePath(path: string): string {
5555
// On Windows, "C:/", "C:\", and "c:/" are all equivalent. We need
5656
// to normalize the paths to ensure they all get resolved to the
5757
// same format. On Windows, we also need to do the comparison
5858
// case-insensitively.
5959
path = resolve(path);
60-
if (platform === "win32") {
60+
if (process.platform === "win32") {
6161
path = path.toLowerCase();
6262
}
6363
return path;
6464
}
6565

66-
export function pathsEqual(
67-
path1: string,
68-
path2: string,
69-
platform: NodeJS.Platform,
70-
): boolean {
71-
return normalizePath(path1, platform) === normalizePath(path2, platform);
66+
export function pathsEqual(path1: string, path2: string): boolean {
67+
return normalizePath(path1) === normalizePath(path2);
7268
}
7369

7470
/**
75-
* Returns true if path1 contains path2.
71+
* Returns true if `parent` contains `child`, or if they are equal.
7672
*/
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),
73+
export function containsPath(parent: string, child: string): boolean {
74+
const relativePath = relative(parent, child);
75+
return (
76+
!relativePath.startsWith("..") &&
77+
// On windows, if the two paths are in different drives, then the
78+
// relative path will be an absolute path to the other drive.
79+
!isAbsolute(relativePath)
8480
);
8581
}
8682

extensions/ql-vscode/test/matchers/toEqualPath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const toEqualPath: MatcherFunction<[expectedPath: unknown]> = function (
1010
throw new Error("These must be of type string!");
1111
}
1212

13-
const pass = pathsEqual(actual, expectedPath, process.platform);
13+
const pass = pathsEqual(actual, expectedPath);
1414
if (pass) {
1515
return {
1616
message: () =>

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

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -204,102 +204,142 @@ describe("pathsEqual", () => {
204204
return;
205205
}
206206

207-
expect(pathsEqual(path1, path2, platform)).toEqual(expected);
207+
expect(pathsEqual(path1, path2)).toEqual(expected);
208208
},
209209
);
210210
});
211211

212212
describe("containsPath", () => {
213213
const testCases: Array<{
214-
path1: string;
215-
path2: string;
214+
parent: string;
215+
child: string;
216216
platform: NodeJS.Platform;
217217
expected: boolean;
218218
}> = [
219219
{
220-
path1:
220+
parent:
221221
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
222-
path2:
222+
child:
223223
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
224224
platform: "linux",
225225
expected: true,
226226
},
227227
{
228-
path1:
228+
parent:
229229
"/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
230-
path2:
230+
child:
231231
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
232232
platform: "linux",
233233
expected: false,
234234
},
235235
{
236-
path1:
236+
parent:
237237
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
238-
path2:
238+
child:
239239
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
240240
platform: "linux",
241241
expected: false,
242242
},
243243
{
244-
path1:
244+
parent:
245+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
246+
child:
247+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
248+
platform: "linux",
249+
expected: false,
250+
},
251+
{
252+
parent:
253+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
254+
child:
255+
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
256+
platform: "linux",
257+
expected: false,
258+
},
259+
{
260+
parent:
245261
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
246-
path2:
262+
child:
247263
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
248264
platform: "win32",
249265
expected: true,
250266
},
251267
{
252-
path1:
268+
parent:
253269
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
254-
path2:
270+
child:
255271
"c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
256272
platform: "win32",
257273
expected: true,
258274
},
259275
{
260-
path1:
276+
parent:
261277
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
262-
path2:
278+
child:
279+
"C:/USERS/GITHUB/PROJECTS/VSCODE-CODEQL-STARTER/CODEQL-CUSTOM-QUERIES-JAVASCRIPT/EXAMPLE.QL",
280+
platform: "win32",
281+
expected: true,
282+
},
283+
{
284+
parent:
285+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
286+
child:
263287
"D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
264288
platform: "win32",
265289
expected: false,
266290
},
267291
{
268-
path1:
292+
parent:
269293
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
270-
path2:
294+
child:
271295
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
272296
platform: "win32",
273297
expected: false,
274298
},
275299
{
276-
path1:
300+
parent:
277301
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
278-
path2:
302+
child:
279303
"C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
280304
platform: "win32",
281305
expected: true,
282306
},
283307
{
284-
path1:
308+
parent:
285309
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
286-
path2:
310+
child:
287311
"D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
288312
platform: "win32",
289313
expected: false,
290314
},
315+
{
316+
parent:
317+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
318+
child:
319+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
320+
platform: "win32",
321+
expected: false,
322+
},
323+
{
324+
parent:
325+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
326+
child:
327+
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
328+
platform: "win32",
329+
expected: false,
330+
},
291331
];
292332

293333
test.each(testCases)(
294-
"$path1 contains $path2 on $platform = $expected",
295-
({ path1, path2, platform, expected }) => {
334+
"$parent contains $child on $platform = $expected",
335+
({ parent, child, platform, expected }) => {
296336
if (platform !== process.platform) {
297337
// We're using the platform-specific path.resolve, so we can't really run
298338
// these tests on all platforms.
299339
return;
300340
}
301341

302-
expect(containsPath(path1, path2, platform)).toEqual(expected);
342+
expect(containsPath(parent, child)).toEqual(expected);
303343
},
304344
);
305345
});

0 commit comments

Comments
 (0)