blob: 0685488edb29767f4815cf45450c9a3919dc2a15 [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
Antoine Pitrou161df942009-10-30 21:41:22 +000013from test.test_support import (
14 TESTFN, forget, rmtree, EnvironmentVarGuard, reap_children)
Benjamin Petersonf5c38da2008-05-18 20:48:07 +000015
Georg Brandl8632cc22008-05-18 16:32:48 +000016from test import pydoc_mod
17
18expected_text_pattern = \
19"""
20NAME
21 test.pydoc_mod - This is a test module for test_pydoc
22
23FILE
24 %s
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000025%s
Georg Brandl8632cc22008-05-18 16:32:48 +000026CLASSES
27 __builtin__.object
28 B
29 A
30\x20\x20\x20\x20
31 class A
32 | Hello and goodbye
33 |\x20\x20
34 | Methods defined here:
35 |\x20\x20
36 | __init__()
37 | Wow, I have no function!
38\x20\x20\x20\x20
39 class B(__builtin__.object)
40 | Data descriptors defined here:
41 |\x20\x20
42 | __dict__
43 | dictionary for instance variables (if defined)
44 |\x20\x20
45 | __weakref__
46 | list of weak references to the object (if defined)
47 |\x20\x20
48 | ----------------------------------------------------------------------
49 | Data and other attributes defined here:
50 |\x20\x20
51 | NO_MEANING = 'eggs'
52
53FUNCTIONS
54 doc_func()
55 This function solves all of the world's problems:
56 hunger
57 lack of Python
58 war
59\x20\x20\x20\x20
60 nodoc_func()
61
62DATA
63 __author__ = 'Benjamin Peterson'
64 __credits__ = 'Nobody'
Georg Brandl8632cc22008-05-18 16:32:48 +000065 __version__ = '1.2.3.4'
66
67VERSION
68 1.2.3.4
69
70AUTHOR
71 Benjamin Peterson
72
73CREDITS
74 Nobody
75""".strip()
76
77expected_html_pattern = \
78"""
79<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
80<tr bgcolor="#7799ee">
81<td valign=bottom>&nbsp;<br>
82<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
83><td align=right valign=bottom
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000084><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 +000085 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
86<p>
87<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
88<tr bgcolor="#ee77aa">
89<td colspan=3 valign=bottom>&nbsp;<br>
90<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
91\x20\x20\x20\x20
92<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
93<td width="100%%"><dl>
94<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
95</font></dt><dd>
96<dl>
97<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
98</font></dt></dl>
99</dd>
100<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
101</font></dt></dl>
102 <p>
103<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
104<tr bgcolor="#ffc8d8">
105<td colspan=3 valign=bottom>&nbsp;<br>
106<font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr>
107\x20\x20\x20\x20
108<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
109<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
110<tr><td>&nbsp;</td>
111<td width="100%%">Methods defined here:<br>
112<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>
113
114</td></tr></table> <p>
115<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
116<tr bgcolor="#ffc8d8">
117<td colspan=3 valign=bottom>&nbsp;<br>
118<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>
119\x20\x20\x20\x20
120<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
121<td width="100%%">Data descriptors defined here:<br>
122<dl><dt><strong>__dict__</strong></dt>
123<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
124</dl>
125<dl><dt><strong>__weakref__</strong></dt>
126<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
127</dl>
128<hr>
129Data and other attributes defined here:<br>
130<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
131
132</td></tr></table></td></tr></table><p>
133<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
134<tr bgcolor="#eeaa77">
135<td colspan=3 valign=bottom>&nbsp;<br>
136<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
137\x20\x20\x20\x20
138<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
139<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>
140hunger<br>
141lack&nbsp;of&nbsp;Python<br>
142war</tt></dd></dl>
143 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
144</td></tr></table><p>
145<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
146<tr bgcolor="#55aa55">
147<td colspan=3 valign=bottom>&nbsp;<br>
148<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
149\x20\x20\x20\x20
150<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
151<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
152<strong>__credits__</strong> = 'Nobody'<br>
Georg Brandl8632cc22008-05-18 16:32:48 +0000153<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
154<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
155<tr bgcolor="#7799ee">
156<td colspan=3 valign=bottom>&nbsp;<br>
157<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
158\x20\x20\x20\x20
159<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
160<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
161<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
162<tr bgcolor="#7799ee">
163<td colspan=3 valign=bottom>&nbsp;<br>
164<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
165\x20\x20\x20\x20
166<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
167<td width="100%%">Nobody</td></tr></table>
168""".strip()
169
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000170
Georg Brandl8632cc22008-05-18 16:32:48 +0000171# output pattern for missing module
172missing_pattern = "no Python documentation found for '%s'"
173
R. David Murrayef087da2009-06-23 18:02:46 +0000174# output pattern for module with bad imports
175badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s"
176
Georg Brandl8632cc22008-05-18 16:32:48 +0000177def run_pydoc(module_name, *args):
178 """
179 Runs pydoc on the specified module. Returns the stripped
180 output of pydoc.
181 """
182 cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name]
Antoine Pitrou161df942009-10-30 21:41:22 +0000183 try:
184 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
185 return output.strip()
186 finally:
187 reap_children()
Georg Brandl8632cc22008-05-18 16:32:48 +0000188
189def get_pydoc_html(module):
190 "Returns pydoc generated output as html"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000191 doc = pydoc.HTMLDoc()
192 output = doc.docmodule(module)
193 loc = doc.getdocloc(pydoc_mod) or ""
194 if loc:
195 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
196 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000197
198def get_pydoc_text(module):
199 "Returns pydoc generated output as text"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000200 doc = pydoc.TextDoc()
201 loc = doc.getdocloc(pydoc_mod) or ""
202 if loc:
203 loc = "\nMODULE DOCS\n " + loc + "\n"
204
205 output = doc.docmodule(module)
Georg Brandl8632cc22008-05-18 16:32:48 +0000206
207 # cleanup the extra text formatting that pydoc preforms
208 patt = re.compile('\b.')
209 output = patt.sub('', output)
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000210 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000211
212def print_diffs(text1, text2):
213 "Prints unified diffs for two texts"
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000214 lines1 = text1.splitlines(True)
215 lines2 = text2.splitlines(True)
216 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
217 tofile='got')
218 print '\n' + ''.join(diffs)
Georg Brandl8632cc22008-05-18 16:32:48 +0000219
Georg Brandl8632cc22008-05-18 16:32:48 +0000220
Georg Brandl8632cc22008-05-18 16:32:48 +0000221class PyDocDocTest(unittest.TestCase):
222
R. David Murrayf28fd242010-02-23 00:24:49 +0000223 @unittest.skipIf(sys.flags.optimize >= 2,
224 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000225 def test_html_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000226 result, doc_loc = get_pydoc_html(pydoc_mod)
Georg Brandlb7e419e2008-05-20 08:10:03 +0000227 mod_file = inspect.getabsfile(pydoc_mod)
Amaury Forgeot d'Arc8e8de4a2008-06-10 21:37:15 +0000228 if sys.platform == 'win32':
229 import nturl2path
230 mod_url = nturl2path.pathname2url(mod_file)
231 else:
232 mod_url = mod_file
233 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000234 if result != expected_html:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000235 print_diffs(expected_html, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000236 self.fail("outputs are not equal, see diff above")
237
R. David Murrayf28fd242010-02-23 00:24:49 +0000238 @unittest.skipIf(sys.flags.optimize >= 2,
239 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000240 def test_text_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000241 result, doc_loc = get_pydoc_text(pydoc_mod)
242 expected_text = expected_text_pattern % \
243 (inspect.getabsfile(pydoc_mod), doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000244 if result != expected_text:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000245 print_diffs(expected_text, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000246 self.fail("outputs are not equal, see diff above")
247
Brian Curtinaeb2e822010-03-31 03:10:21 +0000248 def test_issue8225(self):
249 # Test issue8225 to ensure no doc link appears for xml.etree
250 result, doc_loc = get_pydoc_text(xml.etree)
251 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
252
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000253 def test_not_here(self):
254 missing_module = "test.i_am_not_here"
255 result = run_pydoc(missing_module)
256 expected = missing_pattern % missing_module
257 self.assertEqual(expected, result,
258 "documentation for missing module found")
259
R. David Murrayef087da2009-06-23 18:02:46 +0000260 def test_badimport(self):
261 # This tests the fix for issue 5230, where if pydoc found the module
262 # but the module had an internal import error pydoc would report no doc
263 # found.
264 modname = 'testmod_xyzzy'
265 testpairs = (
266 ('i_am_not_here', 'i_am_not_here'),
267 ('test.i_am_not_here_either', 'i_am_not_here_either'),
268 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
269 ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
270 ('test.{}'.format(modname), modname),
271 )
272
273 @contextmanager
274 def newdirinpath(dir):
275 os.mkdir(dir)
276 sys.path.insert(0, dir)
277 yield
278 sys.path.pop(0)
279 rmtree(dir)
280
281 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
282 env['PYTHONPATH'] = TESTFN
283 fullmodname = os.path.join(TESTFN, modname)
284 sourcefn = fullmodname + os.extsep + "py"
285 for importstring, expectedinmsg in testpairs:
286 f = open(sourcefn, 'w')
287 f.write("import {}\n".format(importstring))
288 f.close()
289 try:
290 result = run_pydoc(modname)
291 finally:
292 forget(modname)
293 expected = badimport_pattern % (modname, expectedinmsg)
294 self.assertEqual(expected, result)
295
R. David Murrayd67ea7d2009-05-27 20:07:21 +0000296 def test_input_strip(self):
297 missing_module = " test.i_am_not_here "
298 result = run_pydoc(missing_module)
299 expected = missing_pattern % missing_module.strip()
300 self.assertEqual(expected, result,
301 "white space was not stripped from module name "
302 "or other error output mismatch")
303
Ezio Melottie511fc72010-02-16 23:26:09 +0000304 def test_stripid(self):
305 # test with strings, other implementations might have different repr()
306 stripid = pydoc.stripid
307 # strip the id
308 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
309 '<function stripid>')
310 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
311 '<function stripid>')
312 # nothing to strip, return the same text
313 self.assertEqual(stripid('42'), '42')
314 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
315 "<type 'exceptions.Exception'>")
316
Georg Brandl8632cc22008-05-18 16:32:48 +0000317
318class TestDescriptions(unittest.TestCase):
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000319
Georg Brandl8632cc22008-05-18 16:32:48 +0000320 def test_module(self):
321 # Check that pydocfodder module can be described
322 from test import pydocfodder
323 doc = pydoc.render_doc(pydocfodder)
Ezio Melottiaa980582010-01-23 23:04:36 +0000324 self.assertIn("pydocfodder", doc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000325
326 def test_classic_class(self):
327 class C: "Classic class"
328 c = C()
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000329 self.assertEqual(pydoc.describe(C), 'class C')
330 self.assertEqual(pydoc.describe(c), 'instance of C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000331 expected = 'instance of C in module %s' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000332 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000333
334 def test_class(self):
335 class C(object): "New-style class"
336 c = C()
337
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000338 self.assertEqual(pydoc.describe(C), 'class C')
339 self.assertEqual(pydoc.describe(c), 'C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000340 expected = 'C in module %s object' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000341 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000342
343
344def test_main():
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000345 test.test_support.run_unittest(PyDocDocTest,
Georg Brandl8632cc22008-05-18 16:32:48 +0000346 TestDescriptions)
347
348if __name__ == "__main__":
349 test_main()