1 #! /usr/bin/env python3
4 # Author: Lea Wiemann <LeWiemann@gmail.com>
5 # Copyright: This module has been placed in the public domain.
8 Test module for the --record-dependencies option.
11 from io
import StringIO
12 from pathlib
import Path
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]))
25 from docutils
.parsers
.rst
.directives
.images
import PIL
27 TEST_ROOT
= Path(__file__
).parent
# ./test/ from the docutils root
28 DATA_ROOT
= TEST_ROOT
/ 'data'
29 CWD
= Path(os
.getcwd())
33 # docutils.utils.DependencyList records POSIX paths,
34 # i.e. "/" as a path separator even on Windows.
35 return os
.path
.relpath(path
, CWD
).replace('\\', '/')
39 'include': relpath(DATA_ROOT
/ 'include.txt'), # included rst file
40 'raw': relpath(DATA_ROOT
/ 'raw.txt'), # included raw "HTML file"
41 'stylesheet': relpath(DATA_ROOT
/ 'stylesheet.txt'),
42 # the "image" and "figure" directives expect a URI and use it literally
43 'scaled-image': '../docs/user/rst/images/biohazard.png',
44 'figure-image': '../docs/user/rst/images/title.png',
47 # avoid latex writer future warnings:
48 latex_settings_overwrites
= {'legacy_column_widths': False,
49 'use_latex_citations': True}
52 class RecordDependenciesTests(unittest
.TestCase
):
56 def get_record(self
, **kwargs
):
57 recordfile
= 'record.txt'
58 recorder
= docutils
.utils
.DependencyList(recordfile
)
59 # (Re) create the record file by running a conversion:
60 kwargs
.setdefault('source_path', str(DATA_ROOT
/'dependencies.txt'))
61 kwargs
.setdefault('settings_overrides', {})
62 kwargs
['settings_overrides'].update(_disable_config
=True,
63 record_dependencies
=recorder
)
64 output
= docutils
.core
.publish_file(destination
=StringIO(), # ignored
67 # Read the record file:
68 with
open(recordfile
, encoding
='utf-8') as record
:
69 return record
.read().splitlines(), output
71 def test_dependencies_xml(self
):
72 # Note: currently, raw input files are read (and hence recorded) while
73 # parsing even if not used in the chosen output format.
74 # This should change (see parsers/rst/directives/misc.py).
75 keys
= ['include', 'raw']
76 if PIL
and TEST_ROOT
== CWD
:
77 keys
+= ['figure-image']
78 expected
= [paths
[key
] for key
in keys
]
79 record
, output
= self
.get_record(writer_name
='xml')
80 # the order of the files is arbitrary
81 self
.assertEqual(sorted(expected
), sorted(record
))
83 def test_dependencies_html(self
):
84 keys
= ['include', 'raw']
85 if PIL
and (TEST_ROOT
== CWD
):
86 keys
+= ['figure-image', 'scaled-image']
87 expected
= [paths
[key
] for key
in keys
]
88 # stylesheets are tested separately in test_stylesheet_dependencies():
89 settings
= {'stylesheet_path': None,
91 'report_level': 4} # drop warning if PIL is missing
92 record
, output
= self
.get_record(writer_name
='html5',
93 settings_overrides
=settings
)
94 # the order of the files is arbitrary
95 self
.assertEqual(sorted(expected
), sorted(record
),
96 msg
='output is:\n'+output
)
98 def test_dependencies_latex(self
):
99 # since 0.9, the latex writer records only really accessed files, too.
100 # Note: currently, raw input files are read (and hence recorded) while
101 # parsing even if not used in the chosen output format.
102 # This should change (see parsers/rst/directives/misc.py).
103 keys
= ['include', 'raw']
104 if PIL
and TEST_ROOT
== CWD
:
105 keys
+= ['figure-image']
106 expected
= [paths
[key
] for key
in keys
]
107 record
, output
= self
.get_record(
109 settings_overrides
=latex_settings_overwrites
)
110 # the order of the files is arbitrary
111 self
.assertEqual(sorted(expected
), sorted(record
),
112 msg
='output is:\n'+output
)
114 def test_csv_dependencies(self
):
115 csvsource
= str(DATA_ROOT
/ 'csv_dep.txt')
116 record
, output
= self
.get_record(source_path
=csvsource
)
117 self
.assertEqual([relpath(DATA_ROOT
/ 'csv_data.txt')], record
,
118 msg
='output is:\n'+output
)
120 def test_stylesheet_dependencies(self
):
121 stylesheet
= paths
['stylesheet']
122 settings
= {'stylesheet_path': paths
['stylesheet'],
124 settings
.update(latex_settings_overwrites
)
125 settings
['embed_stylesheet'] = False
126 record
, output
= self
.get_record(writer_name
='html',
127 settings_overrides
=settings
)
128 self
.assertTrue(stylesheet
not in record
,
129 f
'{stylesheet!r} should not be in {record!r}')
130 record
, output
= self
.get_record(writer_name
='latex',
131 settings_overrides
=settings
)
132 self
.assertTrue(stylesheet
not in record
,
133 f
'{stylesheet!r} should not be in {record!r}')
135 settings
['embed_stylesheet'] = True
136 record
, output
= self
.get_record(writer_name
='html',
137 settings_overrides
=settings
)
138 self
.assertTrue(stylesheet
in record
,
139 f
'{stylesheet!r} should be in {record!r}')
140 settings
['embed_stylesheet'] = True
141 record
, output
= self
.get_record(writer_name
='latex',
142 settings_overrides
=settings
)
143 self
.assertTrue(stylesheet
in record
,
144 f
'{stylesheet!r} should be in {record!r}')
146 def tearDown(self
) -> None:
147 os
.unlink("record.txt")
150 if __name__
== '__main__':