-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_paths.py
More file actions
293 lines (227 loc) · 11.4 KB
/
Copy pathtest_paths.py
File metadata and controls
293 lines (227 loc) · 11.4 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
#
# Copyright (c) nexB Inc. and others.
# SPDX-License-Identifier: Apache-2.0
#
# Visit https://aboutcode.org and https://github.com/nexB/ for support and download.
# ScanCode is a trademark of nexB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
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 expected == test
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 expected == test
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 expected == test
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 expected == test
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 expected == test
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 expected == test
def test_safe_path_windows_style_relative(self):
test = paths.safe_path('includes\\webform.components.inc')
expected = 'includes/webform.components.inc'
assert expected == test
def test_safe_path_windows_style_absolute_trailing_slash(self):
test = paths.safe_path('\\includes\\webform.components.inc\\')
expected = 'includes/webform.components.inc'
assert expected == test
def test_safe_path_posix_style_relative(self):
test = paths.safe_path('includes/webform.components.inc')
expected = 'includes/webform.components.inc'
assert expected == test
def test_safe_path_posix_style_absolute_trailing_slash(self):
test = paths.safe_path('/includes/webform.components.inc/')
expected = 'includes/webform.components.inc'
assert expected == test
def test_safe_path_posix_style_french_char(self):
test = paths.safe_path('/includes/webform.compon\xc3nts.inc/')
expected = 'includes/webform.componAnts.inc'
assert expected == test
def test_safe_path_posix_style_chinese_char(self):
test = paths.safe_path(b'/includes/webform.compon\xd2\xaants.inc/')
expected = 'includes/webform.componS_nts.inc'
assert expected == test
def test_safe_path_windows_style_dots(self):
test = paths.safe_path('\\includes\\..\\webform.components.inc\\')
expected = 'webform.components.inc'
assert expected == test
def test_safe_path_windows_style_many_dots(self):
test = paths.safe_path('.\\includes\\.\\..\\..\\..\\webform.components.inc\\.')
expected = 'dotdot/dotdot/webform.components.inc'
assert expected == test
def test_safe_path_posix_style_dots(self):
test = paths.safe_path('includes/../webform.components.inc')
expected = 'webform.components.inc'
assert expected == test
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 expected == test
def test_resolve_mixed_slash(self):
test = paths.resolve('C:\\..\\./drupal.js')
expected = 'C/drupal.js'
assert expected == test
def test_resolve_2(self):
test = paths.resolve('\\includes\\..\\webform.components.inc\\')
expected = 'webform.components.inc'
assert expected == test
def test_resolve_3(self):
test = paths.resolve('includes/../webform.components.inc')
expected = 'webform.components.inc'
assert expected == test
def test_resolve_4(self):
test = paths.resolve('////.//includes/./../..//..///../webform.components.inc/.')
expected = 'dotdot/dotdot/dotdot/webform.components.inc'
assert expected == test
def test_resolve_5(self):
test = paths.resolve(u'////.//includes/./../..//..///../webform.components.inc/.')
expected = u'dotdot/dotdot/dotdot/webform.components.inc'
assert expected == test
def test_resolve_6(self):
test = paths.resolve('includes/../')
expected = '.'
assert expected == test
def test_portable_filename(self):
expected = 'A___file__with_Spaces.mov'
assert expected == paths.portable_filename("A:\\ file/ with Spaces.mov")
# Unresolved relative paths will be treated as a single filename. Use
# resolve instead if you want to resolve paths:
expected = '___.._.._etc_passwd'
assert expected == paths.portable_filename("../../../etc/passwd")
# Unicode name are transliterated:
expected = 'This_contain_UMLAUT_umlauts.txt'
assert expected == paths.portable_filename(u'This contain UMLAUT \xfcml\xe4uts.txt')
class TestCommonPath(TestCase):
def test_common_path_prefix1(self):
test = paths.common_path_prefix('/a/b/c', '/a/b/c')
assert ('a/b/c', 3) == test
def test_common_path_prefix2(self):
test = paths.common_path_prefix('/a/b/c', '/a/b')
assert ('a/b', 2) == test
def test_common_path_prefix3(self):
test = paths.common_path_prefix('/a/b', '/a/b/c')
assert ('a/b', 2) == test
def test_common_path_prefix4(self):
test = paths.common_path_prefix('/a', '/a')
assert ('a', 1) == test
def test_common_path_prefix_path_root(self):
test = paths.common_path_prefix('/a/b/c', '/')
assert (None, 0) == test
def test_common_path_prefix_root_path(self):
test = paths.common_path_prefix('/', '/a/b/c')
assert (None, 0) == test
def test_common_path_prefix_root_root(self):
test = paths.common_path_prefix('/', '/')
assert (None, 0) == test
def test_common_path_prefix_path_elements_are_similar(self):
test = paths.common_path_prefix('/a/b/c', '/a/b/d')
assert ('a/b', 2) == test
def test_common_path_prefix_no_match(self):
test = paths.common_path_prefix('/abc/d', '/abe/f')
assert (None, 0) == test
def test_common_path_prefix_ignore_training_slashes(self):
test = paths.common_path_prefix('/a/b/c/', '/a/b/c/')
assert ('a/b/c', 3) == test
def test_common_path_prefix8(self):
test = paths.common_path_prefix('/a/b/c/', '/a/b')
assert ('a/b', 2) == test
def test_common_path_prefix10(self):
test = paths.common_path_prefix('/a/b/c.txt', '/a/b/b.txt')
assert ('a/b', 2) == test
def test_common_path_prefix11(self):
test = paths.common_path_prefix('/a/b/c.txt', '/a/b.txt')
assert ('a', 1) == test
def test_common_path_prefix12(self):
test = paths.common_path_prefix('/a/c/e/x.txt', '/a/d/a.txt')
assert ('a', 1) == test
def test_common_path_prefix13(self):
test = paths.common_path_prefix('/a/c/e/x.txt', '/a/d/')
assert ('a', 1) == test
def test_common_path_prefix14(self):
test = paths.common_path_prefix('/a/c/e/', '/a/d/')
assert ('a', 1) == test
def test_common_path_prefix15(self):
test = paths.common_path_prefix('/a/c/e/', '/a/c/a.txt')
assert ('a/c', 2) == test
def test_common_path_prefix16(self):
test = paths.common_path_prefix('/a/c/e/', '/a/c/f/')
assert ('a/c', 2) == test
def test_common_path_prefix17(self):
test = paths.common_path_prefix('/a/a.txt', '/a/b.txt/')
assert ('a', 1) == test
def test_common_path_prefix18(self):
test = paths.common_path_prefix('/a/c/', '/a/')
assert ('a', 1) == test
def test_common_path_prefix19(self):
test = paths.common_path_prefix('/a/c.txt', '/a/')
assert ('a', 1) == test
def test_common_path_prefix20(self):
test = paths.common_path_prefix('/a/c/', '/a/d/')
assert ('a', 1) == test
def test_common_path_suffix(self):
test = paths.common_path_suffix('/a/b/c', '/a/b/c')
assert ('a/b/c', 3) == test
def test_common_path_suffix_absolute_relative(self):
test = paths.common_path_suffix('a/b/c', '/a/b/c')
assert ('a/b/c', 3) == test
def test_common_path_suffix_find_subpath(self):
test = paths.common_path_suffix('/z/b/c', '/a/b/c')
assert ('b/c', 2) == test
def test_common_path_suffix_handles_relative_path(self):
test = paths.common_path_suffix('a/b', 'a/b')
assert ('a/b', 2) == test
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 ('b/c', 2) == test
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 ('b/c', 2) == test
def test_common_path_suffix_return_None_if_no_common_suffix(self):
test = paths.common_path_suffix('/a/b/c', '/')
assert (None, 0) == test
def test_common_path_suffix_return_None_if_no_common_suffix2(self):
test = paths.common_path_suffix('/', '/a/b/c')
assert (None, 0) == test
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 ('great/path', 2) == test
def test_common_path_suffix_two_root(self):
test = paths.common_path_suffix('/', '/')
assert (None, 0) == test
def test_common_path_suffix_empty_root(self):
test = paths.common_path_suffix('', '/')
assert (None, 0) == test
def test_common_path_suffix_root_empty(self):
test = paths.common_path_suffix('/', '')
assert (None, 0) == test
def test_common_path_suffix_empty_empty(self):
test = paths.common_path_suffix('', '')
assert (None, 0) == test