python 2.3 keyword dict method update does not allow keyword args
[docutils/kirr.git] / docutils / test / test_dependencies.py
blob6ae933640af8553fd61e5ed06beb3e3e337ccef0
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',
27 'default-stylesheet': u'../docutils/writers/html4css1/html4css1.css',
31 class RecordDependenciesTests(unittest.TestCase):
33 # python 2.3
34 if not hasattr(unittest.TestCase, "assertTrue"):
35 assertTrue = unittest.TestCase.failUnless
37 def get_record(self, **settings):
38 recordfile = 'record.txt'
39 recorder = docutils.utils.DependencyList(recordfile)
40 # (Re) create the record file by running a conversion:
41 settings.setdefault('source_path',
42 os.path.join('data', 'dependencies.txt'))
43 settings.setdefault('settings_overrides', {})
44 if sys.version_info < (2, 4):
45 # python 2.3 update() takes no keyword arguments
46 settings['settings_overrides'].update({"_disable_config":True,
47 "record_dependencies":recorder})
48 else:
49 settings['settings_overrides'].update(_disable_config=True,
50 record_dependencies=recorder)
51 docutils.core.publish_file(destination=DocutilsTestSupport.DevNull(),
52 **settings)
53 recorder.close()
54 # Read the record file:
55 record = docutils.io.FileInput(source_path=recordfile,
56 encoding='utf8')
57 return record.read().splitlines()
59 def test_dependencies(self):
60 # Note: currently, raw input files are read (and hence recorded) while
61 # parsing even if not used in the chosen output format.
62 # This should change (see parsers/rst/directives/misc.py).
63 keys = ['include', 'raw']
64 if PIL:
65 keys += ['figure-image']
66 expected = [paths[key] for key in keys]
67 record = self.get_record(writer_name='xml')
68 # the order of the files is arbitrary
69 record.sort()
70 expected.sort()
71 self.assertEqual(record, expected)
73 def test_dependencies_html(self):
74 keys = ['include', 'raw', 'default-stylesheet']
75 if PIL:
76 keys += ['figure-image', 'scaled-image']
77 expected = [paths[key] for key in keys]
78 record = self.get_record(writer_name='html')
79 # the order of the files is arbitrary
80 record.sort()
81 expected.sort()
82 self.assertEqual(record, expected)
84 def test_dependencies_latex(self):
85 # since 0.9, the latex writer records only really accessed files, too
86 # Note: currently, raw input files are read (and hence recorded) while
87 # parsing even if not used in the chosen output format.
88 # This should change (see parsers/rst/directives/misc.py).
89 keys = ['include', 'raw']
90 if PIL:
91 keys += ['figure-image']
92 expected = [paths[key] for key in keys]
93 record = self.get_record(writer_name='latex')
94 # the order of the files is arbitrary
95 record.sort()
96 expected.sort()
97 self.assertEqual(record, expected)
99 def test_csv_dependencies(self):
100 try:
101 import csv
102 csvsource = os.path.join('data', 'csv_dep.txt')
103 self.assertEqual(self.get_record(source_path=csvsource),
104 ['data/csv_data.txt'])
105 except ImportError:
106 pass
108 def test_stylesheet_dependencies(self):
109 stylesheet = paths['stylesheet']
110 so = {'stylesheet_path': paths['stylesheet'],
111 'stylesheet': None}
113 so['embed_stylesheet'] = False
114 record = self.get_record(writer_name='html', settings_overrides=so)
115 self.assertTrue(stylesheet not in record,
116 '%r should not be in %r' % (stylesheet, record))
117 record = self.get_record(writer_name='latex', settings_overrides=so)
118 self.assertTrue(stylesheet not in record,
119 '%r should not be in %r' % (stylesheet, record))
121 so['embed_stylesheet'] = True
122 record = self.get_record(writer_name='html', settings_overrides=so)
123 self.assertTrue(stylesheet in record,
124 '%r should be in %r' % (stylesheet, record))
125 record = self.get_record(writer_name='latex', settings_overrides=so)
126 self.assertTrue(stylesheet in record,
127 '%r should be in %r' % (stylesheet, record))
130 if __name__ == '__main__':
131 unittest.main()