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