test_check.py 5.6 KB

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