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