Skip to content

Commit

Permalink
Improve coverage (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Oct 14, 2023
1 parent 065c890 commit b1eef73
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 26 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions examples/global-exclude/docs/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{% include-markdown "**" exclude="./{index,license}.md" %}
{% include "**" exclude="./{index,license}.md" %}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/mkdocs_include_markdown_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
"""Mkdocs Markdown plugin to include files."""
from __future__ import annotations


__all__ = ['IncludeMarkdownPlugin']

from mkdocs_include_markdown_plugin.plugin import IncludeMarkdownPlugin
2 changes: 1 addition & 1 deletion src/mkdocs_include_markdown_plugin/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

try:
from platformdirs import user_data_dir
except ImportError:
except ImportError: # pragma: no cover
CACHE_AVAILABLE = False
else:
CACHE_AVAILABLE = True
Expand Down
2 changes: 1 addition & 1 deletion src/mkdocs_include_markdown_plugin/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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
Expand Down
28 changes: 15 additions & 13 deletions src/mkdocs_include_markdown_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,23 @@ def _include_markdown_tag(self) -> re.Pattern[str]:
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,
Expand All @@ -89,7 +91,7 @@ def on_page_content(
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(
Expand All @@ -100,7 +102,7 @@ def on_serve(
) -> 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(
Expand Down
13 changes: 8 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
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,
},
),
},
Expand Down
23 changes: 21 additions & 2 deletions tests/test_integration/test_cache_integration.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -76,3 +78,20 @@ def run():
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)
2 changes: 0 additions & 2 deletions tests/test_integration/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

import os
import subprocess
import sys
Expand Down

0 comments on commit b1eef73

Please sign in to comment.