Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.check.""" |
| 2 | import unittest |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 3 | from test.support import run_unittest |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 4 | |
| 5 | from distutils.command.check import check, HAS_DOCUTILS |
| 6 | from distutils.tests import support |
| 7 | from distutils.errors import DistutilsSetupError |
| 8 | |
| 9 | class CheckTestCase(support.LoggingSilencer, |
| 10 | support.TempdirManager, |
| 11 | unittest.TestCase): |
| 12 | |
| 13 | def _run(self, metadata=None, **options): |
| 14 | if metadata is None: |
| 15 | metadata = {} |
| 16 | pkg_info, dist = self.create_dist(**metadata) |
| 17 | cmd = check(dist) |
| 18 | cmd.initialize_options() |
| 19 | for name, value in options.items(): |
| 20 | setattr(cmd, name, value) |
| 21 | cmd.ensure_finalized() |
| 22 | cmd.run() |
| 23 | return cmd |
| 24 | |
| 25 | def test_check_metadata(self): |
| 26 | # let's run the command with no metadata at all |
| 27 | # by default, check is checking the metadata |
| 28 | # should have some warnings |
| 29 | cmd = self._run() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 30 | self.assertEqual(cmd._warnings, 2) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 31 | |
| 32 | # now let's add the required fields |
| 33 | # and run it again, to make sure we don't get |
| 34 | # any warning anymore |
| 35 | metadata = {'url': 'xxx', 'author': 'xxx', |
| 36 | 'author_email': 'xxx', |
| 37 | 'name': 'xxx', 'version': 'xxx'} |
| 38 | cmd = self._run(metadata) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 39 | self.assertEqual(cmd._warnings, 0) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 40 | |
| 41 | # now with the strict mode, we should |
| 42 | # get an error if there are missing metadata |
| 43 | self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1}) |
| 44 | |
| 45 | # and of course, no error when all metadata are present |
| 46 | cmd = self._run(metadata, strict=1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 47 | self.assertEqual(cmd._warnings, 0) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 48 | |
Éric Araujo | 5819dcc | 2011-10-09 07:25:33 +0200 | [diff] [blame] | 49 | # now a test with non-ASCII characters |
| 50 | metadata = {'url': 'xxx', 'author': '\u00c9ric', |
| 51 | 'author_email': 'xxx', 'name': 'xxx', |
| 52 | 'version': 'xxx', |
| 53 | 'description': 'Something about esszet \u00df', |
| 54 | 'long_description': 'More things about esszet \u00df'} |
| 55 | cmd = self._run(metadata) |
| 56 | self.assertEqual(cmd._warnings, 0) |
| 57 | |
Serhiy Storchaka | 3c02ece | 2013-12-18 16:41:01 +0200 | [diff] [blame] | 58 | @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 59 | def test_check_document(self): |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 60 | pkg_info, dist = self.create_dist() |
| 61 | cmd = check(dist) |
| 62 | |
| 63 | # let's see if it detects broken rest |
| 64 | broken_rest = 'title\n===\n\ntest' |
| 65 | msgs = cmd._check_rst_data(broken_rest) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 66 | self.assertEqual(len(msgs), 1) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 67 | |
| 68 | # and non-broken rest |
| 69 | rest = 'title\n=====\n\ntest' |
| 70 | msgs = cmd._check_rst_data(rest) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 71 | self.assertEqual(len(msgs), 0) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 72 | |
Serhiy Storchaka | 3c02ece | 2013-12-18 16:41:01 +0200 | [diff] [blame] | 73 | @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 74 | def test_check_restructuredtext(self): |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 75 | # let's see if it detects broken rest in long_description |
| 76 | broken_rest = 'title\n===\n\ntest' |
| 77 | pkg_info, dist = self.create_dist(long_description=broken_rest) |
| 78 | cmd = check(dist) |
| 79 | cmd.check_restructuredtext() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 80 | self.assertEqual(cmd._warnings, 1) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 81 | |
| 82 | # let's see if we have an error with strict=1 |
Tarek Ziadé | f633ff4 | 2009-04-17 14:34:49 +0000 | [diff] [blame] | 83 | metadata = {'url': 'xxx', 'author': 'xxx', |
| 84 | 'author_email': 'xxx', |
| 85 | 'name': 'xxx', 'version': 'xxx', |
| 86 | 'long_description': broken_rest} |
| 87 | self.assertRaises(DistutilsSetupError, self._run, metadata, |
| 88 | **{'strict': 1, 'restructuredtext': 1}) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 89 | |
Éric Araujo | 5819dcc | 2011-10-09 07:25:33 +0200 | [diff] [blame] | 90 | # and non-broken rest, including a non-ASCII character to test #12114 |
| 91 | metadata['long_description'] = 'title\n=====\n\ntest \u00df' |
Tarek Ziadé | f633ff4 | 2009-04-17 14:34:49 +0000 | [diff] [blame] | 92 | cmd = self._run(metadata, strict=1, restructuredtext=1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 93 | self.assertEqual(cmd._warnings, 0) |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 94 | |
Tarek Ziadé | aa4398b | 2009-04-11 15:17:04 +0000 | [diff] [blame] | 95 | def test_check_all(self): |
| 96 | |
| 97 | metadata = {'url': 'xxx', 'author': 'xxx'} |
| 98 | self.assertRaises(DistutilsSetupError, self._run, |
| 99 | {}, **{'strict': 1, |
| 100 | 'restructuredtext': 1}) |
| 101 | |
Tarek Ziadé | f396ecf | 2009-04-11 15:00:43 +0000 | [diff] [blame] | 102 | def test_suite(): |
| 103 | return unittest.makeSuite(CheckTestCase) |
| 104 | |
| 105 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 106 | run_unittest(test_suite()) |