blob: 1438da7a084b0cef56791f6bf58be944a3db52a9 [file] [log] [blame]
Fred Drake91751142001-03-21 18:29:25 +00001"""Test cases for the fnmatch module."""
2
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Fred Drake91751142001-03-21 18:29:25 +00004import unittest
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +03005import os
Fred Drake91751142001-03-21 18:29:25 +00006
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +03007from fnmatch import (fnmatch, fnmatchcase, translate, filter,
8 _MAXCACHE, _cache, _purge)
Fred Drake91751142001-03-21 18:29:25 +00009
10
11class FnmatchTestCase(unittest.TestCase):
R. David Murray2ab02f02010-07-10 14:06:51 +000012
13 def tearDown(self):
14 _purge()
15
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030016 def check_match(self, filename, pattern, should_match=True, fn=fnmatch):
Fred Drake91751142001-03-21 18:29:25 +000017 if should_match:
Georg Brandl6eedef62010-02-08 00:04:54 +000018 self.assertTrue(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000019 "expected %r to match pattern %r"
20 % (filename, pattern))
21 else:
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030022 self.assertFalse(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000023 "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]')
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030036 check('abc', 'ab[de]', False)
37 check('a', '??', False)
38 check('a', 'b', False)
Fred Drake91751142001-03-21 18:29:25 +000039
40 # these test that '\' is handled correctly in character sets;
Gregory P. Smithb98d6b22009-08-16 18:52:58 +000041 # see SF bug #409651
Fred Drake91751142001-03-21 18:29:25 +000042 check('\\', r'[\]')
43 check('a', r'[!\]')
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030044 check('\\', r'[!\]', False)
Fred Drake91751142001-03-21 18:29:25 +000045
Gregory P. Smithb98d6b22009-08-16 18:52:58 +000046 # 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
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030053 def test_mix_unicode_str(self):
54 check = self.check_match
55 check('test', u'*')
56 check(u'test', '*')
57 check('test', u'*', fn=fnmatchcase)
58 check(u'test', '*', fn=fnmatchcase)
59 with test_support.check_warnings(("", UnicodeWarning), quiet=True):
60 check('test\xff', u'*\xff')
61 check(u'test\xff', '*\xff')
62 check('test\xff', u'*\xff', fn=fnmatchcase)
63 check(u'test\xff', '*\xff', fn=fnmatchcase)
64
Georg Brandl308e18b2010-02-07 12:34:26 +000065 def test_fnmatchcase(self):
66 check = self.check_match
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030067 check('abc', 'abc', True, fnmatchcase)
68 check('AbC', 'abc', False, fnmatchcase)
69 check('abc', 'AbC', False, fnmatchcase)
70 check('AbC', 'AbC', True, fnmatchcase)
71
72 check('usr/bin', 'usr/bin', True, fnmatchcase)
73 check('usr\\bin', 'usr/bin', False, fnmatchcase)
74 check('usr/bin', 'usr\\bin', False, fnmatchcase)
75 check('usr\\bin', 'usr\\bin', True, fnmatchcase)
Georg Brandl308e18b2010-02-07 12:34:26 +000076
R. David Murrayabd45532010-07-09 13:16:00 +000077 def test_cache_clearing(self):
78 # check that caches do not grow too large
79 # http://bugs.python.org/issue7846
80
81 # string pattern cache
82 for i in range(_MAXCACHE + 1):
83 fnmatch('foo', '?' * i)
84
85 self.assertLessEqual(len(_cache), _MAXCACHE)
Fred Drake91751142001-03-21 18:29:25 +000086
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +030087 @test_support.requires_unicode
88 def test_unicode(self):
89 with test_support.check_warnings(("", UnicodeWarning), quiet=True):
90 self.check_match(u'test', u'te*')
91 self.check_match(u'test\xff', u'te*\xff')
92 self.check_match(u'test'+unichr(0x20ac), u'te*'+unichr(0x20ac))
93 self.check_match(u'foo\nbar', u'foo*')
94
95 def test_case(self):
96 ignorecase = os.path.normcase('ABC') == os.path.normcase('abc')
97 check = self.check_match
98 check('abc', 'abc')
99 check('AbC', 'abc', ignorecase)
100 check('abc', 'AbC', ignorecase)
101 check('AbC', 'AbC')
102
103 def test_sep(self):
104 normsep = os.path.normcase('\\') == os.path.normcase('/')
105 check = self.check_match
106 check('usr/bin', 'usr/bin')
107 check('usr\\bin', 'usr/bin', normsep)
108 check('usr/bin', 'usr\\bin', normsep)
109 check('usr\\bin', 'usr\\bin')
110
111
112class TranslateTestCase(unittest.TestCase):
113
114 def test_translate(self):
115 self.assertEqual(translate('*'), r'.*\Z(?ms)')
116 self.assertEqual(translate('?'), r'.\Z(?ms)')
117 self.assertEqual(translate('a?b*'), r'a.b.*\Z(?ms)')
118 self.assertEqual(translate('[abc]'), r'[abc]\Z(?ms)')
119 self.assertEqual(translate('[]]'), r'[]]\Z(?ms)')
120 self.assertEqual(translate('[!x]'), r'[^x]\Z(?ms)')
121 self.assertEqual(translate('[^x]'), r'[\^x]\Z(?ms)')
122 self.assertEqual(translate('[x'), r'\[x\Z(?ms)')
123
124
125class FilterTestCase(unittest.TestCase):
126
127 def test_filter(self):
128 self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'),
129 ['Python', 'Perl'])
130 self.assertEqual(filter([u'Python', u'Ruby', u'Perl', u'Tcl'], u'P*'),
131 [u'Python', u'Perl'])
132 with test_support.check_warnings(("", UnicodeWarning), quiet=True):
133 self.assertEqual(filter([u'test\xff'], u'*\xff'), [u'test\xff'])
134
135 @test_support.requires_unicode
136 def test_mix_bytes_str(self):
137 with test_support.check_warnings(("", UnicodeWarning), quiet=True):
138 self.assertEqual(filter(['test'], u'*'), ['test'])
139 self.assertEqual(filter([u'test'], '*'), [u'test'])
140 self.assertEqual(filter(['test\xff'], u'*'), ['test\xff'])
141 self.assertEqual(filter([u'test\xff'], '*'), [u'test\xff'])
142 self.assertEqual(filter(['test\xff'], u'*\xff'), ['test\xff'])
143 self.assertEqual(filter([u'test\xff'], '*\xff'), [u'test\xff'])
144
145 def test_case(self):
146 ignorecase = os.path.normcase('P') == os.path.normcase('p')
147 self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'),
148 ['Test.py', 'Test.PL'] if ignorecase else ['Test.py'])
149 self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'),
150 ['Test.py', 'Test.PL'] if ignorecase else ['Test.PL'])
151
152 def test_sep(self):
153 normsep = os.path.normcase('\\') == os.path.normcase('/')
154 self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'),
155 ['usr/bin', 'usr\\lib'] if normsep else ['usr/bin'])
156 self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'),
157 ['usr/bin', 'usr\\lib'] if normsep else ['usr\\lib'])
158
159
Fred Drake2e2be372001-09-20 21:33:42 +0000160def test_main():
Serhiy Storchaka2b67c7a2017-05-21 10:37:43 +0300161 test_support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000162
163
164if __name__ == "__main__":
165 test_main()