Skip to content

Commit

Permalink
fix: handle directory patterns (#392)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Whitlock <[email protected]>
  • Loading branch information
jshwi committed Jul 26, 2024
1 parent e5ef0f4 commit 2be811b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 41 deletions.
1 change: 1 addition & 0 deletions changelog/392.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
handle directory patterns
28 changes: 14 additions & 14 deletions docsig/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@

_DEFAULT_EXCLUDES = """\
(?x)^(
|\\.?venv
|\\.git
|\\.hg
|\\.idea
|\\.mypy_cache
|\\.nox
|\\.pytest_cache
|\\.svn
|\\.tox
|\\.vscode
|_?build
|__pycache__
|dist
|node_modules
|\\.?venv[\\\\/].*
|\\.git[\\\\/].*
|\\.hg[\\\\/].*
|\\.idea[\\\\/].*
|\\.mypy_cache[\\\\/].*
|\\.nox[\\\\/].*
|\\.pytest_cache[\\\\/].*
|\\.svn[\\\\/].*
|\\.tox[\\\\/].*
|\\.vscode[\\\\/].*
|_?build[\\\\/].*
|__pycache__[\\\\/].*
|dist[\\\\/].*
|node_modules[\\\\/].*
)$
"""

Expand Down
21 changes: 12 additions & 9 deletions docsig/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,24 @@ def __init__( # pylint: disable=too-many-arguments
for path in paths:
self._populate(path)

for path in list(self):
if str(path) != "." and any(
_re.match(i, str(path)) for i in self._excludes
):
_vprint(
FILE_INFO.format(
path=path, msg="in exclude list, skipping"
),
self._verbose,
)
self.remove(path)

self.sort()

def _populate(self, root: _Path) -> None:
if not root.exists():
raise FileNotFoundError(root)

if str(root) != "." and any(
_re.match(i, root.name) for i in self._excludes
):
_vprint(
FILE_INFO.format(path=root, msg="in exclude list, skipping"),
self._verbose,
)
return

if not self._include_ignored and self._gitignore.match_file(root):
_vprint(
FILE_INFO.format(path=root, msg="in gitignore, skipping"),
Expand Down
20 changes: 3 additions & 17 deletions tests/exclude_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,22 +1460,18 @@ def test_exclude_defaults(
f"{Path('CODE_OF_CONDUCT.md')}: Parsing Python code failed",
f"{Path('.pylintrc')}: Parsing Python code failed",
f"{Path('LICENSE')}: Parsing Python code failed",
f"{Path('.pytest_cache')}: in exclude list, skipping",
f"{Path('CHANGELOG.md')}: Parsing Python code failed",
f"{Path('dist')}: in exclude list, skipping",
f"{Path('.pre-commit-config.yaml')}: Parsing Python code failed",
f"{Path('.coverage')}: Parsing Python code failed",
f"{Path('Makefile')}: Parsing Python code failed",
f"{Path('whitelist.py')}: Parsing Python code successful",
f"{Path('.pre-commit-hooks.yaml')}: Parsing Python code failed",
f"{Path('pyproject.toml')}: Parsing Python code successful",
f"{Path('.bumpversion.cfg')}: Parsing Python code failed",
f"{Path('node_modules')}: in exclude list, skipping",
f"{Path('tests/misc_test.py')}: Parsing Python code successful",
f"{Path('tests/conftest.py')}: Parsing Python code successful",
f"{Path('tests/disable_test.py')}: Parsing Python code successful",
f"{Path('tests/__init__.py')}: Parsing Python code successful",
f"{Path('tests/__pycache__')}: in exclude list, skipping",
f"{Path('tests/TESTS.md')}: Parsing Python code failed",
f"{Path('tests/git_test.py')}: Parsing Python code successful",
f"{Path('tests/_test.py')}: Parsing Python code successful",
Expand All @@ -1498,7 +1494,6 @@ def test_exclude_defaults(
f"{Path('docsig/__init__.py')}: Parsing Python code successful",
f"{Path('docsig/_display.py')}: Parsing Python code successful",
f"{Path('docsig/_hooks.py')}: Parsing Python code successful",
f"{Path('docsig/__pycache__')}: in exclude list, skipping",
f"{Path('docsig/_message.py')}: Parsing Python code successful",
f"{Path('docsig/_core.py')}: Parsing Python code successful",
f"{Path('docsig/_decorators.py')}: Parsing Python code successful",
Expand All @@ -1508,7 +1503,6 @@ def test_exclude_defaults(
f"{Path('docsig/__main__.py')}: Parsing Python code successful",
f"{Path('docsig/_utils.py')}: Parsing Python code successful",
f"{Path('docsig/_directives.py')}: Parsing Python code successful",
f"{Path('.mypy_cache')}: in exclude list, skipping",
f"{Path('.gitignore')}: Parsing Python code failed",
f"{Path('package-lock.json')}: Parsing Python code failed",
f"{Path('package.json')}: Parsing Python code failed",
Expand All @@ -1520,8 +1514,6 @@ def test_exclude_defaults(
f"{Path('coverage.xml')}: Parsing Python code failed",
f"{Path('poetry.lock')}: Parsing Python code failed",
f"{Path('README.rst')}: Parsing Python code failed",
f"{Path('.git')}: in exclude list, skipping",
f"{Path('.idea')}: in exclude list, skipping",
]
assert all(i in std.out for i in expected)

Expand Down Expand Up @@ -1555,7 +1547,9 @@ def test_exclude_argument(
new-ssl "${@}"
"""
init_file(template)
assert main(".", long.exclude, "file.py", test_flake8=False) == 0
assert (
main(".", long.exclude, r"module[\\/]file.py", test_flake8=False) == 0
)


def test_gitignore(
Expand Down Expand Up @@ -1738,22 +1732,18 @@ def test_exclude_defaults_and_gitignore(
f"{Path('CODE_OF_CONDUCT.md')}: Parsing Python code failed",
f"{Path('.pylintrc')}: Parsing Python code failed",
f"{Path('LICENSE')}: Parsing Python code failed",
f"{Path('.pytest_cache')}: in exclude list, skipping",
f"{Path('CHANGELOG.md')}: Parsing Python code failed",
f"{Path('dist')}: in exclude list, skipping",
f"{Path('.pre-commit-config.yaml')}: Parsing Python code failed",
f"{Path('.coverage')}: in gitignore, skipping",
f"{Path('Makefile')}: Parsing Python code failed",
f"{Path('whitelist.py')}: Parsing Python code successful",
f"{Path('.pre-commit-hooks.yaml')}: Parsing Python code failed",
f"{Path('pyproject.toml')}: Parsing Python code successful",
f"{Path('.bumpversion.cfg')}: Parsing Python code failed",
f"{Path('node_modules')}: in exclude list, skipping",
f"{Path('tests/misc_test.py')}: Parsing Python code successful",
f"{Path('tests/conftest.py')}: Parsing Python code successful",
f"{Path('tests/disable_test.py')}: Parsing Python code successful",
f"{Path('tests/__init__.py')}: Parsing Python code successful",
f"{Path('tests/__pycache__')}: in exclude list, skipping",
f"{Path('tests/TESTS.md')}: Parsing Python code failed",
f"{Path('tests/git_test.py')}: Parsing Python code successful",
f"{Path('tests/_test.py')}: Parsing Python code successful",
Expand All @@ -1776,7 +1766,6 @@ def test_exclude_defaults_and_gitignore(
f"{Path('docsig/__init__.py')}: Parsing Python code successful",
f"{Path('docsig/_display.py')}: Parsing Python code successful",
f"{Path('docsig/_hooks.py')}: Parsing Python code successful",
f"{Path('docsig/__pycache__')}: in exclude list, skipping",
f"{Path('docsig/_message.py')}: Parsing Python code successful",
f"{Path('docsig/_core.py')}: Parsing Python code successful",
f"{Path('docsig/_decorators.py')}: Parsing Python code successful",
Expand All @@ -1786,7 +1775,6 @@ def test_exclude_defaults_and_gitignore(
f"{Path('docsig/__main__.py')}: Parsing Python code successful",
f"{Path('docsig/_utils.py')}: Parsing Python code successful",
f"{Path('docsig/_directives.py')}: Parsing Python code successful",
f"{Path('.mypy_cache')}: in exclude list, skipping",
f"{Path('.gitignore')}: Parsing Python code failed",
f"{Path('package-lock.json')}: Parsing Python code failed",
f"{Path('package.json')}: Parsing Python code failed",
Expand All @@ -1798,8 +1786,6 @@ def test_exclude_defaults_and_gitignore(
f"{Path('coverage.xml')}: in gitignore, skipping",
f"{Path('poetry.lock')}: Parsing Python code failed",
f"{Path('README.rst')}: Parsing Python code failed",
f"{Path('.git')}: in exclude list, skipping",
f"{Path('.idea')}: in exclude list, skipping",
]
assert all(i in std.out for i in expected)

Expand Down
39 changes: 38 additions & 1 deletion tests/fix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
==================
"""

# pylint: disable=protected-access

import pickle
from pathlib import Path

import pytest

from . import InitFileFixtureType, MockMainType
import docsig

from . import FixtureMakeTree, InitFileFixtureType, MockMainType


def test_fix_optional_return_statements_with_overload_func_sig502(
Expand Down Expand Up @@ -74,3 +78,36 @@ def test_no_fail_on_unicode_decode_error_384(
pickle.dump(serialize, fout)

assert main(pkl, test_flake8=False) == 0


def test_exclude_dirs(
monkeypatch: pytest.MonkeyPatch,
main: MockMainType,
make_tree: FixtureMakeTree,
) -> None:
"""Test dir regexes are correctly excluded.
:param monkeypatch: Mock patch environment and attributes.
:param main: Patch package entry point.
:param make_tree: Create directory tree from dict mapping.
"""
pyproject_toml = Path.cwd() / "pyproject.toml"
pyproject_toml.write_text(
r"""
[tool.docsig]
exclude = '''.*src\/design\/.*'''
""",
encoding="utf-8",
)
path_obj = docsig._core._Paths # define to avoid recursion
paths_list = []

def _paths(*args, **kwargs) -> docsig._core._Paths:
paths = path_obj(*args, **kwargs)
paths_list.append(paths)
return paths

monkeypatch.setattr("docsig._core._Paths", _paths)
make_tree(Path.cwd(), {"src": {"design": {"file.py": []}}})
main(".", test_flake8=False)
assert "src/design/file.py" not in [str(s) for s in paths_list[0]]

0 comments on commit 2be811b

Please sign in to comment.