Debian Codegraph @

tests/test_brace.py

raw symbols SCIP
1 """
2 Test braces.
3
4 Looking for brace test cases, I stumbled on https://github.com/juliangruber/brace-expansion.
5 The project contained great tests that mirror Bash 4.3's behavior. And while this library
6 was written independently, we used their test sweet to bring this up to Bash 4.3 standard.
7 """
8 import unittest
9 import pytest
10 import bracex
11 import ast
12 import re
13
14 RE_REMOVE = re.compile(r'^\[|\]$')
15 BRE_REMOVE = re.compile(br'^\[|\]$')
16
17
18 def get_bash_cases():
19 """Setup module."""
20
21 re_split = re.compile(r'><><><><')
22
23 with open('tests/brace-results.txt', 'r') as r:
24 # Split by test cases
25 results = re_split.split(r.read())
26 results.pop()
27 # Split by line within test case
28 wanted = [x.split('\n') for x in results]
29
30 with open('tests/brace-cases.txt', 'r') as r:
31 # Split by line ignoring commented lines.
32 # We may have blank lines that get included,
33 # But the test will compare those too, so it
34 # isn't a problem.
35 cases = [x.strip() for x in r.read().split('\n') if not x.startswith('#')]
36 cases.pop()
37
38 bash_cases = []
39 while cases:
40 bash_cases.append((cases.pop(0), wanted.pop(0)))
41 return bash_cases
42
43
44 class TestBraces:
45 """Test globbing."""
46
47 dollar_cases = [
48 ['${1..3}', ['${1..3}']],
49 ['${a,b}${c,d}', ['${a,b}${c,d}']],
50 ['x${a,b}x${c,d}x', ['x${a,b}x${c,d}x']]
51 ]
52
53 empty_cases = [
54 ['-v{,,,,}', ['-v', '-v', '-v', '-v', '-v']],
55 ['{,,}', ['']],
56 ['', ['']]
57 ]
58
59 negative_incr_cases = [
60 ['{3..1}', ['3', '2', '1']],
61 ['{10..8}', ['10', '9', '8']],
62 ['{10..08}', ['10', '09', '08']],
63 ['{c..a}', ['c', 'b', 'a']],
64 ['{4..0..2}', ['4', '2', '0']],
65 ['{4..0..-2}', ['4', '2', '0']],
66 ['{e..a..2}', ['e', 'c', 'a']]
67 ]
68
69 nested_cases = [
70 ['{a,b{1..3},c}', ['a', 'b1', 'b2', 'b3', 'c']],
71 ['{{A..Z},{a..z}}', list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')],
72 ['ppp{,config,oe{,conf}}', ['ppp', 'pppconfig', 'pppoe', 'pppoeconf']]
73 ]
74
75 order_cases = [
76 ['a{d,c,b}e', ['ade', 'ace', 'abe']]
77 ]
78
79 pad_cases = [
80 ['{09..11}', ['09', '10', '11']],
81 ['{9..11}', ['9', '10', '11']]
82 ]
83
84 sequence_cases = [
85 ['a{1..2}b{2..3}c', ['a1b2c', 'a1b3c', 'a2b2c', 'a2b3c']],
86 ['{1..2}{2..3}', ['12', '13', '22', '23']],
87 ['{0..8..2}', ['0', '2', '4', '6', '8']],
88 ['{1..8..2}', ['1', '3', '5', '7']],
89 ['{3..-2}', ['3', '2', '1', '0', '-1', '-2']],
90 ['1{a..b}2{b..c}3', ['1a2b3', '1a2c3', '1b2b3', '1b2c3']],
91 ['{a..b}{b..c}', ['ab', 'ac', 'bb', 'bc']],
92 ['{a..k..2}', ['a', 'c', 'e', 'g', 'i', 'k']],
93 ['{b..k..2}', ['b', 'd', 'f', 'h', 'j']]
94 ]
95
96 error_cases = [
97 # This would fail in bash, but we won't fail because we aren't Bash
98 ['{a,b,c}\\', ['a', 'b', 'c']]
99 ]
100
101 @staticmethod
102 def eval_str_esc(string):
103 r"""Evaluate buffer as a string buffer counting things like \\ as \."""
104
105 return ast.literal_eval('"%s"' % string.strip().replace('"', '\\"'))
106
107 @staticmethod
108 def assert_equal(a, b):
109 """Assert equal."""
110
111 assert a == b, "Comparison between objects yielded False."
112
113 @classmethod
114 def eval_brace_cases(cls, case):
115 """Evaluate the brace cases."""
116
117 print("PATTERN: ", case[0])
118 expanded_pattern = []
119 try:
120 expanded_pattern.extend(
121 list(bracex.iexpand(case[0]))
122 )
123 except Exception:
124 expanded_pattern.append(case[0])
125 result = expanded_pattern
126 goal = case[1]
127 print('TEST: ', result, '<=^=>', goal, '\n')
128 cls.assert_equal(result, goal)
129
130 @pytest.mark.parametrize("case", error_cases)
131 def test_internal_errors(self, case):
132 """Test trailing escape."""
133
134 self.eval_brace_cases(case)
135
136 @pytest.mark.parametrize("case", dollar_cases)
137 def test_dollar_expand(self, case):
138 """Test that dollar expansions don't expand."""
139
140 self.eval_brace_cases(case)
141
142 @pytest.mark.parametrize("case", empty_cases)
143 def test_empty_expand(self, case):
144 """Test empty expansion."""
145
146 self.eval_brace_cases(case)
147
148 @pytest.mark.parametrize("case", negative_incr_cases)
149 def test_negative_incr_expand(self, case):
150 """Test negative increment expansion."""
151
152 self.eval_brace_cases(case)
153
154 @pytest.mark.parametrize("case", nested_cases)
155 def test_nested_expand(self, case):
156 """Test nested expansion."""
157
158 self.eval_brace_cases(case)
159
160 @pytest.mark.parametrize("case", order_cases)
161 def test_order_expand(self, case):
162 """Test ordered expansion."""
163
164 self.eval_brace_cases(case)
165
166 @pytest.mark.parametrize("case", pad_cases)
167 def test_pad_expand(self, case):
168 """Test padded expansion."""
169
170 self.eval_brace_cases(case)
171
172 @pytest.mark.parametrize("case", sequence_cases)
173 def test_sequence_expand(self, case):
174 """Test sequence expansion."""
175
176 self.eval_brace_cases(case)
177
178 @pytest.mark.parametrize("case", get_bash_cases())
179 def test_bash_cases(self, case):
180 """Test bash cases."""
181
182 test_case = self.eval_str_esc(case[0])
183 entry = case[1]
184
185 print('TEST: ', test_case)
186 self.assert_equal(test_case, entry[0])
187 expansions = bracex.expand(test_case)
188 if not (len(expansions) == 1 and not expansions[0]):
189 index = 1
190 for a in expansions:
191 b = RE_REMOVE.sub('', entry[index])
192 print(' ', a, '<==>', b)
193 self.assert_equal(a, b)
194 index += 1
195
196 # Bytes
197 test_case = test_case.encode('utf-8')
198 self.assert_equal(test_case, entry[0].encode('utf-8'))
199 expansions = bracex.expand(test_case)
200 if not (len(expansions) == 1 and not expansions[0]):
201 index = 1
202 for a in expansions:
203 b = BRE_REMOVE.sub(b'', entry[index].encode('utf-8'))
204 print(' ', a, '<==>', b)
205 self.assert_equal(a, b)
206 index += 1
207
208
209 class TestExpansionLimit(unittest.TestCase):
210 """Test brace expansion limit."""
211
212 def test_expansion_limit_expand(self):
213 """Test expansion limit."""
214
215 self.assertEqual(len(bracex.expand('text{1,2}text{{3,4,{5,6}text{7,8}},{9}}', limit=14)), 14)
216 with self.assertRaises(bracex.ExpansionLimitException):
217 bracex.expand('text{1,2}text{{3,4,{5,6}text{7,8}},{9}}', limit=13)
218
219 def test_expansion_no_limit_expand(self):
220 """Test expansion no limit."""
221
222 self.assertEqual(len(bracex.expand('text{1,2}text{{3,4,{5,6}text{7,8}},{9}}', limit=0)), 14)
223
224 def test_expansion_char_limt_expand(self):
225 """Test expansion character limit with `expand`."""
226
227 self.assertEqual(len(bracex.expand('{a..k}', limit=11)), 11)
228 with self.assertRaises(bracex.ExpansionLimitException):
229 bracex.expand('{a..k}', limit=10)
230
231 def test_expansion_char_reverse_limt_expand(self):
232 """Test expansion character reversed limit with `expand`."""
233
234 self.assertEqual(len(bracex.expand('{k..a}', limit=11)), 11)
235 with self.assertRaises(bracex.ExpansionLimitException):
236 bracex.expand('{k..a}', limit=10)
237
238 def test_expansion_char_limt_expand_step2(self):
239 """Test expansion character limit with `expand` step 2."""
240
241 self.assertEqual(len(bracex.expand('{a..k..2}', limit=6)), 6)
242 with self.assertRaises(bracex.ExpansionLimitException):
243 bracex.expand('{a..k..2}', limit=5)
244
245 def test_expansion_char_limt_expand_step3(self):
246 """Test expansion character limit with `expand` step 3."""
247
248 self.assertEqual(len(bracex.expand('{a..k..3}', limit=4)), 4)
249 with self.assertRaises(bracex.ExpansionLimitException):
250 bracex.expand('{a..k..3}', limit=3)
251
252 def test_expansion_char_limt_expand_step5(self):
253 """Test expansion character limit with `expand` step 5."""
254
255 self.assertEqual(len(bracex.expand('{a..k..5}', limit=3)), 3)
256 with self.assertRaises(bracex.ExpansionLimitException):
257 bracex.expand('{a..k..5}', limit=2)
258
259 def test_expansion_char_reverse_limt_expand_step2(self):
260 """Test expansion character reversed limit with `expand` step 2."""
261
262 self.assertEqual(len(bracex.expand('{k..a..2}', limit=6)), 6)
263 with self.assertRaises(bracex.ExpansionLimitException):
264 bracex.expand('{k..a..2}', limit=5)
265
266 def test_expansion_char_reverse_limt_expand_step3(self):
267 """Test expansion character reversed limit with `expand` step 3."""
268
269 self.assertEqual(len(bracex.expand('{k..a..3}', limit=4)), 4)
270 with self.assertRaises(bracex.ExpansionLimitException):
271 bracex.expand('{k..a..3}', limit=3)
272
273 def test_expansion_char_reverse_limt_expand_step5(self):
274 """Test expansion character reversed limit with `expand` step 5."""
275
276 self.assertEqual(len(bracex.expand('{k..a..5}', limit=3)), 3)
277 with self.assertRaises(bracex.ExpansionLimitException):
278 bracex.expand('{k..a..5}', limit=2)
279
280 def test_expansion_char_limt_expand_step2_neg(self):
281 """Test expansion character limit with `expand` step -2."""
282
283 self.assertEqual(len(bracex.expand('{a..k..-2}', limit=6)), 6)
284 with self.assertRaises(bracex.ExpansionLimitException):
285 bracex.expand('{a..k..-2}', limit=5)
286
287 def test_expansion_char_limt_expand_step3_neg(self):
288 """Test expansion character limit with `expand` step -3."""
289
290 self.assertEqual(len(bracex.expand('{a..k..-3}', limit=4)), 4)
291 with self.assertRaises(bracex.ExpansionLimitException):
292 bracex.expand('{a..k..-3}', limit=3)
293
294 def test_expansion_char_limt_expand_step5_neg(self):
295 """Test expansion character limit with `expand` step -5."""
296
297 self.assertEqual(len(bracex.expand('{a..k..-5}', limit=3)), 3)
298 with self.assertRaises(bracex.ExpansionLimitException):
299 bracex.expand('{a..k..-5}', limit=2)
300
301 def test_expansion_char_reverse_limt_expand_step2_neg(self):
302 """Test expansion character reversed limit with `expand` step -2."""
303
304 self.assertEqual(len(bracex.expand('{k..a..-2}', limit=6)), 6)
305 with self.assertRaises(bracex.ExpansionLimitException):
306 bracex.expand('{k..a..-2}', limit=5)
307
308 def test_expansion_char_reverse_limt_expand_step3_neg(self):
309 """Test expansion character reversed limit with `expand` step -3."""
310
311 self.assertEqual(len(bracex.expand('{k..a..-3}', limit=4)), 4)
312 with self.assertRaises(bracex.ExpansionLimitException):
313 bracex.expand('{k..a..-3}', limit=3)
314
315 def test_expansion_char_reverse_limt_expand_step5_neg(self):
316 """Test expansion character reversed limit with `expand` step -5."""
317
318 self.assertEqual(len(bracex.expand('{k..a..-5}', limit=3)), 3)
319 with self.assertRaises(bracex.ExpansionLimitException):
320 bracex.expand('{k..a..-5}', limit=2)
321
322 def test_expansion_num_limt_expand(self):
323 """Test expansion numeric limit with `expand`."""
324
325 self.assertEqual(len(bracex.expand('{1..11}', limit=11)), 11)
326 with self.assertRaises(bracex.ExpansionLimitException):
327 bracex.expand('{1..11}', limit=10)
328
329 def test_expansion_num_reverse_limt_expand(self):
330 """Test expansion numeric reversed limit with `expand`."""
331
332 self.assertEqual(len(bracex.expand('{11..1}', limit=11)), 11)
333 with self.assertRaises(bracex.ExpansionLimitException):
334 bracex.expand('{11..1}', limit=10)
335
336 def test_expansion_num_negative_limt_expand(self):
337 """Test expansion numeric negative limit with `expand`."""
338
339 self.assertEqual(len(bracex.expand('{-1..-11}', limit=11)), 11)
340 with self.assertRaises(bracex.ExpansionLimitException):
341 bracex.expand('{-1..-11}', limit=10)
342
343 def test_expansion_num_negative_reversed_limt_expand(self):
344 """Test expansion numeric negative reversed limit with `expand`."""
345
346 self.assertEqual(len(bracex.expand('{-11..-1}', limit=11)), 11)
347 with self.assertRaises(bracex.ExpansionLimitException):
348 bracex.expand('{-11..-1}', limit=10)
349
350 def test_expansion_num_limt_expand_step(self):
351 """Test expansion numeric limit with `expand` step."""
352
353 self.assertEqual(len(bracex.expand('{1..11..5}', limit=3)), 3)
354 with self.assertRaises(bracex.ExpansionLimitException):
355 bracex.expand('{1..11..5}', limit=2)
356
357 def test_expansion_num_reverse_limt_expand_step(self):
358 """Test expansion numeric reversed limit with `expand` step."""
359
360 self.assertEqual(len(bracex.expand('{11..1..3}', limit=4)), 4)
361 with self.assertRaises(bracex.ExpansionLimitException):
362 bracex.expand('{11..1..3}', limit=3)
363
364 def test_expansion_num_negative_limt_expand_step(self):
365 """Test expansion numeric negative limit with `expand`."""
366
367 self.assertEqual(len(bracex.expand('{-1..-11..2}', limit=6)), 6)
368 with self.assertRaises(bracex.ExpansionLimitException):
369 bracex.expand('{-1..-11..2}', limit=5)
370
371 def test_expansion_num_negative_reversed_limt_expand_step(self):
372 """Test expansion numeric negative reversed limit with `expand`."""
373
374 self.assertEqual(len(bracex.expand('{-11..-1..7}', limit=2)), 2)
375 with self.assertRaises(bracex.ExpansionLimitException):
376 bracex.expand('{-11..-1..7}', limit=1)
377
378 def test_expansion_num_limt_expand_neg_step(self):
379 """Test expansion numeric limit with `expand` negative step."""
380
381 self.assertEqual(len(bracex.expand('{1..11..-5}', limit=3)), 3)
382 with self.assertRaises(bracex.ExpansionLimitException):
383 bracex.expand('{1..11..-5}', limit=2)
384
385 def test_expansion_num_reverse_limt_expand_neg_step(self):
386 """Test expansion numeric reversed limit with `expand` negative step."""
387
388 self.assertEqual(len(bracex.expand('{11..1..-3}', limit=4)), 4)
389 with self.assertRaises(bracex.ExpansionLimitException):
390 bracex.expand('{11..1..-3}', limit=3)
391
392 def test_expansion_num_negative_limt_expand_neg_step(self):
393 """Test expansion numeric negative limit with `expand` negative step."""
394
395 self.assertEqual(len(bracex.expand('{-1..-11..-2}', limit=6)), 6)
396 with self.assertRaises(bracex.ExpansionLimitException):
397 bracex.expand('{-1..-11..-2}', limit=5)
398
399 def test_expansion_num_negative_reversed_limt_expand_neg_step(self):
400 """Test expansion numeric negative reversed limit with `expand` negative step."""
401
402 self.assertEqual(len(bracex.expand('{-11..-11..-7}', limit=1)), 1)
403 with self.assertRaises(bracex.ExpansionLimitException):
404 bracex.expand('{-11..-1..-7}', limit=1)