• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests for distutils.command.check."""
2import os
3import textwrap
4import unittest
5from test.support import run_unittest
6
7from distutils.command.check import check, HAS_DOCUTILS
8from distutils.tests import support
9from distutils.errors import DistutilsSetupError
10
11try:
12    import pygments
13except ImportError:
14    pygments = None
15
16
17HERE = os.path.dirname(__file__)
18
19
20class CheckTestCase(support.LoggingSilencer,
21                    support.TempdirManager,
22                    unittest.TestCase):
23
24    def _run(self, metadata=None, cwd=None, **options):
25        if metadata is None:
26            metadata = {}
27        if cwd is not None:
28            old_dir = os.getcwd()
29            os.chdir(cwd)
30        pkg_info, dist = self.create_dist(**metadata)
31        cmd = check(dist)
32        cmd.initialize_options()
33        for name, value in options.items():
34            setattr(cmd, name, value)
35        cmd.ensure_finalized()
36        cmd.run()
37        if cwd is not None:
38            os.chdir(old_dir)
39        return cmd
40
41    def test_check_metadata(self):
42        # let's run the command with no metadata at all
43        # by default, check is checking the metadata
44        # should have some warnings
45        cmd = self._run()
46        self.assertEqual(cmd._warnings, 2)
47
48        # now let's add the required fields
49        # and run it again, to make sure we don't get
50        # any warning anymore
51        metadata = {'url': 'xxx', 'author': 'xxx',
52                    'author_email': 'xxx',
53                    'name': 'xxx', 'version': 'xxx'}
54        cmd = self._run(metadata)
55        self.assertEqual(cmd._warnings, 0)
56
57        # now with the strict mode, we should
58        # get an error if there are missing metadata
59        self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1})
60
61        # and of course, no error when all metadata are present
62        cmd = self._run(metadata, strict=1)
63        self.assertEqual(cmd._warnings, 0)
64
65        # now a test with non-ASCII characters
66        metadata = {'url': 'xxx', 'author': '\u00c9ric',
67                    'author_email': 'xxx', 'name': 'xxx',
68                    'version': 'xxx',
69                    'description': 'Something about esszet \u00df',
70                    'long_description': 'More things about esszet \u00df'}
71        cmd = self._run(metadata)
72        self.assertEqual(cmd._warnings, 0)
73
74    @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
75    def test_check_document(self):
76        pkg_info, dist = self.create_dist()
77        cmd = check(dist)
78
79        # let's see if it detects broken rest
80        broken_rest = 'title\n===\n\ntest'
81        msgs = cmd._check_rst_data(broken_rest)
82        self.assertEqual(len(msgs), 1)
83
84        # and non-broken rest
85        rest = 'title\n=====\n\ntest'
86        msgs = cmd._check_rst_data(rest)
87        self.assertEqual(len(msgs), 0)
88
89    @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
90    def test_check_restructuredtext(self):
91        # let's see if it detects broken rest in long_description
92        broken_rest = 'title\n===\n\ntest'
93        pkg_info, dist = self.create_dist(long_description=broken_rest)
94        cmd = check(dist)
95        cmd.check_restructuredtext()
96        self.assertEqual(cmd._warnings, 1)
97
98        # let's see if we have an error with strict=1
99        metadata = {'url': 'xxx', 'author': 'xxx',
100                    'author_email': 'xxx',
101                    'name': 'xxx', 'version': 'xxx',
102                    'long_description': broken_rest}
103        self.assertRaises(DistutilsSetupError, self._run, metadata,
104                          **{'strict': 1, 'restructuredtext': 1})
105
106        # and non-broken rest, including a non-ASCII character to test #12114
107        metadata['long_description'] = 'title\n=====\n\ntest \u00df'
108        cmd = self._run(metadata, strict=1, restructuredtext=1)
109        self.assertEqual(cmd._warnings, 0)
110
111        # check that includes work to test #31292
112        metadata['long_description'] = 'title\n=====\n\n.. include:: includetest.rst'
113        cmd = self._run(metadata, cwd=HERE, strict=1, restructuredtext=1)
114        self.assertEqual(cmd._warnings, 0)
115
116    @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
117    def test_check_restructuredtext_with_syntax_highlight(self):
118        # Don't fail if there is a `code` or `code-block` directive
119
120        example_rst_docs = []
121        example_rst_docs.append(textwrap.dedent("""\
122            Here's some code:
123
124            .. code:: python
125
126                def foo():
127                    pass
128            """))
129        example_rst_docs.append(textwrap.dedent("""\
130            Here's some code:
131
132            .. code-block:: python
133
134                def foo():
135                    pass
136            """))
137
138        for rest_with_code in example_rst_docs:
139            pkg_info, dist = self.create_dist(long_description=rest_with_code)
140            cmd = check(dist)
141            cmd.check_restructuredtext()
142            msgs = cmd._check_rst_data(rest_with_code)
143            if pygments is not None:
144                self.assertEqual(len(msgs), 0)
145            else:
146                self.assertEqual(len(msgs), 1)
147                self.assertEqual(
148                    str(msgs[0][1]),
149                    'Cannot analyze code. Pygments package not found.'
150                )
151
152    def test_check_all(self):
153
154        metadata = {'url': 'xxx', 'author': 'xxx'}
155        self.assertRaises(DistutilsSetupError, self._run,
156                          {}, **{'strict': 1,
157                                 'restructuredtext': 1})
158
159def test_suite():
160    return unittest.makeSuite(CheckTestCase)
161
162if __name__ == "__main__":
163    run_unittest(test_suite())
164