Repository: mondeja/mkdocs-include-markdown-plugin
Pull request #181 (2023-10-14 19:27:37)

Improve coverage


Patch:
diff --git a/examples/global-exclude/docs/include.md b/examples/global-exclude/docs/include.txt
similarity index 100%
rename from examples/global-exclude/docs/include.md
rename to examples/global-exclude/docs/include.txt
diff --git a/examples/global-exclude/docs/index.md b/examples/global-exclude/docs/index.md
index 57fd8a1..7cfbebe 100644
--- a/examples/global-exclude/docs/index.md
+++ b/examples/global-exclude/docs/index.md
@@ -1,1 +1,2 @@
 {% include-markdown "**" exclude="./{index,license}.md" %}
+{% include "**" exclude="./{index,license}.md" %}
diff --git a/pyproject.toml b/pyproject.toml
index 1a02bb8..363a9de 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [project]
 name = "mkdocs-include-markdown-plugin"
-version = "6.0.2"
+version = "6.0.3"
 description = "Mkdocs Markdown includer plugin."
 readme = "README.md"
 license = "Apache-2.0"
diff --git a/src/mkdocs_include_markdown_plugin/__init__.py b/src/mkdocs_include_markdown_plugin/__init__.py
index e287ee9..64a9cdc 100644
--- a/src/mkdocs_include_markdown_plugin/__init__.py
+++ b/src/mkdocs_include_markdown_plugin/__init__.py
@@ -1,1 +1,7 @@
 """Mkdocs Markdown plugin to include files."""
+from __future__ import annotations
+
+
+__all__ = ['IncludeMarkdownPlugin']
+
+from mkdocs_include_markdown_plugin.plugin import IncludeMarkdownPlugin
diff --git a/src/mkdocs_include_markdown_plugin/cache.py b/src/mkdocs_include_markdown_plugin/cache.py
index 5a9b199..bf61827 100644
--- a/src/mkdocs_include_markdown_plugin/cache.py
+++ b/src/mkdocs_include_markdown_plugin/cache.py
@@ -10,7 +10,7 @@ import time
 
 try:
     from platformdirs import user_data_dir
-except ImportError:
+except ImportError:  # pragma: no cover
     CACHE_AVAILABLE = False
 else:
     CACHE_AVAILABLE = True
