| 1 |
"""Test the versioning implementation.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import pickle |
| 6 |
import shutil |
| 7 |
|
| 8 |
import pytest |
| 9 |
from docutils.utils import DependencyList |
| 10 |
|
| 11 |
from sphinx.testing.util import SphinxTestApp |
| 12 |
from sphinx.versioning import add_uids, get_ratio, merge_doctrees |
| 13 |
|
| 14 |
TYPE_CHECKING = False |
| 15 |
if TYPE_CHECKING: |
| 16 |
from collections.abc import Iterator |
| 17 |
from pathlib import Path |
| 18 |
|
| 19 |
from docutils import nodes |
| 20 |
|
| 21 |
from sphinx.application import Sphinx |
| 22 |
|
| 23 |
original: nodes.document |
| 24 |
original_uids: list[str] |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture(scope='module', autouse=True) |
| 28 |
def _setup_module(rootdir: Path, sphinx_test_tempdir: Path) -> Iterator[None]: |
| 29 |
global original, original_uids # NoQA: PLW0603 |
| 30 |
srcdir = sphinx_test_tempdir / 'test-versioning' |
| 31 |
if not srcdir.exists(): |
| 32 |
shutil.copytree(rootdir / 'test-versioning', srcdir) |
| 33 |
app = SphinxTestApp(srcdir=srcdir) |
| 34 |
app.builder.env._app = app |
| 35 |
app.connect('doctree-resolved', on_doctree_resolved) |
| 36 |
app.build() |
| 37 |
original = doctrees['original'] |
| 38 |
original_uids = [n.uid for n in add_uids(original, is_paragraph)] # type: ignore[attr-defined] |
| 39 |
yield |
| 40 |
app.cleanup() |
| 41 |
|
| 42 |
|
| 43 |
doctrees = {} |
| 44 |
|
| 45 |
|
| 46 |
def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> None: |
| 47 |
doctrees[docname] = doctree |
| 48 |
|
| 49 |
|
| 50 |
def is_paragraph(node: nodes.Node) -> bool: |
| 51 |
return node.__class__.__name__ == 'paragraph' |
| 52 |
|
| 53 |
|
| 54 |
def test_get_ratio() -> None: |
| 55 |
assert get_ratio('', 'a') |
| 56 |
assert get_ratio('a', '') |
| 57 |
|
| 58 |
|
| 59 |
def test_add_uids() -> None: |
| 60 |
assert len(original_uids) == 3 |
| 61 |
|
| 62 |
|
| 63 |
def test_pickleablility() -> None: |
| 64 |
# we have to modify the doctree so we can pickle it |
| 65 |
copy = original.copy() |
| 66 |
copy.reporter = None # type: ignore[assignment] |
| 67 |
copy.transformer = None # type: ignore[assignment] |
| 68 |
copy.settings.warning_stream = None |
| 69 |
copy.settings.env = None |
| 70 |
copy.settings.record_dependencies = DependencyList() |
| 71 |
loaded = pickle.loads(pickle.dumps(copy, pickle.HIGHEST_PROTOCOL)) |
| 72 |
assert all(getattr(n, 'uid', False) for n in loaded.findall(is_paragraph)) |
| 73 |
|
| 74 |
|
| 75 |
def test_modified() -> None: |
| 76 |
modified = doctrees['modified'] |
| 77 |
new_nodes = list(merge_doctrees(original, modified, is_paragraph)) |
| 78 |
uids = [n.uid for n in modified.findall(is_paragraph)] # type: ignore[attr-defined] |
| 79 |
assert not new_nodes |
| 80 |
assert original_uids == uids |
| 81 |
|
| 82 |
|
| 83 |
def test_added() -> None: |
| 84 |
added = doctrees['added'] |
| 85 |
new_nodes = list(merge_doctrees(original, added, is_paragraph)) |
| 86 |
uids = [n.uid for n in added.findall(is_paragraph)] # type: ignore[attr-defined] |
| 87 |
assert len(new_nodes) == 1 |
| 88 |
assert original_uids == uids[:-1] |
| 89 |
|
| 90 |
|
| 91 |
def test_deleted() -> None: |
| 92 |
deleted = doctrees['deleted'] |
| 93 |
new_nodes = list(merge_doctrees(original, deleted, is_paragraph)) |
| 94 |
uids = [n.uid for n in deleted.findall(is_paragraph)] # type: ignore[attr-defined] |
| 95 |
assert not new_nodes |
| 96 |
assert original_uids[::2] == uids |
| 97 |
|
| 98 |
|
| 99 |
def test_deleted_end() -> None: |
| 100 |
deleted_end = doctrees['deleted_end'] |
| 101 |
new_nodes = list(merge_doctrees(original, deleted_end, is_paragraph)) |
| 102 |
uids = [n.uid for n in deleted_end.findall(is_paragraph)] # type: ignore[attr-defined] |
| 103 |
assert not new_nodes |
| 104 |
assert original_uids[:-1] == uids |
| 105 |
|
| 106 |
|
| 107 |
def test_insert() -> None: |
| 108 |
insert = doctrees['insert'] |
| 109 |
new_nodes = list(merge_doctrees(original, insert, is_paragraph)) |
| 110 |
uids = [n.uid for n in insert.findall(is_paragraph)] # type: ignore[attr-defined] |
| 111 |
assert len(new_nodes) == 1 |
| 112 |
assert original_uids[0] == uids[0] |
| 113 |
assert original_uids[1:] == uids[2:] |
| 114 |
|
| 115 |
|
| 116 |
def test_insert_beginning() -> None: |
| 117 |
insert_beginning = doctrees['insert_beginning'] |
| 118 |
new_nodes = list(merge_doctrees(original, insert_beginning, is_paragraph)) |
| 119 |
uids = [n.uid for n in insert_beginning.findall(is_paragraph)] # type: ignore[attr-defined] |
| 120 |
assert len(new_nodes) == 1 |
| 121 |
assert len(uids) == 4 |
| 122 |
assert original_uids == uids[1:] |
| 123 |
assert original_uids[0] != uids[0] |
| 124 |
|
| 125 |
|
| 126 |
def test_insert_similar() -> None: |
| 127 |
insert_similar = doctrees['insert_similar'] |
| 128 |
new_nodes = list(merge_doctrees(original, insert_similar, is_paragraph)) |
| 129 |
uids = [n.uid for n in insert_similar.findall(is_paragraph)] # type: ignore[attr-defined] |
| 130 |
assert len(new_nodes) == 1 |
| 131 |
assert new_nodes[0].rawsource == 'Anyway I need more' # type: ignore[attr-defined] |
| 132 |
assert original_uids[0] == uids[0] |
| 133 |
assert original_uids[1:] == uids[2:] |