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