blob: 10668e4f6103aa0e7e06e221dafee9050d735b5d [file] [log] [blame]
Fred Drake91751142001-03-21 18:29:25 +00001"""Test cases for the fnmatch module."""
2
Fred Drake91751142001-03-21 18:29:25 +00003import unittest
Serhiy Storchaka81755472017-05-21 08:57:00 +03004import os
Serhiy Storchaka23cdbfa2018-02-09 13:30:19 +02005import warnings
Fred Drake91751142001-03-21 18:29:25 +00006
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +00007from fnmatch import fnmatch, fnmatchcase, translate, filter
Fred Drake91751142001-03-21 18:29:25 +00008
9class FnmatchTestCase(unittest.TestCase):
R. David Murray0425a8e2010-07-10 13:52:13 +000010
Serhiy Storchaka81755472017-05-21 08:57:00 +030011 def check_match(self, filename, pattern, should_match=True, fn=fnmatch):
Fred Drake91751142001-03-21 18:29:25 +000012 if should_match:
Georg Brandlc0e22b72010-03-14 10:51:01 +000013 self.assertTrue(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000014 "expected %r to match pattern %r"
15 % (filename, pattern))
16 else:
Serhiy Storchaka81755472017-05-21 08:57:00 +030017 self.assertFalse(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000018 "expected %r not to match pattern %r"
19 % (filename, pattern))
20
21 def test_fnmatch(self):
22 check = self.check_match
23 check('abc', 'abc')
24 check('abc', '?*?')
25 check('abc', '???*')
26 check('abc', '*???')
27 check('abc', '???')
28 check('abc', '*')
29 check('abc', 'ab[cd]')
30 check('abc', 'ab[!de]')
Serhiy Storchaka81755472017-05-21 08:57:00 +030031 check('abc', 'ab[de]', False)
32 check('a', '??', False)
33 check('a', 'b', False)
Fred Drake91751142001-03-21 18:29:25 +000034
35 # these test that '\' is handled correctly in character sets;
Gregory P. Smith01099702009-08-16 18:58:46 +000036 # see SF bug #409651
Fred Drake91751142001-03-21 18:29:25 +000037 check('\\', r'[\]')
38 check('a', r'[!\]')
Serhiy Storchaka81755472017-05-21 08:57:00 +030039 check('\\', r'[!\]', False)
Fred Drake91751142001-03-21 18:29:25 +000040
Gregory P. Smith01099702009-08-16 18:58:46 +000041 # test that filenames with newlines in them are handled correctly.
42 # http://bugs.python.org/issue6665
43 check('foo\nbar', 'foo*')
44 check('foo\nbar\n', 'foo*')
45 check('\nfoo', 'foo*', False)
46 check('\n', '*')
47
Tim Petersb9c46a22020-05-05 21:28:24 -050048 def test_slow_fnmatch(self):
49 check = self.check_match
50 check('a' * 50, '*a*a*a*a*a*a*a*a*a*a')
51 # The next "takes forever" if the regexp translation is
52 # straightforward. See bpo-40480.
53 check('a' * 50 + 'b', '*a*a*a*a*a*a*a*a*a*a', False)
54
Guido van Rossumf0af3e32008-10-02 18:55:37 +000055 def test_mix_bytes_str(self):
56 self.assertRaises(TypeError, fnmatch, 'test', b'*')
57 self.assertRaises(TypeError, fnmatch, b'test', '*')
58 self.assertRaises(TypeError, fnmatchcase, 'test', b'*')
59 self.assertRaises(TypeError, fnmatchcase, b'test', '*')
60
Georg Brandl89fad142010-03-14 10:23:39 +000061 def test_fnmatchcase(self):
62 check = self.check_match
Serhiy Storchaka81755472017-05-21 08:57:00 +030063 check('abc', 'abc', True, fnmatchcase)
64 check('AbC', 'abc', False, fnmatchcase)
65 check('abc', 'AbC', False, fnmatchcase)
66 check('AbC', 'AbC', True, fnmatchcase)
67
68 check('usr/bin', 'usr/bin', True, fnmatchcase)
69 check('usr\\bin', 'usr/bin', False, fnmatchcase)
70 check('usr/bin', 'usr\\bin', False, fnmatchcase)
71 check('usr\\bin', 'usr\\bin', True, fnmatchcase)
Georg Brandl89fad142010-03-14 10:23:39 +000072
Guido van Rossumf0af3e32008-10-02 18:55:37 +000073 def test_bytes(self):
74 self.check_match(b'test', b'te*')
75 self.check_match(b'test\xff', b'te*\xff')
Gregory P. Smith01099702009-08-16 18:58:46 +000076 self.check_match(b'foo\nbar', b'foo*')
77
Serhiy Storchaka81755472017-05-21 08:57:00 +030078 def test_case(self):
79 ignorecase = os.path.normcase('ABC') == os.path.normcase('abc')
80 check = self.check_match
81 check('abc', 'abc')
82 check('AbC', 'abc', ignorecase)
83 check('abc', 'AbC', ignorecase)
84 check('AbC', 'AbC')
85
86 def test_sep(self):
87 normsep = os.path.normcase('\\') == os.path.normcase('/')
88 check = self.check_match
89 check('usr/bin', 'usr/bin')
90 check('usr\\bin', 'usr/bin', normsep)
91 check('usr/bin', 'usr\\bin', normsep)
92 check('usr\\bin', 'usr\\bin')
93
Serhiy Storchaka23cdbfa2018-02-09 13:30:19 +020094 def test_warnings(self):
95 with warnings.catch_warnings():
96 warnings.simplefilter('error', Warning)
97 check = self.check_match
98 check('[', '[[]')
99 check('&', '[a&&b]')
100 check('|', '[a||b]')
101 check('~', '[a~~b]')
102 check(',', '[a-z+--A-Z]')
103 check('.', '[a-z--/A-Z]')
104
Serhiy Storchaka81755472017-05-21 08:57:00 +0300105
Brett Cannon4b16e132010-07-23 16:23:13 +0000106class TranslateTestCase(unittest.TestCase):
107
108 def test_translate(self):
Tim Petersb1b4c792020-05-11 21:19:20 -0500109 import re
Serhiy Storchakabd48d272016-09-11 12:50:02 +0300110 self.assertEqual(translate('*'), r'(?s:.*)\Z')
111 self.assertEqual(translate('?'), r'(?s:.)\Z')
112 self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\Z')
113 self.assertEqual(translate('[abc]'), r'(?s:[abc])\Z')
114 self.assertEqual(translate('[]]'), r'(?s:[]])\Z')
115 self.assertEqual(translate('[!x]'), r'(?s:[^x])\Z')
116 self.assertEqual(translate('[^x]'), r'(?s:[\^x])\Z')
117 self.assertEqual(translate('[x'), r'(?s:\[x)\Z')
Tim Petersb9c46a22020-05-05 21:28:24 -0500118 # from the docs
119 self.assertEqual(translate('*.txt'), r'(?s:.*\.txt)\Z')
120 # squash consecutive stars
121 self.assertEqual(translate('*********'), r'(?s:.*)\Z')
122 self.assertEqual(translate('A*********'), r'(?s:A.*)\Z')
123 self.assertEqual(translate('*********A'), r'(?s:.*A)\Z')
124 self.assertEqual(translate('A*********?[?]?'), r'(?s:A.*.[?].)\Z')
125 # fancy translation to prevent exponential-time match failure
Tim Petersb1b4c792020-05-11 21:19:20 -0500126 t = translate('**a*a****a')
127 digits = re.findall(r'\d+', t)
128 self.assertEqual(len(digits), 4)
129 self.assertEqual(digits[0], digits[1])
130 self.assertEqual(digits[2], digits[3])
131 g1 = f"g{digits[0]}" # e.g., group name "g4"
132 g2 = f"g{digits[2]}" # e.g., group name "g5"
133 self.assertEqual(t,
134 fr'(?s:(?=(?P<{g1}>.*?a))(?P={g1})(?=(?P<{g2}>.*?a))(?P={g2}).*a)\Z')
135 # and try pasting multiple translate results - it's an undocumented
136 # feature that this works; all the pain of generating unique group
137 # names across calls exists to support this
138 r1 = translate('**a**a**a*')
139 r2 = translate('**b**b**b*')
140 r3 = translate('*c*c*c*')
141 fatre = "|".join([r1, r2, r3])
142 self.assertTrue(re.match(fatre, 'abaccad'))
143 self.assertTrue(re.match(fatre, 'abxbcab'))
144 self.assertTrue(re.match(fatre, 'cbabcaxc'))
145 self.assertFalse(re.match(fatre, 'dabccbad'))
Brett Cannon4b16e132010-07-23 16:23:13 +0000146
147class FilterTestCase(unittest.TestCase):
148
149 def test_filter(self):
Serhiy Storchaka81755472017-05-21 08:57:00 +0300150 self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'),
151 ['Python', 'Perl'])
152 self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'),
153 [b'Python', b'Perl'])
154
155 def test_mix_bytes_str(self):
156 self.assertRaises(TypeError, filter, ['test'], b'*')
157 self.assertRaises(TypeError, filter, [b'test'], '*')
158
159 def test_case(self):
160 ignorecase = os.path.normcase('P') == os.path.normcase('p')
161 self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'),
162 ['Test.py', 'Test.PL'] if ignorecase else ['Test.py'])
163 self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'),
164 ['Test.py', 'Test.PL'] if ignorecase else ['Test.PL'])
165
166 def test_sep(self):
167 normsep = os.path.normcase('\\') == os.path.normcase('/')
168 self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'),
169 ['usr/bin', 'usr\\lib'] if normsep else ['usr/bin'])
170 self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'),
171 ['usr/bin', 'usr\\lib'] if normsep else ['usr\\lib'])
Brett Cannon4b16e132010-07-23 16:23:13 +0000172
173
Fred Drake2e2be372001-09-20 21:33:42 +0000174if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500175 unittest.main()