Skip to content

Commit 28f6555

Browse files
authored
100% code coverage (#247)
1 parent 1ffebb2 commit 28f6555

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

src/mkdocs_include_markdown_plugin/cache.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_(self, url: str, encoding: str = 'utf-8') -> str | None: # noqa: D102
4343
is_file = stat.S_ISREG(os.stat(fpath).st_mode)
4444
except (FileNotFoundError, OSError): # pragma: no cover
4545
return None
46-
if is_file:
46+
if is_file: # pragma: no branch
4747
creation_time = self.get_creation_time_from_fpath(fpath)
4848
if time.time() < creation_time + self.expiration_seconds:
4949
return self.read_file(fpath, encoding=encoding)
@@ -79,7 +79,7 @@ def get_cache_directory(cache_dir: str) -> str | None:
7979

8080
try:
8181
from platformdirs import user_data_dir
82-
except ImportError:
82+
except ImportError: # pragma: no cover
8383
return None
8484
else:
8585
return user_data_dir('mkdocs-include-markdown-plugin')

src/mkdocs_include_markdown_plugin/directive.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ def resolve_file_paths_to_exclude(
306306
docs_dir: str,
307307
) -> list[str]:
308308
"""Resolve the file paths to exclude for a directive."""
309-
root_dir = None
310309
if process.is_absolute_path(exclude_string):
311310
return glob.glob(exclude_string, flags=GLOB_FLAGS)
312311

@@ -330,7 +329,7 @@ def resolve_file_paths_to_exclude(
330329
)
331330
]
332331

333-
return glob.glob(
332+
return glob.glob( # pragma: no cover
334333
exclude_string,
335334
flags=GLOB_FLAGS,
336335
root_dir=docs_dir,

src/mkdocs_include_markdown_plugin/event.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,20 @@ def get_file_content( # noqa: PLR0913, PLR0915
8383
http_cache: Cache | None = None,
8484
) -> str:
8585
"""Return the content of the file to include."""
86-
settings_ignore_paths = []
87-
if settings.exclude is not None:
88-
for path in glob.glob(
89-
[
90-
os.path.join(docs_dir, fp)
91-
if not os.path.isabs(fp)
92-
else fp for fp in settings.exclude
93-
],
94-
flags=GLOB_FLAGS,
95-
root_dir=docs_dir,
96-
):
97-
if path not in settings_ignore_paths:
98-
settings_ignore_paths.append(path)
86+
if settings.exclude:
87+
settings_ignore_paths = list(glob.glob(
88+
[
89+
os.path.join(docs_dir, fp)
90+
if not os.path.isabs(fp)
91+
else fp for fp in settings.exclude
92+
],
93+
flags=GLOB_FLAGS,
94+
root_dir=docs_dir,
95+
))
9996
if page_src_path in settings_ignore_paths:
10097
return markdown
98+
else:
99+
settings_ignore_paths = []
101100

102101
new_found_include_contents: list[tuple[str, str]] = []
103102
new_found_include_markdown_contents: list[tuple[str, str]] = []
@@ -150,8 +149,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
150149
for path in resolve_file_paths_to_exclude(
151150
exclude_string, page_src_path, docs_dir,
152151
):
153-
if path not in ignore_paths:
154-
ignore_paths.append(path)
152+
ignore_paths.append(path)
155153

156154
file_paths_to_include, is_url = resolve_file_paths_to_include(
157155
filename,
@@ -361,8 +359,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
361359
for path in resolve_file_paths_to_exclude(
362360
exclude_string, page_src_path, docs_dir,
363361
):
364-
if path not in ignore_paths:
365-
ignore_paths.append(path)
362+
ignore_paths.append(path)
366363

367364
file_paths_to_include, is_url = resolve_file_paths_to_include(
368365
filename,

tests/test_unit/test_glob_include.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99

1010

1111
@unix_only
12-
def test_glob_include_absolute(page, tmp_path, plugin):
12+
@parametrize_directives
13+
def test_glob_include_absolute(directive, page, tmp_path, plugin):
1314
includer_file = tmp_path / 'includer.txt'
1415
included_01_file = tmp_path / 'included_01.txt'
1516
included_02_file = tmp_path / 'included_02.txt'
1617

1718
includer_file_content = f'''foo
1819
1920
{{%
20-
include "./included*.txt"
21+
{directive} "./included*.txt"
2122
%}}
2223
2324
<!-- with absolute path -->
2425
{{%
25-
include "{tmp_path}{os.sep}included*.txt"
26+
{directive} "{tmp_path}{os.sep}included*.txt"
2627
%}}
2728
'''
2829

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

4849

50+
@unix_only
51+
@parametrize_directives
52+
def test_glob_include_fallback(directive, page, tmp_path, plugin):
53+
includer_file = tmp_path / 'includer.txt'
54+
includes_dir = tmp_path / 'includes'
55+
includes_dir.mkdir()
56+
included_01_file = includes_dir / 'included_01.txt'
57+
included_02_file = includes_dir / 'included_02.txt'
58+
59+
includer_file_content = f'''foo
60+
61+
{{%
62+
{directive} "includes/*.txt"
63+
%}}
64+
'''
65+
66+
included_01_content = 'bar'
67+
included_02_content = 'baz'
68+
69+
includer_file.write_text(includer_file_content)
70+
included_01_file.write_text(included_01_content)
71+
included_02_file.write_text(included_02_content)
72+
73+
expected_result = '''foo
74+
75+
barbaz
76+
'''
77+
78+
assert on_page_markdown(
79+
includer_file_content, page(includer_file), tmp_path, plugin,
80+
) == expected_result
81+
82+
4983
@parametrize_directives
5084
@pytest.mark.parametrize(
5185
(

0 commit comments

Comments
 (0)