blob: 2e67290d00ec41bd9b8332dd7abc81ef9e7696b3 [file] [log] [blame]
Georg Brandl8632cc22008-05-18 16:32:48 +00001import sys
Benjamin Peterson3a7305e2008-05-22 23:09:26 +00002import os
R. David Murrayef087da2009-06-23 18:02:46 +00003import os.path
Georg Brandl8632cc22008-05-18 16:32:48 +00004import difflib
5import subprocess
6import re
7import pydoc
Georg Brandlb7e419e2008-05-20 08:10:03 +00008import inspect
Georg Brandl8632cc22008-05-18 16:32:48 +00009import unittest
Brian Curtinaeb2e822010-03-31 03:10:21 +000010import xml.etree
Georg Brandl8632cc22008-05-18 16:32:48 +000011import test.test_support
R. David Murrayef087da2009-06-23 18:02:46 +000012from contextlib import contextmanager
Raymond Hettinger9aa5a342011-03-25 16:00:13 -070013from collections import namedtuple
Antoine Pitrou161df942009-10-30 21:41:22 +000014from test.test_support import (
Raymond Hettinger9aa5a342011-03-25 16:00:13 -070015 TESTFN, forget, rmtree, EnvironmentVarGuard, reap_children, captured_stdout)
Benjamin Petersonf5c38da2008-05-18 20:48:07 +000016
Georg Brandl8632cc22008-05-18 16:32:48 +000017from test import pydoc_mod
18
19expected_text_pattern = \
20"""
21NAME
22 test.pydoc_mod - This is a test module for test_pydoc
23
24FILE
25 %s
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000026%s
Georg Brandl8632cc22008-05-18 16:32:48 +000027CLASSES
28 __builtin__.object
29 B
30 A
31\x20\x20\x20\x20
32 class A
33 | Hello and goodbye
34 |\x20\x20
35 | Methods defined here:
36 |\x20\x20
37 | __init__()
38 | Wow, I have no function!
39\x20\x20\x20\x20
40 class B(__builtin__.object)
41 | Data descriptors defined here:
42 |\x20\x20
43 | __dict__
44 | dictionary for instance variables (if defined)
45 |\x20\x20
46 | __weakref__
47 | list of weak references to the object (if defined)
48 |\x20\x20
49 | ----------------------------------------------------------------------
50 | Data and other attributes defined here:
51 |\x20\x20
52 | NO_MEANING = 'eggs'
53
54FUNCTIONS
55 doc_func()
56 This function solves all of the world's problems:
57 hunger
58 lack of Python
59 war
60\x20\x20\x20\x20
61 nodoc_func()
62
63DATA
64 __author__ = 'Benjamin Peterson'
65 __credits__ = 'Nobody'
Georg Brandl8632cc22008-05-18 16:32:48 +000066 __version__ = '1.2.3.4'
67
68VERSION
69 1.2.3.4
70
71AUTHOR
72 Benjamin Peterson
73
74CREDITS
75 Nobody
76""".strip()
77
78expected_html_pattern = \
79"""
80<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
81<tr bgcolor="#7799ee">
82<td valign=bottom>&nbsp;<br>
83<font color="#ffffff" face="helvetica, arial">&nbsp;<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
84><td align=right valign=bottom
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000085><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
Georg Brandl8632cc22008-05-18 16:32:48 +000086 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
87<p>
88<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
89<tr bgcolor="#ee77aa">
90<td colspan=3 valign=bottom>&nbsp;<br>
91<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
92\x20\x20\x20\x20
93<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
94<td width="100%%"><dl>
95<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
96</font></dt><dd>
97<dl>
98<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
99</font></dt></dl>
100</dd>
101<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
102</font></dt></dl>
103 <p>
104<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
105<tr bgcolor="#ffc8d8">
106<td colspan=3 valign=bottom>&nbsp;<br>
107<font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr>
108\x20\x20\x20\x20
109<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
110<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
111<tr><td>&nbsp;</td>
112<td width="100%%">Methods defined here:<br>
113<dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow,&nbsp;I&nbsp;have&nbsp;no&nbsp;function!</tt></dd></dl>
114
115</td></tr></table> <p>
116<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
117<tr bgcolor="#ffc8d8">
118<td colspan=3 valign=bottom>&nbsp;<br>
119<font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
120\x20\x20\x20\x20
121<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
122<td width="100%%">Data descriptors defined here:<br>
123<dl><dt><strong>__dict__</strong></dt>
124<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
125</dl>
126<dl><dt><strong>__weakref__</strong></dt>
127<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
128</dl>
129<hr>
130Data and other attributes defined here:<br>
131<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
132
133</td></tr></table></td></tr></table><p>
134<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
135<tr bgcolor="#eeaa77">
136<td colspan=3 valign=bottom>&nbsp;<br>
137<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
138\x20\x20\x20\x20
139<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
140<td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This&nbsp;function&nbsp;solves&nbsp;all&nbsp;of&nbsp;the&nbsp;world's&nbsp;problems:<br>
141hunger<br>
142lack&nbsp;of&nbsp;Python<br>
143war</tt></dd></dl>
144 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
145</td></tr></table><p>
146<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
147<tr bgcolor="#55aa55">
148<td colspan=3 valign=bottom>&nbsp;<br>
149<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
150\x20\x20\x20\x20
151<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
152<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
153<strong>__credits__</strong> = 'Nobody'<br>
Georg Brandl8632cc22008-05-18 16:32:48 +0000154<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
155<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
156<tr bgcolor="#7799ee">
157<td colspan=3 valign=bottom>&nbsp;<br>
158<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
159\x20\x20\x20\x20
160<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
161<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
162<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
163<tr bgcolor="#7799ee">
164<td colspan=3 valign=bottom>&nbsp;<br>
165<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
166\x20\x20\x20\x20
167<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
168<td width="100%%">Nobody</td></tr></table>
169""".strip()
170
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000171
Georg Brandl8632cc22008-05-18 16:32:48 +0000172# output pattern for missing module
173missing_pattern = "no Python documentation found for '%s'"
174
R. David Murrayef087da2009-06-23 18:02:46 +0000175# output pattern for module with bad imports
176badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s"
177
Georg Brandl8632cc22008-05-18 16:32:48 +0000178def run_pydoc(module_name, *args):
179 """
180 Runs pydoc on the specified module. Returns the stripped
181 output of pydoc.
182 """
183 cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name]
Antoine Pitrou161df942009-10-30 21:41:22 +0000184 try:
185 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
186 return output.strip()
187 finally:
188 reap_children()
Georg Brandl8632cc22008-05-18 16:32:48 +0000189
190def get_pydoc_html(module):
191 "Returns pydoc generated output as html"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000192 doc = pydoc.HTMLDoc()
193 output = doc.docmodule(module)
194 loc = doc.getdocloc(pydoc_mod) or ""
195 if loc:
196 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
197 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000198
199def get_pydoc_text(module):
200 "Returns pydoc generated output as text"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000201 doc = pydoc.TextDoc()
202 loc = doc.getdocloc(pydoc_mod) or ""
203 if loc:
204 loc = "\nMODULE DOCS\n " + loc + "\n"
205
206 output = doc.docmodule(module)
Georg Brandl8632cc22008-05-18 16:32:48 +0000207
208 # cleanup the extra text formatting that pydoc preforms
209 patt = re.compile('\b.')
210 output = patt.sub('', output)
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000211 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000212
213def print_diffs(text1, text2):
214 "Prints unified diffs for two texts"
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000215 lines1 = text1.splitlines(True)
216 lines2 = text2.splitlines(True)
217 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
218 tofile='got')
219 print '\n' + ''.join(diffs)
Georg Brandl8632cc22008-05-18 16:32:48 +0000220
Georg Brandl8632cc22008-05-18 16:32:48 +0000221
Georg Brandl8632cc22008-05-18 16:32:48 +0000222class PyDocDocTest(unittest.TestCase):
223
R. David Murrayf28fd242010-02-23 00:24:49 +0000224 @unittest.skipIf(sys.flags.optimize >= 2,
225 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000226 def test_html_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000227 result, doc_loc = get_pydoc_html(pydoc_mod)
Georg Brandlb7e419e2008-05-20 08:10:03 +0000228 mod_file = inspect.getabsfile(pydoc_mod)
Amaury Forgeot d'Arc8e8de4a2008-06-10 21:37:15 +0000229 if sys.platform == 'win32':
230 import nturl2path
231 mod_url = nturl2path.pathname2url(mod_file)
232 else:
233 mod_url = mod_file
234 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000235 if result != expected_html:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000236 print_diffs(expected_html, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000237 self.fail("outputs are not equal, see diff above")
238
R. David Murrayf28fd242010-02-23 00:24:49 +0000239 @unittest.skipIf(sys.flags.optimize >= 2,
240 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000241 def test_text_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000242 result, doc_loc = get_pydoc_text(pydoc_mod)
243 expected_text = expected_text_pattern % \
244 (inspect.getabsfile(pydoc_mod), doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000245 if result != expected_text:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000246 print_diffs(expected_text, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000247 self.fail("outputs are not equal, see diff above")
248
Brian Curtinaeb2e822010-03-31 03:10:21 +0000249 def test_issue8225(self):
250 # Test issue8225 to ensure no doc link appears for xml.etree
251 result, doc_loc = get_pydoc_text(xml.etree)
252 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
253
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000254 def test_not_here(self):
255 missing_module = "test.i_am_not_here"
256 result = run_pydoc(missing_module)
257 expected = missing_pattern % missing_module
258 self.assertEqual(expected, result,
259 "documentation for missing module found")
260
R. David Murrayef087da2009-06-23 18:02:46 +0000261 def test_badimport(self):
262 # This tests the fix for issue 5230, where if pydoc found the module
263 # but the module had an internal import error pydoc would report no doc
264 # found.
265 modname = 'testmod_xyzzy'
266 testpairs = (
267 ('i_am_not_here', 'i_am_not_here'),
268 ('test.i_am_not_here_either', 'i_am_not_here_either'),
269 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
270 ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
271 ('test.{}'.format(modname), modname),
272 )
273
274 @contextmanager
275 def newdirinpath(dir):
276 os.mkdir(dir)
277 sys.path.insert(0, dir)
278 yield
279 sys.path.pop(0)
280 rmtree(dir)
281
282 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
283 env['PYTHONPATH'] = TESTFN
284 fullmodname = os.path.join(TESTFN, modname)
285 sourcefn = fullmodname + os.extsep + "py"
286 for importstring, expectedinmsg in testpairs:
287 f = open(sourcefn, 'w')
288 f.write("import {}\n".format(importstring))
289 f.close()
290 try:
291 result = run_pydoc(modname)
292 finally:
293 forget(modname)
294 expected = badimport_pattern % (modname, expectedinmsg)
295 self.assertEqual(expected, result)
296
R. David Murrayd67ea7d2009-05-27 20:07:21 +0000297 def test_input_strip(self):
298 missing_module = " test.i_am_not_here "
299 result = run_pydoc(missing_module)
300 expected = missing_pattern % missing_module.strip()
301 self.assertEqual(expected, result,
302 "white space was not stripped from module name "
303 "or other error output mismatch")
304
Ezio Melottie511fc72010-02-16 23:26:09 +0000305 def test_stripid(self):
306 # test with strings, other implementations might have different repr()
307 stripid = pydoc.stripid
308 # strip the id
309 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
310 '<function stripid>')
311 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
312 '<function stripid>')
313 # nothing to strip, return the same text
314 self.assertEqual(stripid('42'), '42')
315 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
316 "<type 'exceptions.Exception'>")
317
Georg Brandl8632cc22008-05-18 16:32:48 +0000318
319class TestDescriptions(unittest.TestCase):
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000320
Georg Brandl8632cc22008-05-18 16:32:48 +0000321 def test_module(self):
322 # Check that pydocfodder module can be described
323 from test import pydocfodder
324 doc = pydoc.render_doc(pydocfodder)
Ezio Melottiaa980582010-01-23 23:04:36 +0000325 self.assertIn("pydocfodder", doc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000326
327 def test_classic_class(self):
328 class C: "Classic class"
329 c = C()
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000330 self.assertEqual(pydoc.describe(C), 'class C')
331 self.assertEqual(pydoc.describe(c), 'instance of C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000332 expected = 'instance of C in module %s' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000333 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000334
335 def test_class(self):
336 class C(object): "New-style class"
337 c = C()
338
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000339 self.assertEqual(pydoc.describe(C), 'class C')
340 self.assertEqual(pydoc.describe(c), 'C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000341 expected = 'C in module %s object' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000342 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000343
Raymond Hettinger9aa5a342011-03-25 16:00:13 -0700344 def test_namedtuple_public_underscore(self):
345 NT = namedtuple('NT', ['abc', 'def'], rename=True)
346 with captured_stdout() as help_io:
347 help(NT)
348 helptext = help_io.getvalue()
349 self.assertIn('_1', helptext)
350 self.assertIn('_replace', helptext)
351 self.assertIn('_asdict', helptext)
352
Georg Brandl8632cc22008-05-18 16:32:48 +0000353
354def test_main():
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000355 test.test_support.run_unittest(PyDocDocTest,
Georg Brandl8632cc22008-05-18 16:32:48 +0000356 TestDescriptions)
357
358if __name__ == "__main__":
359 test_main()