Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 1 | """Test cases for the fnmatch module.""" |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test import test_support |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 4 | import unittest |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 5 | import os |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 6 | |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 7 | from fnmatch import (fnmatch, fnmatchcase, translate, filter, |
| 8 | _MAXCACHE, _cache, _purge) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class FnmatchTestCase(unittest.TestCase): |
R. David Murray | 2ab02f0 | 2010-07-10 14:06:51 +0000 | [diff] [blame] | 12 | |
| 13 | def tearDown(self): |
| 14 | _purge() |
| 15 | |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 16 | def check_match(self, filename, pattern, should_match=True, fn=fnmatch): |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 17 | if should_match: |
Georg Brandl | 6eedef6 | 2010-02-08 00:04:54 +0000 | [diff] [blame] | 18 | self.assertTrue(fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 19 | "expected %r to match pattern %r" |
| 20 | % (filename, pattern)) |
| 21 | else: |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 22 | self.assertFalse(fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 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]') |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 36 | check('abc', 'ab[de]', False) |
| 37 | check('a', '??', False) |
| 38 | check('a', 'b', False) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 39 | |
| 40 | # these test that '\' is handled correctly in character sets; |
Gregory P. Smith | b98d6b2 | 2009-08-16 18:52:58 +0000 | [diff] [blame] | 41 | # see SF bug #409651 |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 42 | check('\\', r'[\]') |
| 43 | check('a', r'[!\]') |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 44 | check('\\', r'[!\]', False) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 45 | |
Gregory P. Smith | b98d6b2 | 2009-08-16 18:52:58 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 53 | 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 Brandl | 308e18b | 2010-02-07 12:34:26 +0000 | [diff] [blame] | 65 | def test_fnmatchcase(self): |
| 66 | check = self.check_match |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 67 | 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 Brandl | 308e18b | 2010-02-07 12:34:26 +0000 | [diff] [blame] | 76 | |
R. David Murray | abd4553 | 2010-07-09 13:16:00 +0000 | [diff] [blame] | 77 | 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 Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 86 | |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 87 | @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 | |
| 112 | class 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 | |
| 125 | class 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 Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 160 | def test_main(): |
Serhiy Storchaka | 2b67c7a | 2017-05-21 10:37:43 +0300 | [diff] [blame] | 161 | test_support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | if __name__ == "__main__": |
| 165 | test_main() |