| 1 |
|
| 2 |
"""Tests for distutils.command.check.""" |
| 3 |
import os |
| 4 |
import textwrap |
| 5 |
import unittest |
| 6 |
from test.test_support import run_unittest |
| 7 |
|
| 8 |
from distutils.command.check import check, HAS_DOCUTILS |
| 9 |
from distutils.tests import support |
| 10 |
from distutils.errors import DistutilsSetupError |
| 11 |
|
| 12 |
try: |
| 13 |
import pygments |
| 14 |
except ImportError: |
| 15 |
pygments = None |
| 16 |
|
| 17 |
|
| 18 |
HERE = os.path.dirname(__file__) |
| 19 |
|
| 20 |
|
| 21 |
class CheckTestCase(support.LoggingSilencer, |
| 22 |
support.TempdirManager, |
| 23 |
unittest.TestCase): |
| 24 |
|
| 25 |
def _run(self, metadata=None, cwd=None, **options): |
| 26 |
if metadata is None: |
| 27 |
metadata = {} |
| 28 |
if cwd is not None: |
| 29 |
old_dir = os.getcwd() |
| 30 |
os.chdir(cwd) |
| 31 |
pkg_info, dist = self.create_dist(**metadata) |
| 32 |
cmd = check(dist) |
| 33 |
cmd.initialize_options() |
| 34 |
for name, value in options.items(): |
| 35 |
setattr(cmd, name, value) |
| 36 |
cmd.ensure_finalized() |
| 37 |
cmd.run() |
| 38 |
if cwd is not None: |
| 39 |
os.chdir(old_dir) |
| 40 |
return cmd |
| 41 |
|
| 42 |
def test_check_metadata(self): |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
cmd = self._run() |
| 47 |
self.assertEqual(cmd._warnings, 2) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
metadata = {'url': 'xxx', 'author': 'xxx', |
| 53 |
'author_email': 'xxx', |
| 54 |
'name': 'xxx', 'version': 'xxx'} |
| 55 |
cmd = self._run(metadata) |
| 56 |
self.assertEqual(cmd._warnings, 0) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1}) |
| 61 |
|
| 62 |
|
| 63 |
cmd = self._run(metadata, strict=1) |
| 64 |
self.assertEqual(cmd._warnings, 0) |
| 65 |
|
| 66 |
|
| 67 |
metadata = {'url': u'xxx', 'author': u'\u00c9ric', |
| 68 |
'author_email': u'xxx', u'name': 'xxx', |
| 69 |
'version': u'xxx', |
| 70 |
'description': u'Something about esszet \u00df', |
| 71 |
'long_description': u'More things about esszet \u00df'} |
| 72 |
cmd = self._run(metadata) |
| 73 |
self.assertEqual(cmd._warnings, 0) |
| 74 |
|
| 75 |
@unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") |
| 76 |
def test_check_document(self): |
| 77 |
pkg_info, dist = self.create_dist() |
| 78 |
cmd = check(dist) |
| 79 |
|
| 80 |
|
| 81 |
broken_rest = 'title\n===\n\ntest' |
| 82 |
msgs = cmd._check_rst_data(broken_rest) |
| 83 |
self.assertEqual(len(msgs), 1) |
| 84 |
|
| 85 |
|
| 86 |
rest = 'title\n=====\n\ntest' |
| 87 |
msgs = cmd._check_rst_data(rest) |
| 88 |
self.assertEqual(len(msgs), 0) |
| 89 |
|
| 90 |
@unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") |
| 91 |
def test_check_restructuredtext(self): |
| 92 |
|
| 93 |
broken_rest = 'title\n===\n\ntest' |
| 94 |
pkg_info, dist = self.create_dist(long_description=broken_rest) |
| 95 |
cmd = check(dist) |
| 96 |
cmd.check_restructuredtext() |
| 97 |
self.assertEqual(cmd._warnings, 1) |
| 98 |
|
| 99 |
|
| 100 |
metadata = {'url': 'xxx', 'author': 'xxx', |
| 101 |
'author_email': 'xxx', |
| 102 |
'name': 'xxx', 'version': 'xxx', |
| 103 |
'long_description': broken_rest} |
| 104 |
self.assertRaises(DistutilsSetupError, self._run, metadata, |
| 105 |
**{'strict': 1, 'restructuredtext': 1}) |
| 106 |
|
| 107 |
|
| 108 |
metadata['long_description'] = u'title\n=====\n\ntest \u00df' |
| 109 |
cmd = self._run(metadata, strict=1, restructuredtext=1) |
| 110 |
self.assertEqual(cmd._warnings, 0) |
| 111 |
|
| 112 |
|
| 113 |
metadata['long_description'] = 'title\n=====\n\n.. include:: includetest.rst' |
| 114 |
cmd = self._run(metadata, cwd=HERE, strict=1, restructuredtext=1) |
| 115 |
self.assertEqual(cmd._warnings, 0) |
| 116 |
|
| 117 |
@unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") |
| 118 |
def test_check_restructuredtext_with_syntax_highlight(self): |
| 119 |
|
| 120 |
|
| 121 |
example_rst_docs = [] |
| 122 |
example_rst_docs.append(textwrap.dedent("""\ |
| 123 |
Here's some code: |
| 124 |
|
| 125 |
.. code:: python |
| 126 |
|
| 127 |
def foo(): |
| 128 |
pass |
| 129 |
""")) |
| 130 |
example_rst_docs.append(textwrap.dedent("""\ |
| 131 |
Here's some code: |
| 132 |
|
| 133 |
.. code-block:: python |
| 134 |
|
| 135 |
def foo(): |
| 136 |
pass |
| 137 |
""")) |
| 138 |
|
| 139 |
for rest_with_code in example_rst_docs: |
| 140 |
pkg_info, dist = self.create_dist(long_description=rest_with_code) |
| 141 |
cmd = check(dist) |
| 142 |
cmd.check_restructuredtext() |
| 143 |
msgs = cmd._check_rst_data(rest_with_code) |
| 144 |
if pygments is not None: |
| 145 |
self.assertEqual(len(msgs), 0) |
| 146 |
else: |
| 147 |
self.assertEqual(len(msgs), 1) |
| 148 |
self.assertEqual( |
| 149 |
str(msgs[0][1]), |
| 150 |
'Cannot analyze code. Pygments package not found.' |
| 151 |
) |
| 152 |
|
| 153 |
def test_check_all(self): |
| 154 |
|
| 155 |
metadata = {'url': 'xxx', 'author': 'xxx'} |
| 156 |
self.assertRaises(DistutilsSetupError, self._run, |
| 157 |
{}, **{'strict': 1, |
| 158 |
'restructuredtext': 1}) |
| 159 |
|
| 160 |
def test_suite(): |
| 161 |
return unittest.makeSuite(CheckTestCase) |
| 162 |
|
| 163 |
if __name__ == "__main__": |
| 164 |
run_unittest(test_suite()) |