forked from aboutcode-org/commoncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_paths.py
More file actions
311 lines (240 loc) · 12.5 KB
/
test_paths.py
File metadata and controls
311 lines (240 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/commoncode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from unittest import TestCase
from commoncode import paths
class TestPortablePath(TestCase):
def test_safe_path_mixed_slashes(self):
test = paths.safe_path('C:\\Documents and Settings\\Boki\\Desktop\\head\\patches\\drupal6/drupal.js')
expected = 'C/Documents_and_Settings/Boki/Desktop/head/patches/drupal6/drupal.js'
assert test == expected
def test_safe_path_mixed_slashes_and_spaces(self):
test = paths.safe_path('C:\\Documents and Settings\\Boki\\Desktop\\head\\patches\\parallel uploads/drupal.js')
expected = 'C/Documents_and_Settings/Boki/Desktop/head/patches/parallel_uploads/drupal.js'
assert test == expected
def test_safe_path_windows_style(self):
test = paths.safe_path('C:\\Documents and Settings\\Administrator\\Desktop\\siftDemoV4_old\\defs.h')
expected = 'C/Documents_and_Settings/Administrator/Desktop/siftDemoV4_old/defs.h'
assert test == expected
def test_safe_path_windows_style_mixed_slashes_no_spaces(self):
test = paths.safe_path('C:\\Documents and Settings\\Boki\\Desktop\\head\\patches\\imagefield/imagefield.css')
expected = 'C/Documents_and_Settings/Boki/Desktop/head/patches/imagefield/imagefield.css'
assert test == expected
def test_safe_path_windows_style_spaces(self):
test = paths.safe_path('C:\\Documents and Settings\\Boki\\Desktop\\head\\patches\\js delete\\imagefield.css')
expected = 'C/Documents_and_Settings/Boki/Desktop/head/patches/js_delete/imagefield.css'
assert test == expected
def test_safe_path_windows_style_posix_slashes(self):
test = paths.safe_path('C:/Documents and Settings/Alex Burgel/workspace/Hibernate3.2/test/org/hibernate/test/AllTests.java')
expected = 'C/Documents_and_Settings/Alex_Burgel/workspace/Hibernate3.2/test/org/hibernate/test/AllTests.java'
assert test == expected
def test_safe_path_windows_style_relative(self):
test = paths.safe_path('includes\\webform.components.inc')
expected = 'includes/webform.components.inc'
assert test == expected
def test_safe_path_windows_style_absolute_trailing_slash(self):
test = paths.safe_path('\\includes\\webform.components.inc\\')
expected = 'includes/webform.components.inc'
assert test == expected
def test_safe_path_posix_style_relative(self):
test = paths.safe_path('includes/webform.components.inc')
expected = 'includes/webform.components.inc'
assert test == expected
def test_safe_path_posix_style_absolute_trailing_slash(self):
test = paths.safe_path('/includes/webform.components.inc/')
expected = 'includes/webform.components.inc'
assert test == expected
def test_safe_path_posix_style_french_char(self):
test = paths.safe_path('/includes/webform.compon\xc3nts.inc/')
expected = 'includes/webform.componAnts.inc'
assert test == expected
def test_safe_path_posix_style_chinese_char(self):
test = paths.safe_path(b'/includes/webform.compon\xd2\xaants.inc/')
expected = 'includes/webform.componNSnts.inc'
assert test == expected
def test_safe_path_windows_style_dots(self):
test = paths.safe_path('\\includes\\..\\webform.components.inc\\')
expected = 'webform.components.inc'
assert test == expected
def test_safe_path_windows_style_many_dots(self):
test = paths.safe_path('.\\includes\\.\\..\\..\\..\\webform.components.inc\\.')
expected = 'dotdot/dotdot/webform.components.inc'
assert test == expected
def test_safe_path_posix_style_dots(self):
test = paths.safe_path('includes/../webform.components.inc')
expected = 'webform.components.inc'
assert test == expected
def test_safe_path_posix_style_many_dots(self):
test = paths.safe_path('./includes/./../../../../webform.components.inc/.')
expected = 'dotdot/dotdot/dotdot/webform.components.inc'
assert test == expected
def test_safe_path_posix_only(self):
test_path = 'var/lib/dpkg/info/libgsm1:amd64.list'
test = paths.safe_path(test_path)
expected = 'var/lib/dpkg/info/libgsm1_amd64.list'
assert test == expected
test = paths.safe_path(test_path, posix_only=True)
assert test == test_path
def test_resolve_mixed_slash(self):
test = paths.resolve('C:\\..\\./drupal.js')
expected = 'C/drupal.js'
assert test == expected
def test_resolve_2(self):
test = paths.resolve('\\includes\\..\\webform.components.inc\\')
expected = 'webform.components.inc'
assert test == expected
def test_resolve_3(self):
test = paths.resolve('includes/../webform.components.inc')
expected = 'webform.components.inc'
assert test == expected
def test_resolve_4(self):
test = paths.resolve('////.//includes/./../..//..///../webform.components.inc/.')
expected = 'dotdot/dotdot/dotdot/webform.components.inc'
assert test == expected
def test_resolve_5(self):
test = paths.resolve(u'////.//includes/./../..//..///../webform.components.inc/.')
expected = u'dotdot/dotdot/dotdot/webform.components.inc'
assert test == expected
def test_resolve_6(self):
test = paths.resolve('includes/../')
expected = '.'
assert test == expected
def test_portable_filename(self):
expected = 'A___file__with_Spaces.mov'
assert paths.portable_filename("A:\\ file/ with Spaces.mov") == expected
# Test `preserve_spaces` option. Spaces should not be replaced
expected = 'Program Files (x86)'
assert paths.portable_filename("Program Files (x86)", preserve_spaces=True) == expected
# Unresolved relative paths will be treated as a single filename. Use
# resolve instead if you want to resolve paths:
expected = '___.._.._etc_passwd'
assert paths.portable_filename("../../../etc/passwd") == expected
# Unicode name are transliterated:
expected = 'This_contain_UMLAUT_umlauts.txt'
assert paths.portable_filename(u'This contain UMLAUT \xfcml\xe4uts.txt') == expected
# Check to see if illegal Windows filenames are properly handled
for illegal_window_name in paths.ILLEGAL_WINDOWS_NAMES:
# Rename files with names that are illegal on Windows
expected = f'{illegal_window_name}_'
assert paths.portable_filename(illegal_window_name) == expected
# Allow files with names that are illegal on Windows
assert paths.portable_filename(illegal_window_name, posix_only=True) == illegal_window_name
# Check to see if the posix_only option does and does not replace
# punctuation characters that are illegal in Windows filenames
for valid_posix_path_char in paths.posix_legal_punctuation:
test_name = f'test{valid_posix_path_char}'
assert paths.portable_filename(test_name, posix_only=True) == test_name
if valid_posix_path_char not in paths.legal_punctuation:
expected = f'test_'
assert paths.portable_filename(test_name) == expected
class TestCommonPath(TestCase):
def test_common_path_prefix1(self):
test = paths.common_path_prefix('/a/b/c', '/a/b/c')
assert test == ('a/b/c', 3)
def test_common_path_prefix2(self):
test = paths.common_path_prefix('/a/b/c', '/a/b')
assert test == ('a/b', 2)
def test_common_path_prefix3(self):
test = paths.common_path_prefix('/a/b', '/a/b/c')
assert test == ('a/b', 2)
def test_common_path_prefix4(self):
test = paths.common_path_prefix('/a', '/a')
assert test == ('a', 1)
def test_common_path_prefix_path_root(self):
test = paths.common_path_prefix('/a/b/c', '/')
assert test == (None, 0)
def test_common_path_prefix_root_path(self):
test = paths.common_path_prefix('/', '/a/b/c')
assert test == (None, 0)
def test_common_path_prefix_root_root(self):
test = paths.common_path_prefix('/', '/')
assert test == (None, 0)
def test_common_path_prefix_path_elements_are_similar(self):
test = paths.common_path_prefix('/a/b/c', '/a/b/d')
assert test == ('a/b', 2)
def test_common_path_prefix_no_match(self):
test = paths.common_path_prefix('/abc/d', '/abe/f')
assert test == (None, 0)
def test_common_path_prefix_ignore_training_slashes(self):
test = paths.common_path_prefix('/a/b/c/', '/a/b/c/')
assert test == ('a/b/c', 3)
def test_common_path_prefix8(self):
test = paths.common_path_prefix('/a/b/c/', '/a/b')
assert test == ('a/b', 2)
def test_common_path_prefix10(self):
test = paths.common_path_prefix('/a/b/c.txt', '/a/b/b.txt')
assert test == ('a/b', 2)
def test_common_path_prefix11(self):
test = paths.common_path_prefix('/a/b/c.txt', '/a/b.txt')
assert test == ('a', 1)
def test_common_path_prefix12(self):
test = paths.common_path_prefix('/a/c/e/x.txt', '/a/d/a.txt')
assert test == ('a', 1)
def test_common_path_prefix13(self):
test = paths.common_path_prefix('/a/c/e/x.txt', '/a/d/')
assert test == ('a', 1)
def test_common_path_prefix14(self):
test = paths.common_path_prefix('/a/c/e/', '/a/d/')
assert test == ('a', 1)
def test_common_path_prefix15(self):
test = paths.common_path_prefix('/a/c/e/', '/a/c/a.txt')
assert test == ('a/c', 2)
def test_common_path_prefix16(self):
test = paths.common_path_prefix('/a/c/e/', '/a/c/f/')
assert test == ('a/c', 2)
def test_common_path_prefix17(self):
test = paths.common_path_prefix('/a/a.txt', '/a/b.txt/')
assert test == ('a', 1)
def test_common_path_prefix18(self):
test = paths.common_path_prefix('/a/c/', '/a/')
assert test == ('a', 1)
def test_common_path_prefix19(self):
test = paths.common_path_prefix('/a/c.txt', '/a/')
assert test == ('a', 1)
def test_common_path_prefix20(self):
test = paths.common_path_prefix('/a/c/', '/a/d/')
assert test == ('a', 1)
def test_common_path_suffix(self):
test = paths.common_path_suffix('/a/b/c', '/a/b/c')
assert test == ('a/b/c', 3)
def test_common_path_suffix_absolute_relative(self):
test = paths.common_path_suffix('a/b/c', '/a/b/c')
assert test == ('a/b/c', 3)
def test_common_path_suffix_find_subpath(self):
test = paths.common_path_suffix('/z/b/c', '/a/b/c')
assert test == ('b/c', 2)
def test_common_path_suffix_handles_relative_path(self):
test = paths.common_path_suffix('a/b', 'a/b')
assert test == ('a/b', 2)
def test_common_path_suffix_handles_relative_subpath(self):
test = paths.common_path_suffix('zsds/adsds/a/b/b/c', 'a//a/d//b/c')
assert test == ('b/c', 2)
def test_common_path_suffix_ignore_and_strip_trailing_slash(self):
test = paths.common_path_suffix('zsds/adsds/a/b/b/c/', 'a//a/d//b/c/')
assert test == ('b/c', 2)
def test_common_path_suffix_return_None_if_no_common_suffix(self):
test = paths.common_path_suffix('/a/b/c', '/')
assert test == (None, 0)
def test_common_path_suffix_return_None_if_no_common_suffix2(self):
test = paths.common_path_suffix('/', '/a/b/c')
assert test == (None, 0)
def test_common_path_suffix_match_only_whole_segments(self):
# only segments are honored, commonality within segment is ignored
test = paths.common_path_suffix('this/is/aaaa/great/path', 'this/is/aaaaa/great/path')
assert test == ('great/path', 2)
def test_common_path_suffix_two_root(self):
test = paths.common_path_suffix('/', '/')
assert test == (None, 0)
def test_common_path_suffix_empty_root(self):
test = paths.common_path_suffix('', '/')
assert test == (None, 0)
def test_common_path_suffix_root_empty(self):
test = paths.common_path_suffix('/', '')
assert test == (None, 0)
def test_common_path_suffix_empty_empty(self):
test = paths.common_path_suffix('', '')
assert test == (None, 0)