From fad00aa14e8beac0514ec4196944b33276f1ff2c Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Mon, 8 Jul 2024 09:59:45 +0800 Subject: [PATCH] test: edge cases for map fs --- src/fs/map-fs.spec.ts | 9 +++++++++ src/fs/map-fs.ts | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/fs/map-fs.spec.ts b/src/fs/map-fs.spec.ts index 269318d26b..c0c4ea3545 100644 --- a/src/fs/map-fs.spec.ts +++ b/src/fs/map-fs.spec.ts @@ -14,4 +14,13 @@ describe('MapFS', () => { it('should resolve exceeding root', () => { expect(fs.resolve('foo/bar', '../../../coo', '')).toEqual('coo') }) + it('should resolve from absolute path', () => { + expect(fs.resolve('/foo/bar', '../../coo', '')).toEqual('/coo') + }) + it('should resolve exceeding root from absolute path', () => { + expect(fs.resolve('/foo/bar', '../../../coo', '')).toEqual('/coo') + }) + it('should resolve from invalid path', () => { + expect(fs.resolve('foo//bar', '../coo', '')).toEqual('foo/coo') + }) }) diff --git a/src/fs/map-fs.ts b/src/fs/map-fs.ts index 8783b14cce..78bc611253 100644 --- a/src/fs/map-fs.ts +++ b/src/fs/map-fs.ts @@ -32,10 +32,12 @@ export class MapFS { resolve (dir: string, file: string, ext: string) { file += ext if (dir === '.') return file - const segments = dir.split(this.sep) + const segments = dir.split(/\/+/) for (const segment of file.split(this.sep)) { if (segment === '.' || segment === '') continue - else if (segment === '..') segments.pop() + else if (segment === '..') { + if (segments.length > 1 || segments[0] !== '') segments.pop() + } else segments.push(segment) } return segments.join(this.sep)