Skip to content

Commit 1db5f1b

Browse files
authored
Fix failing tests on Windows (#79)
Thank you @Javagedes for the PR.
1 parent 04cbcc3 commit 1db5f1b

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: ubuntu-latest
11+
runs-on: ${{ matrix.os }}
1212
strategy:
1313
matrix:
1414
python-version: ['3.13', '3.12', '3.11', '3.10', '3.9']
15+
os: [ubuntu-latest, windows-latest]
1516
steps:
1617
- uses: actions/checkout@v2
1718
- name: Set up Python ${{ matrix.python-version }}

gitignore_parser.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from os.path import abspath, dirname, join
66
from pathlib import Path
7+
import sys
78
from typing import Reversible, Union
89

910
def handle_negation(file_path, rules: Reversible["IgnoreRule"]):
@@ -129,9 +130,14 @@ def __repr__(self):
129130
def match(self, abs_path: Union[str, Path]):
130131
matched = False
131132
if self.base_path:
132-
rel_path = str(_normalize_path(abs_path).relative_to(self.base_path))
133+
rel_path = _normalize_path(abs_path).relative_to(self.base_path).as_posix()
133134
else:
134-
rel_path = str(_normalize_path(abs_path))
135+
rel_path = _normalize_path(abs_path).as_posix()
136+
# Path() strips the trailing following symbols on windows, so we need to
137+
# preserve it: ' ', '.'
138+
if sys.platform.startswith('win'):
139+
rel_path += ' ' * _count_trailing_symbol(' ', abs_path)
140+
rel_path += '.' * _count_trailing_symbol('.', abs_path)
135141
# Path() strips the trailing slash, so we need to preserve it
136142
# in case of directory-only negation
137143
if self.negation and type(abs_path) == str and abs_path[-1] == '/':
@@ -222,3 +228,14 @@ def _normalize_path(path: Union[str, Path]) -> Path:
222228
`Path.resolve()` does.
223229
"""
224230
return Path(abspath(path))
231+
232+
233+
def _count_trailing_symbol(symbol: str, text: str) -> int:
234+
"""Count the number of trailing characters in a string."""
235+
count = 0
236+
for char in reversed(str(text)):
237+
if char == symbol:
238+
count += 1
239+
else:
240+
break
241+
return count

tests.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from gitignore_parser import parse_gitignore, parse_gitignore_str
66

7-
from unittest import TestCase, main
7+
from unittest import TestCase, main, SkipTest
88

99

1010
class Test(TestCase):
@@ -206,13 +206,20 @@ def test_slash_in_range_does_not_match_dirs(self):
206206

207207
def test_symlink_to_another_directory(self):
208208
with TemporaryDirectory() as project_dir:
209+
project_dir = Path(project_dir).resolve()
209210
with TemporaryDirectory() as another_dir:
211+
another_dir = Path(another_dir).resolve()
210212
matches = parse_gitignore_str('link', base_dir=project_dir)
211213

212214
# Create a symlink to another directory.
213-
link = Path(project_dir, 'link')
214-
target = Path(another_dir, 'target')
215-
link.symlink_to(target)
215+
link = project_dir / 'link'
216+
target = another_dir / 'target'
217+
try:
218+
link.symlink_to(target)
219+
except OSError:
220+
raise SkipTest(
221+
"Current user does not have permissions to perform symlink."
222+
)
216223

217224
# Check the intended behavior according to
218225
# https://git-scm.com/docs/gitignore#_notes:
@@ -222,13 +229,19 @@ def test_symlink_to_another_directory(self):
222229

223230
def test_symlink_to_symlink_directory(self):
224231
with TemporaryDirectory() as project_dir:
232+
project_dir = Path(project_dir).resolve()
225233
with TemporaryDirectory() as link_dir:
226-
link = Path(link_dir, 'link')
227-
link.symlink_to(project_dir)
234+
link_dir = Path(link_dir).resolve()
235+
link = link_dir / 'link'
236+
try:
237+
link.symlink_to(project_dir)
238+
except OSError:
239+
raise SkipTest(
240+
"Current user does not have permissions to perform symlink."
241+
)
228242
file = Path(link, 'file.txt')
229243
matches = parse_gitignore_str('file.txt', base_dir=str(link_dir))
230244
self.assertTrue(matches(file))
231245

232-
233246
if __name__ == '__main__':
234247
main()

0 commit comments

Comments
 (0)