Report table parsing errors with correct line number.
[docutils.git] / test / test_dependencies.py
blob611fba85c66891165747bc1f62e18fbcdd10b7e6
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 def get_record(self, **settings):
34 recordfile = 'record.txt'
35 recorder = docutils.utils.DependencyList(recordfile)
36 # (Re) create the record file by running a conversion:
37 settings.setdefault('source_path',
38 os.path.join('data', 'dependencies.txt'))
39 settings.setdefault('settings_overrides', {})
40 settings['settings_overrides'].update(_disable_config=True,
41 record_dependencies=recorder)
42 docutils.core.publish_file(destination=DocutilsTestSupport.DevNull(),
43 **settings)
44 recorder.close()
45 # Read the record file:
46 record = docutils.io.FileInput(source_path=recordfile,
47 encoding='utf8')
48 return record.read().splitlines()
50 def test_dependencies(self):
51 # Note: currently, raw input files are read (and hence recorded) while
52 # parsing even if not used in the chosen output format.
53 # This should change (see parsers/rst/directives/misc.py).
54 keys = ['include', 'raw']
55 if PIL:
56 keys += ['figure-image']
57 expected = [paths[key] for key in keys]
58 record = self.get_record(writer_name='xml')
59 # the order of the files is arbitrary
60 record.sort()
61 expected.sort()
62 self.assertEqual(record, expected)
64 def test_dependencies_html(self):
65 keys = ['include', 'raw', 'default-stylesheet']
66 if PIL:
67 keys += ['figure-image', 'scaled-image']
68 expected = [paths[key] for key in keys]
69 record = self.get_record(writer_name='html')
70 # the order of the files is arbitrary
71 record.sort()
72 expected.sort()
73 self.assertEqual(record, expected)
75 def test_dependencies_latex(self):
76 # since 0.9, the latex writer records only really accessed files, too
77 # Note: currently, raw input files are read (and hence recorded) while
78 # parsing even if not used in the chosen output format.
79 # This should change (see parsers/rst/directives/misc.py).
80 keys = ['include', 'raw']
81 if PIL:
82 keys += ['figure-image']
83 expected = [paths[key] for key in keys]
84 record = self.get_record(writer_name='latex')
85 # the order of the files is arbitrary
86 record.sort()
87 expected.sort()
88 self.assertEqual(record, expected)
90 def test_csv_dependencies(self):
91 try:
92 import csv
93 csvsource = os.path.join('data', 'csv_dep.txt')
94 self.assertEqual(self.get_record(source_path=csvsource),
95 ['data/csv_data.txt'])
96 except ImportError:
97 pass
99 def test_stylesheet_dependencies(self):
100 stylesheet = paths['stylesheet']
101 so = {'stylesheet_path': paths['stylesheet'],
102 'stylesheet': None}
104 so['embed_stylesheet'] = False
105 record = self.get_record(writer_name='html', settings_overrides=so)
106 self.assert_(stylesheet not in record,
107 '%r should not be in %r' % (stylesheet, record))
108 record = self.get_record(writer_name='latex', settings_overrides=so)
109 self.assert_(stylesheet not in record,
110 '%r should not be in %r' % (stylesheet, record))
112 so['embed_stylesheet'] = True
113 record = self.get_record(writer_name='html', settings_overrides=so)
114 self.assert_(stylesheet in record,
115 '%r should be in %r' % (stylesheet, record))
116 record = self.get_record(writer_name='latex', settings_overrides=so)
117 self.assert_(stylesheet in record,
118 '%r should be in %r' % (stylesheet, record))
121 if __name__ == '__main__':
122 unittest.main()