diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py index 0288afa..4d076f8 100644 --- a/src/mkdocstrings_handlers/python/handler.py +++ b/src/mkdocstrings_handlers/python/handler.py @@ -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) @@ -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)) diff --git a/tests/test_handler.py b/tests/test_handler.py index 5a49a8b..280e479 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -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(): @@ -58,3 +58,28 @@ def test_render_docstring_examples_section(handler): assert "

This is an example.

" 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