| 1 |
"""Tests for custom RST directives: file-tree and mdinclude.""" |
| 2 |
|
| 3 |
from io import StringIO |
| 4 |
|
| 5 |
import pytest |
| 6 |
from docutils.core import publish_string |
| 7 |
|
| 8 |
import formiko.directives # noqa: F401 — registers custom directives |
| 9 |
|
| 10 |
# --------------------------------------------------------------------------- |
| 11 |
# Helpers |
| 12 |
# --------------------------------------------------------------------------- |
| 13 |
|
| 14 |
|
| 15 |
def render(rst: str, source_path: str, writer: str = "pseudoxml") -> str: |
| 16 |
"""Render *rst* with docutils and return the string output. |
| 17 |
|
| 18 |
``halt_level=5`` prevents SEVERE errors from raising exceptions so that |
| 19 |
error cases can be asserted on the returned document tree. |
| 20 |
""" |
| 21 |
return publish_string( |
| 22 |
source=rst, |
| 23 |
source_path=source_path, |
| 24 |
writer=writer, |
| 25 |
settings_overrides={ |
| 26 |
"warning_stream": StringIO(), |
| 27 |
"halt_level": 5, |
| 28 |
}, |
| 29 |
).decode() |
| 30 |
|
| 31 |
|
| 32 |
# --------------------------------------------------------------------------- |
| 33 |
# file-tree directive |
| 34 |
# --------------------------------------------------------------------------- |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def filetree(tmp_path): |
| 39 |
"""Create a sample directory tree used by file-tree tests. |
| 40 |
|
| 41 |
Structure:: |
| 42 |
|
| 43 |
tmp_path/ |
| 44 |
docs/ |
| 45 |
guide.md |
| 46 |
readme.md |
| 47 |
images/ |
| 48 |
logo.png |
| 49 |
notes.txt |
| 50 |
.hidden |
| 51 |
""" |
| 52 |
docs = tmp_path / "docs" |
| 53 |
docs.mkdir() |
| 54 |
(docs / "guide.md").write_text("# Guide") |
| 55 |
(docs / "readme.md").write_text("# Readme") |
| 56 |
images = docs / "images" |
| 57 |
images.mkdir() |
| 58 |
(images / "logo.png").write_bytes(b"") |
| 59 |
(tmp_path / "notes.txt").write_text("notes") |
| 60 |
(tmp_path / ".hidden").write_text("hidden") |
| 61 |
return tmp_path |
| 62 |
|
| 63 |
|
| 64 |
def source(tmp_path): |
| 65 |
"""Return a fake RST source path located inside *tmp_path*.""" |
| 66 |
return str(tmp_path / "source.rst") |
| 67 |
|
| 68 |
|
| 69 |
class TestFileTreeBasic: |
| 70 |
def test_lists_files_and_dirs(self, filetree): |
| 71 |
out = render(".. file-tree:: .", source(filetree)) |
| 72 |
assert "docs" in out |
| 73 |
assert "notes.txt" in out |
| 74 |
|
| 75 |
def test_hidden_entries_skipped(self, filetree): |
| 76 |
out = render(".. file-tree:: .", source(filetree)) |
| 77 |
assert ".hidden" not in out |
| 78 |
|
| 79 |
def test_dirs_sorted_before_files(self, filetree): |
| 80 |
out = render(".. file-tree:: .", source(filetree)) |
| 81 |
assert out.index("docs") < out.index("notes.txt") |
| 82 |
|
| 83 |
def test_files_sorted_alphabetically(self, filetree): |
| 84 |
out = render(".. file-tree:: docs", source(filetree)) |
| 85 |
assert out.index("guide.md") < out.index("readme.md") |
| 86 |
|
| 87 |
def test_nested_subdir_appears(self, filetree): |
| 88 |
out = render(".. file-tree:: .", source(filetree)) |
| 89 |
assert "images" in out |
| 90 |
# logo.png is at depth 3 — hidden with default depth=2 |
| 91 |
assert "logo.png" not in out |
| 92 |
|
| 93 |
def test_empty_dir_yields_no_output(self, tmp_path): |
| 94 |
(tmp_path / "empty").mkdir() |
| 95 |
out = render(".. file-tree:: empty", source(tmp_path)) |
| 96 |
assert "bullet_list" not in out |
| 97 |
|
| 98 |
def test_nonexistent_dir_produces_error(self, filetree): |
| 99 |
out = render(".. file-tree:: no_such_dir", source(filetree)) |
| 100 |
assert "system_message" in out |
| 101 |
assert "ERROR" in out |
| 102 |
|
| 103 |
|
| 104 |
class TestFileTreeDepth: |
| 105 |
def test_depth_zero_returns_empty(self, filetree): |
| 106 |
out = render(".. file-tree:: .\n :depth: 0", source(filetree)) |
| 107 |
assert "bullet_list" not in out |
| 108 |
|
| 109 |
def test_depth_one_lists_top_level_only(self, filetree): |
| 110 |
out = render(".. file-tree:: .\n :depth: 1", source(filetree)) |
| 111 |
assert "docs" in out |
| 112 |
assert "notes.txt" in out |
| 113 |
# files inside docs/ must not appear |
| 114 |
assert "readme.md" not in out |
| 115 |
assert "guide.md" not in out |
| 116 |
|
| 117 |
def test_default_depth_shows_direct_children(self, filetree): |
| 118 |
out = render(".. file-tree:: .", source(filetree)) |
| 119 |
assert "readme.md" in out |
| 120 |
assert "guide.md" in out |
| 121 |
# images/ contents are at depth 3 — must not appear with depth=2 |
| 122 |
assert "logo.png" not in out |
| 123 |
|
| 124 |
def test_depth_two_explicit_matches_default(self, filetree): |
| 125 |
default = render(".. file-tree:: .", source(filetree)) |
| 126 |
explicit = render(".. file-tree:: .\n :depth: 2", source(filetree)) |
| 127 |
assert default == explicit |
| 128 |
|
| 129 |
def test_depth_three_shows_all(self, filetree): |
| 130 |
out = render(".. file-tree:: .\n :depth: 3", source(filetree)) |
| 131 |
assert "logo.png" in out |
| 132 |
|
| 133 |
|
| 134 |
class TestFileTreeIncludeFilter: |
| 135 |
def test_matching_files_shown(self, filetree): |
| 136 |
out = render(".. file-tree:: .\n :include: *.md", source(filetree)) |
| 137 |
assert "readme.md" in out |
| 138 |
assert "guide.md" in out |
| 139 |
|
| 140 |
def test_non_matching_files_hidden(self, filetree): |
| 141 |
out = render(".. file-tree:: .\n :include: *.md", source(filetree)) |
| 142 |
assert "notes.txt" not in out |
| 143 |
|
| 144 |
def test_dir_with_no_matching_files_hidden(self, filetree): |
| 145 |
# images/ contains only .png — must be hidden when filtering for *.md |
| 146 |
out = render(".. file-tree:: .\n :include: *.md", source(filetree)) |
| 147 |
assert "images" not in out |
| 148 |
assert "logo.png" not in out |
| 149 |
|
| 150 |
def test_dir_with_matching_files_shown(self, filetree): |
| 151 |
out = render(".. file-tree:: .\n :include: *.md", source(filetree)) |
| 152 |
assert "docs" in out |
| 153 |
|
| 154 |
def test_include_combined_with_max_depth(self, filetree): |
| 155 |
out = render( |
| 156 |
".. file-tree:: .\n :include: *.md\n :depth: 1", |
| 157 |
source(filetree), |
| 158 |
) |
| 159 |
# docs/ would have matches at depth 2, but max-depth cuts the scan |
| 160 |
assert "readme.md" not in out |
| 161 |
|
| 162 |
|
| 163 |
class TestFileTreeLinks: |
| 164 |
def test_links_off_by_default(self, filetree): |
| 165 |
out = render(".. file-tree:: .", source(filetree)) |
| 166 |
assert "refuri" not in out |
| 167 |
|
| 168 |
def test_links_adds_refuri_to_files(self, filetree): |
| 169 |
out = render(".. file-tree:: .\n :links:", source(filetree)) |
| 170 |
assert 'refuri="notes.txt"' in out |
| 171 |
|
| 172 |
def test_links_path_includes_subdirectory(self, filetree): |
| 173 |
out = render(".. file-tree:: .\n :links:", source(filetree)) |
| 174 |
assert 'refuri="docs/readme.md"' in out |
| 175 |
|
| 176 |
def test_links_dirs_have_no_refuri(self, filetree): |
| 177 |
out = render(".. file-tree:: .\n :links:", source(filetree)) |
| 178 |
# directory names appear as plain text, not as references |
| 179 |
lines_with_docs = [ |
| 180 |
ln for ln in out.splitlines() if "docs" in ln |
| 181 |
] |
| 182 |
assert any("refuri" not in ln for ln in lines_with_docs) |
| 183 |
|
| 184 |
def test_links_text_is_filename_only(self, filetree): |
| 185 |
out = render(".. file-tree:: .\n :links:", source(filetree)) |
| 186 |
assert ">notes.txt<" in out or "notes.txt" in out |
| 187 |
# full path must not appear as link text |
| 188 |
assert ">docs/readme.md<" not in out |
| 189 |
|
| 190 |
def test_links_combined_with_subdir_argument(self, filetree): |
| 191 |
# when scanning docs/ directly, paths are relative to source dir |
| 192 |
out = render(".. file-tree:: docs\n :links:", source(filetree)) |
| 193 |
assert 'refuri="docs/guide.md"' in out |
| 194 |
|
| 195 |
|
| 196 |
class TestFileTreeExclude: |
| 197 |
def test_exclude_hides_matching_files(self, filetree): |
| 198 |
out = render(".. file-tree:: .\n :exclude: *.txt", source(filetree)) |
| 199 |
assert "notes.txt" not in out |
| 200 |
|
| 201 |
def test_exclude_keeps_non_matching_files(self, filetree): |
| 202 |
out = render(".. file-tree:: .\n :exclude: *.txt", source(filetree)) |
| 203 |
assert "readme.md" in out |
| 204 |
|
| 205 |
def test_exclude_hides_dir_when_all_files_excluded(self, filetree): |
| 206 |
# images/ contains only .png — hidden when all its files are excluded |
| 207 |
out = render( |
| 208 |
".. file-tree:: .\n :include: *.md\n :exclude: *.png", |
| 209 |
source(filetree), |
| 210 |
) |
| 211 |
assert "images" not in out |
| 212 |
|
| 213 |
def test_include_and_exclude_combined(self, filetree): |
| 214 |
# include all .md, then exclude readme.md specifically |
| 215 |
out = render( |
| 216 |
".. file-tree:: .\n :include: *.md\n :exclude: readme.md", |
| 217 |
source(filetree), |
| 218 |
) |
| 219 |
assert "guide.md" in out |
| 220 |
assert "readme.md" not in out |
| 221 |
assert "notes.txt" not in out |
| 222 |
|
| 223 |
def test_exclude_alone_keeps_dirs_with_remaining_files(self, filetree): |
| 224 |
# docs/ has .md files; excluding .txt keeps docs/ visible |
| 225 |
out = render(".. file-tree:: .\n :exclude: *.txt", source(filetree)) |
| 226 |
assert "docs" in out |
| 227 |
|
| 228 |
def test_exclude_hides_dir_when_all_children_excluded(self, filetree): |
| 229 |
# exclude both .md and .png → docs/ and images/ should disappear |
| 230 |
out = render( |
| 231 |
".. file-tree:: .\n :exclude: *.md\n :depth: 1", |
| 232 |
source(filetree), |
| 233 |
) |
| 234 |
# notes.txt should still be present |
| 235 |
assert "notes.txt" in out |
| 236 |
|
| 237 |
|
| 238 |
# --------------------------------------------------------------------------- |
| 239 |
# mdinclude directive |
| 240 |
# --------------------------------------------------------------------------- |
| 241 |
|
| 242 |
pytest.importorskip("m2r2", reason="m2r2 not installed") |
| 243 |
|
| 244 |
|
| 245 |
class TestStandaloneMdInclude: |
| 246 |
def test_basic_md_content_included(self, tmp_path): |
| 247 |
(tmp_path / "content.md").write_text("Hello **world**") |
| 248 |
out = render( |
| 249 |
".. mdinclude:: content.md", source(tmp_path), writer="html5", |
| 250 |
) |
| 251 |
assert "Hello" in out |
| 252 |
assert "<strong>world</strong>" in out |
| 253 |
|
| 254 |
def test_heading_included(self, tmp_path): |
| 255 |
(tmp_path / "doc.md").write_text("# My Title\n\nSome text.") |
| 256 |
out = render(".. mdinclude:: doc.md", source(tmp_path)) |
| 257 |
assert "My Title" in out |
| 258 |
assert "Some text." in out |
| 259 |
|
| 260 |
def test_relative_path_resolved_from_source_dir(self, tmp_path): |
| 261 |
sub = tmp_path / "sub" |
| 262 |
sub.mkdir() |
| 263 |
(sub / "nested.md").write_text("Nested content") |
| 264 |
out = render(".. mdinclude:: sub/nested.md", source(tmp_path)) |
| 265 |
assert "Nested content" in out |
| 266 |
|
| 267 |
def test_nonexistent_file_produces_error(self, tmp_path): |
| 268 |
out = render(".. mdinclude:: missing.md", source(tmp_path)) |
| 269 |
assert "system_message" in out |
| 270 |
|
| 271 |
def test_no_sphinx_env_attribute_error_not_raised(self, tmp_path): |
| 272 |
"""Directive must work without Sphinx env on document settings.""" |
| 273 |
(tmp_path / "simple.md").write_text("Plain text") |
| 274 |
# publish_string does not set settings.env — must not raise |
| 275 |
out = render(".. mdinclude:: simple.md", source(tmp_path)) |
| 276 |
assert "Plain text" in out |
| 277 |
|
| 278 |
def test_start_line_option(self, tmp_path): |
| 279 |
content = "\n".join(f"Line {i}" for i in range(5)) |
| 280 |
(tmp_path / "lines.md").write_text(content) |
| 281 |
rst = ".. mdinclude:: lines.md\n :start-line: 3" |
| 282 |
out = render(rst, source(tmp_path)) |
| 283 |
assert "Line 3" in out |
| 284 |
assert "Line 0" not in out |
| 285 |
|
| 286 |
def test_end_line_option(self, tmp_path): |
| 287 |
content = "\n".join(f"Line {i}" for i in range(5)) |
| 288 |
(tmp_path / "lines.md").write_text(content) |
| 289 |
rst = ".. mdinclude:: lines.md\n :end-line: 2" |
| 290 |
out = render(rst, source(tmp_path)) |
| 291 |
assert "Line 0" in out |
| 292 |
assert "Line 4" not in out |