| 1 |
"""Test cases for the fnmatch module.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import string |
| 5 |
import unittest |
| 6 |
import warnings |
| 7 |
from fnmatch import fnmatch, fnmatchcase, translate, filter, filterfalse |
| 8 |
|
| 9 |
|
| 10 |
IGNORECASE = os.path.normcase('P') == os.path.normcase('p') |
| 11 |
NORMSEP = os.path.normcase('\\') == os.path.normcase('/') |
| 12 |
|
| 13 |
|
| 14 |
class FnmatchTestCase(unittest.TestCase): |
| 15 |
|
| 16 |
def check_match(self, filename, pattern, should_match=True, fn=fnmatch): |
| 17 |
if should_match: |
| 18 |
self.assertTrue(fn(filename, pattern), |
| 19 |
"expected %r to match pattern %r" |
| 20 |
% (filename, pattern)) |
| 21 |
else: |
| 22 |
self.assertFalse(fn(filename, pattern), |
| 23 |
"expected %r not to match pattern %r" |
| 24 |
% (filename, pattern)) |
| 25 |
|
| 26 |
def test_fnmatch(self): |
| 27 |
check = self.check_match |
| 28 |
check('abc', 'abc') |
| 29 |
check('abc', '?*?') |
| 30 |
check('abc', '???*') |
| 31 |
check('abc', '*???') |
| 32 |
check('abc', '???') |
| 33 |
check('abc', '*') |
| 34 |
check('abc', 'ab[cd]') |
| 35 |
check('abc', 'ab[!de]') |
| 36 |
check('abc', 'ab[de]', False) |
| 37 |
check('a', '??', False) |
| 38 |
check('a', 'b', False) |
| 39 |
|
| 40 |
# these test that '\' is handled correctly in character sets; |
| 41 |
# see SF bug #409651 |
| 42 |
check('\\', r'[\]') |
| 43 |
check('a', r'[!\]') |
| 44 |
check('\\', r'[!\]', False) |
| 45 |
|
| 46 |
# test that filenames with newlines in them are handled correctly. |
| 47 |
# http://bugs.python.org/issue6665 |
| 48 |
check('foo\nbar', 'foo*') |
| 49 |
check('foo\nbar\n', 'foo*') |
| 50 |
check('\nfoo', 'foo*', False) |
| 51 |
check('\n', '*') |
| 52 |
|
| 53 |
def test_slow_fnmatch(self): |
| 54 |
check = self.check_match |
| 55 |
check('a' * 50, '*a*a*a*a*a*a*a*a*a*a') |
| 56 |
# The next "takes forever" if the regexp translation is |
| 57 |
# straightforward. See bpo-40480. |
| 58 |
check('a' * 50 + 'b', '*a*a*a*a*a*a*a*a*a*a', False) |
| 59 |
|
| 60 |
def test_mix_bytes_str(self): |
| 61 |
self.assertRaises(TypeError, fnmatch, 'test', b'*') |
| 62 |
self.assertRaises(TypeError, fnmatch, b'test', '*') |
| 63 |
self.assertRaises(TypeError, fnmatchcase, 'test', b'*') |
| 64 |
self.assertRaises(TypeError, fnmatchcase, b'test', '*') |
| 65 |
|
| 66 |
def test_fnmatchcase(self): |
| 67 |
check = self.check_match |
| 68 |
check('abc', 'abc', True, fnmatchcase) |
| 69 |
check('AbC', 'abc', False, fnmatchcase) |
| 70 |
check('abc', 'AbC', False, fnmatchcase) |
| 71 |
check('AbC', 'AbC', True, fnmatchcase) |
| 72 |
|
| 73 |
check('usr/bin', 'usr/bin', True, fnmatchcase) |
| 74 |
check('usr\\bin', 'usr/bin', False, fnmatchcase) |
| 75 |
check('usr/bin', 'usr\\bin', False, fnmatchcase) |
| 76 |
check('usr\\bin', 'usr\\bin', True, fnmatchcase) |
| 77 |
|
| 78 |
def test_bytes(self): |
| 79 |
self.check_match(b'test', b'te*') |
| 80 |
self.check_match(b'test\xff', b'te*\xff') |
| 81 |
self.check_match(b'foo\nbar', b'foo*') |
| 82 |
|
| 83 |
def test_case(self): |
| 84 |
check = self.check_match |
| 85 |
check('abc', 'abc') |
| 86 |
check('AbC', 'abc', IGNORECASE) |
| 87 |
check('abc', 'AbC', IGNORECASE) |
| 88 |
check('AbC', 'AbC') |
| 89 |
|
| 90 |
def test_sep(self): |
| 91 |
check = self.check_match |
| 92 |
check('usr/bin', 'usr/bin') |
| 93 |
check('usr\\bin', 'usr/bin', NORMSEP) |
| 94 |
check('usr/bin', 'usr\\bin', NORMSEP) |
| 95 |
check('usr\\bin', 'usr\\bin') |
| 96 |
|
| 97 |
def test_char_set(self): |
| 98 |
check = self.check_match |
| 99 |
tescases = string.ascii_lowercase + string.digits + string.punctuation |
| 100 |
for c in tescases: |
| 101 |
check(c, '[az]', c in 'az') |
| 102 |
check(c, '[!az]', c not in 'az') |
| 103 |
# Case insensitive. |
| 104 |
for c in tescases: |
| 105 |
check(c, '[AZ]', (c in 'az') and IGNORECASE) |
| 106 |
check(c, '[!AZ]', (c not in 'az') or not IGNORECASE) |
| 107 |
for c in string.ascii_uppercase: |
| 108 |
check(c, '[az]', (c in 'AZ') and IGNORECASE) |
| 109 |
check(c, '[!az]', (c not in 'AZ') or not IGNORECASE) |
| 110 |
# Repeated same character. |
| 111 |
for c in tescases: |
| 112 |
check(c, '[aa]', c == 'a') |
| 113 |
# Special cases. |
| 114 |
for c in tescases: |
| 115 |
check(c, '[^az]', c in '^az') |
| 116 |
check(c, '[[az]', c in '[az') |
| 117 |
check(c, r'[!]]', c != ']') |
| 118 |
check('[', '[') |
| 119 |
check('[]', '[]') |
| 120 |
check('[!', '[!') |
| 121 |
check('[!]', '[!]') |
| 122 |
|
| 123 |
def test_range(self): |
| 124 |
check = self.check_match |
| 125 |
tescases = string.ascii_lowercase + string.digits + string.punctuation |
| 126 |
for c in tescases: |
| 127 |
check(c, '[b-d]', c in 'bcd') |
| 128 |
check(c, '[!b-d]', c not in 'bcd') |
| 129 |
check(c, '[b-dx-z]', c in 'bcdxyz') |
| 130 |
check(c, '[!b-dx-z]', c not in 'bcdxyz') |
| 131 |
# Case insensitive. |
| 132 |
for c in tescases: |
| 133 |
check(c, '[B-D]', (c in 'bcd') and IGNORECASE) |
| 134 |
check(c, '[!B-D]', (c not in 'bcd') or not IGNORECASE) |
| 135 |
for c in string.ascii_uppercase: |
| 136 |
check(c, '[b-d]', (c in 'BCD') and IGNORECASE) |
| 137 |
check(c, '[!b-d]', (c not in 'BCD') or not IGNORECASE) |
| 138 |
# Upper bound == lower bound. |
| 139 |
for c in tescases: |
| 140 |
check(c, '[b-b]', c == 'b') |
| 141 |
# Special cases. |
| 142 |
for c in tescases: |
| 143 |
check(c, '[!-#]', c not in '-#') |
| 144 |
check(c, '[!--.]', c not in '-.') |
| 145 |
check(c, '[^-`]', c in '^_`') |
| 146 |
if not (NORMSEP and c == '/'): |
| 147 |
check(c, '[[-^]', c in r'[\]^') |
| 148 |
check(c, r'[\-^]', c in r'\]^') |
| 149 |
check(c, '[b-]', c in '-b') |
| 150 |
check(c, '[!b-]', c not in '-b') |
| 151 |
check(c, '[-b]', c in '-b') |
| 152 |
check(c, '[!-b]', c not in '-b') |
| 153 |
check(c, '[-]', c in '-') |
| 154 |
check(c, '[!-]', c not in '-') |
| 155 |
# Upper bound is less that lower bound: error in RE. |
| 156 |
for c in tescases: |
| 157 |
check(c, '[d-b]', False) |
| 158 |
check(c, '[!d-b]', True) |
| 159 |
check(c, '[d-bx-z]', c in 'xyz') |
| 160 |
check(c, '[!d-bx-z]', c not in 'xyz') |
| 161 |
check(c, '[d-b^-`]', c in '^_`') |
| 162 |
if not (NORMSEP and c == '/'): |
| 163 |
check(c, '[d-b[-^]', c in r'[\]^') |
| 164 |
|
| 165 |
def test_sep_in_char_set(self): |
| 166 |
check = self.check_match |
| 167 |
check('/', r'[/]') |
| 168 |
check('\\', r'[\]') |
| 169 |
check('/', r'[\]', NORMSEP) |
| 170 |
check('\\', r'[/]', NORMSEP) |
| 171 |
check('[/]', r'[/]', False) |
| 172 |
check(r'[\\]', r'[/]', False) |
| 173 |
check('\\', r'[\t]') |
| 174 |
check('/', r'[\t]', NORMSEP) |
| 175 |
check('t', r'[\t]') |
| 176 |
check('\t', r'[\t]', False) |
| 177 |
|
| 178 |
def test_sep_in_range(self): |
| 179 |
check = self.check_match |
| 180 |
check('a/b', 'a[.-0]b', not NORMSEP) |
| 181 |
check('a\\b', 'a[.-0]b', False) |
| 182 |
check('a\\b', 'a[Z-^]b', not NORMSEP) |
| 183 |
check('a/b', 'a[Z-^]b', False) |
| 184 |
|
| 185 |
check('a/b', 'a[/-0]b', not NORMSEP) |
| 186 |
check(r'a\b', 'a[/-0]b', False) |
| 187 |
check('a[/-0]b', 'a[/-0]b', False) |
| 188 |
check(r'a[\-0]b', 'a[/-0]b', False) |
| 189 |
|
| 190 |
check('a/b', 'a[.-/]b') |
| 191 |
check(r'a\b', 'a[.-/]b', NORMSEP) |
| 192 |
check('a[.-/]b', 'a[.-/]b', False) |
| 193 |
check(r'a[.-\]b', 'a[.-/]b', False) |
| 194 |
|
| 195 |
check(r'a\b', r'a[\-^]b') |
| 196 |
check('a/b', r'a[\-^]b', NORMSEP) |
| 197 |
check(r'a[\-^]b', r'a[\-^]b', False) |
| 198 |
check('a[/-^]b', r'a[\-^]b', False) |
| 199 |
|
| 200 |
check(r'a\b', r'a[Z-\]b', not NORMSEP) |
| 201 |
check('a/b', r'a[Z-\]b', False) |
| 202 |
check(r'a[Z-\]b', r'a[Z-\]b', False) |
| 203 |
check('a[Z-/]b', r'a[Z-\]b', False) |
| 204 |
|
| 205 |
def test_warnings(self): |
| 206 |
with warnings.catch_warnings(): |
| 207 |
warnings.simplefilter('error', Warning) |
| 208 |
check = self.check_match |
| 209 |
check('[', '[[]') |
| 210 |
check('&', '[a&&b]') |
| 211 |
check('|', '[a||b]') |
| 212 |
check('~', '[a~~b]') |
| 213 |
check(',', '[a-z+--A-Z]') |
| 214 |
check('.', '[a-z--/A-Z]') |
| 215 |
|
| 216 |
|
| 217 |
class TranslateTestCase(unittest.TestCase): |
| 218 |
|
| 219 |
def test_translate(self): |
| 220 |
import re |
| 221 |
self.assertEqual(translate('*'), r'(?s:.*)\z') |
| 222 |
self.assertEqual(translate('?'), r'(?s:.)\z') |
| 223 |
self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\z') |
| 224 |
self.assertEqual(translate('[abc]'), r'(?s:[abc])\z') |
| 225 |
self.assertEqual(translate('[]]'), r'(?s:[]])\z') |
| 226 |
self.assertEqual(translate('[!x]'), r'(?s:[^x])\z') |
| 227 |
self.assertEqual(translate('[^x]'), r'(?s:[\^x])\z') |
| 228 |
self.assertEqual(translate('[x'), r'(?s:\[x)\z') |
| 229 |
# from the docs |
| 230 |
self.assertEqual(translate('*.txt'), r'(?s:.*\.txt)\z') |
| 231 |
# squash consecutive stars |
| 232 |
self.assertEqual(translate('*********'), r'(?s:.*)\z') |
| 233 |
self.assertEqual(translate('A*********'), r'(?s:A.*)\z') |
| 234 |
self.assertEqual(translate('*********A'), r'(?s:.*A)\z') |
| 235 |
self.assertEqual(translate('A*********?[?]?'), r'(?s:A.*.[?].)\z') |
| 236 |
# fancy translation to prevent exponential-time match failure |
| 237 |
t = translate('**a*a****a') |
| 238 |
self.assertEqual(t, r'(?s:(?>.*?a)(?>.*?a).*a)\z') |
| 239 |
# and try pasting multiple translate results - it's an undocumented |
| 240 |
# feature that this works |
| 241 |
r1 = translate('**a**a**a*') |
| 242 |
r2 = translate('**b**b**b*') |
| 243 |
r3 = translate('*c*c*c*') |
| 244 |
fatre = "|".join([r1, r2, r3]) |
| 245 |
self.assertTrue(re.match(fatre, 'abaccad')) |
| 246 |
self.assertTrue(re.match(fatre, 'abxbcab')) |
| 247 |
self.assertTrue(re.match(fatre, 'cbabcaxc')) |
| 248 |
self.assertFalse(re.match(fatre, 'dabccbad')) |
| 249 |
|
| 250 |
def test_translate_wildcards(self): |
| 251 |
for pattern, expect in [ |
| 252 |
('ab*', r'(?s:ab.*)\z'), |
| 253 |
('ab*cd', r'(?s:ab.*cd)\z'), |
| 254 |
('ab*cd*', r'(?s:ab(?>.*?cd).*)\z'), |
| 255 |
('ab*cd*12', r'(?s:ab(?>.*?cd).*12)\z'), |
| 256 |
('ab*cd*12*', r'(?s:ab(?>.*?cd)(?>.*?12).*)\z'), |
| 257 |
('ab*cd*12*34', r'(?s:ab(?>.*?cd)(?>.*?12).*34)\z'), |
| 258 |
('ab*cd*12*34*', r'(?s:ab(?>.*?cd)(?>.*?12)(?>.*?34).*)\z'), |
| 259 |
]: |
| 260 |
with self.subTest(pattern): |
| 261 |
translated = translate(pattern) |
| 262 |
self.assertEqual(translated, expect, pattern) |
| 263 |
|
| 264 |
for pattern, expect in [ |
| 265 |
('*ab', r'(?s:.*ab)\z'), |
| 266 |
('*ab*', r'(?s:(?>.*?ab).*)\z'), |
| 267 |
('*ab*cd', r'(?s:(?>.*?ab).*cd)\z'), |
| 268 |
('*ab*cd*', r'(?s:(?>.*?ab)(?>.*?cd).*)\z'), |
| 269 |
('*ab*cd*12', r'(?s:(?>.*?ab)(?>.*?cd).*12)\z'), |
| 270 |
('*ab*cd*12*', r'(?s:(?>.*?ab)(?>.*?cd)(?>.*?12).*)\z'), |
| 271 |
('*ab*cd*12*34', r'(?s:(?>.*?ab)(?>.*?cd)(?>.*?12).*34)\z'), |
| 272 |
('*ab*cd*12*34*', r'(?s:(?>.*?ab)(?>.*?cd)(?>.*?12)(?>.*?34).*)\z'), |
| 273 |
]: |
| 274 |
with self.subTest(pattern): |
| 275 |
translated = translate(pattern) |
| 276 |
self.assertEqual(translated, expect, pattern) |
| 277 |
|
| 278 |
def test_translate_expressions(self): |
| 279 |
for pattern, expect in [ |
| 280 |
('[', r'(?s:\[)\z'), |
| 281 |
('[!', r'(?s:\[!)\z'), |
| 282 |
('[]', r'(?s:\[\])\z'), |
| 283 |
('[abc', r'(?s:\[abc)\z'), |
| 284 |
('[!abc', r'(?s:\[!abc)\z'), |
| 285 |
('[abc]', r'(?s:[abc])\z'), |
| 286 |
('[!abc]', r'(?s:[^abc])\z'), |
| 287 |
('[!abc][!def]', r'(?s:[^abc][^def])\z'), |
| 288 |
# with [[ |
| 289 |
('[[', r'(?s:\[\[)\z'), |
| 290 |
('[[a', r'(?s:\[\[a)\z'), |
| 291 |
('[[]', r'(?s:[\[])\z'), |
| 292 |
('[[]a', r'(?s:[\[]a)\z'), |
| 293 |
('[[]]', r'(?s:[\[]\])\z'), |
| 294 |
('[[]a]', r'(?s:[\[]a\])\z'), |
| 295 |
('[[a]', r'(?s:[\[a])\z'), |
| 296 |
('[[a]]', r'(?s:[\[a]\])\z'), |
| 297 |
('[[a]b', r'(?s:[\[a]b)\z'), |
| 298 |
# backslashes |
| 299 |
('[\\', r'(?s:\[\\)\z'), |
| 300 |
(r'[\]', r'(?s:[\\])\z'), |
| 301 |
(r'[\\]', r'(?s:[\\\\])\z'), |
| 302 |
]: |
| 303 |
with self.subTest(pattern): |
| 304 |
translated = translate(pattern) |
| 305 |
self.assertEqual(translated, expect, pattern) |
| 306 |
|
| 307 |
def test_star_indices_locations(self): |
| 308 |
from fnmatch import _translate |
| 309 |
|
| 310 |
blocks = ['a^b', '***', '?', '?', '[a-z]', '[1-9]', '*', '++', '[[a'] |
| 311 |
parts, star_indices = _translate(''.join(blocks), '*', '.') |
| 312 |
expect_parts = ['a', r'\^', 'b', '*', |
| 313 |
'.', '.', '[a-z]', '[1-9]', '*', |
| 314 |
r'\+', r'\+', r'\[', r'\[', 'a'] |
| 315 |
self.assertListEqual(parts, expect_parts) |
| 316 |
self.assertListEqual(star_indices, [3, 8]) |
| 317 |
|
| 318 |
|
| 319 |
class FilterTestCase(unittest.TestCase): |
| 320 |
|
| 321 |
def test_filter(self): |
| 322 |
self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'), |
| 323 |
['Python', 'Perl']) |
| 324 |
self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'), |
| 325 |
[b'Python', b'Perl']) |
| 326 |
|
| 327 |
def test_mix_bytes_str(self): |
| 328 |
self.assertRaises(TypeError, filter, ['test'], b'*') |
| 329 |
self.assertRaises(TypeError, filter, [b'test'], '*') |
| 330 |
|
| 331 |
def test_case(self): |
| 332 |
self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'), |
| 333 |
['Test.py', 'Test.PL'] if IGNORECASE else ['Test.py']) |
| 334 |
self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'), |
| 335 |
['Test.py', 'Test.PL'] if IGNORECASE else ['Test.PL']) |
| 336 |
|
| 337 |
def test_sep(self): |
| 338 |
self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'), |
| 339 |
['usr/bin', 'usr\\lib'] if NORMSEP else ['usr/bin']) |
| 340 |
self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'), |
| 341 |
['usr/bin', 'usr\\lib'] if NORMSEP else ['usr\\lib']) |
| 342 |
|
| 343 |
|
| 344 |
class FilterFalseTestCase(unittest.TestCase): |
| 345 |
|
| 346 |
def test_filterfalse(self): |
| 347 |
actual = filterfalse(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*') |
| 348 |
self.assertListEqual(actual, ['Ruby', 'Tcl']) |
| 349 |
actual = filterfalse([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*') |
| 350 |
self.assertListEqual(actual, [b'Ruby', b'Tcl']) |
| 351 |
|
| 352 |
def test_mix_bytes_str(self): |
| 353 |
self.assertRaises(TypeError, filterfalse, ['test'], b'*') |
| 354 |
self.assertRaises(TypeError, filterfalse, [b'test'], '*') |
| 355 |
|
| 356 |
def test_case(self): |
| 357 |
self.assertEqual(filterfalse(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'), |
| 358 |
['Test.rb'] if IGNORECASE else ['Test.rb', 'Test.PL']) |
| 359 |
self.assertEqual(filterfalse(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'), |
| 360 |
['Test.rb'] if IGNORECASE else ['Test.py', 'Test.rb',]) |
| 361 |
|
| 362 |
def test_sep(self): |
| 363 |
self.assertEqual(filterfalse(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'), |
| 364 |
['usr'] if NORMSEP else ['usr', 'usr\\lib']) |
| 365 |
self.assertEqual(filterfalse(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'), |
| 366 |
['usr'] if NORMSEP else ['usr/bin', 'usr']) |
| 367 |
|
| 368 |
|
| 369 |
if __name__ == "__main__": |
| 370 |
unittest.main() |