Skip to content

Commit

Permalink
100% code coverage (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Dec 17, 2024
1 parent 1ffebb2 commit 28f6555
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/mkdocs_include_markdown_plugin/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_(self, url: str, encoding: str = 'utf-8') -> str | None: # noqa: D102
is_file = stat.S_ISREG(os.stat(fpath).st_mode)
except (FileNotFoundError, OSError): # pragma: no cover
return None
if is_file:
if is_file: # pragma: no branch
creation_time = self.get_creation_time_from_fpath(fpath)
if time.time() < creation_time + self.expiration_seconds:
return self.read_file(fpath, encoding=encoding)
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_cache_directory(cache_dir: str) -> str | None:

try:
from platformdirs import user_data_dir
except ImportError:
except ImportError: # pragma: no cover
return None
else:
return user_data_dir('mkdocs-include-markdown-plugin')
Expand Down
3 changes: 1 addition & 2 deletions src/mkdocs_include_markdown_plugin/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ def resolve_file_paths_to_exclude(
docs_dir: str,
) -> list[str]:
"""Resolve the file paths to exclude for a directive."""
root_dir = None
if process.is_absolute_path(exclude_string):
return glob.glob(exclude_string, flags=GLOB_FLAGS)

Expand All @@ -330,7 +329,7 @@ def resolve_file_paths_to_exclude(
)
]

return glob.glob(
return glob.glob( # pragma: no cover
exclude_string,
flags=GLOB_FLAGS,
root_dir=docs_dir,
Expand Down
31 changes: 14 additions & 17 deletions src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,20 @@ def get_file_content( # noqa: PLR0913, PLR0915
http_cache: Cache | None = None,
) -> str:
"""Return the content of the file to include."""
settings_ignore_paths = []
if settings.exclude is not None:
for path in glob.glob(
[
os.path.join(docs_dir, fp)
if not os.path.isabs(fp)
else fp for fp in settings.exclude
],
flags=GLOB_FLAGS,
root_dir=docs_dir,
):
if path not in settings_ignore_paths:
settings_ignore_paths.append(path)
if settings.exclude:
settings_ignore_paths = list(glob.glob(
[
os.path.join(docs_dir, fp)
if not os.path.isabs(fp)
else fp for fp in settings.exclude
],
flags=GLOB_FLAGS,
root_dir=docs_dir,
))
if page_src_path in settings_ignore_paths:
return markdown
else:
settings_ignore_paths = []

new_found_include_contents: list[tuple[str, str]] = []
new_found_include_markdown_contents: list[tuple[str, str]] = []
Expand Down Expand Up @@ -150,8 +149,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
for path in resolve_file_paths_to_exclude(
exclude_string, page_src_path, docs_dir,
):
if path not in ignore_paths:
ignore_paths.append(path)
ignore_paths.append(path)

file_paths_to_include, is_url = resolve_file_paths_to_include(
filename,
Expand Down Expand Up @@ -361,8 +359,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
for path in resolve_file_paths_to_exclude(
exclude_string, page_src_path, docs_dir,
):
if path not in ignore_paths:
ignore_paths.append(path)
ignore_paths.append(path)

file_paths_to_include, is_url = resolve_file_paths_to_include(
filename,
Expand Down
40 changes: 37 additions & 3 deletions tests/test_unit/test_glob_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@


@unix_only
def test_glob_include_absolute(page, tmp_path, plugin):
@parametrize_directives
def test_glob_include_absolute(directive, page, tmp_path, plugin):
includer_file = tmp_path / 'includer.txt'
included_01_file = tmp_path / 'included_01.txt'
included_02_file = tmp_path / 'included_02.txt'

includer_file_content = f'''foo
{{%
include "./included*.txt"
{directive} "./included*.txt"
%}}
<!-- with absolute path -->
{{%
include "{tmp_path}{os.sep}included*.txt"
{directive} "{tmp_path}{os.sep}included*.txt"
%}}
'''

Expand All @@ -46,6 +47,39 @@ def test_glob_include_absolute(page, tmp_path, plugin):
) == expected_result


@unix_only
@parametrize_directives
def test_glob_include_fallback(directive, page, tmp_path, plugin):
includer_file = tmp_path / 'includer.txt'
includes_dir = tmp_path / 'includes'
includes_dir.mkdir()
included_01_file = includes_dir / 'included_01.txt'
included_02_file = includes_dir / 'included_02.txt'

includer_file_content = f'''foo
{{%
{directive} "includes/*.txt"
%}}
'''

included_01_content = 'bar'
included_02_content = 'baz'

includer_file.write_text(includer_file_content)
included_01_file.write_text(included_01_content)
included_02_file.write_text(included_02_content)

expected_result = '''foo
barbaz
'''

assert on_page_markdown(
includer_file_content, page(includer_file), tmp_path, plugin,
) == expected_result


@parametrize_directives
@pytest.mark.parametrize(
(
Expand Down

0 comments on commit 28f6555

Please sign in to comment.