blob: af28348b8d078909351331e057fca354e1f5d971 [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
Georg Brandlb533e262008-05-25 18:19:30 +000025%s
26CLASSES
27 builtins.object
28 A
29 B
30\x20\x20\x20\x20
31 class A(builtins.object)
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
39 | ----------------------------------------------------------------------
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\x20\x20
48 class B(builtins.object)
49 | Data descriptors defined here:
50 |\x20\x20
51 | __dict__
52 | dictionary for instance variables (if defined)
53 |\x20\x20
54 | __weakref__
55 | list of weak references to the object (if defined)
56 |\x20\x20
57 | ----------------------------------------------------------------------
58 | Data and other attributes defined here:
59 |\x20\x20
60 | NO_MEANING = 'eggs'
61
62FUNCTIONS
63 doc_func()
64 This function solves all of the world's problems:
65 hunger
66 lack of Python
67 war
68\x20\x20\x20\x20
69 nodoc_func()
70
71DATA
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000072 __xyz__ = 'X, Y and Z'
Georg Brandlb533e262008-05-25 18:19:30 +000073
74VERSION
75 1.2.3.4
76
77AUTHOR
78 Benjamin Peterson
79
80CREDITS
81 Nobody
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000082
83FILE
84 %s
Georg Brandlb533e262008-05-25 18:19:30 +000085""".strip()
86
Barry Warsaw28a691b2010-04-17 00:19:56 +000087expected_html_pattern = """
Georg Brandlb533e262008-05-25 18:19:30 +000088<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
89<tr bgcolor="#7799ee">
90<td valign=bottom>&nbsp;<br>
91<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
92><td align=right valign=bottom
93><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
94 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
95<p>
96<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
97<tr bgcolor="#ee77aa">
98<td colspan=3 valign=bottom>&nbsp;<br>
99<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
100\x20\x20\x20\x20
101<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
102<td width="100%%"><dl>
103<dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a>
104</font></dt><dd>
105<dl>
106<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
107</font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
108</font></dt></dl>
109</dd>
110</dl>
111 <p>
112<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
113<tr bgcolor="#ffc8d8">
114<td colspan=3 valign=bottom>&nbsp;<br>
115<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>
116\x20\x20\x20\x20
117<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
118<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
119<tr><td>&nbsp;</td>
120<td width="100%%">Methods defined here:<br>
121<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>
122
123<hr>
124Data descriptors defined here:<br>
125<dl><dt><strong>__dict__</strong></dt>
126<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
127</dl>
128<dl><dt><strong>__weakref__</strong></dt>
129<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
130</dl>
131</td></tr></table> <p>
132<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
133<tr bgcolor="#ffc8d8">
134<td colspan=3 valign=bottom>&nbsp;<br>
135<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>
136\x20\x20\x20\x20
137<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
138<td width="100%%">Data descriptors defined here:<br>
139<dl><dt><strong>__dict__</strong></dt>
140<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
141</dl>
142<dl><dt><strong>__weakref__</strong></dt>
143<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
144</dl>
145<hr>
146Data and other attributes defined here:<br>
147<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
148
149</td></tr></table></td></tr></table><p>
150<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
151<tr bgcolor="#eeaa77">
152<td colspan=3 valign=bottom>&nbsp;<br>
153<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
154\x20\x20\x20\x20
155<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
156<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>
157hunger<br>
158lack&nbsp;of&nbsp;Python<br>
159war</tt></dd></dl>
160 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
161</td></tr></table><p>
162<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
163<tr bgcolor="#55aa55">
164<td colspan=3 valign=bottom>&nbsp;<br>
165<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
166\x20\x20\x20\x20
167<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000168<td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p>
Georg Brandlb533e262008-05-25 18:19:30 +0000169<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
170<tr bgcolor="#7799ee">
171<td colspan=3 valign=bottom>&nbsp;<br>
172<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
173\x20\x20\x20\x20
174<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
175<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
176<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
177<tr bgcolor="#7799ee">
178<td colspan=3 valign=bottom>&nbsp;<br>
179<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
180\x20\x20\x20\x20
181<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
182<td width="100%%">Nobody</td></tr></table>
Barry Warsaw28a691b2010-04-17 00:19:56 +0000183""".strip() # ' <- emacs turd
Georg Brandlb533e262008-05-25 18:19:30 +0000184
185
186# output pattern for missing module
187missing_pattern = "no Python documentation found for '%s'"
188
Benjamin Peterson0289b152009-06-28 17:22:03 +0000189# output pattern for module with bad imports
190badimport_pattern = "problem in %s - ImportError: No module named %s"
191
Georg Brandlb533e262008-05-25 18:19:30 +0000192def run_pydoc(module_name, *args):
193 """
194 Runs pydoc on the specified module. Returns the stripped
195 output of pydoc.
196 """
197 cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name]
Antoine Pitrouaecd3b72009-10-30 21:45:40 +0000198 try:
199 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
200 return output.strip()
201 finally:
202 reap_children()
Georg Brandlb533e262008-05-25 18:19:30 +0000203
204def get_pydoc_html(module):
205 "Returns pydoc generated output as html"
206 doc = pydoc.HTMLDoc()
207 output = doc.docmodule(module)
208 loc = doc.getdocloc(pydoc_mod) or ""
209 if loc:
210 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
211 return output.strip(), loc
212
213def get_pydoc_text(module):
214 "Returns pydoc generated output as text"
215 doc = pydoc.TextDoc()
216 loc = doc.getdocloc(pydoc_mod) or ""
217 if loc:
218 loc = "\nMODULE DOCS\n " + loc + "\n"
219
220 output = doc.docmodule(module)
221
222 # cleanup the extra text formatting that pydoc preforms
223 patt = re.compile('\b.')
224 output = patt.sub('', output)
225 return output.strip(), loc
226
227def print_diffs(text1, text2):
228 "Prints unified diffs for two texts"
229 lines1 = text1.splitlines(True)
230 lines2 = text2.splitlines(True)
231 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
232 tofile='got')
233 print('\n' + ''.join(diffs))
234
235
236class PyDocDocTest(unittest.TestCase):
237
R. David Murray378c0cf2010-02-24 01:46:21 +0000238 @unittest.skipIf(sys.flags.optimize >= 2,
239 "Docstrings are omitted with -O2 and above")
Georg Brandlb533e262008-05-25 18:19:30 +0000240 def test_html_doc(self):
241 result, doc_loc = get_pydoc_html(pydoc_mod)
242 mod_file = inspect.getabsfile(pydoc_mod)
Benjamin Petersonc5e94642008-06-14 23:04:46 +0000243 if sys.platform == 'win32':
244 import nturl2path
245 mod_url = nturl2path.pathname2url(mod_file)
246 else:
247 mod_url = mod_file
248 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandlb533e262008-05-25 18:19:30 +0000249 if result != expected_html:
250 print_diffs(expected_html, result)
251 self.fail("outputs are not equal, see diff above")
252
R. David Murray378c0cf2010-02-24 01:46:21 +0000253 @unittest.skipIf(sys.flags.optimize >= 2,
254 "Docstrings are omitted with -O2 and above")
Georg Brandlb533e262008-05-25 18:19:30 +0000255 def test_text_doc(self):
256 result, doc_loc = get_pydoc_text(pydoc_mod)
257 expected_text = expected_text_pattern % \
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000258 (doc_loc, inspect.getabsfile(pydoc_mod))
Georg Brandlb533e262008-05-25 18:19:30 +0000259 if result != expected_text:
260 print_diffs(expected_text, result)
261 self.fail("outputs are not equal, see diff above")
262
Brian Curtin49c284c2010-03-31 03:19:28 +0000263 def test_issue8225(self):
264 # Test issue8225 to ensure no doc link appears for xml.etree
265 result, doc_loc = get_pydoc_text(xml.etree)
266 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
267
Georg Brandlb533e262008-05-25 18:19:30 +0000268 def test_not_here(self):
269 missing_module = "test.i_am_not_here"
270 result = str(run_pydoc(missing_module), 'ascii')
271 expected = missing_pattern % missing_module
272 self.assertEqual(expected, result,
273 "documentation for missing module found")
274
Benjamin Peterson0289b152009-06-28 17:22:03 +0000275 def test_badimport(self):
276 # This tests the fix for issue 5230, where if pydoc found the module
277 # but the module had an internal import error pydoc would report no doc
278 # found.
279 modname = 'testmod_xyzzy'
280 testpairs = (
281 ('i_am_not_here', 'i_am_not_here'),
282 ('test.i_am_not_here_either', 'i_am_not_here_either'),
283 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
Barry Warsaw28a691b2010-04-17 00:19:56 +0000284 ('i_am_not_here.{}'.format(modname),
285 'i_am_not_here.{}'.format(modname)),
Benjamin Peterson0289b152009-06-28 17:22:03 +0000286 ('test.{}'.format(modname), modname),
287 )
288
289 @contextmanager
290 def newdirinpath(dir):
291 os.mkdir(dir)
292 sys.path.insert(0, dir)
293 yield
294 sys.path.pop(0)
295 rmtree(dir)
296
297 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
298 env['PYTHONPATH'] = TESTFN
299 fullmodname = os.path.join(TESTFN, modname)
300 sourcefn = fullmodname + os.extsep + "py"
301 for importstring, expectedinmsg in testpairs:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000302 with open(sourcefn, 'w') as f:
303 f.write("import {}\n".format(importstring))
Benjamin Peterson0289b152009-06-28 17:22:03 +0000304 try:
305 result = run_pydoc(modname).decode("ascii")
306 finally:
307 forget(modname)
308 expected = badimport_pattern % (modname, expectedinmsg)
309 self.assertEqual(expected, result)
310
R. David Murray1f1b9d32009-05-27 20:56:59 +0000311 def test_input_strip(self):
312 missing_module = " test.i_am_not_here "
313 result = str(run_pydoc(missing_module), 'ascii')
314 expected = missing_pattern % missing_module.strip()
315 self.assertEqual(expected, result)
316
Ezio Melotti412c95a2010-02-16 23:31:04 +0000317 def test_stripid(self):
318 # test with strings, other implementations might have different repr()
319 stripid = pydoc.stripid
320 # strip the id
321 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
322 '<function stripid>')
323 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
324 '<function stripid>')
325 # nothing to strip, return the same text
326 self.assertEqual(stripid('42'), '42')
327 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
328 "<type 'exceptions.Exception'>")
329
Georg Brandlb533e262008-05-25 18:19:30 +0000330
331class TestDescriptions(unittest.TestCase):
332
333 def test_module(self):
334 # Check that pydocfodder module can be described
335 from test import pydocfodder
336 doc = pydoc.render_doc(pydocfodder)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000337 self.assertIn("pydocfodder", doc)
Georg Brandlb533e262008-05-25 18:19:30 +0000338
339 def test_classic_class(self):
340 class C: "Classic class"
341 c = C()
342 self.assertEqual(pydoc.describe(C), 'class C')
343 self.assertEqual(pydoc.describe(c), 'C')
344 expected = 'C in module %s' % __name__
Benjamin Peterson577473f2010-01-19 00:09:57 +0000345 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000346
347 def test_class(self):
348 class C(object): "New-style class"
349 c = C()
350
351 self.assertEqual(pydoc.describe(C), 'class C')
352 self.assertEqual(pydoc.describe(c), 'C')
353 expected = 'C in module %s object' % __name__
Benjamin Peterson577473f2010-01-19 00:09:57 +0000354 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000355
356
357def test_main():
358 test.support.run_unittest(PyDocDocTest, TestDescriptions)
359
360if __name__ == "__main__":
361 test_main()