test_ignore.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # test_ignore.py -- Tests for ignore files.
  2. # Copyright (C) 2017 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for ignore files."""
  21. from io import BytesIO
  22. import re
  23. import unittest
  24. from dulwich.ignore import (
  25. IgnoreFilter,
  26. IgnoreFilterStack,
  27. match_pattern,
  28. read_ignore_patterns,
  29. translate,
  30. )
  31. POSITIVE_MATCH_TESTS = [
  32. (b"foo.c", b"*.c"),
  33. (b"foo/foo.c", b"*.c"),
  34. (b"foo/foo.c", b"foo.c"),
  35. (b"foo.c", b"/*.c"),
  36. (b"foo.c", b"/foo.c"),
  37. (b"foo.c", b"foo.c"),
  38. (b"foo.c", b"foo.[ch]"),
  39. (b"foo/bar/bla.c", b"foo/**"),
  40. (b"foo/bar/bla/blie.c", b"foo/**/blie.c"),
  41. (b"foo/bar/bla.c", b"**/bla.c"),
  42. (b"bla.c", b"**/bla.c"),
  43. (b"foo/bar", b"foo/**/bar"),
  44. (b"foo/bla/bar", b"foo/**/bar"),
  45. (b"foo/bar/", b"bar/"),
  46. (b"foo/bar/", b"bar"),
  47. ]
  48. NEGATIVE_MATCH_TESTS = [
  49. (b"foo.c", b"foo.[dh]"),
  50. (b"foo/foo.c", b"/foo.c"),
  51. (b"foo/foo.c", b"/*.c"),
  52. (b"foo/bar/", b"/bar/"),
  53. ]
  54. TRANSLATE_TESTS = [
  55. (b"*.c", b'(?ms)(.*/)?[^/]+\\.c/?\\Z'),
  56. (b"foo.c", b'(?ms)(.*/)?foo\\.c/?\\Z'),
  57. (b"/*.c", b'(?ms)[^/]+\\.c/?\\Z'),
  58. (b"/foo.c", b'(?ms)foo\\.c/?\\Z'),
  59. (b"foo.c", b'(?ms)(.*/)?foo\\.c/?\\Z'),
  60. (b"foo.[ch]", b'(?ms)(.*/)?foo\\.[ch]/?\\Z'),
  61. (b"bar/", b'(?ms)(.*/)?bar\\/\\Z'),
  62. (b"foo/**", b'(?ms)foo(/.*)?/?\\Z'),
  63. (b"foo/**/blie.c", b'(?ms)foo(/.*)?\\/blie\\.c/?\\Z'),
  64. (b"**/bla.c", b'(?ms)(.*/)?bla\\.c/?\\Z'),
  65. (b"foo/**/bar", b'(?ms)foo(/.*)?\\/bar/?\\Z'),
  66. ]
  67. class TranslateTests(unittest.TestCase):
  68. def test_translate(self):
  69. for (pattern, regex) in TRANSLATE_TESTS:
  70. if re.escape(b'/') == b'/':
  71. # Slash is no longer escaped in Python3.7, so undo the escaping
  72. # in the expected return value..
  73. regex = regex.replace(b'\\/', b'/')
  74. self.assertEqual(
  75. regex, translate(pattern),
  76. "orig pattern: %r, regex: %r, expected: %r" %
  77. (pattern, translate(pattern), regex))
  78. class ReadIgnorePatterns(unittest.TestCase):
  79. def test_read_file(self):
  80. f = BytesIO(b"""
  81. # a comment
  82. # and an empty line:
  83. \#not a comment
  84. !negative
  85. with trailing whitespace
  86. with escaped trailing whitespace\
  87. """)
  88. self.assertEqual(list(read_ignore_patterns(f)), [
  89. b'\\#not a comment',
  90. b'!negative',
  91. b'with trailing whitespace',
  92. b'with escaped trailing whitespace '
  93. ])
  94. class MatchPatternTests(unittest.TestCase):
  95. def test_matches(self):
  96. for (path, pattern) in POSITIVE_MATCH_TESTS:
  97. self.assertTrue(
  98. match_pattern(path, pattern),
  99. "path: %r, pattern: %r" % (path, pattern))
  100. def test_no_matches(self):
  101. for (path, pattern) in NEGATIVE_MATCH_TESTS:
  102. self.assertFalse(
  103. match_pattern(path, pattern),
  104. "path: %r, pattern: %r" % (path, pattern))
  105. class IgnoreFilterTests(unittest.TestCase):
  106. def test_included(self):
  107. filter = IgnoreFilter([b'a.c', b'b.c'])
  108. self.assertTrue(filter.is_ignored(b'a.c'))
  109. self.assertIs(None, filter.is_ignored(b'c.c'))
  110. def test_excluded(self):
  111. filter = IgnoreFilter([b'a.c', b'b.c', b'!c.c'])
  112. self.assertFalse(filter.is_ignored(b'c.c'))
  113. self.assertIs(None, filter.is_ignored(b'd.c'))
  114. def test_include_exclude_include(self):
  115. filter = IgnoreFilter([b'a.c', b'!a.c', b'a.c'])
  116. self.assertTrue(filter.is_ignored(b'a.c'))
  117. class IgnoreFilterStackTests(unittest.TestCase):
  118. def test_stack_first(self):
  119. filter1 = IgnoreFilter([b'[a].c', b'[b].c', b'![d].c'])
  120. filter2 = IgnoreFilter([b'[a].c', b'![b],c', b'[c].c', b'[d].c'])
  121. stack = IgnoreFilterStack([filter1, filter2])
  122. self.assertIs(True, stack.is_ignored(b'a.c'))
  123. self.assertIs(True, stack.is_ignored(b'b.c'))
  124. self.assertIs(True, stack.is_ignored(b'c.c'))
  125. self.assertIs(False, stack.is_ignored(b'd.c'))
  126. self.assertIs(None, stack.is_ignored(b'e.c'))