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