test_check.py 4.9 KB

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