~ubuntu-branches/ubuntu/saucy/python-docutils/saucy-proposed

« back to all changes in this revision

Viewing changes to test/test_dependencies.py

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev
  • Date: 2012-10-19 18:23:15 UTC
  • mfrom: (1.2.1) (11.1.6 experimental)
  • Revision ID: package-import@ubuntu.com-20121019182315-ln3lvct1pqq7mzgm
Tags: 0.9.1+svn7532-0ubuntu1
* Resynchronize with Debian packaging SVN, remaining change:
  - Use dh_python2 instead of dh_pysupport.
* New snapshot from upstream SVN, fixes build with Python 3.3.
* Add XS-Testsuite header.
* debian/patches/move-data-to-usr-share.diff: unfuzz.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/env python
2
2
 
3
 
# $Id: test_dependencies.py 5720 2008-10-31 17:50:17Z goodger $
 
3
# $Id$
4
4
# Author: Lea Wiemann <LeWiemann@gmail.com>
5
5
# Copyright: This module has been placed in the public domain.
6
6
 
14
14
import DocutilsTestSupport              # must be imported before docutils
15
15
import docutils.core
16
16
import docutils.utils
 
17
import docutils.io
 
18
from docutils.parsers.rst.directives.images import PIL
 
19
 
 
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
        }
17
28
 
18
29
 
19
30
class RecordDependenciesTests(unittest.TestCase):
20
31
 
21
 
    # docutils.utils.DependencyList records relative URLs, not platform paths,
22
 
    # so use "/" as a path separator even on Windows (not os.path.join).
23
 
 
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(),
35
42
                                   **settings)
36
 
        settings['settings_overrides']['record_dependencies'].close()
37
 
        return open(recordfile).read().splitlines()
 
43
        recorder.close()
 
44
        # Read the record file:
 
45
        record = docutils.io.FileInput(source_path=recordfile,
 
46
                                       encoding='utf8')
 
47
        return record.read().splitlines()
38
48
 
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'),
43
 
                         ['data/include.txt',
44
 
                          'data/raw.txt',
45
 
                          # this is a URL, not a path:
46
 
                          'some_image.png'])
 
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)
 
62
 
 
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)
 
75
 
 
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)
47
90
 
48
91
    def test_csv_dependencies(self):
49
92
        try:
50
93
            import csv
51
 
            self.assertEqual(
52
 
                self.get_record(source_path=os.path.join('data',
53
 
                                                         'csv_dep.txt')),
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:
56
98
            pass
57
99
 
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'],
 
103
              'stylesheet': None}
 
104
 
 
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))
 
112
 
 
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))
84
120
 
85
121
 
86
122
if __name__ == '__main__':