ยปCore Development>Code coverage>Lib/packaging/tests/test_command_check.py

Python code coverage for Lib/packaging/tests/test_command_check.py

#countcontent
1n/a"""Tests for distutils.command.check."""
2n/a
3n/afrom packaging.command.check import check
4n/afrom packaging.metadata import _HAS_DOCUTILS
5n/afrom packaging.errors import PackagingSetupError, MetadataMissingError
6n/afrom packaging.tests import unittest, support
7n/a
8n/a
9n/aclass CheckTestCase(support.LoggingCatcher,
10n/a support.TempdirManager,
11n/a unittest.TestCase):
12n/a
13n/a def _run(self, metadata=None, **options):
14n/a if metadata is None:
15n/a metadata = {'name': 'xxx', 'version': '1.2'}
16n/a pkg_info, dist = self.create_dist(**metadata)
17n/a cmd = check(dist)
18n/a cmd.initialize_options()
19n/a for name, value in options.items():
20n/a setattr(cmd, name, value)
21n/a cmd.ensure_finalized()
22n/a cmd.run()
23n/a return cmd
24n/a
25n/a def test_check_metadata(self):
26n/a # let's run the command with no metadata at all
27n/a # by default, check is checking the metadata
28n/a # should have some warnings
29n/a self._run()
30n/a # trick: using assertNotEqual with an empty list will give us a more
31n/a # useful error message than assertGreater(.., 0) when the code change
32n/a # and the test fails
33n/a self.assertNotEqual(self.get_logs(), [])
34n/a
35n/a # now let's add the required fields
36n/a # and run it again, to make sure we don't get
37n/a # any warning anymore
38n/a metadata = {'home_page': 'xxx', 'author': 'xxx',
39n/a 'author_email': 'xxx',
40n/a 'name': 'xxx', 'version': '4.2',
41n/a }
42n/a self._run(metadata)
43n/a self.assertEqual(self.get_logs(), [])
44n/a
45n/a # now with the strict mode, we should
46n/a # get an error if there are missing metadata
47n/a self.assertRaises(MetadataMissingError, self._run, {}, **{'strict': 1})
48n/a self.assertRaises(PackagingSetupError, self._run,
49n/a {'name': 'xxx', 'version': 'xxx'}, **{'strict': 1})
50n/a
51n/a # clear warnings from the previous calls
52n/a self.loghandler.flush()
53n/a
54n/a # and of course, no error when all metadata fields are present
55n/a self._run(metadata, strict=True)
56n/a self.assertEqual(self.get_logs(), [])
57n/a
58n/a # now a test with non-ASCII characters
59n/a metadata = {'home_page': 'xxx', 'author': '\u00c9ric',
60n/a 'author_email': 'xxx', 'name': 'xxx',
61n/a 'version': '1.2',
62n/a 'summary': 'Something about esszet \u00df',
63n/a 'description': 'More things about esszet \u00df'}
64n/a self._run(metadata)
65n/a self.assertEqual(self.get_logs(), [])
66n/a
67n/a def test_check_metadata_1_2(self):
68n/a # let's run the command with no metadata at all
69n/a # by default, check is checking the metadata
70n/a # should have some warnings
71n/a self._run()
72n/a self.assertNotEqual(self.get_logs(), [])
73n/a
74n/a # now let's add the required fields and run it again, to make sure we
75n/a # don't get any warning anymore let's use requires_python as a marker
76n/a # to enforce Metadata-Version 1.2
77n/a metadata = {'home_page': 'xxx', 'author': 'xxx',
78n/a 'author_email': 'xxx',
79n/a 'name': 'xxx', 'version': '4.2',
80n/a 'requires_python': '2.4',
81n/a }
82n/a self._run(metadata)
83n/a self.assertEqual(self.get_logs(), [])
84n/a
85n/a # now with the strict mode, we should
86n/a # get an error if there are missing metadata
87n/a self.assertRaises(MetadataMissingError, self._run, {}, **{'strict': 1})
88n/a self.assertRaises(PackagingSetupError, self._run,
89n/a {'name': 'xxx', 'version': 'xxx'}, **{'strict': 1})
90n/a
91n/a # complain about version format
92n/a metadata['version'] = 'xxx'
93n/a self.assertRaises(PackagingSetupError, self._run, metadata,
94n/a **{'strict': 1})
95n/a
96n/a # clear warnings from the previous calls
97n/a self.loghandler.flush()
98n/a
99n/a # now with correct version format again
100n/a metadata['version'] = '4.2'
101n/a self._run(metadata, strict=True)
102n/a self.assertEqual(self.get_logs(), [])
103n/a
104n/a @unittest.skipUnless(_HAS_DOCUTILS, "requires docutils")
105n/a def test_check_restructuredtext(self):
106n/a # let's see if it detects broken rest in description
107n/a broken_rest = 'title\n===\n\ntest'
108n/a pkg_info, dist = self.create_dist(description=broken_rest)
109n/a cmd = check(dist)
110n/a cmd.check_restructuredtext()
111n/a self.assertEqual(len(self.get_logs()), 1)
112n/a
113n/a # let's see if we have an error with strict=1
114n/a metadata = {'home_page': 'xxx', 'author': 'xxx',
115n/a 'author_email': 'xxx',
116n/a 'name': 'xxx', 'version': '1.2',
117n/a 'description': broken_rest}
118n/a self.assertRaises(PackagingSetupError, self._run, metadata,
119n/a strict=True, all=True)
120n/a self.loghandler.flush()
121n/a
122n/a # and non-broken rest, including a non-ASCII character to test #12114
123n/a dist = self.create_dist(description='title\n=====\n\ntest \u00df')[1]
124n/a cmd = check(dist)
125n/a cmd.check_restructuredtext()
126n/a self.assertEqual(self.get_logs(), [])
127n/a
128n/a def test_check_all(self):
129n/a self.assertRaises(PackagingSetupError, self._run,
130n/a {'name': 'xxx', 'version': 'xxx'}, **{'strict': 1,
131n/a 'all': 1})
132n/a self.assertRaises(MetadataMissingError, self._run,
133n/a {}, **{'strict': 1,
134n/a 'all': 1})
135n/a
136n/a def test_check_hooks(self):
137n/a pkg_info, dist = self.create_dist()
138n/a dist.command_options['install_dist'] = {
139n/a 'pre_hook': ('file', {"a": 'some.nonextistant.hook.ghrrraarrhll'}),
140n/a }
141n/a cmd = check(dist)
142n/a cmd.check_hooks_resolvable()
143n/a self.assertEqual(len(self.get_logs()), 1)
144n/a
145n/a def test_warn(self):
146n/a _, dist = self.create_dist()
147n/a cmd = check(dist)
148n/a self.assertEqual(self.get_logs(), [])
149n/a cmd.warn('hello')
150n/a self.assertEqual(self.get_logs(), ['check: hello'])
151n/a cmd.warn('hello %s', 'world')
152n/a self.assertEqual(self.get_logs(), ['check: hello world'])
153n/a cmd.warn('hello %s %s', 'beautiful', 'world')
154n/a self.assertEqual(self.get_logs(), ['check: hello beautiful world'])
155n/a
156n/a
157n/adef test_suite():
158n/a return unittest.makeSuite(CheckTestCase)
159n/a
160n/aif __name__ == "__main__":
161n/a unittest.main(defaultTest="test_suite")