Home | History | Annotate | Download | only in tests
      1 # -*- encoding: utf8 -*-
      2 """Tests for distutils.command.check."""
      3 import unittest
      4 from test.test_support import run_unittest
      5 
      6 from distutils.command.check import check, HAS_DOCUTILS
      7 from distutils.tests import support
      8 from distutils.errors import DistutilsSetupError
      9 
     10 class CheckTestCase(support.LoggingSilencer,
     11                     support.TempdirManager,
     12                     unittest.TestCase):
     13 
     14     def _run(self, metadata=None, **options):
     15         if metadata is None:
     16             metadata = {}
     17         pkg_info, dist = self.create_dist(**metadata)
     18         cmd = check(dist)
     19         cmd.initialize_options()
     20         for name, value in options.items():
     21             setattr(cmd, name, value)
     22         cmd.ensure_finalized()
     23         cmd.run()
     24         return cmd
     25 
     26     def test_check_metadata(self):
     27         # let's run the command with no metadata at all
     28         # by default, check is checking the metadata
     29         # should have some warnings
     30         cmd = self._run()
     31         self.assertEqual(cmd._warnings, 2)
     32 
     33         # now let's add the required fields
     34         # and run it again, to make sure we don't get
     35         # any warning anymore
     36         metadata = {'url': 'xxx', 'author': 'xxx',
     37                     'author_email': 'xxx',
     38                     'name': 'xxx', 'version': 'xxx'}
     39         cmd = self._run(metadata)
     40         self.assertEqual(cmd._warnings, 0)
     41 
     42         # now with the strict mode, we should
     43         # get an error if there are missing metadata
     44         self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1})
     45 
     46         # and of course, no error when all metadata are present
     47         cmd = self._run(metadata, strict=1)
     48         self.assertEqual(cmd._warnings, 0)
     49 
     50         # now a test with Unicode entries
     51         metadata = {'url': u'xxx', 'author': u'\u00c9ric',
     52                     'author_email': u'xxx', u'name': 'xxx',
     53                     'version': u'xxx',
     54                     'description': u'Something about esszet \u00df',
     55                     'long_description': u'More things about esszet \u00df'}
     56         cmd = self._run(metadata)
     57         self.assertEqual(cmd._warnings, 0)
     58 
     59     def test_check_document(self):
     60         if not HAS_DOCUTILS: # won't test without docutils
     61             return
     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     def test_check_restructuredtext(self):
     76         if not HAS_DOCUTILS: # won't test without docutils
     77             return
     78         # let's see if it detects broken rest in long_description
     79         broken_rest = 'title\n===\n\ntest'
     80         pkg_info, dist = self.create_dist(long_description=broken_rest)
     81         cmd = check(dist)
     82         cmd.check_restructuredtext()
     83         self.assertEqual(cmd._warnings, 1)
     84 
     85         # let's see if we have an error with strict=1
     86         metadata = {'url': 'xxx', 'author': 'xxx',
     87                     'author_email': 'xxx',
     88                     'name': 'xxx', 'version': 'xxx',
     89                     'long_description': broken_rest}
     90         self.assertRaises(DistutilsSetupError, self._run, metadata,
     91                           **{'strict': 1, 'restructuredtext': 1})
     92 
     93         # and non-broken rest, including a non-ASCII character to test #12114
     94         metadata['long_description'] = u'title\n=====\n\ntest \u00df'
     95         cmd = self._run(metadata, strict=1, restructuredtext=1)
     96         self.assertEqual(cmd._warnings, 0)
     97 
     98     def test_check_all(self):
     99 
    100         metadata = {'url': 'xxx', 'author': 'xxx'}
    101         self.assertRaises(DistutilsSetupError, self._run,
    102                           {}, **{'strict': 1,
    103                                  'restructuredtext': 1})
    104 
    105 def test_suite():
    106     return unittest.makeSuite(CheckTestCase)
    107 
    108 if __name__ == "__main__":
    109     run_unittest(test_suite())
    110