|
| 1 | +import { isSubPath } from '..'; |
| 2 | +import * as Path from 'path'; |
| 3 | + |
| 4 | +describe('utils | isSubPath', () => { |
| 5 | + it('should return true if path is a subpath', () => { |
| 6 | + expect(isSubPath('/root', '/root/child')).toBeTruthy(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should return false if path is not a subpath', () => { |
| 10 | + expect(isSubPath('/root/foo', '/root/bar/baz')).toBeFalsy(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should return false if path has a similar substring', () => { |
| 14 | + expect(isSubPath('/root/foo', '/root/foo bar/baz')).toBeFalsy(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should return true if path has special path .. symbols and it's a subpath", () => { |
| 18 | + expect(isSubPath('/root/foo', '/root/../root/foo/bar')).toBeTruthy(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should return true if path has special path . symbol and it's a subpath", () => { |
| 22 | + expect(isSubPath('/root/foo', '/root/./foo/bar')).toBeTruthy(); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('utils | isSubPath [win32]', () => { |
| 27 | + let relativeSpy: jest.SpyInstance; |
| 28 | + let isAbsoluteSpy: jest.SpyInstance; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + relativeSpy = jest |
| 32 | + .spyOn(Path, 'relative') |
| 33 | + .mockImplementationOnce(Path.win32.relative); |
| 34 | + isAbsoluteSpy = jest |
| 35 | + .spyOn(Path, 'isAbsolute') |
| 36 | + .mockImplementationOnce(Path.win32.isAbsolute); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + relativeSpy.mockRestore(); |
| 41 | + isAbsoluteSpy.mockRestore(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return true if path is a subpath', () => { |
| 45 | + expect(isSubPath('C:\\Foo', 'C:\\Foo\\Bar')).toBeTruthy(); |
| 46 | + }); |
| 47 | + it('should return false if path is not a subpath', () => { |
| 48 | + expect(isSubPath('C:\\Foo\\Bar', 'D:\\Foo\\Bar')).toBeFalsy(); |
| 49 | + }); |
| 50 | +}); |
0 commit comments