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