backstage
74 строки · 2.8 Кб
1/*
2* Copyright 2021 The Backstage Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17import { posix, win32 } from 'path';18
19describe('isChildPath', () => {20it('should check child posix paths', () => {21jest.isolateModules(() => {22jest.setMock('path', posix);23const { isChildPath } = require('./isChildPath');24
25expect(isChildPath('/', '/')).toBe(true);26expect(isChildPath('/x', '/x')).toBe(true);27expect(isChildPath('/x', '/x/y')).toBe(true);28expect(isChildPath('/x', '/x/x')).toBe(true);29expect(isChildPath('/x', '/x/y/z')).toBe(true);30expect(isChildPath('/x/y', '/x/y/z')).toBe(true);31expect(isChildPath('/x/y/z', '/x/y/z')).toBe(true);32expect(isChildPath('/x/a b c/z', '/x/a b c/z')).toBe(true);33expect(isChildPath('/', '/ yz')).toBe(true);34
35expect(isChildPath('/x', '/y')).toBe(false);36expect(isChildPath('/x', '/')).toBe(false);37expect(isChildPath('/x', '/x y')).toBe(false);38expect(isChildPath('/x y', '/x yz')).toBe(false);39expect(isChildPath('/ yz', '/')).toBe(false);40expect(isChildPath('/x', '/')).toBe(false);41
42jest.dontMock('path');43});44});45
46it('should check child win32 paths', () => {47jest.isolateModules(() => {48jest.setMock('path', win32);49const { isChildPath } = require('./isChildPath');50
51expect(isChildPath('/x', '/x')).toBe(true);52expect(isChildPath('/x', '/x/y')).toBe(true);53expect(isChildPath('/x', '/x/x')).toBe(true);54expect(isChildPath('/x', '/x/y/z')).toBe(true);55expect(isChildPath('/x/y', '/x/y/z')).toBe(true);56expect(isChildPath('/x/y/z', '/x/y/z')).toBe(true);57expect(isChildPath('Z:', 'Z:')).toBe(true);58expect(isChildPath('C:/', 'c:/')).toBe(true);59expect(isChildPath('C:/x', 'C:/x')).toBe(true);60expect(isChildPath('C:/x', 'c:/x')).toBe(true);61expect(isChildPath('C:/x', 'C:/x/y')).toBe(true);62expect(isChildPath('d:/x', 'D:/x/y')).toBe(true);63
64expect(isChildPath('/x', '/y')).toBe(false);65expect(isChildPath('/x', '/')).toBe(false);66expect(isChildPath('C:/', 'D:/')).toBe(false);67expect(isChildPath('C:/x', 'D:/x')).toBe(false);68expect(isChildPath('D:/x', 'CD:/x')).toBe(false);69expect(isChildPath('D:/x', 'D:/y')).toBe(false);70
71jest.dontMock('path');72});73});74});75