diff --git a/src/mkdocs_include_markdown_plugin/directive.py b/src/mkdocs_include_markdown_plugin/directive.py
index c1efaba..41145df 100644
--- a/src/mkdocs_include_markdown_plugin/directive.py
+++ b/src/mkdocs_include_markdown_plugin/directive.py
@@ -184,7 +184,7 @@ def resolve_file_paths_to_include(
         return [filename_or_url], True
 
     if process.is_absolute_path(filename_or_url):
-        if os.name == 'nt':
+        if os.name == 'nt':  # pragma: nt cover
             # Windows
             fpath = os.path.normpath(filename_or_url)
             if not os.path.isfile(fpath):
diff --git a/src/mkdocs_include_markdown_plugin/event.py b/src/mkdocs_include_markdown_plugin/event.py
index 0867642..5ad7b21 100644
--- a/src/mkdocs_include_markdown_plugin/event.py
+++ b/src/mkdocs_include_markdown_plugin/event.py
@@ -27,7 +27,7 @@ from mkdocs_include_markdown_plugin.directive import (
 from mkdocs_include_markdown_plugin.files_watcher import FilesWatcher
 
 
-if TYPE_CHECKING:  # remove this for mypyc compiling
+if TYPE_CHECKING:
     from typing import TypedDict
 
     from mkdocs.structure.pages import Page
diff --git a/src/mkdocs_include_markdown_plugin/plugin.py b/src/mkdocs_include_markdown_plugin/plugin.py
index 65eabe0..baef0da 100644
--- a/src/mkdocs_include_markdown_plugin/plugin.py
+++ b/src/mkdocs_include_markdown_plugin/plugin.py
@@ -65,21 +65,23 @@ class IncludeMarkdownPlugin(BasePlugin[PluginConfig]):
     def _files_watcher(self) -> FilesWatcher:
         return FilesWatcher()
 
-    def _watch_included_files(self) -> None:  # pragma: no cover
-        assert self._server is not None
+    def _update_watched_files(self) -> None:  # pragma: no cover
+        """Function executed on server reload.
+
+        At this execution point, the ``self._server`` attribute must be set.
+        """
+        watcher, server = self._files_watcher, self._server
 
         # unwatch previous watched files not needed anymore
-        for filepath in self._files_watcher.prev_included_files:
-            if filepath not in self._files_watcher.included_files:
-                self._server.unwatch(filepath)
-        self._files_watcher.prev_included_files = (
-            self._files_watcher.included_files[:]
-        )
+        for file_path in watcher.prev_included_files:
+            if file_path not in watcher.included_files:
+                server.unwatch(file_path)  # type: ignore
+        watcher.prev_included_files = watcher.included_files[:]
 
         # watch new included files
-        for filepath in self._files_watcher.included_files:
-            self._server.watch(filepath, recursive=False)
-        self._files_watcher.included_files = []
+        for file_path in watcher.included_files:
+            server.watch(file_path, recursive=False)  # type: ignore
+        watcher.included_files = []
 
     def on_page_content(
             self,
@@ -89,7 +91,7 @@ class IncludeMarkdownPlugin(BasePlugin[PluginConfig]):
             files: Files,  # noqa: ARG002
     ) -> str:
         if self._server is not None:  # pragma: no cover
-            self._watch_included_files()
+            self._update_watched_files()
         return html
 
     def on_serve(
@@ -100,7 +102,7 @@ class IncludeMarkdownPlugin(BasePlugin[PluginConfig]):
     ) -> None:
         if self._server is None:  # pragma: no cover
             self._server = server
-            self._watch_included_files()
+            self._update_watched_files()
 
     @event_priority(100)
     def on_page_markdown(


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index e45aed6..bde4fdb 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -2,23 +2,26 @@ import os
 import sys
 
 import pytest
-from mkdocs_include_markdown_plugin.plugin import IncludeMarkdownPlugin
 
 
 TESTS_DIR = os.path.abspath(os.path.dirname(__file__))
-if TESTS_DIR not in sys.path:
-    sys.path.append(TESTS_DIR)
+SRC_DIR = os.path.abspath(os.path.join(TESTS_DIR, '..', 'src'))
+for d in (SRC_DIR, TESTS_DIR):
+    if d not in sys.path:
+        sys.path.insert(0, d)
+
+from mkdocs_include_markdown_plugin import IncludeMarkdownPlugin  # noqa: E402
 
 
 @pytest.fixture
 def page():
     """Fake mkdocs page object."""
-    def _page(filepath):
+    def _page(file_path):
         return type(
             'FakeMkdocsPage', (), {
                 'file': type(
                     'FakeMdocsPageFile', (), {
-                        'abs_src_path': filepath,
+                        'abs_src_path': file_path,
                     },
                 ),
             },
diff --git a/tests/test_integration/test_cache_integration.py b/tests/test_integration/test_cache_integration.py
index b825316..f6e4055 100644
--- a/tests/test_integration/test_cache_integration.py
+++ b/tests/test_integration/test_cache_integration.py
@@ -1,8 +1,10 @@
-from __future__ import annotations
-
 import os
+from dataclasses import dataclass
 
+import mkdocs_include_markdown_plugin.cache
 import pytest
+from mkdocs.exceptions import PluginError
+from mkdocs_include_markdown_plugin import IncludeMarkdownPlugin
 from mkdocs_include_markdown_plugin.cache import (
     CACHE_AVAILABLE,
     Cache,
@@ -76,3 +78,20 @@ def test_page_included_by_url_is_cached(
     assert os.path.isfile(file_path)
 
     os.remove(file_path)
+
+
+def test_cache_setting_when_not_available_raises_error(monkeypatch):
+    @dataclass
+    class FakeConfig:
+        cache: int
+
+    monkeypatch.setattr(
+        mkdocs_include_markdown_plugin.cache,
+        'CACHE_AVAILABLE',
+        False,
+    )
+    plugin = IncludeMarkdownPlugin()
+    plugin.config = FakeConfig(cache=600)
+    with pytest.raises(PluginError) as exc:
+        plugin.on_config({})
+    assert 'The "platformdirs" package is required' in str(exc.value)
diff --git a/tests/test_integration/test_examples.py b/tests/test_integration/test_examples.py
index 3127ba1..0c0b0d8 100644
--- a/tests/test_integration/test_examples.py
+++ b/tests/test_integration/test_examples.py
@@ -1,5 +1,3 @@
-from __future__ import annotations
-
 import os
 import subprocess
 import sys