1"""Test cases for the fnmatch module."""
2
3import unittest
4import os
5import warnings
6
7from fnmatch import fnmatch, fnmatchcase, translate, filter
8
9class FnmatchTestCase(unittest.TestCase):
10
11    def check_match(self, filename, pattern, should_match=True, fn=fnmatch):
12        if should_match:
13            self.assertTrue(fn(filename, pattern),
14                         "expected %r to match pattern %r"
15                         % (filename, pattern))
16        else:
17            self.assertFalse(fn(filename, pattern),
18                         "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]')
31        check('abc', 'ab[de]', False)
32        check('a', '??', False)
33        check('a', 'b', False)
34
35        # these test that '\' is handled correctly in character sets;
36        # see SF bug #409651
37        check('\\', r'[\]')
38        check('a', r'[!\]')
39        check('\\', r'[!\]', False)
40
41        # 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
48    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
55    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
61    def test_fnmatchcase(self):
62        check = self.check_match
63        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)
72
73    def test_bytes(self):
74        self.check_match(b'test', b'te*')
75        self.check_match(b'test\xff', b'te*\xff')
76        self.check_match(b'foo\nbar', b'foo*')
77
78    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
94    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
105
106class TranslateTestCase(unittest.TestCase):
107
108    def test_translate(self):
109        import re
110        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')
118        # 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
126        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'))
146
147class FilterTestCase(unittest.TestCase):
148
149    def test_filter(self):
150        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'])
172
173
174if __name__ == "__main__":
175    unittest.main()
176