| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Test module for the --record-dependencies option. |
| 9 |
""" |
| 10 |
|
| 11 |
from io import StringIO |
| 12 |
from pathlib import Path |
| 13 |
import os.path |
| 14 |
import sys |
| 15 |
import unittest |
| 16 |
|
| 17 |
if __name__ == '__main__': |
| 18 |
|
| 19 |
|
| 20 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 21 |
|
| 22 |
import docutils.core |
| 23 |
import docutils.utils |
| 24 |
import docutils.io |
| 25 |
from docutils.parsers.rst.directives.images import PIL |
| 26 |
from docutils.writers import html4css1, html5_polyglot, latex2e, docutils_xml |
| 27 |
|
| 28 |
TEST_ROOT = Path(__file__).parent |
| 29 |
DATA_ROOT = TEST_ROOT / 'data' |
| 30 |
CWD = Path(os.getcwd()) |
| 31 |
|
| 32 |
|
| 33 |
def relpath(path): |
| 34 |
|
| 35 |
|
| 36 |
return os.path.relpath(path, CWD).replace('\\', '/') |
| 37 |
|
| 38 |
|
| 39 |
paths = { |
| 40 |
'include': relpath(DATA_ROOT / 'include.rst'), |
| 41 |
'raw': relpath(DATA_ROOT / 'raw.rst'), |
| 42 |
'stylesheet': relpath(DATA_ROOT / 'stylesheet.rst'), |
| 43 |
|
| 44 |
'scaled-image': '../docs/user/rst/images/biohazard.png', |
| 45 |
'figure-image': '../docs/user/rst/images/title.png', |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
latex_settings_overwrites = {'legacy_column_widths': False, |
| 50 |
'use_latex_citations': True} |
| 51 |
|
| 52 |
|
| 53 |
class RecordDependenciesTests(unittest.TestCase): |
| 54 |
|
| 55 |
maxDiff = None |
| 56 |
|
| 57 |
def get_record(self, **kwargs): |
| 58 |
recordfile = 'record.rst' |
| 59 |
recorder = docutils.utils.DependencyList(recordfile) |
| 60 |
|
| 61 |
kwargs.setdefault('source_path', str(DATA_ROOT/'dependencies.rst')) |
| 62 |
kwargs.setdefault('settings_overrides', {}) |
| 63 |
kwargs['settings_overrides'].update(_disable_config=True, |
| 64 |
record_dependencies=recorder) |
| 65 |
output = docutils.core.publish_file(destination=StringIO(), |
| 66 |
**kwargs) |
| 67 |
recorder.close() |
| 68 |
|
| 69 |
records = Path(recordfile).read_text(encoding='utf-8').splitlines() |
| 70 |
return records, output |
| 71 |
|
| 72 |
def test_dependencies_xml(self): |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
keys = ['include', 'raw'] |
| 77 |
if PIL and os.path.exists('../docs/user/rst/images/'): |
| 78 |
keys += ['figure-image'] |
| 79 |
expected = [paths[key] for key in keys] |
| 80 |
record, _output = self.get_record(writer=docutils_xml.Writer()) |
| 81 |
|
| 82 |
self.assertEqual(sorted(expected), sorted(record)) |
| 83 |
|
| 84 |
def test_dependencies_html(self): |
| 85 |
keys = ['include', 'raw'] |
| 86 |
if PIL and os.path.exists('../docs/user/rst/images/'): |
| 87 |
keys += ['figure-image', 'scaled-image'] |
| 88 |
expected = [paths[key] for key in keys] |
| 89 |
|
| 90 |
settings = {'stylesheet_path': None, |
| 91 |
'stylesheet': None, |
| 92 |
'report_level': 4} |
| 93 |
record, output = self.get_record(writer=html5_polyglot.Writer(), |
| 94 |
settings_overrides=settings) |
| 95 |
|
| 96 |
self.assertEqual(sorted(expected), sorted(record), |
| 97 |
msg='output is:\n'+output) |
| 98 |
|
| 99 |
def test_dependencies_latex(self): |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
keys = ['include', 'raw'] |
| 105 |
if PIL and os.path.exists('../docs/user/rst/images/'): |
| 106 |
keys += ['figure-image'] |
| 107 |
expected = [paths[key] for key in keys] |
| 108 |
record, output = self.get_record( |
| 109 |
writer=latex2e.Writer(), |
| 110 |
settings_overrides=latex_settings_overwrites) |
| 111 |
|
| 112 |
self.assertEqual(sorted(expected), sorted(record), |
| 113 |
msg='output is:\n'+output) |
| 114 |
|
| 115 |
def test_csv_dependencies(self): |
| 116 |
csvsource = str(DATA_ROOT / 'csv_dep.rst') |
| 117 |
record, output = self.get_record(source_path=csvsource) |
| 118 |
self.assertEqual([relpath(DATA_ROOT / 'csv_data.txt')], record, |
| 119 |
msg='output is:\n'+output) |
| 120 |
|
| 121 |
def test_stylesheet_dependencies(self): |
| 122 |
stylesheet = paths['stylesheet'] |
| 123 |
settings = {'stylesheet_path': paths['stylesheet'], |
| 124 |
'stylesheet': None} |
| 125 |
settings.update(latex_settings_overwrites) |
| 126 |
settings['embed_stylesheet'] = False |
| 127 |
record, _output = self.get_record(writer=html4css1.Writer(), |
| 128 |
settings_overrides=settings) |
| 129 |
self.assertTrue(stylesheet not in record, |
| 130 |
f'{stylesheet!r} should not be in {record!r}') |
| 131 |
record, _output = self.get_record(writer=latex2e.Writer(), |
| 132 |
settings_overrides=settings) |
| 133 |
self.assertTrue(stylesheet not in record, |
| 134 |
f'{stylesheet!r} should not be in {record!r}') |
| 135 |
|
| 136 |
settings['embed_stylesheet'] = True |
| 137 |
record, _output = self.get_record(writer=html4css1.Writer(), |
| 138 |
settings_overrides=settings) |
| 139 |
self.assertTrue(stylesheet in record, |
| 140 |
f'{stylesheet!r} should be in {record!r}') |
| 141 |
settings['embed_stylesheet'] = True |
| 142 |
record, _output = self.get_record(writer=latex2e.Writer(), |
| 143 |
settings_overrides=settings) |
| 144 |
self.assertTrue(stylesheet in record, |
| 145 |
f'{stylesheet!r} should be in {record!r}') |
| 146 |
|
| 147 |
def tearDown(self) -> None: |
| 148 |
os.unlink("record.rst") |
| 149 |
|
| 150 |
|
| 151 |
if __name__ == '__main__': |
| 152 |
unittest.main() |