test_ignore.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 unittest
  23. from dulwich.ignore import (
  24. IgnoreFilter,
  25. IgnoreFilterStack,
  26. match_pattern,
  27. read_ignore_patterns,
  28. translate,
  29. )
  30. POSITIVE_MATCH_TESTS = [
  31. (b"foo.c", b"*.c"),
  32. (b"foo/foo.c", b"*.c"),
  33. (b"foo/foo.c", b"foo.c"),
  34. (b"foo.c", b"/*.c"),
  35. (b"foo.c", b"/foo.c"),
  36. (b"foo.c", b"foo.c"),
  37. (b"foo.c", b"foo.[ch]"),
  38. (b"foo/bar/bla.c", b"foo/**"),
  39. (b"foo/bar/bla/blie.c", b"foo/**/blie.c"),
  40. (b"foo/bar/bla.c", b"**/bla.c"),
  41. (b"bla.c", b"**/bla.c"),
  42. (b"foo/bar", b"foo/**/bar"),
  43. (b"foo/bla/bar", b"foo/**/bar"),
  44. ]
  45. NEGATIVE_MATCH_TESTS = [
  46. (b"foo.c", b"foo.[dh]"),
  47. (b"foo/foo.c", b"/foo.c"),
  48. (b"foo/foo.c", b"/*.c"),
  49. ]
  50. TRANSLATE_TESTS = [
  51. (b"*.c", b'(?ms)(.*\\/)?[^\\/]+\\.c\\Z'),
  52. (b"foo.c", b'(?ms)(.*\\/)?foo\\.c\\Z'),
  53. (b"/*.c", b'(?ms)[^\\/]+\\.c\\Z'),
  54. (b"/foo.c", b'(?ms)foo\\.c\\Z'),
  55. (b"foo.c", b'(?ms)(.*\\/)?foo\\.c\\Z'),
  56. (b"foo.[ch]", b'(?ms)(.*\\/)?foo\\.[ch]\\Z'),
  57. (b"foo/**", b'(?ms)foo(\\/.*)?\\Z'),
  58. (b"foo/**/blie.c", b'(?ms)foo(\\/.*)?\\/blie\\.c\\Z'),
  59. (b"**/bla.c", b'(?ms)(.*\\/)?bla\\.c\\Z'),
  60. (b"foo/**/bar", b'(?ms)foo(\\/.*)?\\/bar\\Z'),
  61. ]
  62. class TranslateTests(unittest.TestCase):
  63. def test_translate(self):
  64. for (pattern, regex) in TRANSLATE_TESTS:
  65. self.assertEqual(
  66. regex, translate(pattern),
  67. "orig pattern: %r, regex: %r, expected: %r" %
  68. (pattern, translate(pattern), regex))
  69. class ReadIgnorePatterns(unittest.TestCase):
  70. def test_read_file(self):
  71. f = BytesIO(b"""
  72. # a comment
  73. # and an empty line:
  74. \#not a comment
  75. !negative
  76. with trailing whitespace
  77. with escaped trailing whitespace\
  78. """)
  79. self.assertEqual(list(read_ignore_patterns(f)), [
  80. b'\\#not a comment',
  81. b'!negative',
  82. b'with trailing whitespace',
  83. b'with escaped trailing whitespace '
  84. ])
  85. class MatchPatternTests(unittest.TestCase):
  86. def test_matches(self):
  87. for (path, pattern) in POSITIVE_MATCH_TESTS:
  88. self.assertTrue(
  89. match_pattern(path, pattern),
  90. "path: %r, pattern: %r" % (path, pattern))
  91. def test_no_matches(self):
  92. for (path, pattern) in NEGATIVE_MATCH_TESTS:
  93. self.assertFalse(
  94. match_pattern(path, pattern),
  95. "path: %r, pattern: %r" % (path, pattern))
  96. class IgnoreFilterTests(unittest.TestCase):
  97. def test_included(self):
  98. filter = IgnoreFilter([b'a.c', b'b.c'])
  99. self.assertTrue(filter.is_ignored(b'a.c'))
  100. self.assertIs(None, filter.is_ignored(b'c.c'))
  101. def test_excluded(self):
  102. filter = IgnoreFilter([b'a.c', b'b.c', b'!c.c'])
  103. self.assertFalse(filter.is_ignored(b'c.c'))
  104. self.assertIs(None, filter.is_ignored(b'd.c'))
  105. class IgnoreFilterStackTests(unittest.TestCase):
  106. def test_stack_first(self):
  107. filter1 = IgnoreFilter([b'a.c', b'b.c', b'!d.c'])
  108. filter2 = IgnoreFilter([b'a.c', b'!b,c', b'c.c', b'd.c'])
  109. stack = IgnoreFilterStack([filter1, filter2])
  110. self.assertIs(True, stack.is_ignored(b'a.c'))
  111. self.assertIs(True, stack.is_ignored(b'b.c'))
  112. self.assertIs(True, stack.is_ignored(b'c.c'))
  113. self.assertIs(False, stack.is_ignored(b'd.c'))
  114. self.assertIs(None, stack.is_ignored(b'e.c'))