test_ignore.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ]
  46. NEGATIVE_MATCH_TESTS = [
  47. (b"foo.c", b"foo.[dh]"),
  48. (b"foo/foo.c", b"/foo.c"),
  49. (b"foo/foo.c", b"/*.c"),
  50. ]
  51. TRANSLATE_TESTS = [
  52. (b"*.c", b'(?ms)(.*/)?[^/]+\\.c\\Z'),
  53. (b"foo.c", b'(?ms)(.*/)?foo\\.c\\Z'),
  54. (b"/*.c", b'(?ms)[^/]+\\.c\\Z'),
  55. (b"/foo.c", b'(?ms)foo\\.c\\Z'),
  56. (b"foo.c", b'(?ms)(.*/)?foo\\.c\\Z'),
  57. (b"foo.[ch]", b'(?ms)(.*/)?foo\\.[ch]\\Z'),
  58. (b"foo/**", b'(?ms)foo(/.*)?\\Z'),
  59. (b"foo/**/blie.c", b'(?ms)foo(/.*)?\\/blie\\.c\\Z'),
  60. (b"**/bla.c", b'(?ms)(.*/)?bla\\.c\\Z'),
  61. (b"foo/**/bar", b'(?ms)foo(/.*)?\\/bar\\Z'),
  62. ]
  63. class TranslateTests(unittest.TestCase):
  64. def test_translate(self):
  65. for (pattern, regex) in TRANSLATE_TESTS:
  66. if re.escape(b'/') == b'/':
  67. # Slash is no longer escaped in Python3.7, so undo the escaping
  68. # in the expected return value..
  69. regex = regex.replace(b'\\/', b'/')
  70. self.assertEqual(
  71. regex, translate(pattern),
  72. "orig pattern: %r, regex: %r, expected: %r" %
  73. (pattern, translate(pattern), regex))
  74. class ReadIgnorePatterns(unittest.TestCase):
  75. def test_read_file(self):
  76. f = BytesIO(b"""
  77. # a comment
  78. # and an empty line:
  79. \#not a comment
  80. !negative
  81. with trailing whitespace
  82. with escaped trailing whitespace\
  83. """)
  84. self.assertEqual(list(read_ignore_patterns(f)), [
  85. b'\\#not a comment',
  86. b'!negative',
  87. b'with trailing whitespace',
  88. b'with escaped trailing whitespace '
  89. ])
  90. class MatchPatternTests(unittest.TestCase):
  91. def test_matches(self):
  92. for (path, pattern) in POSITIVE_MATCH_TESTS:
  93. self.assertTrue(
  94. match_pattern(path, pattern),
  95. "path: %r, pattern: %r" % (path, pattern))
  96. def test_no_matches(self):
  97. for (path, pattern) in NEGATIVE_MATCH_TESTS:
  98. self.assertFalse(
  99. match_pattern(path, pattern),
  100. "path: %r, pattern: %r" % (path, pattern))
  101. class IgnoreFilterTests(unittest.TestCase):
  102. def test_included(self):
  103. filter = IgnoreFilter([b'a.c', b'b.c'])
  104. self.assertTrue(filter.is_ignored(b'a.c'))
  105. self.assertIs(None, filter.is_ignored(b'c.c'))
  106. def test_excluded(self):
  107. filter = IgnoreFilter([b'a.c', b'b.c', b'!c.c'])
  108. self.assertFalse(filter.is_ignored(b'c.c'))
  109. self.assertIs(None, filter.is_ignored(b'd.c'))
  110. class IgnoreFilterStackTests(unittest.TestCase):
  111. def test_stack_first(self):
  112. filter1 = IgnoreFilter([b'[a].c', b'[b].c', b'![d].c'])
  113. filter2 = IgnoreFilter([b'[a].c', b'![b],c', b'[c].c', b'[d].c'])
  114. stack = IgnoreFilterStack([filter1, filter2])
  115. self.assertIs(True, stack.is_ignored(b'a.c'))
  116. self.assertIs(True, stack.is_ignored(b'b.c'))
  117. self.assertIs(True, stack.is_ignored(b'c.c'))
  118. self.assertIs(False, stack.is_ignored(b'd.c'))
  119. self.assertIs(None, stack.is_ignored(b'e.c'))