refactor: centralize test path containment checks

This commit is contained in:
Peter Steinberger
2026-01-09 18:47:52 +01:00
parent f0a909f6dd
commit 837cec64af
3 changed files with 40 additions and 44 deletions

19
test/helpers/paths.ts Normal file
View File

@@ -0,0 +1,19 @@
import path from "node:path";
export function isPathWithinBase(base: string, target: string): boolean {
if (process.platform === "win32") {
const normalizedBase = path.win32.normalize(path.win32.resolve(base));
const normalizedTarget = path.win32.normalize(path.win32.resolve(target));
const rel = path.win32.relative(
normalizedBase.toLowerCase(),
normalizedTarget.toLowerCase(),
);
return rel === "" || (!rel.startsWith("..") && !path.win32.isAbsolute(rel));
}
const normalizedBase = path.resolve(base);
const normalizedTarget = path.resolve(target);
const rel = path.relative(normalizedBase, normalizedTarget);
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
}