Skip to content

Commit

Permalink
Clean up test code
Browse files Browse the repository at this point in the history
  • Loading branch information
mherrmann committed Jan 19, 2024
1 parent 3861152 commit 2e3ba32
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,17 @@ def test_comment(self):
self.assertTrue(matches('/home/michael/#imnocomment'))

def test_ignore_directory(self):
matches = _parse_gitignore_string('.venv/', fake_base_dir='/home/michael')
matches = \
_parse_gitignore_string('.venv/', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/.venv'))
self.assertTrue(matches('/home/michael/.venv/folder'))
self.assertTrue(matches('/home/michael/.venv/file.txt'))
self.assertFalse(matches('/home/michael/.venv_other_folder'))
self.assertFalse(matches('/home/michael/.venv_no_folder.py'))

def test_ignore_directory_asterisk(self):
matches = _parse_gitignore_string('.venv/*', fake_base_dir='/home/michael')
matches = \
_parse_gitignore_string('.venv/*', fake_base_dir='/home/michael')
self.assertFalse(matches('/home/michael/.venv'))
self.assertTrue(matches('/home/michael/.venv/folder'))
self.assertTrue(matches('/home/michael/.venv/file.txt'))
Expand All @@ -112,20 +114,25 @@ def test_negation(self):
self.assertTrue(matches('/home/michael/waste.ignore'))

def test_literal_exclamation_mark(self):
matches = _parse_gitignore_string('\\!ignore_me!', fake_base_dir='/home/michael')
matches = _parse_gitignore_string(
'\\!ignore_me!', fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/!ignore_me!'))
self.assertFalse(matches('/home/michael/ignore_me!'))
self.assertFalse(matches('/home/michael/ignore_me'))

def test_double_asterisks(self):
matches = _parse_gitignore_string('foo/**/Bar', fake_base_dir='/home/michael')
matches = _parse_gitignore_string(
'foo/**/Bar', fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/foo/hello/Bar'))
self.assertTrue(matches('/home/michael/foo/world/Bar'))
self.assertTrue(matches('/home/michael/foo/Bar'))
self.assertFalse(matches('/home/michael/foo/BarBar'))

def test_double_asterisk_without_slashes_handled_like_single_asterisk(self):
matches = _parse_gitignore_string('a/b**c/d', fake_base_dir='/home/michael')
matches = \
_parse_gitignore_string('a/b**c/d', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/a/bc/d'))
self.assertTrue(matches('/home/michael/a/bXc/d'))
self.assertTrue(matches('/home/michael/a/bbc/d'))
Expand All @@ -136,10 +143,12 @@ def test_double_asterisk_without_slashes_handled_like_single_asterisk(self):
self.assertFalse(matches('/home/michael/a/bb/XX/cc/d'))

def test_more_asterisks_handled_like_single_asterisk(self):
matches = _parse_gitignore_string('***a/b', fake_base_dir='/home/michael')
matches = \
_parse_gitignore_string('***a/b', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/XYZa/b'))
self.assertFalse(matches('/home/michael/foo/a/b'))
matches = _parse_gitignore_string('a/b***', fake_base_dir='/home/michael')
matches = \
_parse_gitignore_string('a/b***', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/a/bXYZ'))
self.assertFalse(matches('/home/michael/a/b/foo'))

Expand All @@ -157,7 +166,9 @@ def test_directory_only_negation(self):
self.assertFalse(matches('/home/michael/data/01_raw/raw_file.csv'))
self.assertFalse(matches('/home/michael/data/02_processed/'))
self.assertFalse(matches('/home/michael/data/02_processed/.gitkeep'))
self.assertTrue(matches('/home/michael/data/02_processed/processed_file.csv'))
self.assertTrue(
matches('/home/michael/data/02_processed/processed_file.csv')
)

def test_single_asterisk(self):
matches = _parse_gitignore_string('*', fake_base_dir='/home/michael')
Expand All @@ -166,12 +177,16 @@ def test_single_asterisk(self):
self.assertTrue(matches('/home/michael/directory-trailing/'))

def test_supports_path_type_argument(self):
matches = _parse_gitignore_string('file1\n!file2', fake_base_dir='/home/michael')
matches = _parse_gitignore_string(
'file1\n!file2', fake_base_dir='/home/michael'
)
self.assertTrue(matches(Path('/home/michael/file1')))
self.assertFalse(matches(Path('/home/michael/file2')))

def test_slash_in_range_does_not_match_dirs(self):
matches = _parse_gitignore_string('abc[X-Z/]def', fake_base_dir='/home/michael')
matches = _parse_gitignore_string(
'abc[X-Z/]def', fake_base_dir='/home/michael'
)
self.assertFalse(matches('/home/michael/abcdef'))
self.assertTrue(matches('/home/michael/abcXdef'))
self.assertTrue(matches('/home/michael/abcYdef'))
Expand All @@ -180,34 +195,30 @@ def test_slash_in_range_does_not_match_dirs(self):
self.assertFalse(matches('/home/michael/abcXYZdef'))

def test_symlink_to_another_directory(self):
"""Test the behavior of a symlink to another directory.
The issue https://github.com/mherrmann/gitignore_parser/issues/29 describes how
a symlink to another directory caused an exception to be raised during matching.
This test ensures that the issue is now fixed.
"""
with TemporaryDirectory() as project_dir, TemporaryDirectory() as another_dir:
matches = _parse_gitignore_string('link', fake_base_dir=project_dir)
with TemporaryDirectory() as project_dir:
with TemporaryDirectory() as another_dir:
matches = \
_parse_gitignore_string('link', fake_base_dir=project_dir)

# Create a symlink to another directory.
link = Path(project_dir, 'link')
target = Path(another_dir, 'target')
link.symlink_to(target)
# Create a symlink to another directory.
link = Path(project_dir, 'link')
target = Path(another_dir, 'target')
link.symlink_to(target)

# Check the intended behavior according to
# https://git-scm.com/docs/gitignore#_notes:
# Symbolic links are not followed and are matched as if they were regular
# files.
self.assertTrue(matches(link))
# Check the intended behavior according to
# https://git-scm.com/docs/gitignore#_notes:
# Symbolic links are not followed and are matched as if they
# were regular files.
self.assertTrue(matches(link))

def test_symlink_to_symlink_directory(self):
with TemporaryDirectory() as project_dir:
with TemporaryDirectory() as link_dir:
link = Path(link_dir, 'link')
link.symlink_to(project_dir)
file = Path(link, 'file.txt')
matches = _parse_gitignore_string('file.txt', fake_base_dir=str(link))
matches = \
_parse_gitignore_string('file.txt', fake_base_dir=str(link))
self.assertTrue(matches(file))


Expand Down

0 comments on commit 2e3ba32

Please sign in to comment.