2
0

test_ignore.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # test_ignore.py -- Tests for ignore files.
  2. # Copyright (C) 2017 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for ignore files."""
  22. import os
  23. import re
  24. import shutil
  25. import tempfile
  26. from io import BytesIO
  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. from . import TestCase
  38. POSITIVE_MATCH_TESTS = [
  39. (b"foo.c", b"*.c"),
  40. (b".c", b"*.c"),
  41. (b"foo/foo.c", b"*.c"),
  42. (b"foo/foo.c", b"foo.c"),
  43. (b"foo.c", b"/*.c"),
  44. (b"foo.c", b"/foo.c"),
  45. (b"foo.c", b"foo.c"),
  46. (b"foo.c", b"foo.[ch]"),
  47. (b"foo/bar/bla.c", b"foo/**"),
  48. (b"foo/bar/bla/blie.c", b"foo/**/blie.c"),
  49. (b"foo/bar/bla.c", b"**/bla.c"),
  50. (b"bla.c", b"**/bla.c"),
  51. (b"foo/bar", b"foo/**/bar"),
  52. (b"foo/bla/bar", b"foo/**/bar"),
  53. (b"foo/bar/", b"bar/"),
  54. (b"foo/bar/", b"bar"),
  55. (b"foo/bar/something", b"foo/bar/*"),
  56. ]
  57. NEGATIVE_MATCH_TESTS = [
  58. (b"foo.c", b"foo.[dh]"),
  59. (b"foo/foo.c", b"/foo.c"),
  60. (b"foo/foo.c", b"/*.c"),
  61. (b"foo/bar/", b"/bar/"),
  62. (b"foo/bar/", b"foo/bar/*"),
  63. (b"foo/bar", b"foo?bar"),
  64. ]
  65. TRANSLATE_TESTS = [
  66. (b"*.c", b"(?ms)(.*/)?[^/]*\\.c/?\\Z"),
  67. (b"foo.c", b"(?ms)(.*/)?foo\\.c/?\\Z"),
  68. (b"/*.c", b"(?ms)[^/]*\\.c/?\\Z"),
  69. (b"/foo.c", b"(?ms)foo\\.c/?\\Z"),
  70. (b"foo.c", b"(?ms)(.*/)?foo\\.c/?\\Z"),
  71. (b"foo.[ch]", b"(?ms)(.*/)?foo\\.[ch]/?\\Z"),
  72. (b"bar/", b"(?ms)(.*/)?bar\\/\\Z"),
  73. (b"foo/**", b"(?ms)foo(/.*)?/?\\Z"),
  74. (b"foo/**/blie.c", b"(?ms)foo(/.*)?\\/blie\\.c/?\\Z"),
  75. (b"**/bla.c", b"(?ms)(.*/)?bla\\.c/?\\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[0-9]", b"(?ms)foo[0-9]/?\\Z"),
  81. ]
  82. class TranslateTests(TestCase):
  83. def test_translate(self) -> None:
  84. for pattern, regex in TRANSLATE_TESTS:
  85. if re.escape(b"/") == b"/":
  86. # Slash is no longer escaped in Python3.7, so undo the escaping
  87. # in the expected return value..
  88. regex = regex.replace(b"\\/", b"/")
  89. self.assertEqual(
  90. regex,
  91. translate(pattern),
  92. f"orig pattern: {pattern!r}, regex: {translate(pattern)!r}, expected: {regex!r}",
  93. )
  94. class ReadIgnorePatterns(TestCase):
  95. def test_read_file(self) -> None:
  96. f = BytesIO(
  97. b"""
  98. # a comment
  99. \x20\x20
  100. # and an empty line:
  101. \\#not a comment
  102. !negative
  103. with trailing whitespace
  104. with escaped trailing whitespace\\
  105. """
  106. )
  107. self.assertEqual(
  108. list(read_ignore_patterns(f)),
  109. [
  110. b"\\#not a comment",
  111. b"!negative",
  112. b"with trailing whitespace",
  113. b"with escaped trailing whitespace ",
  114. ],
  115. )
  116. class MatchPatternTests(TestCase):
  117. def test_matches(self) -> None:
  118. for path, pattern in POSITIVE_MATCH_TESTS:
  119. self.assertTrue(
  120. match_pattern(path, pattern),
  121. f"path: {path!r}, pattern: {pattern!r}",
  122. )
  123. def test_no_matches(self) -> None:
  124. for path, pattern in NEGATIVE_MATCH_TESTS:
  125. self.assertFalse(
  126. match_pattern(path, pattern),
  127. f"path: {path!r}, pattern: {pattern!r}",
  128. )
  129. class IgnoreFilterTests(TestCase):
  130. def test_included(self) -> None:
  131. filter = IgnoreFilter([b"a.c", b"b.c"])
  132. self.assertTrue(filter.is_ignored(b"a.c"))
  133. self.assertIs(None, filter.is_ignored(b"c.c"))
  134. self.assertEqual([Pattern(b"a.c")], list(filter.find_matching(b"a.c")))
  135. self.assertEqual([], list(filter.find_matching(b"c.c")))
  136. def test_included_ignorecase(self) -> None:
  137. filter = IgnoreFilter([b"a.c", b"b.c"], ignorecase=False)
  138. self.assertTrue(filter.is_ignored(b"a.c"))
  139. self.assertFalse(filter.is_ignored(b"A.c"))
  140. filter = IgnoreFilter([b"a.c", b"b.c"], ignorecase=True)
  141. self.assertTrue(filter.is_ignored(b"a.c"))
  142. self.assertTrue(filter.is_ignored(b"A.c"))
  143. self.assertTrue(filter.is_ignored(b"A.C"))
  144. def test_excluded(self) -> None:
  145. filter = IgnoreFilter([b"a.c", b"b.c", b"!c.c"])
  146. self.assertFalse(filter.is_ignored(b"c.c"))
  147. self.assertIs(None, filter.is_ignored(b"d.c"))
  148. self.assertEqual([Pattern(b"!c.c")], list(filter.find_matching(b"c.c")))
  149. self.assertEqual([], list(filter.find_matching(b"d.c")))
  150. def test_include_exclude_include(self) -> None:
  151. filter = IgnoreFilter([b"a.c", b"!a.c", b"a.c"])
  152. self.assertTrue(filter.is_ignored(b"a.c"))
  153. self.assertEqual(
  154. [Pattern(b"a.c"), Pattern(b"!a.c"), Pattern(b"a.c")],
  155. list(filter.find_matching(b"a.c")),
  156. )
  157. def test_manpage(self) -> None:
  158. # A specific example from the gitignore manpage
  159. filter = IgnoreFilter([b"/*", b"!/foo", b"/foo/*", b"!/foo/bar"])
  160. self.assertTrue(filter.is_ignored(b"a.c"))
  161. self.assertTrue(filter.is_ignored(b"foo/blie"))
  162. self.assertFalse(filter.is_ignored(b"foo"))
  163. self.assertFalse(filter.is_ignored(b"foo/bar"))
  164. self.assertFalse(filter.is_ignored(b"foo/bar/"))
  165. self.assertFalse(filter.is_ignored(b"foo/bar/bloe"))
  166. def test_regex_special(self) -> None:
  167. # See https://github.com/dulwich/dulwich/issues/930#issuecomment-1026166429
  168. filter = IgnoreFilter([b"/foo\\[bar\\]", b"/foo"])
  169. self.assertTrue(filter.is_ignored("foo"))
  170. self.assertTrue(filter.is_ignored("foo[bar]"))
  171. class IgnoreFilterStackTests(TestCase):
  172. def test_stack_first(self) -> None:
  173. filter1 = IgnoreFilter([b"[a].c", b"[b].c", b"![d].c"])
  174. filter2 = IgnoreFilter([b"[a].c", b"![b],c", b"[c].c", b"[d].c"])
  175. stack = IgnoreFilterStack([filter1, filter2])
  176. self.assertIs(True, stack.is_ignored(b"a.c"))
  177. self.assertIs(True, stack.is_ignored(b"b.c"))
  178. self.assertIs(True, stack.is_ignored(b"c.c"))
  179. self.assertIs(False, stack.is_ignored(b"d.c"))
  180. self.assertIs(None, stack.is_ignored(b"e.c"))
  181. class IgnoreFilterManagerTests(TestCase):
  182. def test_load_ignore(self) -> None:
  183. tmp_dir = tempfile.mkdtemp()
  184. self.addCleanup(shutil.rmtree, tmp_dir)
  185. repo = Repo.init(tmp_dir)
  186. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  187. f.write(b"/foo/bar\n")
  188. f.write(b"/dir2\n")
  189. f.write(b"/dir3/\n")
  190. os.mkdir(os.path.join(repo.path, "dir"))
  191. with open(os.path.join(repo.path, "dir", ".gitignore"), "wb") as f:
  192. f.write(b"/blie\n")
  193. with open(os.path.join(repo.path, "dir", "blie"), "wb") as f:
  194. f.write(b"IGNORED")
  195. p = os.path.join(repo.controldir(), "info", "exclude")
  196. with open(p, "wb") as f:
  197. f.write(b"/excluded\n")
  198. m = IgnoreFilterManager.from_repo(repo)
  199. self.assertTrue(m.is_ignored("dir/blie"))
  200. self.assertIs(None, m.is_ignored(os.path.join("dir", "bloe")))
  201. self.assertIs(None, m.is_ignored("dir"))
  202. self.assertTrue(m.is_ignored(os.path.join("foo", "bar")))
  203. self.assertTrue(m.is_ignored(os.path.join("excluded")))
  204. self.assertTrue(m.is_ignored(os.path.join("dir2", "fileinignoreddir")))
  205. self.assertFalse(m.is_ignored("dir3"))
  206. self.assertTrue(m.is_ignored("dir3/"))
  207. self.assertTrue(m.is_ignored("dir3/bla"))
  208. def test_nested_gitignores(self) -> None:
  209. tmp_dir = tempfile.mkdtemp()
  210. self.addCleanup(shutil.rmtree, tmp_dir)
  211. repo = Repo.init(tmp_dir)
  212. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  213. f.write(b"/*\n")
  214. f.write(b"!/foo\n")
  215. os.mkdir(os.path.join(repo.path, "foo"))
  216. with open(os.path.join(repo.path, "foo", ".gitignore"), "wb") as f:
  217. f.write(b"/bar\n")
  218. with open(os.path.join(repo.path, "foo", "bar"), "wb") as f:
  219. f.write(b"IGNORED")
  220. m = IgnoreFilterManager.from_repo(repo)
  221. self.assertTrue(m.is_ignored("foo/bar"))
  222. def test_load_ignore_ignorecase(self) -> None:
  223. tmp_dir = tempfile.mkdtemp()
  224. self.addCleanup(shutil.rmtree, tmp_dir)
  225. repo = Repo.init(tmp_dir)
  226. config = repo.get_config()
  227. config.set(b"core", b"ignorecase", True)
  228. config.write_to_path()
  229. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  230. f.write(b"/foo/bar\n")
  231. f.write(b"/dir\n")
  232. m = IgnoreFilterManager.from_repo(repo)
  233. self.assertTrue(m.is_ignored(os.path.join("dir", "blie")))
  234. self.assertTrue(m.is_ignored(os.path.join("DIR", "blie")))
  235. def test_ignored_contents(self) -> None:
  236. tmp_dir = tempfile.mkdtemp()
  237. self.addCleanup(shutil.rmtree, tmp_dir)
  238. repo = Repo.init(tmp_dir)
  239. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  240. f.write(b"a/*\n")
  241. f.write(b"!a/*.txt\n")
  242. m = IgnoreFilterManager.from_repo(repo)
  243. os.mkdir(os.path.join(repo.path, "a"))
  244. self.assertIs(None, m.is_ignored("a"))
  245. self.assertIs(None, m.is_ignored("a/"))
  246. self.assertFalse(m.is_ignored("a/b.txt"))
  247. self.assertTrue(m.is_ignored("a/c.dat"))