blob: fa49bb8ea0385722b74d08980dd4481f41ba4567 [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
Benjamin Petersond76c8da2009-06-28 17:35:48 +00003import os.path
Georg Brandlb533e262008-05-25 18:19:30 +00004import difflib
5import subprocess
6import re
7import pydoc
8import inspect
Ezio Melottib185a042011-04-28 07:42:55 +03009import keyword
Georg Brandlb533e262008-05-25 18:19:30 +000010import unittest
11import test.support
Brian Curtin0d8a1dd2010-03-31 03:22:46 +000012import xml.etree
Benjamin Petersond76c8da2009-06-28 17:35:48 +000013from contextlib import contextmanager
14from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard
Georg Brandlb533e262008-05-25 18:19:30 +000015
16from test import pydoc_mod
17
18expected_text_pattern = \
19"""
20NAME
21 test.pydoc_mod - This is a test module for test_pydoc
Georg Brandlb533e262008-05-25 18:19:30 +000022%s
23CLASSES
24 builtins.object
25 A
26 B
27\x20\x20\x20\x20
28 class A(builtins.object)
29 | Hello and goodbye
30 |\x20\x20
31 | Methods defined here:
32 |\x20\x20
33 | __init__()
34 | Wow, I have no function!
35 |\x20\x20
36 | ----------------------------------------------------------------------
37 | Data descriptors defined here:
38 |\x20\x20
39 | __dict__
40 | dictionary for instance variables (if defined)
41 |\x20\x20
42 | __weakref__
43 | list of weak references to the object (if defined)
44\x20\x20\x20\x20
45 class B(builtins.object)
46 | Data descriptors defined here:
47 |\x20\x20
48 | __dict__
49 | dictionary for instance variables (if defined)
50 |\x20\x20
51 | __weakref__
52 | list of weak references to the object (if defined)
53 |\x20\x20
54 | ----------------------------------------------------------------------
55 | Data and other attributes defined here:
56 |\x20\x20
57 | NO_MEANING = 'eggs'
58
59FUNCTIONS
60 doc_func()
61 This function solves all of the world's problems:
62 hunger
63 lack of Python
64 war
65\x20\x20\x20\x20
66 nodoc_func()
67
68DATA
Alexander Belopolsky4979b9b2010-11-18 01:58:16 +000069 __xyz__ = 'X, Y and Z'
Georg Brandlb533e262008-05-25 18:19:30 +000070
71VERSION
72 1.2.3.4
73
74AUTHOR
75 Benjamin Peterson
76
77CREDITS
78 Nobody
Alexander Belopolsky4979b9b2010-11-18 01:58:16 +000079
80FILE
81 %s
Georg Brandlb533e262008-05-25 18:19:30 +000082""".strip()
83
84expected_html_pattern = \
85"""
86<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
87<tr bgcolor="#7799ee">
88<td valign=bottom>&nbsp;<br>
89<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
90><td align=right valign=bottom
91><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
92 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
93<p>
94<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
95<tr bgcolor="#ee77aa">
96<td colspan=3 valign=bottom>&nbsp;<br>
97<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
98\x20\x20\x20\x20
99<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
100<td width="100%%"><dl>
101<dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a>
102</font></dt><dd>
103<dl>
104<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
105</font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
106</font></dt></dl>
107</dd>
108</dl>
109 <p>
110<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
111<tr bgcolor="#ffc8d8">
112<td colspan=3 valign=bottom>&nbsp;<br>
113<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>
114\x20\x20\x20\x20
115<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
116<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
117<tr><td>&nbsp;</td>
118<td width="100%%">Methods defined here:<br>
119<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>
120
121<hr>
122Data 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</td></tr></table> <p>
130<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
131<tr bgcolor="#ffc8d8">
132<td colspan=3 valign=bottom>&nbsp;<br>
133<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>
134\x20\x20\x20\x20
135<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
136<td width="100%%">Data descriptors defined here:<br>
137<dl><dt><strong>__dict__</strong></dt>
138<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
139</dl>
140<dl><dt><strong>__weakref__</strong></dt>
141<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
142</dl>
143<hr>
144Data and other attributes defined here:<br>
145<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
146
147</td></tr></table></td></tr></table><p>
148<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
149<tr bgcolor="#eeaa77">
150<td colspan=3 valign=bottom>&nbsp;<br>
151<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
152\x20\x20\x20\x20
153<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
154<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>
155hunger<br>
156lack&nbsp;of&nbsp;Python<br>
157war</tt></dd></dl>
158 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
159</td></tr></table><p>
160<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
161<tr bgcolor="#55aa55">
162<td colspan=3 valign=bottom>&nbsp;<br>
163<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
164\x20\x20\x20\x20
165<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
Alexander Belopolsky4979b9b2010-11-18 01:58:16 +0000166<td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p>
Georg Brandlb533e262008-05-25 18:19:30 +0000167<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
168<tr bgcolor="#7799ee">
169<td colspan=3 valign=bottom>&nbsp;<br>
170<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
171\x20\x20\x20\x20
172<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
173<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
174<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
175<tr bgcolor="#7799ee">
176<td colspan=3 valign=bottom>&nbsp;<br>
177<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
178\x20\x20\x20\x20
179<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
180<td width="100%%">Nobody</td></tr></table>
181""".strip()
182
183
184# output pattern for missing module
185missing_pattern = "no Python documentation found for '%s'"
186
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000187# output pattern for module with bad imports
188badimport_pattern = "problem in %s - ImportError: No module named %s"
189
Georg Brandlb533e262008-05-25 18:19:30 +0000190def run_pydoc(module_name, *args):
191 """
192 Runs pydoc on the specified module. Returns the stripped
193 output of pydoc.
194 """
195 cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name]
196 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read()
197 return output.strip()
198
199def get_pydoc_html(module):
200 "Returns pydoc generated output as html"
201 doc = pydoc.HTMLDoc()
202 output = doc.docmodule(module)
203 loc = doc.getdocloc(pydoc_mod) or ""
204 if loc:
205 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
206 return output.strip(), loc
207
208def get_pydoc_text(module):
209 "Returns pydoc generated output as text"
210 doc = pydoc.TextDoc()
211 loc = doc.getdocloc(pydoc_mod) or ""
212 if loc:
213 loc = "\nMODULE DOCS\n " + loc + "\n"
214
215 output = doc.docmodule(module)
216
217 # cleanup the extra text formatting that pydoc preforms
218 patt = re.compile('\b.')
219 output = patt.sub('', output)
220 return output.strip(), loc
221
222def print_diffs(text1, text2):
223 "Prints unified diffs for two texts"
224 lines1 = text1.splitlines(True)
225 lines2 = text2.splitlines(True)
226 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
227 tofile='got')
228 print('\n' + ''.join(diffs))
229
230
231class PyDocDocTest(unittest.TestCase):
232
233 def test_html_doc(self):
234 result, doc_loc = get_pydoc_html(pydoc_mod)
235 mod_file = inspect.getabsfile(pydoc_mod)
Benjamin Petersonc5e94642008-06-14 23:04:46 +0000236 if sys.platform == 'win32':
237 import nturl2path
238 mod_url = nturl2path.pathname2url(mod_file)
239 else:
240 mod_url = mod_file
241 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandlb533e262008-05-25 18:19:30 +0000242 if result != expected_html:
243 print_diffs(expected_html, result)
244 self.fail("outputs are not equal, see diff above")
245
246 def test_text_doc(self):
247 result, doc_loc = get_pydoc_text(pydoc_mod)
248 expected_text = expected_text_pattern % \
Alexander Belopolsky4979b9b2010-11-18 01:58:16 +0000249 (doc_loc, inspect.getabsfile(pydoc_mod))
Georg Brandlb533e262008-05-25 18:19:30 +0000250 if result != expected_text:
251 print_diffs(expected_text, result)
252 self.fail("outputs are not equal, see diff above")
253
Brian Curtin0d8a1dd2010-03-31 03:22:46 +0000254 def test_issue8225(self):
255 # Test issue8225 to ensure no doc link appears for xml.etree
256 result, doc_loc = get_pydoc_text(xml.etree)
257 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
258
Georg Brandlb533e262008-05-25 18:19:30 +0000259 def test_not_here(self):
260 missing_module = "test.i_am_not_here"
261 result = str(run_pydoc(missing_module), 'ascii')
262 expected = missing_pattern % missing_module
263 self.assertEqual(expected, result,
264 "documentation for missing module found")
265
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000266 def test_badimport(self):
267 # This tests the fix for issue 5230, where if pydoc found the module
268 # but the module had an internal import error pydoc would report no doc
269 # found.
270 modname = 'testmod_xyzzy'
271 testpairs = (
272 ('i_am_not_here', 'i_am_not_here'),
273 ('test.i_am_not_here_either', 'i_am_not_here_either'),
274 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
275 ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
276 ('test.{}'.format(modname), modname),
277 )
278
279 @contextmanager
280 def newdirinpath(dir):
281 os.mkdir(dir)
282 sys.path.insert(0, dir)
283 yield
284 sys.path.pop(0)
285 rmtree(dir)
286
287 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
288 env['PYTHONPATH'] = TESTFN
289 fullmodname = os.path.join(TESTFN, modname)
290 sourcefn = fullmodname + os.extsep + "py"
291 for importstring, expectedinmsg in testpairs:
292 f = open(sourcefn, 'w')
293 f.write("import {}\n".format(importstring))
294 f.close()
295 try:
296 result = run_pydoc(modname).decode("ascii")
297 finally:
298 forget(modname)
299 expected = badimport_pattern % (modname, expectedinmsg)
300 self.assertEqual(expected, result)
301
R. David Murray1f1b9d32009-05-27 20:56:59 +0000302 def test_input_strip(self):
303 missing_module = " test.i_am_not_here "
304 result = str(run_pydoc(missing_module), 'ascii')
305 expected = missing_pattern % missing_module.strip()
306 self.assertEqual(expected, result)
307
Ezio Melottibf839b92010-02-16 23:32:24 +0000308 def test_stripid(self):
309 # test with strings, other implementations might have different repr()
310 stripid = pydoc.stripid
311 # strip the id
312 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
313 '<function stripid>')
314 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
315 '<function stripid>')
316 # nothing to strip, return the same text
317 self.assertEqual(stripid('42'), '42')
318 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
319 "<type 'exceptions.Exception'>")
320
Georg Brandlb533e262008-05-25 18:19:30 +0000321
322class TestDescriptions(unittest.TestCase):
323
324 def test_module(self):
325 # Check that pydocfodder module can be described
326 from test import pydocfodder
327 doc = pydoc.render_doc(pydocfodder)
Georg Brandlab91fde2009-08-13 08:51:18 +0000328 self.assertTrue("pydocfodder" in doc)
Georg Brandlb533e262008-05-25 18:19:30 +0000329
330 def test_classic_class(self):
331 class C: "Classic class"
332 c = C()
333 self.assertEqual(pydoc.describe(C), 'class C')
334 self.assertEqual(pydoc.describe(c), 'C')
335 expected = 'C in module %s' % __name__
Georg Brandlab91fde2009-08-13 08:51:18 +0000336 self.assertTrue(expected in pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000337
338 def test_class(self):
339 class C(object): "New-style class"
340 c = C()
341
342 self.assertEqual(pydoc.describe(C), 'class C')
343 self.assertEqual(pydoc.describe(c), 'C')
344 expected = 'C in module %s object' % __name__
Georg Brandlab91fde2009-08-13 08:51:18 +0000345 self.assertTrue(expected in pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000346
347
Ezio Melottib185a042011-04-28 07:42:55 +0300348class TestHelper(unittest.TestCase):
349 def test_keywords(self):
350 self.assertEqual(sorted(pydoc.Helper.keywords),
351 sorted(keyword.kwlist))
352
Georg Brandlb533e262008-05-25 18:19:30 +0000353def test_main():
Ezio Melottib185a042011-04-28 07:42:55 +0300354 test.support.run_unittest(PyDocDocTest, TestDescriptions, TestHelper)
Georg Brandlb533e262008-05-25 18:19:30 +0000355
356if __name__ == "__main__":
357 test_main()