14
14
import DocutilsTestSupport # must be imported before docutils
15
15
import docutils.core
16
16
import docutils.utils
18
from docutils.parsers.rst.directives.images import PIL
20
# docutils.utils.DependencyList records POSIX paths,
21
# i.e. "/" as a path separator even on Windows (not os.path.join).
22
paths = {'include': u'data/include.txt', # included rst file
23
'raw': u'data/raw.txt', # included raw "HTML file"
24
'scaled-image': u'../docs/user/rst/images/biohazard.png',
25
'figure-image': u'../docs/user/rst/images/title.png',
26
'stylesheet': u'data/stylesheet.txt',
19
30
class RecordDependenciesTests(unittest.TestCase):
21
# docutils.utils.DependencyList records relative URLs, not platform paths,
22
# so use "/" as a path separator even on Windows (not os.path.join).
24
32
def get_record(self, **settings):
25
33
recordfile = 'record.txt'
34
recorder = docutils.utils.DependencyList(recordfile)
35
# (Re) create the record file by running a conversion:
26
36
settings.setdefault('source_path',
27
37
os.path.join('data', 'dependencies.txt'))
28
38
settings.setdefault('settings_overrides', {})
29
settings['settings_overrides'] = settings['settings_overrides'].copy()
30
settings['settings_overrides']['_disable_config'] = 1
31
if 'record_dependencies' not in settings['settings_overrides']:
32
settings['settings_overrides']['record_dependencies'] = \
33
docutils.utils.DependencyList(recordfile)
39
settings['settings_overrides'].update(_disable_config=True,
40
record_dependencies=recorder)
34
41
docutils.core.publish_file(destination=DocutilsTestSupport.DevNull(),
36
settings['settings_overrides']['record_dependencies'].close()
37
return open(recordfile).read().splitlines()
44
# Read the record file:
45
record = docutils.io.FileInput(source_path=recordfile,
47
return record.read().splitlines()
39
49
def test_dependencies(self):
40
self.assertEqual(self.get_record(),
41
['data/include.txt', 'data/raw.txt'])
42
self.assertEqual(self.get_record(writer_name='latex'),
45
# this is a URL, not a path:
50
# Note: currently, raw input files are read (and hence recorded) while
51
# parsing even if not used in the chosen output format.
52
# This should change (see parsers/rst/directives/misc.py).
53
keys = ['include', 'raw']
55
keys += ['figure-image']
56
expected = [paths[key] for key in keys]
57
record = self.get_record(writer_name='xml')
58
# the order of the files is arbitrary
61
self.assertEqual(record, expected)
63
def test_dependencies_html(self):
64
keys = ['include', 'raw']
66
keys += ['figure-image', 'scaled-image']
67
expected = [paths[key] for key in keys]
68
# stylesheets are tested separately in test_stylesheet_dependencies():
69
so = {'stylesheet_path': None, 'stylesheet': None}
70
record = self.get_record(writer_name='html', settings_overrides=so)
71
# the order of the files is arbitrary
74
self.assertEqual(record, expected)
76
def test_dependencies_latex(self):
77
# since 0.9, the latex writer records only really accessed files, too
78
# Note: currently, raw input files are read (and hence recorded) while
79
# parsing even if not used in the chosen output format.
80
# This should change (see parsers/rst/directives/misc.py).
81
keys = ['include', 'raw']
83
keys += ['figure-image']
84
expected = [paths[key] for key in keys]
85
record = self.get_record(writer_name='latex')
86
# the order of the files is arbitrary
89
self.assertEqual(record, expected)
48
91
def test_csv_dependencies(self):
52
self.get_record(source_path=os.path.join('data',
54
['data/csv_data.txt'])
94
csvsource = os.path.join('data', 'csv_dep.txt')
95
self.assertEqual(self.get_record(source_path=csvsource),
96
['data/csv_data.txt'])
55
97
except ImportError:
58
100
def test_stylesheet_dependencies(self):
59
# Parameters to publish_file.
60
s = {'settings_overrides': {}}
61
so = s['settings_overrides']
62
so['embed_stylesheet'] = 0
63
# must use '/', not os.sep or os.path.join, because of URL handling
64
# (see docutils.utils.relative_path):
65
stylesheet_path = 'data/stylesheet.txt'
66
so['stylesheet_path'] = stylesheet_path
67
so['stylesheet'] = None
68
s['writer_name'] = 'html'
69
record = self.get_record(**s)
70
self.assert_(stylesheet_path not in record,
71
'%r should not be in %r' % (stylesheet_path, record))
72
so['embed_stylesheet'] = 1
73
record = self.get_record(**s)
74
self.assert_(stylesheet_path in record,
75
'%r should be in %r' % (stylesheet_path, record))
76
s['writer_name'] = 'latex'
77
record = self.get_record(**s)
78
self.assert_(stylesheet_path in record,
79
'%r should be in %r' % (stylesheet_path, record))
80
del so['embed_stylesheet']
81
record = self.get_record(**s)
82
self.assert_(stylesheet_path not in record,
83
'%r should not be in %r' % (stylesheet_path, record))
101
stylesheet = paths['stylesheet']
102
so = {'stylesheet_path': paths['stylesheet'],
105
so['embed_stylesheet'] = False
106
record = self.get_record(writer_name='html', settings_overrides=so)
107
self.assertTrue(stylesheet not in record,
108
'%r should not be in %r' % (stylesheet, record))
109
record = self.get_record(writer_name='latex', settings_overrides=so)
110
self.assertTrue(stylesheet not in record,
111
'%r should not be in %r' % (stylesheet, record))
113
so['embed_stylesheet'] = True
114
record = self.get_record(writer_name='html', settings_overrides=so)
115
self.assertTrue(stylesheet in record,
116
'%r should be in %r' % (stylesheet, record))
117
record = self.get_record(writer_name='latex', settings_overrides=so)
118
self.assertTrue(stylesheet in record,
119
'%r should be in %r' % (stylesheet, record))
86
122
if __name__ == '__main__':