ยปCore Development>Code coverage>Lib/distutils/tests/test_check.py

Python code coverage for Lib/distutils/tests/test_check.py

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