|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import tempfile |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from mkdocs_jupyter.plugin import _compute_cache_key, _get_cache_path |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def sample_nb(tmp_path): |
| 14 | + """Create a minimal .ipynb file for cache key tests.""" |
| 15 | + nb = { |
| 16 | + "cells": [ |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": ["# Hello World"], |
| 21 | + } |
| 22 | + ], |
| 23 | + "metadata": { |
| 24 | + "kernelspec": { |
| 25 | + "display_name": "Python 3", |
| 26 | + "language": "python", |
| 27 | + "name": "python3", |
| 28 | + } |
| 29 | + }, |
| 30 | + "nbformat": 4, |
| 31 | + "nbformat_minor": 5, |
| 32 | + } |
| 33 | + nb_path = tmp_path / "test.ipynb" |
| 34 | + nb_path.write_text(json.dumps(nb)) |
| 35 | + return nb_path |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def base_config(): |
| 40 | + return { |
| 41 | + "kernel_name": "", |
| 42 | + "theme": "", |
| 43 | + "allow_errors": True, |
| 44 | + "show_input": True, |
| 45 | + "no_input": False, |
| 46 | + "remove_tag_config": {}, |
| 47 | + "highlight_extra_classes": "", |
| 48 | + "include_requirejs": False, |
| 49 | + "custom_mathjax_url": "", |
| 50 | + "toc_depth": 6, |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +class TestComputeCacheKey: |
| 55 | + def test_same_file_same_config_same_key(self, sample_nb, base_config): |
| 56 | + key1 = _compute_cache_key(str(sample_nb), base_config, False) |
| 57 | + key2 = _compute_cache_key(str(sample_nb), base_config, False) |
| 58 | + assert key1 == key2 |
| 59 | + |
| 60 | + def test_different_file_content_different_key(self, tmp_path, base_config): |
| 61 | + nb1 = tmp_path / "nb1.ipynb" |
| 62 | + nb2 = tmp_path / "nb2.ipynb" |
| 63 | + nb1.write_text(json.dumps({"cells": [], "metadata": {}, "nbformat": 4})) |
| 64 | + nb2.write_text( |
| 65 | + json.dumps( |
| 66 | + { |
| 67 | + "cells": [{"cell_type": "code"}], |
| 68 | + "metadata": {}, |
| 69 | + "nbformat": 4, |
| 70 | + } |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + key1 = _compute_cache_key(str(nb1), base_config, False) |
| 75 | + key2 = _compute_cache_key(str(nb2), base_config, False) |
| 76 | + assert key1 != key2 |
| 77 | + |
| 78 | + def test_different_config_different_key(self, sample_nb, base_config): |
| 79 | + key1 = _compute_cache_key(str(sample_nb), base_config, False) |
| 80 | + |
| 81 | + config2 = {**base_config, "allow_errors": False} |
| 82 | + key2 = _compute_cache_key(str(sample_nb), config2, False) |
| 83 | + assert key1 != key2 |
| 84 | + |
| 85 | + def test_different_exec_nb_different_key(self, sample_nb, base_config): |
| 86 | + """Resolved exec_nb (after execute_ignore) affects the key.""" |
| 87 | + key1 = _compute_cache_key(str(sample_nb), base_config, True) |
| 88 | + key2 = _compute_cache_key(str(sample_nb), base_config, False) |
| 89 | + assert key1 != key2 |
| 90 | + |
| 91 | + def test_modified_file_different_key(self, sample_nb, base_config): |
| 92 | + key1 = _compute_cache_key(str(sample_nb), base_config, False) |
| 93 | + |
| 94 | + # Modify the file |
| 95 | + nb = json.loads(sample_nb.read_text()) |
| 96 | + nb["cells"].append({"cell_type": "code", "metadata": {}, "source": ["x = 1"]}) |
| 97 | + sample_nb.write_text(json.dumps(nb)) |
| 98 | + |
| 99 | + key2 = _compute_cache_key(str(sample_nb), base_config, False) |
| 100 | + assert key1 != key2 |
| 101 | + |
| 102 | + |
| 103 | +class TestGetCachePath: |
| 104 | + def test_returns_json_path(self): |
| 105 | + path = _get_cache_path("/tmp/cache", "abc123") |
| 106 | + assert path == pathlib.Path("/tmp/cache/abc123.json") |
| 107 | + |
| 108 | + def test_path_under_cache_dir(self): |
| 109 | + path = _get_cache_path(".cache/mkdocs-jupyter", "deadbeef") |
| 110 | + assert str(path) == ".cache/mkdocs-jupyter/deadbeef.json" |
| 111 | + |
| 112 | + |
| 113 | +class TestCacheIntegration: |
| 114 | + """Integration tests using full mkdocs build.""" |
| 115 | + |
| 116 | + def test_cache_populated_on_first_build(self): |
| 117 | + """First build should create cache files.""" |
| 118 | + from mkdocs.commands.build import build |
| 119 | + from mkdocs.config import load_config |
| 120 | + |
| 121 | + this_dir = os.path.dirname(os.path.realpath(__file__)) |
| 122 | + config_file = os.path.join(this_dir, "mkdocs/base-with-nbs.yml") |
| 123 | + |
| 124 | + with tempfile.TemporaryDirectory() as cache_dir: |
| 125 | + cfg = load_config(config_file) |
| 126 | + cfg["plugins"]["mkdocs-jupyter"].config["cache"] = True |
| 127 | + cfg["plugins"]["mkdocs-jupyter"].config["cache_dir"] = cache_dir |
| 128 | + |
| 129 | + build(cfg) |
| 130 | + |
| 131 | + cache_files = list(pathlib.Path(cache_dir).glob("*.json")) |
| 132 | + assert len(cache_files) > 0, "Cache files should be created on first build" |
| 133 | + |
| 134 | + def test_cache_hit_on_second_build(self): |
| 135 | + """Second build should use cached results (nb2html not called again).""" |
| 136 | + from mkdocs.commands.build import build |
| 137 | + from mkdocs.config import load_config |
| 138 | + |
| 139 | + this_dir = os.path.dirname(os.path.realpath(__file__)) |
| 140 | + config_file = os.path.join(this_dir, "mkdocs/base-with-nbs.yml") |
| 141 | + |
| 142 | + with tempfile.TemporaryDirectory() as cache_dir: |
| 143 | + cfg = load_config(config_file) |
| 144 | + cfg["plugins"]["mkdocs-jupyter"].config["cache"] = True |
| 145 | + cfg["plugins"]["mkdocs-jupyter"].config["cache_dir"] = cache_dir |
| 146 | + |
| 147 | + # First build populates cache |
| 148 | + build(cfg) |
| 149 | + |
| 150 | + # Second build should hit cache — nb2html should not be called |
| 151 | + cfg2 = load_config(config_file) |
| 152 | + cfg2["plugins"]["mkdocs-jupyter"].config["cache"] = True |
| 153 | + cfg2["plugins"]["mkdocs-jupyter"].config["cache_dir"] = cache_dir |
| 154 | + |
| 155 | + with patch("mkdocs_jupyter.convert.nb2html") as mock_nb2html: |
| 156 | + build(cfg2) |
| 157 | + mock_nb2html.assert_not_called() |
| 158 | + |
| 159 | + def test_cache_disabled(self): |
| 160 | + """When cache=False, no cache files should be created.""" |
| 161 | + from mkdocs.commands.build import build |
| 162 | + from mkdocs.config import load_config |
| 163 | + |
| 164 | + this_dir = os.path.dirname(os.path.realpath(__file__)) |
| 165 | + config_file = os.path.join(this_dir, "mkdocs/base-with-nbs.yml") |
| 166 | + |
| 167 | + with tempfile.TemporaryDirectory() as cache_dir: |
| 168 | + cfg = load_config(config_file) |
| 169 | + cfg["plugins"]["mkdocs-jupyter"].config["cache"] = False |
| 170 | + cfg["plugins"]["mkdocs-jupyter"].config["cache_dir"] = cache_dir |
| 171 | + |
| 172 | + build(cfg) |
| 173 | + |
| 174 | + cache_files = list(pathlib.Path(cache_dir).glob("*.json")) |
| 175 | + assert len(cache_files) == 0, "No cache files when cache is disabled" |
| 176 | + |
| 177 | + def test_stale_cache_evicted(self): |
| 178 | + """Stale cache files from previous builds are cleaned up.""" |
| 179 | + from mkdocs.commands.build import build |
| 180 | + from mkdocs.config import load_config |
| 181 | + |
| 182 | + this_dir = os.path.dirname(os.path.realpath(__file__)) |
| 183 | + config_file = os.path.join(this_dir, "mkdocs/base-with-nbs.yml") |
| 184 | + |
| 185 | + with tempfile.TemporaryDirectory() as cache_dir: |
| 186 | + # Plant a stale cache file |
| 187 | + stale = pathlib.Path(cache_dir) / "stale_old_entry.json" |
| 188 | + stale.write_text("{}") |
| 189 | + |
| 190 | + cfg = load_config(config_file) |
| 191 | + cfg["plugins"]["mkdocs-jupyter"].config["cache"] = True |
| 192 | + cfg["plugins"]["mkdocs-jupyter"].config["cache_dir"] = cache_dir |
| 193 | + |
| 194 | + build(cfg) |
| 195 | + |
| 196 | + assert not stale.exists(), "Stale cache file should be evicted" |
| 197 | + # But valid cache files should remain |
| 198 | + cache_files = list(pathlib.Path(cache_dir).glob("*.json")) |
| 199 | + assert len(cache_files) > 0 |
0 commit comments