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