blob: 00a2ada6c596a6699602ece1295fb5dd71e7e05f [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
9import unittest
10import test.support
Benjamin Petersond76c8da2009-06-28 17:35:48 +000011from contextlib import contextmanager
12from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard
Georg Brandlb533e262008-05-25 18:19:30 +000013
14from test import pydoc_mod
15
16expected_text_pattern = \
17"""
18NAME
19 test.pydoc_mod - This is a test module for test_pydoc
20
21FILE
22 %s
23%s
24CLASSES
25 builtins.object
26 A
27 B
28\x20\x20\x20\x20
29 class A(builtins.object)
30 | Hello and goodbye
31 |\x20\x20
32 | Methods defined here:
33 |\x20\x20
34 | __init__()
35 | Wow, I have no function!
36 |\x20\x20
37 | ----------------------------------------------------------------------
38 | Data descriptors defined here:
39 |\x20\x20
40 | __dict__
41 | dictionary for instance variables (if defined)
42 |\x20\x20
43 | __weakref__
44 | list of weak references to the object (if defined)
45\x20\x20\x20\x20
46 class B(builtins.object)
47 | Data descriptors defined here:
48 |\x20\x20
49 | __dict__
50 | dictionary for instance variables (if defined)
51 |\x20\x20
52 | __weakref__
53 | list of weak references to the object (if defined)
54 |\x20\x20
55 | ----------------------------------------------------------------------
56 | Data and other attributes defined here:
57 |\x20\x20
58 | NO_MEANING = 'eggs'
59
60FUNCTIONS
61 doc_func()
62 This function solves all of the world's problems:
63 hunger
64 lack of Python
65 war
66\x20\x20\x20\x20
67 nodoc_func()
68
69DATA
70 __author__ = 'Benjamin Peterson'
71 __credits__ = 'Nobody'
Georg Brandlb533e262008-05-25 18:19:30 +000072 __version__ = '1.2.3.4'
73
74VERSION
75 1.2.3.4
76
77AUTHOR
78 Benjamin Peterson
79
80CREDITS
81 Nobody
82""".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>
166<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
167<strong>__credits__</strong> = 'Nobody'<br>
Georg Brandlb533e262008-05-25 18:19:30 +0000168<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
169<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>
183""".strip()
184
185
186# output pattern for missing module
187missing_pattern = "no Python documentation found for '%s'"
188
Benjamin Petersond76c8da2009-06-28 17:35:48 +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]
198 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read()
199 return output.strip()
200
201def get_pydoc_html(module):
202 "Returns pydoc generated output as html"
203 doc = pydoc.HTMLDoc()
204 output = doc.docmodule(module)
205 loc = doc.getdocloc(pydoc_mod) or ""
206 if loc:
207 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
208 return output.strip(), loc
209
210def get_pydoc_text(module):
211 "Returns pydoc generated output as text"
212 doc = pydoc.TextDoc()
213 loc = doc.getdocloc(pydoc_mod) or ""
214 if loc:
215 loc = "\nMODULE DOCS\n " + loc + "\n"
216
217 output = doc.docmodule(module)
218
219 # cleanup the extra text formatting that pydoc preforms
220 patt = re.compile('\b.')
221 output = patt.sub('', output)
222 return output.strip(), loc
223
224def print_diffs(text1, text2):
225 "Prints unified diffs for two texts"
226 lines1 = text1.splitlines(True)
227 lines2 = text2.splitlines(True)
228 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
229 tofile='got')
230 print('\n' + ''.join(diffs))
231
232
233class PyDocDocTest(unittest.TestCase):
234
235 def test_html_doc(self):
236 result, doc_loc = get_pydoc_html(pydoc_mod)
237 mod_file = inspect.getabsfile(pydoc_mod)
Benjamin Petersonc5e94642008-06-14 23:04:46 +0000238 if sys.platform == 'win32':
239 import nturl2path
240 mod_url = nturl2path.pathname2url(mod_file)
241 else:
242 mod_url = mod_file
243 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandlb533e262008-05-25 18:19:30 +0000244 if result != expected_html:
245 print_diffs(expected_html, result)
246 self.fail("outputs are not equal, see diff above")
247
248 def test_text_doc(self):
249 result, doc_loc = get_pydoc_text(pydoc_mod)
250 expected_text = expected_text_pattern % \
251 (inspect.getabsfile(pydoc_mod), doc_loc)
252 if result != expected_text:
253 print_diffs(expected_text, result)
254 self.fail("outputs are not equal, see diff above")
255
256 def test_not_here(self):
257 missing_module = "test.i_am_not_here"
258 result = str(run_pydoc(missing_module), 'ascii')
259 expected = missing_pattern % missing_module
260 self.assertEqual(expected, result,
261 "documentation for missing module found")
262
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000263 def test_badimport(self):
264 # This tests the fix for issue 5230, where if pydoc found the module
265 # but the module had an internal import error pydoc would report no doc
266 # found.
267 modname = 'testmod_xyzzy'
268 testpairs = (
269 ('i_am_not_here', 'i_am_not_here'),
270 ('test.i_am_not_here_either', 'i_am_not_here_either'),
271 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
272 ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
273 ('test.{}'.format(modname), modname),
274 )
275
276 @contextmanager
277 def newdirinpath(dir):
278 os.mkdir(dir)
279 sys.path.insert(0, dir)
280 yield
281 sys.path.pop(0)
282 rmtree(dir)
283
284 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
285 env['PYTHONPATH'] = TESTFN
286 fullmodname = os.path.join(TESTFN, modname)
287 sourcefn = fullmodname + os.extsep + "py"
288 for importstring, expectedinmsg in testpairs:
289 f = open(sourcefn, 'w')
290 f.write("import {}\n".format(importstring))
291 f.close()
292 try:
293 result = run_pydoc(modname).decode("ascii")
294 finally:
295 forget(modname)
296 expected = badimport_pattern % (modname, expectedinmsg)
297 self.assertEqual(expected, result)
298
R. David Murray1f1b9d32009-05-27 20:56:59 +0000299 def test_input_strip(self):
300 missing_module = " test.i_am_not_here "
301 result = str(run_pydoc(missing_module), 'ascii')
302 expected = missing_pattern % missing_module.strip()
303 self.assertEqual(expected, result)
304
Georg Brandlb533e262008-05-25 18:19:30 +0000305
306class TestDescriptions(unittest.TestCase):
307
308 def test_module(self):
309 # Check that pydocfodder module can be described
310 from test import pydocfodder
311 doc = pydoc.render_doc(pydocfodder)
312 self.assert_("pydocfodder" in doc)
313
314 def test_classic_class(self):
315 class C: "Classic class"
316 c = C()
317 self.assertEqual(pydoc.describe(C), 'class C')
318 self.assertEqual(pydoc.describe(c), 'C')
319 expected = 'C in module %s' % __name__
320 self.assert_(expected in pydoc.render_doc(c))
321
322 def test_class(self):
323 class C(object): "New-style class"
324 c = C()
325
326 self.assertEqual(pydoc.describe(C), 'class C')
327 self.assertEqual(pydoc.describe(c), 'C')
328 expected = 'C in module %s object' % __name__
329 self.assert_(expected in pydoc.render_doc(c))
330
331
332def test_main():
333 test.support.run_unittest(PyDocDocTest, TestDescriptions)
334
335if __name__ == "__main__":
336 test_main()