test_ignore.py 10 KB

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