Skip to content

Commit

Permalink
Fix ValueError for some symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieumarrast authored and mherrmann committed Jan 19, 2024
1 parent 6c8bd44 commit 3861152
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 1 addition & 3 deletions gitignore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def parse_gitignore(full_path, base_dir=None):
for line in ignore_file:
counter += 1
line = line.rstrip('\n')
rule = rule_from_pattern(line, base_path=Path(base_dir).resolve(),
rule = rule_from_pattern(line, base_path=_normalize_path(base_dir),
source=(full_path, counter))
if rule:
rules.append(rule)
Expand All @@ -41,8 +41,6 @@ def rule_from_pattern(pattern, base_path=None, source=None):
Because git allows for nested .gitignore files, a base_path value
is required for correct behavior. The base path should be absolute.
"""
if base_path and base_path != Path(base_path).resolve():
raise ValueError('base_path must be absolute')
# Store the exact pattern for our repr and string functions
orig_pattern = pattern
# Early returns follow
Expand Down
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ def test_symlink_to_another_directory(self):
# 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))
self.assertTrue(matches(file))


def _parse_gitignore_string(data: str, fake_base_dir: str = None):
with patch('builtins.open', mock_open(read_data=data)):
success = parse_gitignore(f'{fake_base_dir}/.gitignore', fake_base_dir)
Expand Down

0 comments on commit 3861152

Please sign in to comment.