Skip to content

Commit

Permalink
fix: Expand globs relative to configuration file path
Browse files Browse the repository at this point in the history
Co-authored-by: Timothée Mazzucotelli <pawamoy@pm.me>
Issue #42: #42
PR #43: #43
  • Loading branch information
veghdev committed Nov 19, 2022
1 parent c4d32e1 commit 0dc45ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/mkdocstrings_handlers/python/handler.py
Expand Up @@ -25,6 +25,22 @@

from mkdocstrings_handlers.python import rendering

if sys.version_info >= (3, 11):
from contextlib import chdir
else:
# TODO: remove once support for Python 3.10 is dropped
from contextlib import contextmanager

@contextmanager # noqa: WPS440
def chdir(path: str): # noqa: D103,WPS440
old_wd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_wd)


logger = get_logger(__name__)

patch_loggers(get_logger)
Expand Down Expand Up @@ -129,7 +145,8 @@ def __init__(
super().__init__(*args, **kwargs)
self._config_file_path = config_file_path
paths = paths or []
resolved_globs = [glob.glob(path) for path in paths]
with chdir(os.path.dirname(config_file_path) if config_file_path else "."):
resolved_globs = [glob.glob(path) for path in paths]
paths = [path for glob_list in resolved_globs for path in glob_list]
if not paths and config_file_path:
paths.append(os.path.dirname(config_file_path))
Expand Down
27 changes: 26 additions & 1 deletion tests/test_handler.py
Expand Up @@ -3,7 +3,7 @@
import pytest
from griffe.docstrings.dataclasses import DocstringSectionExamples, DocstringSectionKind

from mkdocstrings_handlers.python.handler import CollectionError, get_handler
from mkdocstrings_handlers.python.handler import CollectionError, PythonHandler, get_handler


def test_collect_missing_module():
Expand Down Expand Up @@ -58,3 +58,28 @@ def test_render_docstring_examples_section(handler):
assert "<p>This is an example.</p>" in rendered
assert "print" in rendered
assert "Hello" in rendered


def test_expand_globs(tmp_path):
"""Assert globs are correctly expanded.
Parameters:
tmp_path: Pytext fixture that creates a temporary directory.
"""
globbed_names = (
"expanded_a",
"expanded_b",
"other_expanded_c",
"other_expanded_d",
)
globbed_paths = [tmp_path.joinpath(globbed_name) for globbed_name in globbed_names]
for path in globbed_paths:
path.touch()
handler = PythonHandler(
handler="python",
theme="material",
config_file_path=tmp_path / "mkdocs.yml",
paths=["*exp*"],
)
for path in globbed_paths: # noqa: WPS440
assert str(path) in handler._paths # noqa: WPS437

0 comments on commit 0dc45ae

Please sign in to comment.