Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Side by Side Diff: Lib/test/test_pydoc.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_posix.py ('k') | Lib/test/test_raise.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import os 1 import os
2 import sys 2 import sys
3 import difflib 3 import difflib
4 import inspect 4 import inspect
5 import pydoc 5 import pydoc
6 import keyword 6 import keyword
7 import re 7 import re
8 import string 8 import string
9 import subprocess 9 import subprocess
10 import test.support 10 import test.support
11 import time 11 import time
12 import unittest 12 import unittest
13 import xml.etree 13 import xml.etree
14 import textwrap 14 import textwrap
15 from io import StringIO 15 from io import StringIO
16 from collections import namedtuple 16 from collections import namedtuple
17 from contextlib import contextmanager 17 from contextlib import contextmanager
18 from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard, \
19 reap_children, captured_output, captured_stdout
18 20
19 from test.script_helper import assert_python_ok
20 from test.support import (
21 TESTFN, forget, rmtree, EnvironmentVarGuard,
22 reap_children, reap_threads, captured_output, captured_stdout, unlink
23 )
24 from test import pydoc_mod 21 from test import pydoc_mod
25 22
26 try: 23 try:
27 import threading 24 import threading
28 except ImportError: 25 except ImportError:
29 threading = None 26 threading = None
30 27
31 # Just in case sys.modules["test"] has the optional attribute __loader__. 28 # Just in case sys.modules["test"] has the optional attribute __loader__.
32 if hasattr(pydoc_mod, "__loader__"): 29 if hasattr(pydoc_mod, "__loader__"):
33 del pydoc_mod.__loader__ 30 del pydoc_mod.__loader__
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 <td width="100%%">Nobody</td></tr></table> 192 <td width="100%%">Nobody</td></tr></table>
196 """.strip() # ' <- emacs turd 193 """.strip() # ' <- emacs turd
197 194
198 195
199 # output pattern for missing module 196 # output pattern for missing module
200 missing_pattern = "no Python documentation found for '%s'" 197 missing_pattern = "no Python documentation found for '%s'"
201 198
202 # output pattern for module with bad imports 199 # output pattern for module with bad imports
203 badimport_pattern = "problem in %s - ImportError: No module named %r" 200 badimport_pattern = "problem in %s - ImportError: No module named %r"
204 201
205 def run_pydoc(module_name, *args, **env): 202 def run_pydoc(module_name, *args):
206 """ 203 """
207 Runs pydoc on the specified module. Returns the stripped 204 Runs pydoc on the specified module. Returns the stripped
208 output of pydoc. 205 output of pydoc.
209 """ 206 """
210 args = args + (module_name,) 207 cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name]
211 rc, out, err = assert_python_ok(pydoc.__file__, *args, **env) 208 try:
212 return out.strip() 209 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
210 return output.strip()
211 finally:
212 reap_children()
213 213
214 def get_pydoc_html(module): 214 def get_pydoc_html(module):
215 "Returns pydoc generated output as html" 215 "Returns pydoc generated output as html"
216 doc = pydoc.HTMLDoc() 216 doc = pydoc.HTMLDoc()
217 output = doc.docmodule(module) 217 output = doc.docmodule(module)
218 loc = doc.getdocloc(pydoc_mod) or "" 218 loc = doc.getdocloc(pydoc_mod) or ""
219 if loc: 219 if loc:
220 loc = "<br><a href=\"" + loc + "\">Module Docs</a>" 220 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
221 return output.strip(), loc 221 return output.strip(), loc
222 222
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), 305 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
306 ('i_am_not_here.{}'.format(modname), 306 ('i_am_not_here.{}'.format(modname),
307 'i_am_not_here.{}'.format(modname)), 307 'i_am_not_here.{}'.format(modname)),
308 ('test.{}'.format(modname), modname), 308 ('test.{}'.format(modname), modname),
309 ) 309 )
310 310
311 @contextmanager 311 @contextmanager
312 def newdirinpath(dir): 312 def newdirinpath(dir):
313 os.mkdir(dir) 313 os.mkdir(dir)
314 sys.path.insert(0, dir) 314 sys.path.insert(0, dir)
315 try: 315 yield
316 yield 316 sys.path.pop(0)
317 finally: 317 rmtree(dir)
318 sys.path.pop(0)
319 rmtree(dir)
320 318
321 with newdirinpath(TESTFN): 319 with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
320 env['PYTHONPATH'] = TESTFN
322 fullmodname = os.path.join(TESTFN, modname) 321 fullmodname = os.path.join(TESTFN, modname)
323 sourcefn = fullmodname + os.extsep + "py" 322 sourcefn = fullmodname + os.extsep + "py"
324 for importstring, expectedinmsg in testpairs: 323 for importstring, expectedinmsg in testpairs:
325 with open(sourcefn, 'w') as f: 324 with open(sourcefn, 'w') as f:
326 f.write("import {}\n".format(importstring)) 325 f.write("import {}\n".format(importstring))
327 try: 326 try:
328 result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii ") 327 result = run_pydoc(modname).decode("ascii")
329 finally: 328 finally:
330 forget(modname) 329 forget(modname)
331 expected = badimport_pattern % (modname, expectedinmsg) 330 expected = badimport_pattern % (modname, expectedinmsg)
332 self.assertEqual(expected, result) 331 self.assertEqual(expected, result)
333 332
334 def test_input_strip(self): 333 def test_input_strip(self):
335 missing_module = " test.i_am_not_here " 334 missing_module = " test.i_am_not_here "
336 result = str(run_pydoc(missing_module), 'ascii') 335 result = str(run_pydoc(missing_module), 'ascii')
337 expected = missing_pattern % missing_module.strip() 336 expected = missing_pattern % missing_module.strip()
338 self.assertEqual(expected, result) 337 self.assertEqual(expected, result)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 pydoc.getpager = getpager_old 387 pydoc.getpager = getpager_old
389 388
390 def test_namedtuple_public_underscore(self): 389 def test_namedtuple_public_underscore(self):
391 NT = namedtuple('NT', ['abc', 'def'], rename=True) 390 NT = namedtuple('NT', ['abc', 'def'], rename=True)
392 with captured_stdout() as help_io: 391 with captured_stdout() as help_io:
393 help(NT) 392 help(NT)
394 helptext = help_io.getvalue() 393 helptext = help_io.getvalue()
395 self.assertIn('_1', helptext) 394 self.assertIn('_1', helptext)
396 self.assertIn('_replace', helptext) 395 self.assertIn('_replace', helptext)
397 self.assertIn('_asdict', helptext) 396 self.assertIn('_asdict', helptext)
398
399 def test_synopsis(self):
400 self.addCleanup(unlink, TESTFN)
401 for encoding in ('ISO-8859-1', 'UTF-8'):
402 with open(TESTFN, 'w', encoding=encoding) as script:
403 if encoding != 'UTF-8':
404 print('#coding: {}'.format(encoding), file=script)
405 print('"""line 1: h\xe9', file=script)
406 print('line 2: hi"""', file=script)
407 synopsis = pydoc.synopsis(TESTFN, {})
408 self.assertEqual(synopsis, 'line 1: h\xe9')
409 397
410 398
411 class TestDescriptions(unittest.TestCase): 399 class TestDescriptions(unittest.TestCase):
412 400
413 def test_module(self): 401 def test_module(self):
414 # Check that pydocfodder module can be described 402 # Check that pydocfodder module can be described
415 from test import pydocfodder 403 from test import pydocfodder
416 doc = pydoc.render_doc(pydocfodder) 404 doc = pydoc.render_doc(pydocfodder)
417 self.assertIn("pydocfodder", doc) 405 self.assertIn("pydocfodder", doc)
418 406
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 text = pydoc._url_handler(url, "text/html") 476 text = pydoc._url_handler(url, "text/html")
489 result = get_html_title(text) 477 result = get_html_title(text)
490 self.assertEqual(result, title) 478 self.assertEqual(result, title)
491 479
492 480
493 class TestHelper(unittest.TestCase): 481 class TestHelper(unittest.TestCase):
494 def test_keywords(self): 482 def test_keywords(self):
495 self.assertEqual(sorted(pydoc.Helper.keywords), 483 self.assertEqual(sorted(pydoc.Helper.keywords),
496 sorted(keyword.kwlist)) 484 sorted(keyword.kwlist))
497 485
498 @reap_threads
499 def test_main(): 486 def test_main():
500 try: 487 test.support.run_unittest(PydocDocTest,
501 test.support.run_unittest(PydocDocTest, 488 TestDescriptions,
502 TestDescriptions, 489 PydocServerTest,
503 PydocServerTest, 490 PydocUrlHandlerTest,
504 PydocUrlHandlerTest, 491 TestHelper,
505 TestHelper, 492 )
506 )
507 finally:
508 reap_children()
509 493
510 if __name__ == "__main__": 494 if __name__ == "__main__":
511 test_main() 495 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_posix.py ('k') | Lib/test/test_raise.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+