summaryrefslogtreecommitdiff
path: root/tests/test_util_docutils.py
blob: 6b9dd8a0c3d94eab86d57662ee7ad117dcbb00f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
    test_util_docutils
    ~~~~~~~~~~~~~~~~~~

    Tests util.utils functions.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import os

from docutils import nodes

from sphinx.util.docutils import SphinxFileOutput, docutils_namespace, register_node


def test_register_node():
    class custom_node(nodes.Element):
        pass

    with docutils_namespace():
        register_node(custom_node)

        # check registered
        assert hasattr(nodes.GenericNodeVisitor, 'visit_custom_node')
        assert hasattr(nodes.GenericNodeVisitor, 'depart_custom_node')
        assert hasattr(nodes.SparseNodeVisitor, 'visit_custom_node')
        assert hasattr(nodes.SparseNodeVisitor, 'depart_custom_node')

    # check unregistered outside namespace
    assert not hasattr(nodes.GenericNodeVisitor, 'visit_custom_node')
    assert not hasattr(nodes.GenericNodeVisitor, 'depart_custom_node')
    assert not hasattr(nodes.SparseNodeVisitor, 'visit_custom_node')
    assert not hasattr(nodes.SparseNodeVisitor, 'depart_custom_node')


def test_SphinxFileOutput(tmpdir):
    content = 'Hello Sphinx World'

    # write test.txt at first
    filename = str(tmpdir / 'test.txt')
    output = SphinxFileOutput(destination_path=filename)
    output.write(content)
    os.utime(filename, (0, 0))

    # overrite it again
    output.write(content)
    assert os.stat(filename).st_mtime != 0  # updated

    # write test2.txt at first
    filename = str(tmpdir / 'test2.txt')
    output = SphinxFileOutput(destination_path=filename, overwrite_if_changed=True)
    output.write(content)
    os.utime(filename, (0, 0))

    # overrite it again
    output.write(content)
    assert os.stat(filename).st_mtime == 0  # not updated

    # overrite it again (content changed)
    output.write(content + "; content change")
    assert os.stat(filename).st_mtime != 0  # updated