fix: use realpath for fs.contains

This commit is contained in:
Yang Jun
2026-04-05 03:57:54 +08:00
parent abc058be0f
commit cca3da6147
38 changed files with 188 additions and 48 deletions
+14 -2
View File
@@ -8,7 +8,7 @@ interface FileDescriptor {
}
let files: { [path: string]: FileDescriptor } = {}
const { readFile, exists, readFileSync, existsSync } = fs
const { readFile, exists, readFileSync, existsSync, contains, containsSync } = fs
export function mock (options: { [path: string]: (string | FileDescriptor) }) {
forOwn(options, (val, key) => {
@@ -30,6 +30,16 @@ export function mock (options: { [path: string]: (string | FileDescriptor) }) {
};
(fs as any).existsSync = function (path: string) {
return !!files[path]
};
(fs as any).contains = async (root: string, file: string) => {
root = resolve(root)
if (!root.endsWith('/')) root += '/'
return file.startsWith(root)
};
(fs as any).containsSync = (root: string, file: string) => {
root = resolve(root)
if (!root.endsWith('/')) root += '/'
return file.startsWith(root)
}
}
@@ -38,5 +48,7 @@ export function restore () {
(fs as any).readFileSync = readFileSync;
(fs as any).existsSync = existsSync;
(fs as any).readFile = readFile;
(fs as any).exists = exists
(fs as any).exists = exists;
(fs as any).contains = contains;
(fs as any).containsSync = containsSync
}