Debian Codegraph @

test/test_dependencies.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_dependencies.py 9914 2024-08-21 13:45:08Z milde $
4 # Author: Lea Wiemann <[email protected]>
5 # Copyright: This module has been placed in the public domain.
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 # prepend the "docutils root" to the Python library path
19 # so we import the local `docutils` package.
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 # ./test/ from the docutils root
29 DATA_ROOT = TEST_ROOT / 'data'
30 CWD = Path(os.getcwd())
31
32
33 def relpath(path):
34 # docutils.utils.DependencyList records POSIX paths,
35 # i.e. "/" as a path separator even on Windows.
36 return os.path.relpath(path, CWD).replace('\\', '/')
37
38
39 paths = {
40 'include': relpath(DATA_ROOT / 'include.rst'), # included rst file
41 'raw': relpath(DATA_ROOT / 'raw.rst'), # included raw "HTML file"
42 'stylesheet': relpath(DATA_ROOT / 'stylesheet.rst'),
43 # the "image" and "figure" directives expect a URI and use it literally
44 'scaled-image': '../docs/user/rst/images/biohazard.png',
45 'figure-image': '../docs/user/rst/images/title.png',
46 }
47
48 # avoid latex writer future warnings:
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 # (Re) create the record file by running a conversion:
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(), # ignored
66 **kwargs)
67 recorder.close()
68 # Read the record file:
69 records = Path(recordfile).read_text(encoding='utf-8').splitlines()
70 return records, output
71
72 def test_dependencies_xml(self):
73 # Note: currently, raw input files are read (and hence recorded) while
74 # parsing even if not used in the chosen output format.
75 # This should change (see parsers/rst/directives/misc.py).
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 # the order of the files is arbitrary
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 # stylesheets are tested separately in test_stylesheet_dependencies():
90 settings = {'stylesheet_path': None,
91 'stylesheet': None,
92 'report_level': 4} # drop warning if PIL is missing
93 record, output = self.get_record(writer=html5_polyglot.Writer(),
94 settings_overrides=settings)
95 # the order of the files is arbitrary
96 self.assertEqual(sorted(expected), sorted(record),
97 msg='output is:\n'+output)
98
99 def test_dependencies_latex(self):
100 # since 0.9, the latex writer records only really accessed files, too.
101 # Note: currently, raw input files are read (and hence recorded) while
102 # parsing even if not used in the chosen output format.
103 # This should change (see parsers/rst/directives/misc.py).
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 # the order of the files is arbitrary
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()