test_ignore.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 os
  23. import re
  24. import shutil
  25. import tempfile
  26. from dulwich.tests import TestCase
  27. from dulwich.ignore import (
  28. IgnoreFilter,
  29. IgnoreFilterManager,
  30. IgnoreFilterStack,
  31. Pattern,
  32. match_pattern,
  33. read_ignore_patterns,
  34. translate,
  35. )
  36. from dulwich.repo import Repo
  37. POSITIVE_MATCH_TESTS = [
  38. (b"foo.c", b"*.c"),
  39. (b".c", b"*.c"),
  40. (b"foo/foo.c", b"*.c"),
  41. (b"foo/foo.c", b"foo.c"),
  42. (b"foo.c", b"/*.c"),
  43. (b"foo.c", b"/foo.c"),
  44. (b"foo.c", b"foo.c"),
  45. (b"foo.c", b"foo.[ch]"),
  46. (b"foo/bar/bla.c", b"foo/**"),
  47. (b"foo/bar/bla/blie.c", b"foo/**/blie.c"),
  48. (b"foo/bar/bla.c", b"**/bla.c"),
  49. (b"bla.c", b"**/bla.c"),
  50. (b"foo/bar", b"foo/**/bar"),
  51. (b"foo/bla/bar", b"foo/**/bar"),
  52. (b"foo/bar/", b"bar/"),
  53. (b"foo/bar/", b"bar"),
  54. (b"foo/bar/", b"foo/bar/*"),
  55. ]
  56. NEGATIVE_MATCH_TESTS = [
  57. (b"foo.c", b"foo.[dh]"),
  58. (b"foo/foo.c", b"/foo.c"),
  59. (b"foo/foo.c", b"/*.c"),
  60. (b"foo/bar/", b"/bar/"),
  61. ]
  62. TRANSLATE_TESTS = [
  63. (b"*.c", b'(?ms)(.*/)?[^/]*\\.c/?\\Z'),
  64. (b"foo.c", b'(?ms)(.*/)?foo\\.c/?\\Z'),
  65. (b"/*.c", b'(?ms)[^/]*\\.c/?\\Z'),
  66. (b"/foo.c", b'(?ms)foo\\.c/?\\Z'),
  67. (b"foo.c", b'(?ms)(.*/)?foo\\.c/?\\Z'),
  68. (b"foo.[ch]", b'(?ms)(.*/)?foo\\.[ch]/?\\Z'),
  69. (b"bar/", b'(?ms)(.*/)?bar\\/\\Z'),
  70. (b"foo/**", b'(?ms)foo(/.*)?/?\\Z'),
  71. (b"foo/**/blie.c", b'(?ms)foo(/.*)?\\/blie\\.c/?\\Z'),
  72. (b"**/bla.c", b'(?ms)(.*/)?bla\\.c/?\\Z'),
  73. (b"foo/**/bar", b'(?ms)foo(/.*)?\\/bar/?\\Z'),
  74. (b"foo/bar/*", b'(?ms)foo\\/bar\\/[^/]*/?\\Z'),
  75. ]
  76. class TranslateTests(TestCase):
  77. def test_translate(self):
  78. for (pattern, regex) in TRANSLATE_TESTS:
  79. if re.escape(b'/') == b'/':
  80. # Slash is no longer escaped in Python3.7, so undo the escaping
  81. # in the expected return value..
  82. regex = regex.replace(b'\\/', b'/')
  83. self.assertEqual(
  84. regex, translate(pattern),
  85. "orig pattern: %r, regex: %r, expected: %r" %
  86. (pattern, translate(pattern), regex))
  87. class ReadIgnorePatterns(TestCase):
  88. def test_read_file(self):
  89. f = BytesIO(b"""
  90. # a comment
  91. # and an empty line:
  92. \#not a comment
  93. !negative
  94. with trailing whitespace
  95. with escaped trailing whitespace\
  96. """) # noqa: W291
  97. self.assertEqual(list(read_ignore_patterns(f)), [
  98. b'\\#not a comment',
  99. b'!negative',
  100. b'with trailing whitespace',
  101. b'with escaped trailing whitespace '
  102. ])
  103. class MatchPatternTests(TestCase):
  104. def test_matches(self):
  105. for (path, pattern) in POSITIVE_MATCH_TESTS:
  106. self.assertTrue(
  107. match_pattern(path, pattern),
  108. "path: %r, pattern: %r" % (path, pattern))
  109. def test_no_matches(self):
  110. for (path, pattern) in NEGATIVE_MATCH_TESTS:
  111. self.assertFalse(
  112. match_pattern(path, pattern),
  113. "path: %r, pattern: %r" % (path, pattern))
  114. class IgnoreFilterTests(TestCase):
  115. def test_included(self):
  116. filter = IgnoreFilter([b'a.c', b'b.c'])
  117. self.assertTrue(filter.is_ignored(b'a.c'))
  118. self.assertIs(None, filter.is_ignored(b'c.c'))
  119. self.assertEqual(
  120. [Pattern(b'a.c')],
  121. list(filter.find_matching(b'a.c')))
  122. self.assertEqual(
  123. [],
  124. list(filter.find_matching(b'c.c')))
  125. def test_included_ignorecase(self):
  126. filter = IgnoreFilter([b'a.c', b'b.c'], ignorecase=False)
  127. self.assertTrue(filter.is_ignored(b'a.c'))
  128. self.assertFalse(filter.is_ignored(b'A.c'))
  129. filter = IgnoreFilter([b'a.c', b'b.c'], ignorecase=True)
  130. self.assertTrue(filter.is_ignored(b'a.c'))
  131. self.assertTrue(filter.is_ignored(b'A.c'))
  132. self.assertTrue(filter.is_ignored(b'A.C'))
  133. def test_excluded(self):
  134. filter = IgnoreFilter([b'a.c', b'b.c', b'!c.c'])
  135. self.assertFalse(filter.is_ignored(b'c.c'))
  136. self.assertIs(None, filter.is_ignored(b'd.c'))
  137. self.assertEqual(
  138. [Pattern(b'!c.c')],
  139. list(filter.find_matching(b'c.c')))
  140. self.assertEqual([], list(filter.find_matching(b'd.c')))
  141. def test_include_exclude_include(self):
  142. filter = IgnoreFilter([b'a.c', b'!a.c', b'a.c'])
  143. self.assertTrue(filter.is_ignored(b'a.c'))
  144. self.assertEqual(
  145. [Pattern(b'a.c'), Pattern(b'!a.c'), Pattern(b'a.c')],
  146. list(filter.find_matching(b'a.c')))
  147. def test_manpage(self):
  148. # A specific example from the gitignore manpage
  149. filter = IgnoreFilter([
  150. b'/*',
  151. b'!/foo',
  152. b'/foo/*',
  153. b'!/foo/bar'])
  154. self.assertTrue(filter.is_ignored(b'a.c'))
  155. self.assertTrue(filter.is_ignored(b'foo/blie'))
  156. self.assertFalse(filter.is_ignored(b'foo'))
  157. self.assertFalse(filter.is_ignored(b'foo/bar'))
  158. self.assertFalse(filter.is_ignored(b'foo/bar/'))
  159. self.assertFalse(filter.is_ignored(b'foo/bar/bloe'))
  160. class IgnoreFilterStackTests(TestCase):
  161. def test_stack_first(self):
  162. filter1 = IgnoreFilter([b'[a].c', b'[b].c', b'![d].c'])
  163. filter2 = IgnoreFilter([b'[a].c', b'![b],c', b'[c].c', b'[d].c'])
  164. stack = IgnoreFilterStack([filter1, filter2])
  165. self.assertIs(True, stack.is_ignored(b'a.c'))
  166. self.assertIs(True, stack.is_ignored(b'b.c'))
  167. self.assertIs(True, stack.is_ignored(b'c.c'))
  168. self.assertIs(False, stack.is_ignored(b'd.c'))
  169. self.assertIs(None, stack.is_ignored(b'e.c'))
  170. class IgnoreFilterManagerTests(TestCase):
  171. def test_load_ignore(self):
  172. tmp_dir = tempfile.mkdtemp()
  173. self.addCleanup(shutil.rmtree, tmp_dir)
  174. repo = Repo.init(tmp_dir)
  175. with open(os.path.join(repo.path, '.gitignore'), 'wb') as f:
  176. f.write(b'/foo/bar\n')
  177. f.write(b'/dir2\n')
  178. f.write(b'/dir3/\n')
  179. os.mkdir(os.path.join(repo.path, 'dir'))
  180. with open(os.path.join(repo.path, 'dir', '.gitignore'), 'wb') as f:
  181. f.write(b'/blie\n')
  182. with open(os.path.join(repo.path, 'dir', 'blie'), 'wb') as f:
  183. f.write(b'IGNORED')
  184. p = os.path.join(repo.controldir(), 'info', 'exclude')
  185. with open(p, 'wb') as f:
  186. f.write(b'/excluded\n')
  187. m = IgnoreFilterManager.from_repo(repo)
  188. self.assertTrue(m.is_ignored('dir/blie'))
  189. self.assertIs(None,
  190. m.is_ignored(os.path.join(repo.path, 'dir', 'bloe')))
  191. self.assertIs(None, m.is_ignored(os.path.join(repo.path, 'dir')))
  192. self.assertTrue(m.is_ignored(os.path.join(repo.path, 'foo', 'bar')))
  193. self.assertTrue(m.is_ignored(os.path.join(repo.path, 'excluded')))
  194. self.assertTrue(m.is_ignored(os.path.join(
  195. repo.path, 'dir2', 'fileinignoreddir')))
  196. self.assertFalse(m.is_ignored('dir3'))
  197. self.assertTrue(m.is_ignored('dir3/'))
  198. self.assertTrue(m.is_ignored('dir3/bla'))
  199. def test_load_ignore_ignorecase(self):
  200. tmp_dir = tempfile.mkdtemp()
  201. self.addCleanup(shutil.rmtree, tmp_dir)
  202. repo = Repo.init(tmp_dir)
  203. config = repo.get_config()
  204. config.set(b'core', b'ignorecase', True)
  205. config.write_to_path()
  206. with open(os.path.join(repo.path, '.gitignore'), 'wb') as f:
  207. f.write(b'/foo/bar\n')
  208. f.write(b'/dir\n')
  209. m = IgnoreFilterManager.from_repo(repo)
  210. self.assertTrue(m.is_ignored(os.path.join('dir', 'blie')))
  211. self.assertTrue(m.is_ignored(os.path.join('DIR', 'blie')))