Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 3 | import os.path |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 4 | import difflib |
| 5 | import subprocess |
| 6 | import re |
| 7 | import pydoc |
| 8 | import inspect |
| 9 | import unittest |
| 10 | import test.support |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 11 | from contextlib import contextmanager |
Antoine Pitrou | aecd3b7 | 2009-10-30 21:45:40 +0000 | [diff] [blame] | 12 | from test.support import ( |
| 13 | TESTFN, forget, rmtree, EnvironmentVarGuard, reap_children) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 14 | |
| 15 | from test import pydoc_mod |
| 16 | |
Florent Xicluna | 085a656 | 2010-03-06 14:04:16 +0000 | [diff] [blame^] | 17 | # Just in case sys.modules["test"] has the optional attribute __loader__. |
| 18 | if hasattr(pydoc_mod, "__loader__"): |
| 19 | del pydoc_mod.__loader__ |
| 20 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 21 | expected_text_pattern = \ |
| 22 | """ |
| 23 | NAME |
| 24 | test.pydoc_mod - This is a test module for test_pydoc |
| 25 | |
| 26 | FILE |
| 27 | %s |
| 28 | %s |
| 29 | CLASSES |
| 30 | builtins.object |
| 31 | A |
| 32 | B |
| 33 | \x20\x20\x20\x20 |
| 34 | class A(builtins.object) |
| 35 | | Hello and goodbye |
| 36 | |\x20\x20 |
| 37 | | Methods defined here: |
| 38 | |\x20\x20 |
| 39 | | __init__() |
| 40 | | Wow, I have no function! |
| 41 | |\x20\x20 |
| 42 | | ---------------------------------------------------------------------- |
| 43 | | Data descriptors defined here: |
| 44 | |\x20\x20 |
| 45 | | __dict__ |
| 46 | | dictionary for instance variables (if defined) |
| 47 | |\x20\x20 |
| 48 | | __weakref__ |
| 49 | | list of weak references to the object (if defined) |
| 50 | \x20\x20\x20\x20 |
| 51 | class B(builtins.object) |
| 52 | | Data descriptors defined here: |
| 53 | |\x20\x20 |
| 54 | | __dict__ |
| 55 | | dictionary for instance variables (if defined) |
| 56 | |\x20\x20 |
| 57 | | __weakref__ |
| 58 | | list of weak references to the object (if defined) |
| 59 | |\x20\x20 |
| 60 | | ---------------------------------------------------------------------- |
| 61 | | Data and other attributes defined here: |
| 62 | |\x20\x20 |
| 63 | | NO_MEANING = 'eggs' |
| 64 | |
| 65 | FUNCTIONS |
| 66 | doc_func() |
| 67 | This function solves all of the world's problems: |
| 68 | hunger |
| 69 | lack of Python |
| 70 | war |
| 71 | \x20\x20\x20\x20 |
| 72 | nodoc_func() |
| 73 | |
| 74 | DATA |
| 75 | __author__ = 'Benjamin Peterson' |
| 76 | __credits__ = 'Nobody' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 77 | __version__ = '1.2.3.4' |
| 78 | |
| 79 | VERSION |
| 80 | 1.2.3.4 |
| 81 | |
| 82 | AUTHOR |
| 83 | Benjamin Peterson |
| 84 | |
| 85 | CREDITS |
| 86 | Nobody |
| 87 | """.strip() |
| 88 | |
| 89 | expected_html_pattern = \ |
| 90 | """ |
| 91 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 92 | <tr bgcolor="#7799ee"> |
| 93 | <td valign=bottom> <br> |
| 94 | <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="test.html"><font color="#ffffff">test</font></a>.pydoc_mod</strong></big></big> (version 1.2.3.4)</font></td |
| 95 | ><td align=right valign=bottom |
| 96 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
| 97 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 98 | <p> |
| 99 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 100 | <tr bgcolor="#ee77aa"> |
| 101 | <td colspan=3 valign=bottom> <br> |
| 102 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 103 | \x20\x20\x20\x20 |
| 104 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 105 | <td width="100%%"><dl> |
| 106 | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
| 107 | </font></dt><dd> |
| 108 | <dl> |
| 109 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 110 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 111 | </font></dt></dl> |
| 112 | </dd> |
| 113 | </dl> |
| 114 | <p> |
| 115 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 116 | <tr bgcolor="#ffc8d8"> |
| 117 | <td colspan=3 valign=bottom> <br> |
| 118 | <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 119 | \x20\x20\x20\x20 |
| 120 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 121 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 122 | <tr><td> </td> |
| 123 | <td width="100%%">Methods defined here:<br> |
| 124 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 125 | |
| 126 | <hr> |
| 127 | Data descriptors defined here:<br> |
| 128 | <dl><dt><strong>__dict__</strong></dt> |
| 129 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 130 | </dl> |
| 131 | <dl><dt><strong>__weakref__</strong></dt> |
| 132 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 133 | </dl> |
| 134 | </td></tr></table> <p> |
| 135 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 136 | <tr bgcolor="#ffc8d8"> |
| 137 | <td colspan=3 valign=bottom> <br> |
| 138 | <font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 139 | \x20\x20\x20\x20 |
| 140 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 141 | <td width="100%%">Data descriptors defined here:<br> |
| 142 | <dl><dt><strong>__dict__</strong></dt> |
| 143 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 144 | </dl> |
| 145 | <dl><dt><strong>__weakref__</strong></dt> |
| 146 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 147 | </dl> |
| 148 | <hr> |
| 149 | Data and other attributes defined here:<br> |
| 150 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 151 | |
| 152 | </td></tr></table></td></tr></table><p> |
| 153 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 154 | <tr bgcolor="#eeaa77"> |
| 155 | <td colspan=3 valign=bottom> <br> |
| 156 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 157 | \x20\x20\x20\x20 |
| 158 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 159 | <td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This function solves all of the world's problems:<br> |
| 160 | hunger<br> |
| 161 | lack of Python<br> |
| 162 | war</tt></dd></dl> |
| 163 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 164 | </td></tr></table><p> |
| 165 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 166 | <tr bgcolor="#55aa55"> |
| 167 | <td colspan=3 valign=bottom> <br> |
| 168 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 169 | \x20\x20\x20\x20 |
| 170 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
| 171 | <td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br> |
| 172 | <strong>__credits__</strong> = 'Nobody'<br> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 173 | <strong>__version__</strong> = '1.2.3.4'</td></tr></table><p> |
| 174 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 175 | <tr bgcolor="#7799ee"> |
| 176 | <td colspan=3 valign=bottom> <br> |
| 177 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 178 | \x20\x20\x20\x20 |
| 179 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 180 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 181 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 182 | <tr bgcolor="#7799ee"> |
| 183 | <td colspan=3 valign=bottom> <br> |
| 184 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 185 | \x20\x20\x20\x20 |
| 186 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 187 | <td width="100%%">Nobody</td></tr></table> |
| 188 | """.strip() |
| 189 | |
| 190 | |
| 191 | # output pattern for missing module |
| 192 | missing_pattern = "no Python documentation found for '%s'" |
| 193 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 194 | # output pattern for module with bad imports |
| 195 | badimport_pattern = "problem in %s - ImportError: No module named %s" |
| 196 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 197 | def run_pydoc(module_name, *args): |
| 198 | """ |
| 199 | Runs pydoc on the specified module. Returns the stripped |
| 200 | output of pydoc. |
| 201 | """ |
| 202 | cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name] |
Antoine Pitrou | aecd3b7 | 2009-10-30 21:45:40 +0000 | [diff] [blame] | 203 | try: |
| 204 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 205 | return output.strip() |
| 206 | finally: |
| 207 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 208 | |
| 209 | def get_pydoc_html(module): |
| 210 | "Returns pydoc generated output as html" |
| 211 | doc = pydoc.HTMLDoc() |
| 212 | output = doc.docmodule(module) |
| 213 | loc = doc.getdocloc(pydoc_mod) or "" |
| 214 | if loc: |
| 215 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 216 | return output.strip(), loc |
| 217 | |
| 218 | def get_pydoc_text(module): |
| 219 | "Returns pydoc generated output as text" |
| 220 | doc = pydoc.TextDoc() |
| 221 | loc = doc.getdocloc(pydoc_mod) or "" |
| 222 | if loc: |
| 223 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 224 | |
| 225 | output = doc.docmodule(module) |
| 226 | |
| 227 | # cleanup the extra text formatting that pydoc preforms |
| 228 | patt = re.compile('\b.') |
| 229 | output = patt.sub('', output) |
| 230 | return output.strip(), loc |
| 231 | |
| 232 | def print_diffs(text1, text2): |
| 233 | "Prints unified diffs for two texts" |
| 234 | lines1 = text1.splitlines(True) |
| 235 | lines2 = text2.splitlines(True) |
| 236 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 237 | tofile='got') |
| 238 | print('\n' + ''.join(diffs)) |
| 239 | |
| 240 | |
| 241 | class PyDocDocTest(unittest.TestCase): |
| 242 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 243 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 244 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 245 | def test_html_doc(self): |
| 246 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 247 | mod_file = inspect.getabsfile(pydoc_mod) |
Benjamin Peterson | c5e9464 | 2008-06-14 23:04:46 +0000 | [diff] [blame] | 248 | if sys.platform == 'win32': |
| 249 | import nturl2path |
| 250 | mod_url = nturl2path.pathname2url(mod_file) |
| 251 | else: |
| 252 | mod_url = mod_file |
| 253 | expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 254 | if result != expected_html: |
| 255 | print_diffs(expected_html, result) |
| 256 | self.fail("outputs are not equal, see diff above") |
| 257 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 258 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 259 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 260 | def test_text_doc(self): |
| 261 | result, doc_loc = get_pydoc_text(pydoc_mod) |
| 262 | expected_text = expected_text_pattern % \ |
| 263 | (inspect.getabsfile(pydoc_mod), doc_loc) |
| 264 | if result != expected_text: |
| 265 | print_diffs(expected_text, result) |
| 266 | self.fail("outputs are not equal, see diff above") |
| 267 | |
| 268 | def test_not_here(self): |
| 269 | missing_module = "test.i_am_not_here" |
| 270 | result = str(run_pydoc(missing_module), 'ascii') |
| 271 | expected = missing_pattern % missing_module |
| 272 | self.assertEqual(expected, result, |
| 273 | "documentation for missing module found") |
| 274 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 275 | def test_badimport(self): |
| 276 | # This tests the fix for issue 5230, where if pydoc found the module |
| 277 | # but the module had an internal import error pydoc would report no doc |
| 278 | # found. |
| 279 | modname = 'testmod_xyzzy' |
| 280 | testpairs = ( |
| 281 | ('i_am_not_here', 'i_am_not_here'), |
| 282 | ('test.i_am_not_here_either', 'i_am_not_here_either'), |
| 283 | ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), |
| 284 | ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)), |
| 285 | ('test.{}'.format(modname), modname), |
| 286 | ) |
| 287 | |
| 288 | @contextmanager |
| 289 | def newdirinpath(dir): |
| 290 | os.mkdir(dir) |
| 291 | sys.path.insert(0, dir) |
| 292 | yield |
| 293 | sys.path.pop(0) |
| 294 | rmtree(dir) |
| 295 | |
| 296 | with newdirinpath(TESTFN), EnvironmentVarGuard() as env: |
| 297 | env['PYTHONPATH'] = TESTFN |
| 298 | fullmodname = os.path.join(TESTFN, modname) |
| 299 | sourcefn = fullmodname + os.extsep + "py" |
| 300 | for importstring, expectedinmsg in testpairs: |
| 301 | f = open(sourcefn, 'w') |
| 302 | f.write("import {}\n".format(importstring)) |
| 303 | f.close() |
| 304 | try: |
| 305 | result = run_pydoc(modname).decode("ascii") |
| 306 | finally: |
| 307 | forget(modname) |
| 308 | expected = badimport_pattern % (modname, expectedinmsg) |
| 309 | self.assertEqual(expected, result) |
| 310 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 311 | def test_input_strip(self): |
| 312 | missing_module = " test.i_am_not_here " |
| 313 | result = str(run_pydoc(missing_module), 'ascii') |
| 314 | expected = missing_pattern % missing_module.strip() |
| 315 | self.assertEqual(expected, result) |
| 316 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 317 | def test_stripid(self): |
| 318 | # test with strings, other implementations might have different repr() |
| 319 | stripid = pydoc.stripid |
| 320 | # strip the id |
| 321 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 322 | '<function stripid>') |
| 323 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 324 | '<function stripid>') |
| 325 | # nothing to strip, return the same text |
| 326 | self.assertEqual(stripid('42'), '42') |
| 327 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 328 | "<type 'exceptions.Exception'>") |
| 329 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 330 | |
| 331 | class TestDescriptions(unittest.TestCase): |
| 332 | |
| 333 | def test_module(self): |
| 334 | # Check that pydocfodder module can be described |
| 335 | from test import pydocfodder |
| 336 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 337 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 338 | |
| 339 | def test_classic_class(self): |
| 340 | class C: "Classic class" |
| 341 | c = C() |
| 342 | self.assertEqual(pydoc.describe(C), 'class C') |
| 343 | self.assertEqual(pydoc.describe(c), 'C') |
| 344 | expected = 'C in module %s' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 345 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 346 | |
| 347 | def test_class(self): |
| 348 | class C(object): "New-style class" |
| 349 | c = C() |
| 350 | |
| 351 | self.assertEqual(pydoc.describe(C), 'class C') |
| 352 | self.assertEqual(pydoc.describe(c), 'C') |
| 353 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 354 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | def test_main(): |
| 358 | test.support.run_unittest(PyDocDocTest, TestDescriptions) |
| 359 | |
| 360 | if __name__ == "__main__": |
| 361 | test_main() |