|
1 | 1 | import pytest |
| 2 | +from docutils import nodes |
| 3 | +from docutils.core import publish_doctree |
| 4 | +from docutils.parsers.rst import Parser |
2 | 5 |
|
3 | 6 | from pep_sphinx_extensions.pep_processor.transforms import pep_headers |
4 | 7 | from pep_sphinx_extensions.pep_zero_generator.constants import ( |
|
17 | 20 | ) |
18 | 21 |
|
19 | 22 |
|
| 23 | +def _render_pep_headers(post_history, *, discussions_to=""): |
| 24 | + document = publish_doctree( |
| 25 | + f"PEP: 9000\nTitle: Test PEP\n{discussions_to}Status: Draft\n{post_history}", |
| 26 | + source_path="pep-9000.rst", |
| 27 | + parser=Parser(rfc2822=True), |
| 28 | + settings_overrides={"docinfo_xform": False}, |
| 29 | + ) |
| 30 | + pep_headers.PEPHeaders(document).apply() |
| 31 | + return document |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + ("last_url", "expected_url", "label"), |
| 36 | + [ |
| 37 | + pytest.param( |
| 38 | + "https://discuss.python.org/t/current/200/3", |
| 39 | + "https://discuss.python.org/t/current/200", |
| 40 | + "Discourse thread", |
| 41 | + id="reply-url", |
| 42 | + ), |
| 43 | + pytest.param( |
| 44 | + "https://discuss.python.org/t/200/3/", |
| 45 | + "https://discuss.python.org/t/200", |
| 46 | + "Discourse thread", |
| 47 | + id="reply-url-without-a-topic-slug", |
| 48 | + ), |
| 49 | + pytest.param( |
| 50 | + "https://discuss.python.org/t/current/200/", |
| 51 | + "https://discuss.python.org/t/current/200/", |
| 52 | + "Discourse thread", |
| 53 | + id="topic-url", |
| 54 | + ), |
| 55 | + pytest.param( |
| 56 | + "https://example.com/thread/200/3", |
| 57 | + "https://example.com/thread/200/3", |
| 58 | + "https://example.com/thread/200/3", |
| 59 | + id="other-forum", |
| 60 | + ), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_discussions_to_uses_last_post_history_link(last_url, expected_url, label): |
| 64 | + first_url = "https://discuss.python.org/t/older/100" |
| 65 | + document = _render_pep_headers( |
| 66 | + f"Post-History: `01-Jan-2026 <{first_url}>`__, " |
| 67 | + f"`02-Jan-2026 <{last_url}>`__, 03-Jan-2026\n" |
| 68 | + ) |
| 69 | + |
| 70 | + fields = {field[0].astext(): field[1] for field in document[0]} |
| 71 | + links = list(fields["Discussions-To"].findall(nodes.reference)) |
| 72 | + assert [(link["refuri"], link.astext()) for link in links] == [ |
| 73 | + (expected_url, label) |
| 74 | + ] |
| 75 | + assert list(fields).index("Discussions-To") < list(fields).index("Status") |
| 76 | + assert [ |
| 77 | + link["refuri"] for link in fields["Post-History"].findall(nodes.reference) |
| 78 | + ] == [first_url, last_url] |
| 79 | + assert "Discussions-To" not in document["headers"] |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize( |
| 83 | + ("value", "label", "expected_urls"), |
| 84 | + [ |
| 85 | + pytest.param( |
| 86 | + "https://discuss.python.org/t/explicit/300", |
| 87 | + "Discourse thread", |
| 88 | + ["https://discuss.python.org/t/explicit/300"], |
| 89 | + id="explicit-link", |
| 90 | + ), |
| 91 | + pytest.param("Pending", "Pending", [], id="explicit-pending"), |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_explicit_discussions_to_takes_precedence(value, label, expected_urls): |
| 95 | + document = _render_pep_headers( |
| 96 | + "Post-History: `02-Jan-2026 <https://discuss.python.org/t/current/200/3>`__\n", |
| 97 | + discussions_to=f"Discussions-To: {value}\n", |
| 98 | + ) |
| 99 | + |
| 100 | + fields = {field[0].astext(): field[1] for field in document[0]} |
| 101 | + body = fields["Discussions-To"] |
| 102 | + assert body.astext() == label |
| 103 | + assert [link["refuri"] for link in body.findall(nodes.reference)] == expected_urls |
| 104 | + assert document["headers"]["Discussions-To"] == value |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + "post_history", |
| 109 | + [ |
| 110 | + pytest.param("Post-History: 02-Jan-2026\n", id="date-without-a-link"), |
| 111 | + pytest.param("Post-History: Pending\n", id="pending-history"), |
| 112 | + pytest.param("", id="no-history"), |
| 113 | + ], |
| 114 | +) |
| 115 | +def test_no_discussions_to_without_post_history_links(post_history): |
| 116 | + document = _render_pep_headers(post_history) |
| 117 | + |
| 118 | + assert "Discussions-To" not in {field[0].astext() for field in document[0]} |
| 119 | + |
| 120 | + |
20 | 121 | @pytest.mark.parametrize( |
21 | 122 | ("test_input", "expected"), |
22 | 123 | [ |
|
0 commit comments