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