SafeString._str__ must not return a binary string with Python 3.x.
[docutils.git] / test / test_dependencies.py
blob7fd971c852d39ea6367d5846c6753d014333ca5a
1 #! /usr/bin/env python
3 # $Id$
4 # Author: Lea Wiemann <LeWiemann@gmail.com>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Test module for the --record-dependencies option.
9 """
11 import os.path
12 import unittest
13 import sys
14 import DocutilsTestSupport # must be imported before docutils
15 import docutils.core
16 import docutils.utils
17 import docutils.io
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',
30 class RecordDependenciesTests(unittest.TestCase):
32 def get_record(self, **settings):
33 recordfile = 'record.txt'
34 recorder = docutils.utils.DependencyList(recordfile)
35 # (Re) create the record file by running a conversion:
36 settings.setdefault('source_path',
37 os.path.join('data', 'dependencies.txt'))
38 settings.setdefault('settings_overrides', {})
39 settings['settings_overrides'].update(_disable_config=True,
40 record_dependencies=recorder)
41 docutils.core.publish_file(destination=DocutilsTestSupport.DevNull(),
42 **settings)
43 recorder.close()
44 # Read the record file:
45 record = docutils.io.FileInput(source_path=recordfile,
46 encoding='utf8')
47 return record.read().splitlines()
49 def test_dependencies(self):
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']
54 if PIL:
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
59 record.sort()
60 expected.sort()
61 self.assertEqual(record, expected)
63 def test_dependencies_html(self):
64 keys = ['include', 'raw']
65 if PIL:
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
72 record.sort()
73 expected.sort()
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']
82 if PIL:
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
87 record.sort()
88 expected.sort()
89 self.assertEqual(record, expected)
91 def test_csv_dependencies(self):
92 try:
93 import csv
94 csvsource = os.path.join('data', 'csv_dep.txt')
95 self.assertEqual(self.get_record(source_path=csvsource),
96 ['data/csv_data.txt'])
97 except ImportError:
98 pass
100 def test_stylesheet_dependencies(self):
101 stylesheet = paths['stylesheet']
102 so = {'stylesheet_path': paths['stylesheet'],
103 'stylesheet': None}
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))
122 if __name__ == '__main__':
123 unittest.main()