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 |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 3 | import builtins |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 4 | import difflib |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 5 | import inspect |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 6 | import pydoc |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 7 | import keyword |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 8 | import re |
| 9 | import string |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 10 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 11 | import time |
| 12 | import unittest |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 13 | import xml.etree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 14 | import textwrap |
| 15 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 16 | from collections import namedtuple |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 17 | from test.script_helper import assert_python_ok |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 18 | from test.support import ( |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 19 | TESTFN, rmtree, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 20 | reap_children, reap_threads, captured_output, captured_stdout, unlink |
| 21 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 22 | from test import pydoc_mod |
| 23 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 24 | try: |
| 25 | import threading |
| 26 | except ImportError: |
| 27 | threading = None |
| 28 | |
Florent Xicluna | 085a656 | 2010-03-06 14:04:16 +0000 | [diff] [blame] | 29 | # Just in case sys.modules["test"] has the optional attribute __loader__. |
| 30 | if hasattr(pydoc_mod, "__loader__"): |
| 31 | del pydoc_mod.__loader__ |
| 32 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 33 | expected_text_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 34 | NAME |
| 35 | test.pydoc_mod - This is a test module for test_pydoc |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 36 | %s |
| 37 | CLASSES |
| 38 | builtins.object |
| 39 | A |
| 40 | B |
| 41 | \x20\x20\x20\x20 |
| 42 | class A(builtins.object) |
| 43 | | Hello and goodbye |
| 44 | |\x20\x20 |
| 45 | | Methods defined here: |
| 46 | |\x20\x20 |
| 47 | | __init__() |
| 48 | | Wow, I have no function! |
| 49 | |\x20\x20 |
| 50 | | ---------------------------------------------------------------------- |
| 51 | | Data descriptors defined here: |
| 52 | |\x20\x20 |
| 53 | | __dict__ |
| 54 | | dictionary for instance variables (if defined) |
| 55 | |\x20\x20 |
| 56 | | __weakref__ |
| 57 | | list of weak references to the object (if defined) |
| 58 | \x20\x20\x20\x20 |
| 59 | class B(builtins.object) |
| 60 | | Data descriptors defined here: |
| 61 | |\x20\x20 |
| 62 | | __dict__ |
| 63 | | dictionary for instance variables (if defined) |
| 64 | |\x20\x20 |
| 65 | | __weakref__ |
| 66 | | list of weak references to the object (if defined) |
| 67 | |\x20\x20 |
| 68 | | ---------------------------------------------------------------------- |
| 69 | | Data and other attributes defined here: |
| 70 | |\x20\x20 |
| 71 | | NO_MEANING = 'eggs' |
| 72 | |
| 73 | FUNCTIONS |
| 74 | doc_func() |
| 75 | This function solves all of the world's problems: |
| 76 | hunger |
| 77 | lack of Python |
| 78 | war |
| 79 | \x20\x20\x20\x20 |
| 80 | nodoc_func() |
| 81 | |
| 82 | DATA |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 83 | __xyz__ = 'X, Y and Z' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 84 | |
| 85 | VERSION |
| 86 | 1.2.3.4 |
| 87 | |
| 88 | AUTHOR |
| 89 | Benjamin Peterson |
| 90 | |
| 91 | CREDITS |
| 92 | Nobody |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 93 | |
| 94 | FILE |
| 95 | %s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 96 | """.strip() |
| 97 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 98 | expected_html_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 99 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 100 | <tr bgcolor="#7799ee"> |
| 101 | <td valign=bottom> <br> |
| 102 | <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 |
| 103 | ><td align=right valign=bottom |
| 104 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
| 105 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 106 | <p> |
| 107 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 108 | <tr bgcolor="#ee77aa"> |
| 109 | <td colspan=3 valign=bottom> <br> |
| 110 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 111 | \x20\x20\x20\x20 |
| 112 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 113 | <td width="100%%"><dl> |
| 114 | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
| 115 | </font></dt><dd> |
| 116 | <dl> |
| 117 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 118 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 119 | </font></dt></dl> |
| 120 | </dd> |
| 121 | </dl> |
| 122 | <p> |
| 123 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 124 | <tr bgcolor="#ffc8d8"> |
| 125 | <td colspan=3 valign=bottom> <br> |
| 126 | <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> |
| 127 | \x20\x20\x20\x20 |
| 128 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 129 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 130 | <tr><td> </td> |
| 131 | <td width="100%%">Methods defined here:<br> |
| 132 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 133 | |
| 134 | <hr> |
| 135 | Data descriptors defined here:<br> |
| 136 | <dl><dt><strong>__dict__</strong></dt> |
| 137 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 138 | </dl> |
| 139 | <dl><dt><strong>__weakref__</strong></dt> |
| 140 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 141 | </dl> |
| 142 | </td></tr></table> <p> |
| 143 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 144 | <tr bgcolor="#ffc8d8"> |
| 145 | <td colspan=3 valign=bottom> <br> |
| 146 | <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> |
| 147 | \x20\x20\x20\x20 |
| 148 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 149 | <td width="100%%">Data descriptors defined here:<br> |
| 150 | <dl><dt><strong>__dict__</strong></dt> |
| 151 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 152 | </dl> |
| 153 | <dl><dt><strong>__weakref__</strong></dt> |
| 154 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 155 | </dl> |
| 156 | <hr> |
| 157 | Data and other attributes defined here:<br> |
| 158 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 159 | |
| 160 | </td></tr></table></td></tr></table><p> |
| 161 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 162 | <tr bgcolor="#eeaa77"> |
| 163 | <td colspan=3 valign=bottom> <br> |
| 164 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 165 | \x20\x20\x20\x20 |
| 166 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 167 | <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> |
| 168 | hunger<br> |
| 169 | lack of Python<br> |
| 170 | war</tt></dd></dl> |
| 171 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 172 | </td></tr></table><p> |
| 173 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 174 | <tr bgcolor="#55aa55"> |
| 175 | <td colspan=3 valign=bottom> <br> |
| 176 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 177 | \x20\x20\x20\x20 |
| 178 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 179 | <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] | 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>Author</strong></big></font></td></tr> |
| 184 | \x20\x20\x20\x20 |
| 185 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 186 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 187 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 188 | <tr bgcolor="#7799ee"> |
| 189 | <td colspan=3 valign=bottom> <br> |
| 190 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 191 | \x20\x20\x20\x20 |
| 192 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 193 | <td width="100%%">Nobody</td></tr></table> |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 194 | """.strip() # ' <- emacs turd |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | # output pattern for missing module |
| 198 | missing_pattern = "no Python documentation found for '%s'" |
| 199 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 200 | # output pattern for module with bad imports |
| 201 | badimport_pattern = "problem in %s - ImportError: No module named %s" |
| 202 | |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 203 | def run_pydoc(module_name, *args, **env): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 204 | """ |
| 205 | Runs pydoc on the specified module. Returns the stripped |
| 206 | output of pydoc. |
| 207 | """ |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 208 | args = args + (module_name,) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 209 | # do not write bytecode files to avoid caching errors |
| 210 | rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env) |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 211 | return out.strip() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 212 | |
| 213 | def get_pydoc_html(module): |
| 214 | "Returns pydoc generated output as html" |
| 215 | doc = pydoc.HTMLDoc() |
| 216 | output = doc.docmodule(module) |
| 217 | loc = doc.getdocloc(pydoc_mod) or "" |
| 218 | if loc: |
| 219 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 220 | return output.strip(), loc |
| 221 | |
| 222 | def get_pydoc_text(module): |
| 223 | "Returns pydoc generated output as text" |
| 224 | doc = pydoc.TextDoc() |
| 225 | loc = doc.getdocloc(pydoc_mod) or "" |
| 226 | if loc: |
| 227 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 228 | |
| 229 | output = doc.docmodule(module) |
| 230 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 231 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 232 | patt = re.compile('\b.') |
| 233 | output = patt.sub('', output) |
| 234 | return output.strip(), loc |
| 235 | |
| 236 | def print_diffs(text1, text2): |
| 237 | "Prints unified diffs for two texts" |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 238 | # XXX now obsolete, use unittest built-in support |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 239 | lines1 = text1.splitlines(True) |
| 240 | lines2 = text2.splitlines(True) |
| 241 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 242 | tofile='got') |
| 243 | print('\n' + ''.join(diffs)) |
| 244 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 245 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 246 | # Bit of hack, but good enough for test purposes |
| 247 | header, _, _ = text.partition("</head>") |
| 248 | _, _, title = header.partition("<title>") |
| 249 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 250 | return title |
| 251 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 252 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 253 | class PydocDocTest(unittest.TestCase): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 254 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 255 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 256 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 257 | def test_html_doc(self): |
| 258 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 259 | mod_file = inspect.getabsfile(pydoc_mod) |
Benjamin Peterson | c5e9464 | 2008-06-14 23:04:46 +0000 | [diff] [blame] | 260 | if sys.platform == 'win32': |
| 261 | import nturl2path |
| 262 | mod_url = nturl2path.pathname2url(mod_file) |
| 263 | else: |
| 264 | mod_url = mod_file |
| 265 | expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 266 | if result != expected_html: |
| 267 | print_diffs(expected_html, result) |
| 268 | self.fail("outputs are not equal, see diff above") |
| 269 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 270 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 271 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 272 | def test_text_doc(self): |
| 273 | result, doc_loc = get_pydoc_text(pydoc_mod) |
| 274 | expected_text = expected_text_pattern % \ |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 275 | (doc_loc, inspect.getabsfile(pydoc_mod)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 276 | if result != expected_text: |
| 277 | print_diffs(expected_text, result) |
| 278 | self.fail("outputs are not equal, see diff above") |
| 279 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 280 | def test_issue8225(self): |
| 281 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 282 | result, doc_loc = get_pydoc_text(xml.etree) |
| 283 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 284 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 285 | def test_not_here(self): |
| 286 | missing_module = "test.i_am_not_here" |
| 287 | result = str(run_pydoc(missing_module), 'ascii') |
| 288 | expected = missing_pattern % missing_module |
| 289 | self.assertEqual(expected, result, |
| 290 | "documentation for missing module found") |
| 291 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 292 | def test_input_strip(self): |
| 293 | missing_module = " test.i_am_not_here " |
| 294 | result = str(run_pydoc(missing_module), 'ascii') |
| 295 | expected = missing_pattern % missing_module.strip() |
| 296 | self.assertEqual(expected, result) |
| 297 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 298 | def test_stripid(self): |
| 299 | # test with strings, other implementations might have different repr() |
| 300 | stripid = pydoc.stripid |
| 301 | # strip the id |
| 302 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 303 | '<function stripid>') |
| 304 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 305 | '<function stripid>') |
| 306 | # nothing to strip, return the same text |
| 307 | self.assertEqual(stripid('42'), '42') |
| 308 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 309 | "<type 'exceptions.Exception'>") |
| 310 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 311 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 312 | 'Docstrings are omitted with -O2 and above') |
| 313 | def test_help_output_redirect(self): |
| 314 | # issue 940286, if output is set in Helper, then all output from |
| 315 | # Helper.help should be redirected |
| 316 | old_pattern = expected_text_pattern |
| 317 | getpager_old = pydoc.getpager |
| 318 | getpager_new = lambda: (lambda x: x) |
| 319 | self.maxDiff = None |
| 320 | |
| 321 | buf = StringIO() |
| 322 | helper = pydoc.Helper(output=buf) |
| 323 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 324 | module = "test.pydoc_mod" |
| 325 | help_header = """ |
| 326 | Help on module test.pydoc_mod in test: |
| 327 | |
| 328 | """.lstrip() |
| 329 | help_header = textwrap.dedent(help_header) |
| 330 | expected_help_pattern = help_header + expected_text_pattern |
| 331 | |
| 332 | pydoc.getpager = getpager_new |
| 333 | try: |
| 334 | with captured_output('stdout') as output, \ |
| 335 | captured_output('stderr') as err: |
| 336 | helper.help(module) |
| 337 | result = buf.getvalue().strip() |
| 338 | expected_text = expected_help_pattern % \ |
| 339 | (doc_loc, inspect.getabsfile(pydoc_mod)) |
| 340 | self.assertEqual('', output.getvalue()) |
| 341 | self.assertEqual('', err.getvalue()) |
| 342 | self.assertEqual(expected_text, result) |
| 343 | finally: |
| 344 | pydoc.getpager = getpager_old |
| 345 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 346 | def test_namedtuple_public_underscore(self): |
| 347 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 348 | with captured_stdout() as help_io: |
| 349 | help(NT) |
| 350 | helptext = help_io.getvalue() |
| 351 | self.assertIn('_1', helptext) |
| 352 | self.assertIn('_replace', helptext) |
| 353 | self.assertIn('_asdict', helptext) |
| 354 | |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 355 | def test_synopsis(self): |
| 356 | self.addCleanup(unlink, TESTFN) |
| 357 | for encoding in ('ISO-8859-1', 'UTF-8'): |
| 358 | with open(TESTFN, 'w', encoding=encoding) as script: |
| 359 | if encoding != 'UTF-8': |
| 360 | print('#coding: {}'.format(encoding), file=script) |
| 361 | print('"""line 1: h\xe9', file=script) |
| 362 | print('line 2: hi"""', file=script) |
| 363 | synopsis = pydoc.synopsis(TESTFN, {}) |
| 364 | self.assertEqual(synopsis, 'line 1: h\xe9') |
| 365 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 366 | |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 367 | class PydocImportTest(unittest.TestCase): |
| 368 | |
| 369 | def setUp(self): |
| 370 | self.test_dir = os.mkdir(TESTFN) |
| 371 | self.addCleanup(rmtree, TESTFN) |
| 372 | |
| 373 | def test_badimport(self): |
| 374 | # This tests the fix for issue 5230, where if pydoc found the module |
| 375 | # but the module had an internal import error pydoc would report no doc |
| 376 | # found. |
| 377 | modname = 'testmod_xyzzy' |
| 378 | testpairs = ( |
| 379 | ('i_am_not_here', 'i_am_not_here'), |
| 380 | ('test.i_am_not_here_either', 'i_am_not_here_either'), |
| 381 | ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), |
| 382 | ('i_am_not_here.{}'.format(modname), |
| 383 | 'i_am_not_here.{}'.format(modname)), |
| 384 | ('test.{}'.format(modname), modname), |
| 385 | ) |
| 386 | |
| 387 | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
| 388 | for importstring, expectedinmsg in testpairs: |
| 389 | with open(sourcefn, 'w') as f: |
| 390 | f.write("import {}\n".format(importstring)) |
| 391 | result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
| 392 | expected = badimport_pattern % (modname, expectedinmsg) |
| 393 | self.assertEqual(expected, result) |
| 394 | |
| 395 | def test_apropos_with_bad_package(self): |
| 396 | # Issue 7425 - pydoc -k failed when bad package on path |
| 397 | pkgdir = os.path.join(TESTFN, "syntaxerr") |
| 398 | os.mkdir(pkgdir) |
| 399 | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
| 400 | with open(badsyntax, 'w') as f: |
| 401 | f.write("invalid python syntax = $1\n") |
Ned Deily | 3429491 | 2012-02-03 23:14:37 +0100 | [diff] [blame] | 402 | result = run_pydoc('zqwykjv', '-k', PYTHONPATH=TESTFN) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 403 | self.assertEqual(b'', result) |
| 404 | |
| 405 | def test_apropos_with_unreadable_dir(self): |
| 406 | # Issue 7367 - pydoc -k failed when unreadable dir on path |
| 407 | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
| 408 | os.mkdir(self.unreadable_dir, 0) |
| 409 | self.addCleanup(os.rmdir, self.unreadable_dir) |
| 410 | # Note, on Windows the directory appears to be still |
| 411 | # readable so this is not really testing the issue there |
Ned Deily | 3429491 | 2012-02-03 23:14:37 +0100 | [diff] [blame] | 412 | result = run_pydoc('zqwykjv', '-k', PYTHONPATH=TESTFN) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 413 | self.assertEqual(b'', result) |
| 414 | |
| 415 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 416 | class TestDescriptions(unittest.TestCase): |
| 417 | |
| 418 | def test_module(self): |
| 419 | # Check that pydocfodder module can be described |
| 420 | from test import pydocfodder |
| 421 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 422 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 423 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 424 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 425 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 426 | c = C() |
| 427 | |
| 428 | self.assertEqual(pydoc.describe(C), 'class C') |
| 429 | self.assertEqual(pydoc.describe(c), 'C') |
| 430 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 431 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 432 | |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 433 | def test_builtin(self): |
| 434 | for name in ('str', 'str.translate', 'builtins.str', |
| 435 | 'builtins.str.translate'): |
| 436 | # test low-level function |
| 437 | self.assertIsNotNone(pydoc.locate(name)) |
| 438 | # test high-level function |
| 439 | try: |
| 440 | pydoc.render_doc(name) |
| 441 | except ImportError: |
| 442 | self.fail('finding the doc of {!r} failed'.format(o)) |
| 443 | |
| 444 | for name in ('notbuiltins', 'strrr', 'strr.translate', |
| 445 | 'str.trrrranslate', 'builtins.strrr', |
| 446 | 'builtins.str.trrranslate'): |
| 447 | self.assertIsNone(pydoc.locate(name)) |
| 448 | self.assertRaises(ImportError, pydoc.render_doc, name) |
| 449 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 450 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 451 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 452 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 453 | """Tests for pydoc._start_server""" |
| 454 | |
| 455 | def test_server(self): |
| 456 | |
| 457 | # Minimal test that starts the server, then stops it. |
| 458 | def my_url_handler(url, content_type): |
| 459 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 460 | return text |
| 461 | |
| 462 | serverthread = pydoc._start_server(my_url_handler, port=0) |
| 463 | starttime = time.time() |
| 464 | timeout = 1 #seconds |
| 465 | |
| 466 | while serverthread.serving: |
| 467 | time.sleep(.01) |
| 468 | if serverthread.serving and time.time() - starttime > timeout: |
| 469 | serverthread.stop() |
| 470 | break |
| 471 | |
| 472 | self.assertEqual(serverthread.error, None) |
| 473 | |
| 474 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 475 | class PydocUrlHandlerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 476 | """Tests for pydoc._url_handler""" |
| 477 | |
| 478 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 479 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 480 | self.assertRaises(TypeError, f, 'A', '') |
| 481 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 482 | |
| 483 | def test_url_requests(self): |
| 484 | # Test for the correct title in the html pages returned. |
| 485 | # This tests the different parts of the URL handler without |
| 486 | # getting too picky about the exact html. |
| 487 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 488 | ("", "Pydoc: Index of Modules"), |
| 489 | ("get?key=", "Pydoc: Index of Modules"), |
| 490 | ("index", "Pydoc: Index of Modules"), |
| 491 | ("topics", "Pydoc: Topics"), |
| 492 | ("keywords", "Pydoc: Keywords"), |
| 493 | ("pydoc", "Pydoc: module pydoc"), |
| 494 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 495 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 496 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 497 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 498 | ("foobar", "Pydoc: Error - foobar"), |
| 499 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 500 | ] |
| 501 | |
| 502 | for url, title in requests: |
| 503 | text = pydoc._url_handler(url, "text/html") |
| 504 | result = get_html_title(text) |
| 505 | self.assertEqual(result, title) |
| 506 | |
| 507 | path = string.__file__ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 508 | title = "Pydoc: getfile " + path |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 509 | url = "getfile?key=" + path |
| 510 | text = pydoc._url_handler(url, "text/html") |
| 511 | result = get_html_title(text) |
| 512 | self.assertEqual(result, title) |
| 513 | |
| 514 | |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 515 | class TestHelper(unittest.TestCase): |
| 516 | def test_keywords(self): |
| 517 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 518 | sorted(keyword.kwlist)) |
| 519 | |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 520 | @reap_threads |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 521 | def test_main(): |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 522 | try: |
| 523 | test.support.run_unittest(PydocDocTest, |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 524 | PydocImportTest, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 525 | TestDescriptions, |
| 526 | PydocServerTest, |
| 527 | PydocUrlHandlerTest, |
| 528 | TestHelper, |
| 529 | ) |
| 530 | finally: |
| 531 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 532 | |
| 533 | if __name__ == "__main__": |
| 534 | test_main() |