Skip to content

Commit

Permalink
Better.
Browse files Browse the repository at this point in the history
  • Loading branch information
excitoon committed Sep 4, 2022
1 parent d522bcd commit 8c07645
Showing 1 changed file with 107 additions and 116 deletions.
223 changes: 107 additions & 116 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def test_caleb_05_match_entries_empty(self):
self.assertFalse(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_05_match_entries_empty_rule(self):
matches = self.__parse_gitignore_string(["# Hey"], mock_base_path="/home/caleb")
matches = self.__parse_gitignore_string([""], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/X", is_dir=is_dir))
Expand All @@ -581,51 +581,23 @@ def test_caleb_05_match_entries_empty_rule(self):
self.assertFalse(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_01_absolute(self):
"""
Tests an absolute path pattern.
This should match:
an/absolute/file/path
an/absolute/file/path/foo
This should NOT match:
foo/an/absolute/file/path
"""
regex, include = GitWildMatchPattern.pattern_to_regex("/an/absolute/file/path")
self.assertTrue(include)
self.assertEqual(regex, "^an/absolute/file/path(?:/.*)?$")
matches = self.__parse_gitignore_string(["/an/absolute/file/path"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertTrue(matches("/home/caleb/an/absolute/file/path", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/an/absolute/file/path/foo", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/foo/an/absolute/file/path", is_dir=is_dir))

pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(
pattern.match(
[
"an/absolute/file/path",
"an/absolute/file/path/foo",
"foo/an/absolute/file/path",
]
)
)
self.assertEqual(
results,
{
"an/absolute/file/path",
"an/absolute/file/path/foo",
},
)
def test_caleb_01_absolute_without_leading_slash(self):
matches = self.__parse_gitignore_string(["an/absolute/file/path"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertTrue(matches("/home/caleb/an/absolute/file/path", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/an/absolute/file/path/foo", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/foo/an/absolute/file/path", is_dir=is_dir))

def test_caleb_01_absolute_ignore(self):
"""
Tests an ignore absolute path pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex("!/foo/build")
self.assertFalse(include)
self.assertEqual(regex, "^foo/build(?:/.*)?$")

# NOTE: The pattern match is backwards because the pattern itself
# does not consider the include attribute.
pattern = GitWildMatchPattern(re.compile(regex), include)
matches = self.__parse_gitignore_string(["!/foo/build"], mock_base_path="/home/caleb")
results = set(
pattern.match(
[
Expand All @@ -642,33 +614,82 @@ def test_caleb_01_absolute_ignore(self):
)

def test_caleb_01_absolute_root(self):
"""
Tests a single root absolute path pattern.
matches = self.__parse_gitignore_string(["/"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/X", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

This should NOT match any file (according to git check-ignore
(v2.4.1)).
"""
regex, include = GitWildMatchPattern.pattern_to_regex("/")
self.assertIsNone(include)
self.assertIsNone(regex)
def test_caleb_01_asterisk(self):
matches = self.__parse_gitignore_string(["*"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertTrue(matches("/home/caleb/X", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_01_relative(self):
"""
Tests a relative path pattern.
def test_caleb_01_absolute_root_with_asterisk(self):
matches = self.__parse_gitignore_string(["/*"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertTrue(matches("/home/caleb/X", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

This should match:
def test_caleb_01_two_asterisks(self):
matches = self.__parse_gitignore_string(["**"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertTrue(matches("/home/caleb/X", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

spam
spam/
foo/spam
spam/foo
foo/spam/bar
"""
regex, include = GitWildMatchPattern.pattern_to_regex("spam")
self.assertTrue(include)
self.assertEqual(regex, "^(?:.+/)?spam(?:/.*)?$")
def test_caleb_01_absolute_root_with_two_asterisks(self):
matches = self.__parse_gitignore_string(["/**"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertTrue(matches("/home/caleb/X", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

pattern = GitWildMatchPattern(re.compile(regex), include)
def test_caleb_01_relative(self):
matches = self.__parse_gitignore_string(["spam"], mock_base_path="/home/caleb")
results = set(
pattern.match(
[
Expand All @@ -692,23 +713,7 @@ def test_caleb_01_relative(self):
)

def test_caleb_01_relative_nested(self):
"""
Tests a relative nested path pattern.
This should match:
foo/spam
foo/spam/bar
This should **not** match (according to git check-ignore (v2.4.1)):
bar/foo/spam
"""
regex, include = GitWildMatchPattern.pattern_to_regex("foo/spam")
self.assertTrue(include)
self.assertEqual(regex, "^foo/spam(?:/.*)?$")

pattern = GitWildMatchPattern(re.compile(regex), include)
matches = self.__parse_gitignore_string(["foo/spam"], mock_base_path="/home/caleb")
results = set(
pattern.match(
[
Expand All @@ -727,42 +732,28 @@ def test_caleb_01_relative_nested(self):
)

def test_caleb_02_comment(self):
"""
Tests a comment pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex("# Cork soakers.")
self.assertIsNone(include)
self.assertIsNone(regex)
matches = self.__parse_gitignore_string(["# Cork soakers."], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/X", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_02_ignore(self):
"""
Tests an exclude pattern.
This should NOT match (according to git check-ignore (v2.4.1)):
temp/foo
"""
regex, include = GitWildMatchPattern.pattern_to_regex("!temp")
self.assertIsNotNone(include)
self.assertFalse(include)
self.assertEqual(regex, "^(?:.+/)?temp(?:/.*)?$")

# NOTE: The pattern match is backwards because the pattern itself
# does not consider the include attribute.
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(
pattern.match(
[
"temp/foo",
]
)
)
self.assertEqual(
results,
{
"temp/foo",
},
)
matches = self.__parse_gitignore_string(["!temp"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/temp", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/foo/temp", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/temp/foo", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/foo/temp/bar", is_dir=is_dir))

def test_caleb_03_child_double_asterisk(self):
"""
Expand Down

0 comments on commit 8c07645

Please sign in to comment.