test_ignore.py 9.0 KB

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