From 13ccc93a9ce8aa3fbe2b4a8de4aa6672db320973 Mon Sep 17 00:00:00 2001 From: Florian Hofer Date: Sat, 19 Nov 2022 22:31:25 +0100 Subject: [PATCH 1/2] fix: ensure dirname(config_file_path) is not empty --- src/mkdocstrings_handlers/python/handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py index 4d076f8b..53938b13 100644 --- a/src/mkdocstrings_handlers/python/handler.py +++ b/src/mkdocstrings_handlers/python/handler.py @@ -145,7 +145,7 @@ def __init__( super().__init__(*args, **kwargs) self._config_file_path = config_file_path paths = paths or [] - with chdir(os.path.dirname(config_file_path) if config_file_path else "."): + with chdir((os.path.dirname(config_file_path) or ".") 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: From db45f9ed407b60e1f70f10e199efc4f113d06ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sat, 19 Nov 2022 22:59:41 +0100 Subject: [PATCH 2/2] fixup! fix: ensure dirname(config_file_path) is not empty --- src/mkdocstrings_handlers/python/handler.py | 3 ++- tests/test_handler.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py index 53938b13..afb8ed98 100644 --- a/src/mkdocstrings_handlers/python/handler.py +++ b/src/mkdocstrings_handlers/python/handler.py @@ -145,7 +145,8 @@ def __init__( super().__init__(*args, **kwargs) self._config_file_path = config_file_path paths = paths or [] - with chdir((os.path.dirname(config_file_path) or ".") if config_file_path else "."): + glob_base_dir = os.path.dirname(os.path.abspath(config_file_path)) if config_file_path else "." + with chdir(glob_base_dir): 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: diff --git a/tests/test_handler.py b/tests/test_handler.py index 280e4791..93148d5f 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -1,5 +1,8 @@ """Tests for the `handler` module.""" +import os +from glob import glob + import pytest from griffe.docstrings.dataclasses import DocstringSectionExamples, DocstringSectionKind @@ -83,3 +86,15 @@ def test_expand_globs(tmp_path): ) for path in globbed_paths: # noqa: WPS440 assert str(path) in handler._paths # noqa: WPS437 + + +def test_expand_globs_without_changing_directory(): + """Assert globs are correctly expanded when we are already in the right directory.""" + handler = PythonHandler( + handler="python", + theme="material", + config_file_path="mkdocs.yml", + paths=["*.md"], + ) + for path in list(glob(os.path.abspath(".") + "/*.md")): + assert path in handler._paths # noqa: WPS437