Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ export class DatabaseItemImpl implements DatabaseItem {
return pathsEqual(
databasePath,
join(testdir, `${testdirbase}.testproj`),
process.platform,
);
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export class DatabaseManager extends DisposableObject {
private isExtensionControlledLocation(uri: vscode.Uri) {
const storageUri = this.ctx.storageUri || this.ctx.globalStorageUri;
if (storageUri) {
return containsPath(storageUri.fsPath, uri.fsPath, process.platform);
return containsPath(storageUri.fsPath, uri.fsPath);
}
return false;
}
Expand Down
30 changes: 13 additions & 17 deletions extensions/ql-vscode/src/pure/files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pathExists, stat, readdir, opendir } from "fs-extra";
import { join, resolve } from "path";
import { isAbsolute, join, relative, resolve } from "path";

/**
* Recursively finds all .ql files in this set of Uris.
Expand Down Expand Up @@ -51,36 +51,32 @@ export async function getDirectoryNamesInsidePath(
return dirNames;
}

function normalizePath(path: string, platform: NodeJS.Platform): string {
function normalizePath(path: string): 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") {
if (process.platform === "win32") {
path = path.toLowerCase();
}
return path;
}

export function pathsEqual(
path1: string,
path2: string,
platform: NodeJS.Platform,
): boolean {
return normalizePath(path1, platform) === normalizePath(path2, platform);
export function pathsEqual(path1: string, path2: string): boolean {
return normalizePath(path1) === normalizePath(path2);
}

/**
* Returns true if path1 contains path2.
* Returns true if `parent` contains `child`, or if they are equal.
*/
export function containsPath(
path1: string,
path2: string,
platform: NodeJS.Platform,
): boolean {
return normalizePath(path2, platform).startsWith(
normalizePath(path1, platform),
export function containsPath(parent: string, child: string): boolean {
const relativePath = relative(parent, child);
return (
!relativePath.startsWith("..") &&
// On windows, if the two paths are in different drives, then the
// relative path will be an absolute path to the other drive.
!isAbsolute(relativePath)
);
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/ql-vscode/test/matchers/toEqualPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const toEqualPath: MatcherFunction<[expectedPath: unknown]> = function (
throw new Error("These must be of type string!");
}

const pass = pathsEqual(actual, expectedPath, process.platform);
const pass = pathsEqual(actual, expectedPath);
if (pass) {
return {
message: () =>
Expand Down
88 changes: 64 additions & 24 deletions extensions/ql-vscode/test/unit-tests/pure/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,102 +204,142 @@ describe("pathsEqual", () => {
return;
}

expect(pathsEqual(path1, path2, platform)).toEqual(expected);
expect(pathsEqual(path1, path2)).toEqual(expected);
},
);
});

describe("containsPath", () => {
const testCases: Array<{
path1: string;
path2: string;
parent: string;
child: string;
platform: NodeJS.Platform;
expected: boolean;
}> = [
{
path1:
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: true,
},
{
path1:
parent:
"/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: false,
},
{
path1:
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "linux",
expected: false,
},
{
path1:
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "linux",
expected: false,
},
{
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
path1:
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
path1:
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"C:/USERS/GITHUB/PROJECTS/VSCODE-CODEQL-STARTER/CODEQL-CUSTOM-QUERIES-JAVASCRIPT/EXAMPLE.QL",
platform: "win32",
expected: true,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: false,
},
{
path1:
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "win32",
expected: false,
},
{
path1:
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: true,
},
{
path1:
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
path2:
child:
"D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "win32",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"C:/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 }) => {
"$parent contains $child on $platform = $expected",
({ parent, child, 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);
expect(containsPath(parent, child)).toEqual(expected);
},
);
});
Expand Down