Skip to content

Commit a661b8f

Browse files
authored
Add more tests and fix coverage reporting (#170)
1 parent 7ec8c3b commit a661b8f

File tree

7 files changed

+99
-24
lines changed

7 files changed

+99
-24
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ jobs:
7878
- name: Convert coverage to XML
7979
run: |
8080
pip install coverage covdefaults
81+
coverage combine
8182
coverage xml
8283
- name: Upload coverage
8384
uses: codecov/codecov-action@v3

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ style = [
113113
source = ["src"]
114114
omit = ["src/mkdocs_include_markdown_plugin/plugin.py"]
115115
plugins = ["covdefaults"]
116+
parallel = true
117+
data_file = ".coverage/.coverage"
116118

117119
[tool.coverage.report]
118120
exclude_lines = [

src/mkdocs_include_markdown_plugin/cache.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
import time
99

1010

11+
try:
12+
from platformdirs import user_data_dir
13+
except ImportError:
14+
CACHE_AVAILABLE = False
15+
else:
16+
CACHE_AVAILABLE = True
17+
18+
1119
class Cache:
1220
"""Cache for arbitrary content, one file per entry."""
1321

@@ -18,7 +26,7 @@ def __init__(self, cache_dir: str, expiration_seconds: int = 0):
1826
def get_creation_time_from_fpath(self, fpath: str) -> int:
1927
"""Get creation time of an entry in the cache given its path."""
2028
with open(fpath, encoding='utf-8') as f:
21-
return int(f.readline().strip())
29+
return int(f.readline())
2230

2331
@classmethod
2432
def generate_unique_key_from_url(cls, url: str) -> str:
@@ -29,7 +37,7 @@ def generate_unique_key_from_url(cls, url: str) -> str:
2937

3038
def read_file(self, fpath: str) -> str: # noqa: D102
3139
with open(fpath, encoding='utf-8') as f:
32-
return '\n'.join(f.read().split('\n')[1:])
40+
return f.read().split('\n', 1)[1]
3341

3442
def get_(self, url: str) -> str | None: # noqa: D102
3543
key = self.generate_unique_key_from_url(url)
@@ -62,9 +70,7 @@ def clean(self) -> None:
6270

6371
def get_cache_directory() -> str | None:
6472
"""Get the cache directory."""
65-
try:
66-
from platformdirs import user_data_dir
67-
except ImportError:
73+
if not CACHE_AVAILABLE:
6874
return None
6975

7076
cache_dir = user_data_dir('mkdocs-include-markdown-plugin')
@@ -77,7 +83,6 @@ def get_cache_directory() -> str | None:
7783
def initialize_cache(expiration_seconds: int) -> Cache | None:
7884
"""Initialize a cache instance."""
7985
cache_dir = get_cache_directory()
80-
if cache_dir is None:
81-
return None
82-
83-
return Cache(cache_dir, expiration_seconds)
86+
return None if cache_dir is None else Cache(
87+
cache_dir, expiration_seconds,
88+
)

src/mkdocs_include_markdown_plugin/files_watcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55

66
class FilesWatcher: # noqa: D101
7-
def __init__(self) -> None:
7+
def __init__(self) -> None: # pragma: no cover
88
self.prev_included_files: list[str] = []
99
self.included_files: list[str] = []

tests/test_integration/test_cache.py renamed to tests/test_integration/test_cache_integration.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import importlib
21
import os
32

43
import pytest
54
from testing_helpers import parametrize_directives
65

76
from mkdocs_include_markdown_plugin.cache import (
7+
CACHE_AVAILABLE,
88
Cache,
99
get_cache_directory,
1010
initialize_cache,
@@ -42,9 +42,7 @@ def test_page_included_by_url_is_cached(
4242
tmp_path,
4343
):
4444
cache_dir = get_cache_directory()
45-
try:
46-
importlib.import_module('platformdirs')
47-
except ImportError:
45+
if not CACHE_AVAILABLE:
4846
assert cache_dir is None
4947
assert initialize_cache(600) is None
5048
return
Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
import importlib
21
import os
32
import subprocess
43
import sys
54

65
import pytest
6+
from mkdocs import config
7+
from mkdocs.commands.build import build
8+
from mkdocs.exceptions import Abort
79
from testing_helpers import rootdir
810

11+
from mkdocs_include_markdown_plugin.cache import CACHE_AVAILABLE
12+
913

1014
EXAMPLES_DIR = os.path.join(rootdir, 'examples')
1115

1216

13-
@pytest.mark.parametrize('dirname', os.listdir(EXAMPLES_DIR))
14-
def test_examples(dirname):
15-
expected_returncode = 0
17+
def config_is_using_cache_setting(config_file_path):
18+
with open(config_file_path, encoding='utf-8') as f:
19+
return 'cache:' in f.read()
20+
1621

22+
@pytest.mark.parametrize('dirname', os.listdir(EXAMPLES_DIR))
23+
def test_examples_subprocess(dirname):
1724
example_dir = os.path.join(EXAMPLES_DIR, dirname)
1825
config_file = os.path.join(example_dir, 'mkdocs.yml')
19-
with open(config_file, encoding='utf-8') as f:
20-
if 'cache:' in f.read():
21-
try:
22-
importlib.import_module('platformdirs')
23-
except ImportError:
24-
expected_returncode = 1
26+
expected_returncode = 1 if config_is_using_cache_setting(
27+
config_file,
28+
) and not CACHE_AVAILABLE else 0
2529

2630
proc = subprocess.Popen(
2731
[sys.executable, '-mmkdocs', 'build'],
@@ -34,3 +38,22 @@ def test_examples(dirname):
3438
assert proc.returncode == expected_returncode, (
3539
f'{stdout.decode("utf-8")}\n{stderr.decode("utf-8")}'
3640
)
41+
42+
43+
@pytest.mark.parametrize('dirname', os.listdir(EXAMPLES_DIR))
44+
def test_examples_api(dirname):
45+
example_dir = os.path.join(EXAMPLES_DIR, dirname)
46+
config_file = os.path.join(example_dir, 'mkdocs.yml')
47+
expected_to_raise_exc = (
48+
config_is_using_cache_setting(config_file) and not CACHE_AVAILABLE
49+
)
50+
51+
def run():
52+
cfg = config.load_config(config_file=config_file)
53+
build(cfg, dirty=False)
54+
55+
if expected_to_raise_exc:
56+
with pytest.raises(Abort):
57+
run()
58+
else:
59+
run()

tests/test_unit/test_cache.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import time
3+
4+
from mkdocs_include_markdown_plugin.cache import (
5+
CACHE_AVAILABLE,
6+
Cache,
7+
get_cache_directory,
8+
initialize_cache,
9+
)
10+
11+
12+
def test_cache_read_file(tmp_path):
13+
cache = Cache(tmp_path)
14+
assert cache.read_file('pyproject.toml').split('\n', 1)[0] == (
15+
'name = "mkdocs-include-markdown-plugin"'
16+
)
17+
18+
19+
def test_cache_clean(tmp_path):
20+
now_ts = int(time.time())
21+
22+
file1 = tmp_path / 'file1'
23+
file1.write_text(f'{now_ts}\n')
24+
file2 = tmp_path / 'file2'
25+
file2.write_text(f'{now_ts}\n')
26+
27+
assert len(os.listdir(tmp_path)) == 2
28+
29+
cache = Cache(tmp_path, 0)
30+
cache.clean()
31+
32+
assert len(os.listdir(tmp_path)) == 0
33+
34+
35+
def test_get_cache_directory():
36+
if not CACHE_AVAILABLE:
37+
assert get_cache_directory() is None
38+
else:
39+
assert isinstance(get_cache_directory(), str)
40+
41+
42+
def test_initialize_cache_instance():
43+
if not CACHE_AVAILABLE:
44+
assert initialize_cache(300) is None
45+
else:
46+
assert isinstance(initialize_cache(300), Cache)

0 commit comments

Comments
 (0)