Skip to content

Commit

Permalink
test: edge cases for map fs
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jul 9, 2024
1 parent df27ac6 commit fad00aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/fs/map-fs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
6 changes: 4 additions & 2 deletions src/fs/map-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fad00aa

Please sign in to comment.