From 1b0ef86062cc893691d801bb3076c47003152c53 Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Wed, 25 Oct 2023 08:12:38 -0700 Subject: [PATCH 1/4] Initial commit --- .github/workflows/ci.yml | 3 ++- gitignore_parser.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40cee41..36152a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,10 +8,11 @@ on: jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: matrix: python-version: ['3.11', '3.10', '3.9', '3.8', '3.7'] + os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} diff --git a/gitignore_parser.py b/gitignore_parser.py index 00a56aa..abae088 100644 --- a/gitignore_parser.py +++ b/gitignore_parser.py @@ -4,6 +4,7 @@ from os.path import abspath, dirname from pathlib import Path +import sys from typing import Reversible, Union def handle_negation(file_path, rules: Reversible["IgnoreRule"]): @@ -128,6 +129,10 @@ def match(self, abs_path: Union[str, Path]): rel_path = str(_normalize_path(abs_path).relative_to(self.base_path)) else: rel_path = str(_normalize_path(abs_path)) + # Path() strips the trailing whitespace on windows, so we need to + # preserve it. + if sys.platform.startswith('win'): + rel_path += ' ' * _count_trailing_whitespace(abs_path) # Path() strips the trailing slash, so we need to preserve it # in case of directory-only negation if self.negation and type(abs_path) == str and abs_path[-1] == '/': @@ -218,3 +223,14 @@ def _normalize_path(path: Union[str, Path]) -> Path: `Path.resolve()` does. """ return Path(abspath(path)) + + +def _count_trailing_whitespace(text: str) -> int: + """Count the number of trailing whitespace characters in a string.""" + count = 0 + for char in reversed(str(text)): + if char.isspace(): + count += 1 + else: + break + return count From e767745c04d8cbc5eca9067eb7ce0af07b79e1f9 Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Wed, 25 Oct 2023 08:16:19 -0700 Subject: [PATCH 2/4] Add python 3.12, fully resolve temporary directory --- .github/workflows/ci.yml | 2 +- tests.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 36152a5..a7bcd18 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - python-version: ['3.11', '3.10', '3.9', '3.8', '3.7'] + python-version: ['3.12', '3.11', '3.10', '3.9', '3.8', '3.7'] os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 diff --git a/tests.py b/tests.py index f337b64..4de350c 100644 --- a/tests.py +++ b/tests.py @@ -188,11 +188,13 @@ def test_symlink_to_another_directory(self): This test ensures that the issue is now fixed. """ with TemporaryDirectory() as project_dir, TemporaryDirectory() as another_dir: + project_dir = Path(project_dir).resolve() + another_dir = Path(another_dir).resolve() 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 = project_dir / 'link' + target = another_dir / 'target' link.symlink_to(target) # Check the intended behavior according to From 861f19f3c85686e1499225c0e2307b8b60450245 Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Wed, 25 Oct 2023 09:36:46 -0700 Subject: [PATCH 3/4] switch to posix, rather than str for performing search --- gitignore_parser.py | 17 +++++++++-------- tests.py | 10 +++++++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/gitignore_parser.py b/gitignore_parser.py index abae088..82bd016 100644 --- a/gitignore_parser.py +++ b/gitignore_parser.py @@ -126,13 +126,14 @@ def __repr__(self): def match(self, abs_path: Union[str, Path]): matched = False if self.base_path: - rel_path = str(_normalize_path(abs_path).relative_to(self.base_path)) + rel_path = _normalize_path(abs_path).relative_to(self.base_path).as_posix() else: - rel_path = str(_normalize_path(abs_path)) - # Path() strips the trailing whitespace on windows, so we need to - # preserve it. + rel_path = _normalize_path(abs_path).as_posix() + # Path() strips the trailing following symbols on windows, so we need to + # preserve it: ' ', '.' if sys.platform.startswith('win'): - rel_path += ' ' * _count_trailing_whitespace(abs_path) + rel_path += ' ' * _count_trailing_symbol(' ', abs_path) + rel_path += '.' * _count_trailing_symbol('.', abs_path) # Path() strips the trailing slash, so we need to preserve it # in case of directory-only negation if self.negation and type(abs_path) == str and abs_path[-1] == '/': @@ -225,11 +226,11 @@ def _normalize_path(path: Union[str, Path]) -> Path: return Path(abspath(path)) -def _count_trailing_whitespace(text: str) -> int: - """Count the number of trailing whitespace characters in a string.""" +def _count_trailing_symbol(symbol: str, text: str) -> int: + """Count the number of trailing characters in a string.""" count = 0 for char in reversed(str(text)): - if char.isspace(): + if char == symbol: count += 1 else: break diff --git a/tests.py b/tests.py index 4de350c..6b44ee2 100644 --- a/tests.py +++ b/tests.py @@ -4,7 +4,7 @@ from gitignore_parser import parse_gitignore -from unittest import TestCase, main +from unittest import TestCase, main, SkipTest class Test(TestCase): @@ -195,8 +195,12 @@ def test_symlink_to_another_directory(self): # Create a symlink to another directory. link = project_dir / 'link' target = another_dir / 'target' - link.symlink_to(target) - + + try: + link.symlink_to(target) + except OSError: + e = "Current User does not have permissions to perform symlink/" + raise SkipTest(e) # 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 From c1fa2cb6b6365c208816ac2ca39aa7b258025428 Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Wed, 25 Oct 2023 11:48:24 -0700 Subject: [PATCH 4/4] update --- tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.py b/tests.py index 6b44ee2..93aa68a 100644 --- a/tests.py +++ b/tests.py @@ -199,7 +199,7 @@ def test_symlink_to_another_directory(self): try: link.symlink_to(target) except OSError: - e = "Current User does not have permissions to perform symlink/" + e = "Current user does not have permissions to perform symlink." raise SkipTest(e) # Check the intended behavior according to # https://git-scm.com/docs/gitignore#_notes: