Home | History | Annotate | Download | only in tests
      1 """Tests for distutils.command.check."""
      2 import unittest
      3 from test.test_support import run_unittest
      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()
     30         self.assertEqual(cmd._warnings, 2)
     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)
     39         self.assertEqual(cmd._warnings, 0)
     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)
     47         self.assertEqual(cmd._warnings, 0)
     48 
     49     def test_check_document(self):
     50         if not HAS_DOCUTILS: # won't test without docutils

     51             return
     52         pkg_info, dist = self.create_dist()
     53         cmd = check(dist)
     54 
     55         # let's see if it detects broken rest

     56         broken_rest = 'title\n===\n\ntest'
     57         msgs = cmd._check_rst_data(broken_rest)
     58         self.assertEqual(len(msgs), 1)
     59 
     60         # and non-broken rest

     61         rest = 'title\n=====\n\ntest'
     62         msgs = cmd._check_rst_data(rest)
     63         self.assertEqual(len(msgs), 0)
     64 
     65     def test_check_restructuredtext(self):
     66         if not HAS_DOCUTILS: # won't test without docutils

     67             return
     68         # let's see if it detects broken rest in long_description

     69         broken_rest = 'title\n===\n\ntest'
     70         pkg_info, dist = self.create_dist(long_description=broken_rest)
     71         cmd = check(dist)
     72         cmd.check_restructuredtext()
     73         self.assertEqual(cmd._warnings, 1)
     74 
     75         # let's see if we have an error with strict=1

     76         metadata = {'url': 'xxx', 'author': 'xxx',
     77                     'author_email': 'xxx',
     78                     'name': 'xxx', 'version': 'xxx',
     79                     'long_description': broken_rest}
     80         self.assertRaises(DistutilsSetupError, self._run, metadata,
     81                           **{'strict': 1, 'restructuredtext': 1})
     82 
     83         # and non-broken rest

     84         metadata['long_description'] = 'title\n=====\n\ntest'
     85         cmd = self._run(metadata, strict=1, restructuredtext=1)
     86         self.assertEqual(cmd._warnings, 0)
     87 
     88     def test_check_all(self):
     89 
     90         metadata = {'url': 'xxx', 'author': 'xxx'}
     91         self.assertRaises(DistutilsSetupError, self._run,
     92                           {}, **{'strict': 1,
     93                                  'restructuredtext': 1})
     94 
     95 def test_suite():
     96     return unittest.makeSuite(CheckTestCase)
     97 
     98 if __name__ == "__main__":
     99     run_unittest(test_suite())
    100