|
8 | 8 | (e.g. converting `<div class="section">` to `<section>`) |
9 | 9 | """ |
10 | 10 |
|
| 11 | +from __future__ import annotations |
| 12 | + |
11 | 13 | import os |
12 | 14 | import re |
| 15 | +from pathlib import Path |
13 | 16 |
|
14 | 17 | import pytest |
| 18 | +import sphinx |
15 | 19 | from docutils import VersionInfo, __version_info__ |
16 | 20 |
|
17 | 21 | SOURCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "sourcedirs")) |
@@ -583,3 +587,45 @@ def test_texinfo(app, status, warning): |
583 | 587 | assert "build succeeded" in status.getvalue() # Build succeeded |
584 | 588 | warnings = warning.getvalue().strip() |
585 | 589 | assert warnings == "" |
| 590 | + |
| 591 | + |
| 592 | +@pytest.mark.skipif( |
| 593 | + sphinx.version_info < (7, 2, 5), reason="include-read event added in sphinx 7.2.5" |
| 594 | +) |
| 595 | +@pytest.mark.sphinx( |
| 596 | + buildername="html", |
| 597 | + srcdir=os.path.join(SOURCE_DIR, "includes"), |
| 598 | + freshenv=True, |
| 599 | +) |
| 600 | +def test_include_read_event(app, status, warning): |
| 601 | + """Test that include-read event is emitted correctly.""" |
| 602 | + |
| 603 | + include_read_events = [] |
| 604 | + |
| 605 | + def handle_include_read( |
| 606 | + app, relative_path: Path, parent_docname: str, content: list[str] |
| 607 | + ) -> None: |
| 608 | + include_read_events.append((relative_path, parent_docname, content)) |
| 609 | + |
| 610 | + app.connect("include-read", handle_include_read) |
| 611 | + app.build() |
| 612 | + assert "build succeeded" in status.getvalue() # Build succeeded |
| 613 | + warnings = warning.getvalue().strip() |
| 614 | + assert warnings == "" |
| 615 | + expected = [ |
| 616 | + ("../include_from_rst/include.md", "index"), |
| 617 | + ("include1.inc.md", "index"), |
| 618 | + (os.path.join("subfolder", "include2.inc.md"), "include1.inc"), |
| 619 | + ("include_code.py", "index"), |
| 620 | + ("include_code.py", "index"), |
| 621 | + ("include_literal.txt", "index"), |
| 622 | + ("include_literal.txt", "index"), |
| 623 | + ] |
| 624 | + expected_events = [] |
| 625 | + for include_file_name, parent_docname in expected: |
| 626 | + with open(os.path.join(SOURCE_DIR, "includes", include_file_name)) as file: |
| 627 | + content = file.read() |
| 628 | + expected_events.append((Path(include_file_name), parent_docname, [content])) |
| 629 | + assert len(include_read_events) == len(expected_events), "Wrong number of events" |
| 630 | + for evt in expected_events: |
| 631 | + assert evt in include_read_events |
0 commit comments