test: edge cases for map fs

This commit is contained in:
Yang Jun
2024-07-09 22:51:11 +08:00
committed by Jun Yang
parent df27ac6947
commit fad00aa14e
2 changed files with 13 additions and 2 deletions
+9
View File
@@ -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')
})
})
+4 -2
View File
@@ -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)