123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- """Tests for ignore files."""
- from io import BytesIO
- import os
- import re
- import shutil
- import tempfile
- from dulwich.tests import TestCase
- from dulwich.ignore import (
- IgnoreFilter,
- IgnoreFilterManager,
- IgnoreFilterStack,
- Pattern,
- match_pattern,
- read_ignore_patterns,
- translate,
- )
- from dulwich.repo import Repo
- POSITIVE_MATCH_TESTS = [
- (b"foo.c", b"*.c"),
- (b".c", b"*.c"),
- (b"foo/foo.c", b"*.c"),
- (b"foo/foo.c", b"foo.c"),
- (b"foo.c", b"/*.c"),
- (b"foo.c", b"/foo.c"),
- (b"foo.c", b"foo.c"),
- (b"foo.c", b"foo.[ch]"),
- (b"foo/bar/bla.c", b"foo/**"),
- (b"foo/bar/bla/blie.c", b"foo/**/blie.c"),
- (b"foo/bar/bla.c", b"**/bla.c"),
- (b"bla.c", b"**/bla.c"),
- (b"foo/bar", b"foo/**/bar"),
- (b"foo/bla/bar", b"foo/**/bar"),
- (b"foo/bar/", b"bar/"),
- (b"foo/bar/", b"bar"),
- (b"foo/bar/", b"foo/bar/*"),
- ]
- NEGATIVE_MATCH_TESTS = [
- (b"foo.c", b"foo.[dh]"),
- (b"foo/foo.c", b"/foo.c"),
- (b"foo/foo.c", b"/*.c"),
- (b"foo/bar/", b"/bar/"),
- ]
- TRANSLATE_TESTS = [
- (b"*.c", b'(?ms)(.*/)?[^/]*\\.c/?\\Z'),
- (b"foo.c", b'(?ms)(.*/)?foo\\.c/?\\Z'),
- (b"/*.c", b'(?ms)[^/]*\\.c/?\\Z'),
- (b"/foo.c", b'(?ms)foo\\.c/?\\Z'),
- (b"foo.c", b'(?ms)(.*/)?foo\\.c/?\\Z'),
- (b"foo.[ch]", b'(?ms)(.*/)?foo\\.[ch]/?\\Z'),
- (b"bar/", b'(?ms)(.*/)?bar\\/\\Z'),
- (b"foo/**", b'(?ms)foo(/.*)?/?\\Z'),
- (b"foo/**/blie.c", b'(?ms)foo(/.*)?\\/blie\\.c/?\\Z'),
- (b"**/bla.c", b'(?ms)(.*/)?bla\\.c/?\\Z'),
- (b"foo/**/bar", b'(?ms)foo(/.*)?\\/bar/?\\Z'),
- (b"foo/bar/*", b'(?ms)foo\\/bar\\/[^/]*/?\\Z'),
- ]
- class TranslateTests(TestCase):
- def test_translate(self):
- for (pattern, regex) in TRANSLATE_TESTS:
- if re.escape(b'/') == b'/':
-
-
- regex = regex.replace(b'\\/', b'/')
- self.assertEqual(
- regex, translate(pattern),
- "orig pattern: %r, regex: %r, expected: %r" %
- (pattern, translate(pattern), regex))
- class ReadIgnorePatterns(TestCase):
- def test_read_file(self):
- f = BytesIO(b"""
- # a comment
- # and an empty line:
- \#not a comment
- !negative
- with trailing whitespace
- with escaped trailing whitespace\
- """)
- self.assertEqual(list(read_ignore_patterns(f)), [
- b'\\#not a comment',
- b'!negative',
- b'with trailing whitespace',
- b'with escaped trailing whitespace '
- ])
- class MatchPatternTests(TestCase):
- def test_matches(self):
- for (path, pattern) in POSITIVE_MATCH_TESTS:
- self.assertTrue(
- match_pattern(path, pattern),
- "path: %r, pattern: %r" % (path, pattern))
- def test_no_matches(self):
- for (path, pattern) in NEGATIVE_MATCH_TESTS:
- self.assertFalse(
- match_pattern(path, pattern),
- "path: %r, pattern: %r" % (path, pattern))
- class IgnoreFilterTests(TestCase):
- def test_included(self):
- filter = IgnoreFilter([b'a.c', b'b.c'])
- self.assertTrue(filter.is_ignored(b'a.c'))
- self.assertIs(None, filter.is_ignored(b'c.c'))
- self.assertEqual(
- [Pattern(b'a.c')],
- list(filter.find_matching(b'a.c')))
- self.assertEqual(
- [],
- list(filter.find_matching(b'c.c')))
- def test_included_ignorecase(self):
- filter = IgnoreFilter([b'a.c', b'b.c'], ignorecase=False)
- self.assertTrue(filter.is_ignored(b'a.c'))
- self.assertFalse(filter.is_ignored(b'A.c'))
- filter = IgnoreFilter([b'a.c', b'b.c'], ignorecase=True)
- self.assertTrue(filter.is_ignored(b'a.c'))
- self.assertTrue(filter.is_ignored(b'A.c'))
- self.assertTrue(filter.is_ignored(b'A.C'))
- def test_excluded(self):
- filter = IgnoreFilter([b'a.c', b'b.c', b'!c.c'])
- self.assertFalse(filter.is_ignored(b'c.c'))
- self.assertIs(None, filter.is_ignored(b'd.c'))
- self.assertEqual(
- [Pattern(b'!c.c')],
- list(filter.find_matching(b'c.c')))
- self.assertEqual([], list(filter.find_matching(b'd.c')))
- def test_include_exclude_include(self):
- filter = IgnoreFilter([b'a.c', b'!a.c', b'a.c'])
- self.assertTrue(filter.is_ignored(b'a.c'))
- self.assertEqual(
- [Pattern(b'a.c'), Pattern(b'!a.c'), Pattern(b'a.c')],
- list(filter.find_matching(b'a.c')))
- def test_manpage(self):
-
- filter = IgnoreFilter([
- b'/*',
- b'!/foo',
- b'/foo/*',
- b'!/foo/bar'])
- self.assertTrue(filter.is_ignored(b'a.c'))
- self.assertTrue(filter.is_ignored(b'foo/blie'))
- self.assertFalse(filter.is_ignored(b'foo'))
- self.assertFalse(filter.is_ignored(b'foo/bar'))
- self.assertFalse(filter.is_ignored(b'foo/bar/'))
- self.assertFalse(filter.is_ignored(b'foo/bar/bloe'))
- class IgnoreFilterStackTests(TestCase):
- def test_stack_first(self):
- filter1 = IgnoreFilter([b'[a].c', b'[b].c', b'![d].c'])
- filter2 = IgnoreFilter([b'[a].c', b'![b],c', b'[c].c', b'[d].c'])
- stack = IgnoreFilterStack([filter1, filter2])
- self.assertIs(True, stack.is_ignored(b'a.c'))
- self.assertIs(True, stack.is_ignored(b'b.c'))
- self.assertIs(True, stack.is_ignored(b'c.c'))
- self.assertIs(False, stack.is_ignored(b'd.c'))
- self.assertIs(None, stack.is_ignored(b'e.c'))
- class IgnoreFilterManagerTests(TestCase):
- def test_load_ignore(self):
- tmp_dir = tempfile.mkdtemp()
- self.addCleanup(shutil.rmtree, tmp_dir)
- repo = Repo.init(tmp_dir)
- with open(os.path.join(repo.path, '.gitignore'), 'wb') as f:
- f.write(b'/foo/bar\n')
- f.write(b'/dir2\n')
- f.write(b'/dir3/\n')
- os.mkdir(os.path.join(repo.path, 'dir'))
- with open(os.path.join(repo.path, 'dir', '.gitignore'), 'wb') as f:
- f.write(b'/blie\n')
- with open(os.path.join(repo.path, 'dir', 'blie'), 'wb') as f:
- f.write(b'IGNORED')
- p = os.path.join(repo.controldir(), 'info', 'exclude')
- with open(p, 'wb') as f:
- f.write(b'/excluded\n')
- m = IgnoreFilterManager.from_repo(repo)
- self.assertTrue(m.is_ignored('dir/blie'))
- self.assertIs(None,
- m.is_ignored(os.path.join(repo.path, 'dir', 'bloe')))
- self.assertIs(None, m.is_ignored(os.path.join(repo.path, 'dir')))
- self.assertTrue(m.is_ignored(os.path.join(repo.path, 'foo', 'bar')))
- self.assertTrue(m.is_ignored(os.path.join(repo.path, 'excluded')))
- self.assertTrue(m.is_ignored(os.path.join(
- repo.path, 'dir2', 'fileinignoreddir')))
- self.assertFalse(m.is_ignored('dir3'))
- self.assertTrue(m.is_ignored('dir3/'))
- self.assertTrue(m.is_ignored('dir3/bla'))
- def test_load_ignore_ignorecase(self):
- tmp_dir = tempfile.mkdtemp()
- self.addCleanup(shutil.rmtree, tmp_dir)
- repo = Repo.init(tmp_dir)
- config = repo.get_config()
- config.set(b'core', b'ignorecase', True)
- config.write_to_path()
- with open(os.path.join(repo.path, '.gitignore'), 'wb') as f:
- f.write(b'/foo/bar\n')
- f.write(b'/dir\n')
- m = IgnoreFilterManager.from_repo(repo)
- self.assertTrue(m.is_ignored(os.path.join('dir', 'blie')))
- self.assertTrue(m.is_ignored(os.path.join('DIR', 'blie')))
|