| 1 |
"""Tests util.utils functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
from docutils import nodes |
| 10 |
|
| 11 |
from sphinx.util.docutils import ( |
| 12 |
SphinxFileOutput, |
| 13 |
SphinxTranslator, |
| 14 |
docutils_namespace, |
| 15 |
new_document, |
| 16 |
register_node, |
| 17 |
) |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from pathlib import Path |
| 21 |
|
| 22 |
from sphinx.builders import Builder |
| 23 |
from sphinx.testing.util import SphinxTestApp |
| 24 |
|
| 25 |
|
| 26 |
def test_register_node() -> None: |
| 27 |
class custom_node(nodes.Element): |
| 28 |
pass |
| 29 |
|
| 30 |
with docutils_namespace(): |
| 31 |
register_node(custom_node) |
| 32 |
|
| 33 |
# check registered |
| 34 |
assert hasattr(nodes.GenericNodeVisitor, 'visit_custom_node') |
| 35 |
assert hasattr(nodes.GenericNodeVisitor, 'depart_custom_node') |
| 36 |
assert hasattr(nodes.SparseNodeVisitor, 'visit_custom_node') |
| 37 |
assert hasattr(nodes.SparseNodeVisitor, 'depart_custom_node') |
| 38 |
|
| 39 |
# check unregistered outside namespace |
| 40 |
assert not hasattr(nodes.GenericNodeVisitor, 'visit_custom_node') |
| 41 |
assert not hasattr(nodes.GenericNodeVisitor, 'depart_custom_node') |
| 42 |
assert not hasattr(nodes.SparseNodeVisitor, 'visit_custom_node') |
| 43 |
assert not hasattr(nodes.SparseNodeVisitor, 'depart_custom_node') |
| 44 |
|
| 45 |
|
| 46 |
def test_SphinxFileOutput(tmp_path: Path) -> None: |
| 47 |
content = 'Hello Sphinx World' |
| 48 |
|
| 49 |
# write test.txt at first |
| 50 |
filename = tmp_path / 'test.txt' |
| 51 |
output = SphinxFileOutput(destination_path=str(filename)) |
| 52 |
output.write(content) |
| 53 |
os.utime(filename, ns=(0, 0)) |
| 54 |
|
| 55 |
# overwrite it again |
| 56 |
output.write(content) |
| 57 |
assert filename.stat().st_mtime_ns != 0 # updated |
| 58 |
|
| 59 |
# write test2.txt at first |
| 60 |
filename = tmp_path / 'test2.txt' |
| 61 |
output = SphinxFileOutput(destination_path=str(filename), overwrite_if_changed=True) |
| 62 |
output.write(content) |
| 63 |
os.utime(filename, ns=(0, 0)) |
| 64 |
|
| 65 |
# overwrite it again |
| 66 |
output.write(content) |
| 67 |
assert filename.stat().st_mtime_ns == 0 # not updated |
| 68 |
|
| 69 |
# overwrite it again (content changed) |
| 70 |
output.write(content + '; content change') |
| 71 |
assert filename.stat().st_mtime_ns != 0 # updated |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.sphinx('html', testroot='root') |
| 75 |
def test_SphinxTranslator(app: SphinxTestApp) -> None: |
| 76 |
class CustomNode(nodes.inline): |
| 77 |
pass |
| 78 |
|
| 79 |
class MyTranslator(SphinxTranslator): |
| 80 |
def __init__(self, document: nodes.document, builder: Builder): |
| 81 |
self.called: list[str] = [] |
| 82 |
super().__init__(document, builder) |
| 83 |
|
| 84 |
def visit_document(self, node: nodes.document) -> None: |
| 85 |
pass |
| 86 |
|
| 87 |
def depart_document(self, node: nodes.document) -> None: |
| 88 |
pass |
| 89 |
|
| 90 |
def visit_inline(self, node: nodes.inline) -> None: |
| 91 |
self.called.append('visit_inline') |
| 92 |
|
| 93 |
def depart_inline(self, node: nodes.inline) -> None: |
| 94 |
self.called.append('depart_inline') |
| 95 |
|
| 96 |
document = new_document('') |
| 97 |
document += CustomNode() |
| 98 |
|
| 99 |
translator = MyTranslator(document, app.builder) |
| 100 |
document.walkabout(translator) |
| 101 |
|
| 102 |
# MyTranslator does not have visit_CustomNode. But it calls visit_inline instead. |
| 103 |
assert translator.called == ['visit_inline', 'depart_inline'] |