2
0

test_ignore.py 10.0 KB

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