filepath.test.ts
This file
Path
src/utils/filepath.test.ts
78 lines 路 3.4 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { getFilePath, getFilePathWithoutDefaultDocument } from './filepath'
describe('getFilePathWithoutDefaultDocument', () => { it('Should return file path correctly', async () => { expect(getFilePathWithoutDefaultDocument({ filename: 'foo.txt' })).toBe('foo.txt') expect(getFilePathWithoutDefaultDocument({ filename: 'foo.txt', root: 'bar' })).toBe( 'bar/foo.txt' )
expect(getFilePathWithoutDefaultDocument({ filename: '../foo' })).toBeUndefined() expect(getFilePathWithoutDefaultDocument({ filename: '/../foo' })).toBeUndefined() expect(getFilePathWithoutDefaultDocument({ filename: './../foo' })).toBeUndefined() expect(getFilePathWithoutDefaultDocument({ filename: 'foo..bar.txt' })).toBe('foo..bar.txt') expect(getFilePathWithoutDefaultDocument({ filename: '/foo..bar.txt' })).toBe('foo..bar.txt') expect(getFilePathWithoutDefaultDocument({ filename: './foo..bar.txt' })).toBe('foo..bar.txt') expect(getFilePathWithoutDefaultDocument({ filename: './..foo/bar.txt' })).toBe('..foo/bar.txt') expect(getFilePathWithoutDefaultDocument({ filename: './foo../bar.txt' })).toBe('foo../bar.txt') expect(getFilePathWithoutDefaultDocument({ filename: './..foo../bar.txt' })).toBe( '..foo../bar.txt' )
expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('/../foo') }) ).toBeUndefined() expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('./../foo') }) ).toBeUndefined() expect(getFilePathWithoutDefaultDocument({ filename: slashToBackslash('foo..bar.txt') })).toBe( 'foo..bar.txt' ) expect(getFilePathWithoutDefaultDocument({ filename: slashToBackslash('/foo..bar.txt') })).toBe( 'foo..bar.txt' ) expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('./foo..bar.txt') }) ).toBe('foo..bar.txt') expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('./..foo/bar.txt') }) ).toBe('..foo/bar.txt') expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('./foo../bar.txt') }) ).toBe('foo../bar.txt') expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('./..foo../bar.txt') }) ).toBe('..foo../bar.txt')
// Multiple path segments: every backslash must be normalized, not just the first expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('foo/bar/baz.txt') }) ).toBe('foo/bar/baz.txt') expect( getFilePathWithoutDefaultDocument({ filename: slashToBackslash('foo/bar/baz.txt'), root: 'assets', }) ).toBe('assets/foo/bar/baz.txt') })})
describe('getFilePath', () => { it('Should return file path correctly', async () => { expect(getFilePath({ filename: 'foo' })).toBe('foo/index.html')
expect(getFilePath({ filename: 'foo', root: 'bar' })).toBe('bar/foo/index.html')
expect(getFilePath({ filename: 'foo', defaultDocument: 'index.txt' })).toBe('foo/index.txt') expect(getFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe( 'bar/foo/index.txt' )
expect(getFilePath({ filename: 'filename.suffix_index' })).toBe('filename.suffix_index') expect(getFilePath({ filename: 'filename.suffix-index' })).toBe('filename.suffix-index') })})
function slashToBackslash(filename: string) { return filename.split('/').join('\\')}