Skip to content

Commit b1eef73

Browse files
authored
Improve coverage (#181)
1 parent 065c890 commit b1eef73

File tree

11 files changed

+55
-26
lines changed

11 files changed

+55
-26
lines changed
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
{% include-markdown "**" exclude="./{index,license}.md" %}
2+
{% include "**" exclude="./{index,license}.md" %}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "6.0.2"
3+
version = "6.0.3"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
"""Mkdocs Markdown plugin to include files."""
2+
from __future__ import annotations
3+
4+
5+
__all__ = ['IncludeMarkdownPlugin']
6+
7+
from mkdocs_include_markdown_plugin.plugin import IncludeMarkdownPlugin

src/mkdocs_include_markdown_plugin/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
try:
1212
from platformdirs import user_data_dir
13-
except ImportError:
13+
except ImportError: # pragma: no cover
1414
CACHE_AVAILABLE = False
1515
else:
1616
CACHE_AVAILABLE = True

src/mkdocs_include_markdown_plugin/directive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def resolve_file_paths_to_include(
184184
return [filename_or_url], True
185185

186186
if process.is_absolute_path(filename_or_url):
187-
if os.name == 'nt':
187+
if os.name == 'nt': # pragma: nt cover
188188
# Windows
189189
fpath = os.path.normpath(filename_or_url)
190190
if not os.path.isfile(fpath):

src/mkdocs_include_markdown_plugin/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from mkdocs_include_markdown_plugin.files_watcher import FilesWatcher
2828

2929

30-
if TYPE_CHECKING: # remove this for mypyc compiling
30+
if TYPE_CHECKING:
3131
from typing import TypedDict
3232

3333
from mkdocs.structure.pages import Page

src/mkdocs_include_markdown_plugin/plugin.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,23 @@ def _include_markdown_tag(self) -> re.Pattern[str]:
6565
def _files_watcher(self) -> FilesWatcher:
6666
return FilesWatcher()
6767

68-
def _watch_included_files(self) -> None: # pragma: no cover
69-
assert self._server is not None
68+
def _update_watched_files(self) -> None: # pragma: no cover
69+
"""Function executed on server reload.
70+
71+
At this execution point, the ``self._server`` attribute must be set.
72+
"""
73+
watcher, server = self._files_watcher, self._server
7074

7175
# unwatch previous watched files not needed anymore
72-
for filepath in self._files_watcher.prev_included_files:
73-
if filepath not in self._files_watcher.included_files:
74-
self._server.unwatch(filepath)
75-
self._files_watcher.prev_included_files = (
76-
self._files_watcher.included_files[:]
77-
)
76+
for file_path in watcher.prev_included_files:
77+
if file_path not in watcher.included_files:
78+
server.unwatch(file_path) # type: ignore
79+
watcher.prev_included_files = watcher.included_files[:]
7880

7981
# watch new included files
80-
for filepath in self._files_watcher.included_files:
81-
self._server.watch(filepath, recursive=False)
82-
self._files_watcher.included_files = []
82+
for file_path in watcher.included_files:
83+
server.watch(file_path, recursive=False) # type: ignore
84+
watcher.included_files = []
8385

8486
def on_page_content(
8587
self,
@@ -89,7 +91,7 @@ def on_page_content(
8991
files: Files, # noqa: ARG002
9092
) -> str:
9193
if self._server is not None: # pragma: no cover
92-
self._watch_included_files()
94+
self._update_watched_files()
9395
return html
9496

9597
def on_serve(
@@ -100,7 +102,7 @@ def on_serve(
100102
) -> None:
101103
if self._server is None: # pragma: no cover
102104
self._server = server
103-
self._watch_included_files()
105+
self._update_watched_files()
104106

105107
@event_priority(100)
106108
def on_page_markdown(

tests/conftest.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
import sys
33

44
import pytest
5-
from mkdocs_include_markdown_plugin.plugin import IncludeMarkdownPlugin
65

76

87
TESTS_DIR = os.path.abspath(os.path.dirname(__file__))
9-
if TESTS_DIR not in sys.path:
10-
sys.path.append(TESTS_DIR)
8+
SRC_DIR = os.path.abspath(os.path.join(TESTS_DIR, '..', 'src'))
9+
for d in (SRC_DIR, TESTS_DIR):
10+
if d not in sys.path:
11+
sys.path.insert(0, d)
12+
13+
from mkdocs_include_markdown_plugin import IncludeMarkdownPlugin # noqa: E402
1114

1215

1316
@pytest.fixture
1417
def page():
1518
"""Fake mkdocs page object."""
16-
def _page(filepath):
19+
def _page(file_path):
1720
return type(
1821
'FakeMkdocsPage', (), {
1922
'file': type(
2023
'FakeMdocsPageFile', (), {
21-
'abs_src_path': filepath,
24+
'abs_src_path': file_path,
2225
},
2326
),
2427
},

tests/test_integration/test_cache_integration.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from __future__ import annotations
2-
31
import os
2+
from dataclasses import dataclass
43

4+
import mkdocs_include_markdown_plugin.cache
55
import pytest
6+
from mkdocs.exceptions import PluginError
7+
from mkdocs_include_markdown_plugin import IncludeMarkdownPlugin
68
from mkdocs_include_markdown_plugin.cache import (
79
CACHE_AVAILABLE,
810
Cache,
@@ -76,3 +78,20 @@ def run():
7678
assert os.path.isfile(file_path)
7779

7880
os.remove(file_path)
81+
82+
83+
def test_cache_setting_when_not_available_raises_error(monkeypatch):
84+
@dataclass
85+
class FakeConfig:
86+
cache: int
87+
88+
monkeypatch.setattr(
89+
mkdocs_include_markdown_plugin.cache,
90+
'CACHE_AVAILABLE',
91+
False,
92+
)
93+
plugin = IncludeMarkdownPlugin()
94+
plugin.config = FakeConfig(cache=600)
95+
with pytest.raises(PluginError) as exc:
96+
plugin.on_config({})
97+
assert 'The "platformdirs" package is required' in str(exc.value)

0 commit comments

Comments
 (0)