Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | import os |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2 | import sys |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 3 | import difflib |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 4 | import inspect |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 5 | import pydoc |
| 6 | import re |
| 7 | import string |
| 8 | import subprocess |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 9 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 10 | import time |
| 11 | import unittest |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 12 | import xml.etree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 13 | import textwrap |
| 14 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 15 | from collections import namedtuple |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 16 | from contextlib import contextmanager |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 17 | from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard, \ |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 18 | reap_children, captured_output, captured_stdout |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 19 | |
| 20 | from test import pydoc_mod |
| 21 | |
Florent Xicluna | 085a656 | 2010-03-06 14:04:16 +0000 | [diff] [blame] | 22 | # Just in case sys.modules["test"] has the optional attribute __loader__. |
| 23 | if hasattr(pydoc_mod, "__loader__"): |
| 24 | del pydoc_mod.__loader__ |
| 25 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 26 | expected_text_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 27 | NAME |
| 28 | test.pydoc_mod - This is a test module for test_pydoc |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 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 |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 76 | __xyz__ = 'X, Y and Z' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 77 | |
| 78 | VERSION |
| 79 | 1.2.3.4 |
| 80 | |
| 81 | AUTHOR |
| 82 | Benjamin Peterson |
| 83 | |
| 84 | CREDITS |
| 85 | Nobody |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 86 | |
| 87 | FILE |
| 88 | %s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 89 | """.strip() |
| 90 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 91 | expected_html_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 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> |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 172 | <td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 173 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 174 | <tr bgcolor="#7799ee"> |
| 175 | <td colspan=3 valign=bottom> <br> |
| 176 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 177 | \x20\x20\x20\x20 |
| 178 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 179 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 180 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 181 | <tr bgcolor="#7799ee"> |
| 182 | <td colspan=3 valign=bottom> <br> |
| 183 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 184 | \x20\x20\x20\x20 |
| 185 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 186 | <td width="100%%">Nobody</td></tr></table> |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 187 | """.strip() # ' <- emacs turd |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | # output pattern for missing module |
| 191 | missing_pattern = "no Python documentation found for '%s'" |
| 192 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 193 | # output pattern for module with bad imports |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 194 | badimport_pattern = "problem in %s - ImportError: No module named %r" |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 195 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 196 | def run_pydoc(module_name, *args): |
| 197 | """ |
| 198 | Runs pydoc on the specified module. Returns the stripped |
| 199 | output of pydoc. |
| 200 | """ |
| 201 | cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name] |
Antoine Pitrou | aecd3b7 | 2009-10-30 21:45:40 +0000 | [diff] [blame] | 202 | try: |
| 203 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 204 | return output.strip() |
| 205 | finally: |
| 206 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 207 | |
| 208 | def get_pydoc_html(module): |
| 209 | "Returns pydoc generated output as html" |
| 210 | doc = pydoc.HTMLDoc() |
| 211 | output = doc.docmodule(module) |
| 212 | loc = doc.getdocloc(pydoc_mod) or "" |
| 213 | if loc: |
| 214 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 215 | return output.strip(), loc |
| 216 | |
| 217 | def get_pydoc_text(module): |
| 218 | "Returns pydoc generated output as text" |
| 219 | doc = pydoc.TextDoc() |
| 220 | loc = doc.getdocloc(pydoc_mod) or "" |
| 221 | if loc: |
| 222 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 223 | |
| 224 | output = doc.docmodule(module) |
| 225 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 226 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 227 | patt = re.compile('\b.') |
| 228 | output = patt.sub('', output) |
| 229 | return output.strip(), loc |
| 230 | |
| 231 | def print_diffs(text1, text2): |
| 232 | "Prints unified diffs for two texts" |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 233 | # XXX now obsolete, use unittest built-in support |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 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 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 240 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 241 | # Bit of hack, but good enough for test purposes |
| 242 | header, _, _ = text.partition("</head>") |
| 243 | _, _, title = header.partition("<title>") |
| 244 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 245 | return title |
| 246 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 247 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 248 | class PydocDocTest(unittest.TestCase): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 249 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 250 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 251 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 252 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 253 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 254 | def test_html_doc(self): |
| 255 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 256 | mod_file = inspect.getabsfile(pydoc_mod) |
Benjamin Peterson | c5e9464 | 2008-06-14 23:04:46 +0000 | [diff] [blame] | 257 | if sys.platform == 'win32': |
| 258 | import nturl2path |
| 259 | mod_url = nturl2path.pathname2url(mod_file) |
| 260 | else: |
| 261 | mod_url = mod_file |
| 262 | expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 263 | if result != expected_html: |
| 264 | print_diffs(expected_html, result) |
| 265 | self.fail("outputs are not equal, see diff above") |
| 266 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 267 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 268 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 269 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 270 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 271 | def test_text_doc(self): |
| 272 | result, doc_loc = get_pydoc_text(pydoc_mod) |
| 273 | expected_text = expected_text_pattern % \ |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 274 | (doc_loc, inspect.getabsfile(pydoc_mod)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 275 | if result != expected_text: |
| 276 | print_diffs(expected_text, result) |
| 277 | self.fail("outputs are not equal, see diff above") |
| 278 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 279 | def test_issue8225(self): |
| 280 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 281 | result, doc_loc = get_pydoc_text(xml.etree) |
| 282 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 283 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 284 | def test_not_here(self): |
| 285 | missing_module = "test.i_am_not_here" |
| 286 | result = str(run_pydoc(missing_module), 'ascii') |
| 287 | expected = missing_pattern % missing_module |
| 288 | self.assertEqual(expected, result, |
| 289 | "documentation for missing module found") |
| 290 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 291 | def test_badimport(self): |
| 292 | # This tests the fix for issue 5230, where if pydoc found the module |
| 293 | # but the module had an internal import error pydoc would report no doc |
| 294 | # found. |
| 295 | modname = 'testmod_xyzzy' |
| 296 | testpairs = ( |
| 297 | ('i_am_not_here', 'i_am_not_here'), |
| 298 | ('test.i_am_not_here_either', 'i_am_not_here_either'), |
| 299 | ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 300 | ('i_am_not_here.{}'.format(modname), |
| 301 | 'i_am_not_here.{}'.format(modname)), |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 302 | ('test.{}'.format(modname), modname), |
| 303 | ) |
| 304 | |
| 305 | @contextmanager |
| 306 | def newdirinpath(dir): |
| 307 | os.mkdir(dir) |
| 308 | sys.path.insert(0, dir) |
| 309 | yield |
| 310 | sys.path.pop(0) |
| 311 | rmtree(dir) |
| 312 | |
| 313 | with newdirinpath(TESTFN), EnvironmentVarGuard() as env: |
| 314 | env['PYTHONPATH'] = TESTFN |
| 315 | fullmodname = os.path.join(TESTFN, modname) |
| 316 | sourcefn = fullmodname + os.extsep + "py" |
| 317 | for importstring, expectedinmsg in testpairs: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 318 | with open(sourcefn, 'w') as f: |
| 319 | f.write("import {}\n".format(importstring)) |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 320 | try: |
| 321 | result = run_pydoc(modname).decode("ascii") |
| 322 | finally: |
| 323 | forget(modname) |
| 324 | expected = badimport_pattern % (modname, expectedinmsg) |
| 325 | self.assertEqual(expected, result) |
| 326 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 327 | def test_input_strip(self): |
| 328 | missing_module = " test.i_am_not_here " |
| 329 | result = str(run_pydoc(missing_module), 'ascii') |
| 330 | expected = missing_pattern % missing_module.strip() |
| 331 | self.assertEqual(expected, result) |
| 332 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 333 | def test_stripid(self): |
| 334 | # test with strings, other implementations might have different repr() |
| 335 | stripid = pydoc.stripid |
| 336 | # strip the id |
| 337 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 338 | '<function stripid>') |
| 339 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 340 | '<function stripid>') |
| 341 | # nothing to strip, return the same text |
| 342 | self.assertEqual(stripid('42'), '42') |
| 343 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 344 | "<type 'exceptions.Exception'>") |
| 345 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 346 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 347 | 'Docstrings are omitted with -O2 and above') |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 348 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 349 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 350 | def test_help_output_redirect(self): |
| 351 | # issue 940286, if output is set in Helper, then all output from |
| 352 | # Helper.help should be redirected |
| 353 | old_pattern = expected_text_pattern |
| 354 | getpager_old = pydoc.getpager |
| 355 | getpager_new = lambda: (lambda x: x) |
| 356 | self.maxDiff = None |
| 357 | |
| 358 | buf = StringIO() |
| 359 | helper = pydoc.Helper(output=buf) |
| 360 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 361 | module = "test.pydoc_mod" |
| 362 | help_header = """ |
| 363 | Help on module test.pydoc_mod in test: |
| 364 | |
| 365 | """.lstrip() |
| 366 | help_header = textwrap.dedent(help_header) |
| 367 | expected_help_pattern = help_header + expected_text_pattern |
| 368 | |
| 369 | pydoc.getpager = getpager_new |
| 370 | try: |
| 371 | with captured_output('stdout') as output, \ |
| 372 | captured_output('stderr') as err: |
| 373 | helper.help(module) |
| 374 | result = buf.getvalue().strip() |
| 375 | expected_text = expected_help_pattern % \ |
| 376 | (doc_loc, inspect.getabsfile(pydoc_mod)) |
| 377 | self.assertEqual('', output.getvalue()) |
| 378 | self.assertEqual('', err.getvalue()) |
| 379 | self.assertEqual(expected_text, result) |
| 380 | finally: |
| 381 | pydoc.getpager = getpager_old |
| 382 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 383 | def test_namedtuple_public_underscore(self): |
| 384 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 385 | with captured_stdout() as help_io: |
| 386 | help(NT) |
| 387 | helptext = help_io.getvalue() |
| 388 | self.assertIn('_1', helptext) |
| 389 | self.assertIn('_replace', helptext) |
| 390 | self.assertIn('_asdict', helptext) |
| 391 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 392 | |
| 393 | class TestDescriptions(unittest.TestCase): |
| 394 | |
| 395 | def test_module(self): |
| 396 | # Check that pydocfodder module can be described |
| 397 | from test import pydocfodder |
| 398 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 399 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 400 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 401 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 402 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 403 | c = C() |
| 404 | |
| 405 | self.assertEqual(pydoc.describe(C), 'class C') |
| 406 | self.assertEqual(pydoc.describe(c), 'C') |
| 407 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 408 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 409 | |
| 410 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 411 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 412 | """Tests for pydoc._start_server""" |
| 413 | |
| 414 | def test_server(self): |
| 415 | |
| 416 | # Minimal test that starts the server, then stops it. |
| 417 | def my_url_handler(url, content_type): |
| 418 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 419 | return text |
| 420 | |
| 421 | serverthread = pydoc._start_server(my_url_handler, port=0) |
| 422 | starttime = time.time() |
| 423 | timeout = 1 #seconds |
| 424 | |
| 425 | while serverthread.serving: |
| 426 | time.sleep(.01) |
| 427 | if serverthread.serving and time.time() - starttime > timeout: |
| 428 | serverthread.stop() |
| 429 | break |
| 430 | |
| 431 | self.assertEqual(serverthread.error, None) |
| 432 | |
| 433 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 434 | class PydocUrlHandlerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 435 | """Tests for pydoc._url_handler""" |
| 436 | |
| 437 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 438 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 439 | self.assertRaises(TypeError, f, 'A', '') |
| 440 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 441 | |
| 442 | def test_url_requests(self): |
| 443 | # Test for the correct title in the html pages returned. |
| 444 | # This tests the different parts of the URL handler without |
| 445 | # getting too picky about the exact html. |
| 446 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 447 | ("", "Pydoc: Index of Modules"), |
| 448 | ("get?key=", "Pydoc: Index of Modules"), |
| 449 | ("index", "Pydoc: Index of Modules"), |
| 450 | ("topics", "Pydoc: Topics"), |
| 451 | ("keywords", "Pydoc: Keywords"), |
| 452 | ("pydoc", "Pydoc: module pydoc"), |
| 453 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 454 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 455 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 456 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 457 | ("foobar", "Pydoc: Error - foobar"), |
| 458 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 459 | ] |
| 460 | |
| 461 | for url, title in requests: |
| 462 | text = pydoc._url_handler(url, "text/html") |
| 463 | result = get_html_title(text) |
| 464 | self.assertEqual(result, title) |
| 465 | |
| 466 | path = string.__file__ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 467 | title = "Pydoc: getfile " + path |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 468 | url = "getfile?key=" + path |
| 469 | text = pydoc._url_handler(url, "text/html") |
| 470 | result = get_html_title(text) |
| 471 | self.assertEqual(result, title) |
| 472 | |
| 473 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 474 | def test_main(): |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 475 | test.support.run_unittest(PydocDocTest, |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 476 | TestDescriptions, |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 477 | PydocServerTest, |
| 478 | PydocUrlHandlerTest, |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 479 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 480 | |
| 481 | if __name__ == "__main__": |
| 482 | test_main() |