test_ignore.py 9.0 KB

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