blob: 603755ad0ee89a920f8bb2d3f287796c8de5174c [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
Benjamin Peterson0289b152009-06-28 17:22:03 +00003import os.path
Georg Brandlb533e262008-05-25 18:19:30 +00004import difflib
5import subprocess
6import re
7import pydoc
8import inspect
9import unittest
10import test.support
Brian Curtin49c284c2010-03-31 03:19:28 +000011import xml.etree
Benjamin Peterson0289b152009-06-28 17:22:03 +000012from contextlib import contextmanager
Antoine Pitrouaecd3b72009-10-30 21:45:40 +000013from test.support import (
14 TESTFN, forget, rmtree, EnvironmentVarGuard, reap_children)
Georg Brandlb533e262008-05-25 18:19:30 +000015
16from test import pydoc_mod
17
Florent Xicluna085a6562010-03-06 14:04:16 +000018# Just in case sys.modules["test"] has the optional attribute __loader__.
19if hasattr(pydoc_mod, "__loader__"):
20 del pydoc_mod.__loader__
21
Barry Warsaw28a691b2010-04-17 00:19:56 +000022expected_text_pattern = """
Georg Brandlb533e262008-05-25 18:19:30 +000023NAME
24 test.pydoc_mod - This is a test module for test_pydoc
25
26FILE
27 %s
28%s
29CLASSES
30 builtins.object
31 A
32 B
33\x20\x20\x20\x20
34 class A(builtins.object)
35 | Hello and goodbye
36 |\x20\x20
37 | Methods defined here:
38 |\x20\x20
39 | __init__()
40 | Wow, I have no function!
41 |\x20\x20
42 | ----------------------------------------------------------------------
43 | Data descriptors defined here:
44 |\x20\x20
45 | __dict__
46 | dictionary for instance variables (if defined)
47 |\x20\x20
48 | __weakref__
49 | list of weak references to the object (if defined)
50\x20\x20\x20\x20
51 class B(builtins.object)
52 | Data descriptors defined here:
53 |\x20\x20
54 | __dict__
55 | dictionary for instance variables (if defined)
56 |\x20\x20
57 | __weakref__
58 | list of weak references to the object (if defined)
59 |\x20\x20
60 | ----------------------------------------------------------------------
61 | Data and other attributes defined here:
62 |\x20\x20
63 | NO_MEANING = 'eggs'
64
65FUNCTIONS
66 doc_func()
67 This function solves all of the world's problems:
68 hunger
69 lack of Python
70 war
71\x20\x20\x20\x20
72 nodoc_func()
73
74DATA
75 __author__ = 'Benjamin Peterson'
76 __credits__ = 'Nobody'
Georg Brandlb533e262008-05-25 18:19:30 +000077 __version__ = '1.2.3.4'
78
79VERSION
80 1.2.3.4
81
82AUTHOR
83 Benjamin Peterson
84
85CREDITS
86 Nobody
87""".strip()
88
Barry Warsaw28a691b2010-04-17 00:19:56 +000089expected_html_pattern = """
Georg Brandlb533e262008-05-25 18:19:30 +000090<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
91<tr bgcolor="#7799ee">
92<td valign=bottom>&nbsp;<br>
93<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
94><td align=right valign=bottom
95><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
96 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
97<p>
98<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
99<tr bgcolor="#ee77aa">
100<td colspan=3 valign=bottom>&nbsp;<br>
101<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
102\x20\x20\x20\x20
103<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
104<td width="100%%"><dl>
105<dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a>
106</font></dt><dd>
107<dl>
108<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
109</font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
110</font></dt></dl>
111</dd>
112</dl>
113 <p>
114<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
115<tr bgcolor="#ffc8d8">
116<td colspan=3 valign=bottom>&nbsp;<br>
117<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>
118\x20\x20\x20\x20
119<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
120<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
121<tr><td>&nbsp;</td>
122<td width="100%%">Methods defined here:<br>
123<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>
124
125<hr>
126Data descriptors defined here:<br>
127<dl><dt><strong>__dict__</strong></dt>
128<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
129</dl>
130<dl><dt><strong>__weakref__</strong></dt>
131<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
132</dl>
133</td></tr></table> <p>
134<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
135<tr bgcolor="#ffc8d8">
136<td colspan=3 valign=bottom>&nbsp;<br>
137<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>
138\x20\x20\x20\x20
139<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
140<td width="100%%">Data descriptors defined here:<br>
141<dl><dt><strong>__dict__</strong></dt>
142<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
143</dl>
144<dl><dt><strong>__weakref__</strong></dt>
145<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
146</dl>
147<hr>
148Data and other attributes defined here:<br>
149<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
150
151</td></tr></table></td></tr></table><p>
152<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
153<tr bgcolor="#eeaa77">
154<td colspan=3 valign=bottom>&nbsp;<br>
155<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
156\x20\x20\x20\x20
157<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
158<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>
159hunger<br>
160lack&nbsp;of&nbsp;Python<br>
161war</tt></dd></dl>
162 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
163</td></tr></table><p>
164<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
165<tr bgcolor="#55aa55">
166<td colspan=3 valign=bottom>&nbsp;<br>
167<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
168\x20\x20\x20\x20
169<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
170<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
171<strong>__credits__</strong> = 'Nobody'<br>
Georg Brandlb533e262008-05-25 18:19:30 +0000172<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
173<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
174<tr bgcolor="#7799ee">
175<td colspan=3 valign=bottom>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
179<td width="100%%">Benjamin&nbsp;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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
186<td width="100%%">Nobody</td></tr></table>
Barry Warsaw28a691b2010-04-17 00:19:56 +0000187""".strip() # ' <- emacs turd
Georg Brandlb533e262008-05-25 18:19:30 +0000188
189
190# output pattern for missing module
191missing_pattern = "no Python documentation found for '%s'"
192
Benjamin Peterson0289b152009-06-28 17:22:03 +0000193# output pattern for module with bad imports
194badimport_pattern = "problem in %s - ImportError: No module named %s"
195
Georg Brandlb533e262008-05-25 18:19:30 +0000196def 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 Pitrouaecd3b72009-10-30 21:45:40 +0000202 try:
203 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
204 return output.strip()
205 finally:
206 reap_children()
Georg Brandlb533e262008-05-25 18:19:30 +0000207
208def 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
217def 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
226 # cleanup the extra text formatting that pydoc preforms
227 patt = re.compile('\b.')
228 output = patt.sub('', output)
229 return output.strip(), loc
230
231def print_diffs(text1, text2):
232 "Prints unified diffs for two texts"
233 lines1 = text1.splitlines(True)
234 lines2 = text2.splitlines(True)
235 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
236 tofile='got')
237 print('\n' + ''.join(diffs))
238
239
240class PyDocDocTest(unittest.TestCase):
241
R. David Murray378c0cf2010-02-24 01:46:21 +0000242 @unittest.skipIf(sys.flags.optimize >= 2,
243 "Docstrings are omitted with -O2 and above")
Georg Brandlb533e262008-05-25 18:19:30 +0000244 def test_html_doc(self):
245 result, doc_loc = get_pydoc_html(pydoc_mod)
246 mod_file = inspect.getabsfile(pydoc_mod)
Benjamin Petersonc5e94642008-06-14 23:04:46 +0000247 if sys.platform == 'win32':
248 import nturl2path
249 mod_url = nturl2path.pathname2url(mod_file)
250 else:
251 mod_url = mod_file
252 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandlb533e262008-05-25 18:19:30 +0000253 if result != expected_html:
254 print_diffs(expected_html, result)
255 self.fail("outputs are not equal, see diff above")
256
R. David Murray378c0cf2010-02-24 01:46:21 +0000257 @unittest.skipIf(sys.flags.optimize >= 2,
258 "Docstrings are omitted with -O2 and above")
Georg Brandlb533e262008-05-25 18:19:30 +0000259 def test_text_doc(self):
260 result, doc_loc = get_pydoc_text(pydoc_mod)
261 expected_text = expected_text_pattern % \
262 (inspect.getabsfile(pydoc_mod), doc_loc)
263 if result != expected_text:
264 print_diffs(expected_text, result)
265 self.fail("outputs are not equal, see diff above")
266
Brian Curtin49c284c2010-03-31 03:19:28 +0000267 def test_issue8225(self):
268 # Test issue8225 to ensure no doc link appears for xml.etree
269 result, doc_loc = get_pydoc_text(xml.etree)
270 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
271
Georg Brandlb533e262008-05-25 18:19:30 +0000272 def test_not_here(self):
273 missing_module = "test.i_am_not_here"
274 result = str(run_pydoc(missing_module), 'ascii')
275 expected = missing_pattern % missing_module
276 self.assertEqual(expected, result,
277 "documentation for missing module found")
278
Benjamin Peterson0289b152009-06-28 17:22:03 +0000279 def test_badimport(self):
280 # This tests the fix for issue 5230, where if pydoc found the module
281 # but the module had an internal import error pydoc would report no doc
282 # found.
283 modname = 'testmod_xyzzy'
284 testpairs = (
285 ('i_am_not_here', 'i_am_not_here'),
286 ('test.i_am_not_here_either', 'i_am_not_here_either'),
287 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
Barry Warsaw28a691b2010-04-17 00:19:56 +0000288 ('i_am_not_here.{}'.format(modname),
289 'i_am_not_here.{}'.format(modname)),
Benjamin Peterson0289b152009-06-28 17:22:03 +0000290 ('test.{}'.format(modname), modname),
291 )
292
293 @contextmanager
294 def newdirinpath(dir):
295 os.mkdir(dir)
296 sys.path.insert(0, dir)
297 yield
298 sys.path.pop(0)
299 rmtree(dir)
300
301 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
302 env['PYTHONPATH'] = TESTFN
303 fullmodname = os.path.join(TESTFN, modname)
304 sourcefn = fullmodname + os.extsep + "py"
305 for importstring, expectedinmsg in testpairs:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000306 with open(sourcefn, 'w') as f:
307 f.write("import {}\n".format(importstring))
Benjamin Peterson0289b152009-06-28 17:22:03 +0000308 try:
309 result = run_pydoc(modname).decode("ascii")
310 finally:
311 forget(modname)
312 expected = badimport_pattern % (modname, expectedinmsg)
313 self.assertEqual(expected, result)
314
R. David Murray1f1b9d32009-05-27 20:56:59 +0000315 def test_input_strip(self):
316 missing_module = " test.i_am_not_here "
317 result = str(run_pydoc(missing_module), 'ascii')
318 expected = missing_pattern % missing_module.strip()
319 self.assertEqual(expected, result)
320
Ezio Melotti412c95a2010-02-16 23:31:04 +0000321 def test_stripid(self):
322 # test with strings, other implementations might have different repr()
323 stripid = pydoc.stripid
324 # strip the id
325 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
326 '<function stripid>')
327 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
328 '<function stripid>')
329 # nothing to strip, return the same text
330 self.assertEqual(stripid('42'), '42')
331 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
332 "<type 'exceptions.Exception'>")
333
Georg Brandlb533e262008-05-25 18:19:30 +0000334
335class TestDescriptions(unittest.TestCase):
336
337 def test_module(self):
338 # Check that pydocfodder module can be described
339 from test import pydocfodder
340 doc = pydoc.render_doc(pydocfodder)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000341 self.assertIn("pydocfodder", doc)
Georg Brandlb533e262008-05-25 18:19:30 +0000342
343 def test_classic_class(self):
344 class C: "Classic class"
345 c = C()
346 self.assertEqual(pydoc.describe(C), 'class C')
347 self.assertEqual(pydoc.describe(c), 'C')
348 expected = 'C in module %s' % __name__
Benjamin Peterson577473f2010-01-19 00:09:57 +0000349 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000350
351 def test_class(self):
352 class C(object): "New-style class"
353 c = C()
354
355 self.assertEqual(pydoc.describe(C), 'class C')
356 self.assertEqual(pydoc.describe(c), 'C')
357 expected = 'C in module %s object' % __name__
Benjamin Peterson577473f2010-01-19 00:09:57 +0000358 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000359
360
361def test_main():
362 test.support.run_unittest(PyDocDocTest, TestDescriptions)
363
364if __name__ == "__main__":
365 test_main()