backstage

Форк
0
/
isChildPath.test.ts 
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

17
import { posix, win32 } from 'path';
18

19
describe('isChildPath', () => {
20
  it('should check child posix paths', () => {
21
    jest.isolateModules(() => {
22
      jest.setMock('path', posix);
23
      const { isChildPath } = require('./isChildPath');
24

25
      expect(isChildPath('/', '/')).toBe(true);
26
      expect(isChildPath('/x', '/x')).toBe(true);
27
      expect(isChildPath('/x', '/x/y')).toBe(true);
28
      expect(isChildPath('/x', '/x/x')).toBe(true);
29
      expect(isChildPath('/x', '/x/y/z')).toBe(true);
30
      expect(isChildPath('/x/y', '/x/y/z')).toBe(true);
31
      expect(isChildPath('/x/y/z', '/x/y/z')).toBe(true);
32
      expect(isChildPath('/x/a b c/z', '/x/a b c/z')).toBe(true);
33
      expect(isChildPath('/', '/ yz')).toBe(true);
34

35
      expect(isChildPath('/x', '/y')).toBe(false);
36
      expect(isChildPath('/x', '/')).toBe(false);
37
      expect(isChildPath('/x', '/x y')).toBe(false);
38
      expect(isChildPath('/x y', '/x yz')).toBe(false);
39
      expect(isChildPath('/ yz', '/')).toBe(false);
40
      expect(isChildPath('/x', '/')).toBe(false);
41

42
      jest.dontMock('path');
43
    });
44
  });
45

46
  it('should check child win32 paths', () => {
47
    jest.isolateModules(() => {
48
      jest.setMock('path', win32);
49
      const { isChildPath } = require('./isChildPath');
50

51
      expect(isChildPath('/x', '/x')).toBe(true);
52
      expect(isChildPath('/x', '/x/y')).toBe(true);
53
      expect(isChildPath('/x', '/x/x')).toBe(true);
54
      expect(isChildPath('/x', '/x/y/z')).toBe(true);
55
      expect(isChildPath('/x/y', '/x/y/z')).toBe(true);
56
      expect(isChildPath('/x/y/z', '/x/y/z')).toBe(true);
57
      expect(isChildPath('Z:', 'Z:')).toBe(true);
58
      expect(isChildPath('C:/', 'c:/')).toBe(true);
59
      expect(isChildPath('C:/x', 'C:/x')).toBe(true);
60
      expect(isChildPath('C:/x', 'c:/x')).toBe(true);
61
      expect(isChildPath('C:/x', 'C:/x/y')).toBe(true);
62
      expect(isChildPath('d:/x', 'D:/x/y')).toBe(true);
63

64
      expect(isChildPath('/x', '/y')).toBe(false);
65
      expect(isChildPath('/x', '/')).toBe(false);
66
      expect(isChildPath('C:/', 'D:/')).toBe(false);
67
      expect(isChildPath('C:/x', 'D:/x')).toBe(false);
68
      expect(isChildPath('D:/x', 'CD:/x')).toBe(false);
69
      expect(isChildPath('D:/x', 'D:/y')).toBe(false);
70

71
      jest.dontMock('path');
72
    });
73
  });
74
});
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.