Skip to content

Commit

Permalink
Merge pull request #17 from linw1995/bugfix/cant_exclude_directory
Browse files Browse the repository at this point in the history
Fix files finding error if the glob result of excludes containing directory.
  • Loading branch information
frostming authored Apr 26, 2021
2 parents 4f75219 + 891cb99 commit af56365
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions news/bugfix.17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix files finding error if the glob result of excludes containing directory.
11 changes: 6 additions & 5 deletions pdm/pep517/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class BuildError(RuntimeError):
pass


def _match_path(path: str, pattern: str) -> bool:
return normalize_path(os.path.abspath(path)) == normalize_path(
os.path.abspath(pattern)
def is_same_or_descendant_path(target: str, path: str) -> bool:
"""Check target is same or descendant with path"""
return normalize_path(os.path.abspath(target)).startswith(
normalize_path(os.path.abspath(path))
)


Expand Down Expand Up @@ -167,7 +168,7 @@ def _find_files_iter(self, for_sdist: bool = False) -> Iterator[str]:

includes, excludes = _merge_globs(include_globs, excludes_globs)
for path in find_froms:
if any(_match_path(path, item) for item in dont_find_froms):
if any(is_same_or_descendant_path(path, item) for item in dont_find_froms):
continue
path_base = os.path.dirname(path)
if not path_base or path_base == ".":
Expand All @@ -178,7 +179,7 @@ def _find_files_iter(self, for_sdist: bool = False) -> Iterator[str]:

for filename in filenames:
if filename.endswith(".pyc") or any(
_match_path(os.path.join(root, filename), item)
is_same_or_descendant_path(os.path.join(root, filename), item)
for item in excludes
):
continue
Expand Down
18 changes: 17 additions & 1 deletion tests/test_file_finder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

import pytest

from pdm.pep517 import utils
from pdm.pep517.base import Builder
from pdm.pep517.base import Builder, is_same_or_descendant_path
from tests import FIXTURES


Expand All @@ -22,3 +24,17 @@ def test_auto_include_tests_for_sdist():
path = Path(file)
assert path in sdist_files
assert path not in wheel_files


@pytest.mark.parametrize(
"target,path,expect",
[
("a/b", "a", True),
("a/b/c", "a/b/c", True),
("b/c", "a", False),
("a", "a/b", False),
("a", "b/c", False),
],
)
def test_is_same_or_descendant_path(target, path, expect):
assert is_same_or_descendant_path(target, path) == expect

0 comments on commit af56365

Please sign in to comment.