Commit b1da5693 authored by Ned Deily's avatar Ned Deily

Issue #7425: Refactor test_pydoc test case for '-k' behavior and add

new test cases for importing bad packages and unreadable packages dirs.
parent 9802b9ae
import os import os
import sys import sys
import difflib import difflib
import subprocess
import __builtin__ import __builtin__
import re import re
import pydoc import pydoc
...@@ -10,10 +9,10 @@ import keyword ...@@ -10,10 +9,10 @@ import keyword
import unittest import unittest
import xml.etree import xml.etree
import test.test_support import test.test_support
from contextlib import contextmanager
from collections import namedtuple from collections import namedtuple
from test.script_helper import assert_python_ok
from test.test_support import ( from test.test_support import (
TESTFN, forget, rmtree, EnvironmentVarGuard, reap_children, captured_stdout) TESTFN, rmtree, reap_children, captured_stdout)
from test import pydoc_mod from test import pydoc_mod
...@@ -176,17 +175,15 @@ missing_pattern = "no Python documentation found for '%s'" ...@@ -176,17 +175,15 @@ missing_pattern = "no Python documentation found for '%s'"
# output pattern for module with bad imports # output pattern for module with bad imports
badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s" badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s"
def run_pydoc(module_name, *args): def run_pydoc(module_name, *args, **env):
""" """
Runs pydoc on the specified module. Returns the stripped Runs pydoc on the specified module. Returns the stripped
output of pydoc. output of pydoc.
""" """
cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name] args = args + (module_name,)
try: # do not write bytecode files to avoid caching errors
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env)
return output.strip() return out.strip()
finally:
reap_children()
def get_pydoc_html(module): def get_pydoc_html(module):
"Returns pydoc generated output as html" "Returns pydoc generated output as html"
...@@ -259,42 +256,6 @@ class PyDocDocTest(unittest.TestCase): ...@@ -259,42 +256,6 @@ class PyDocDocTest(unittest.TestCase):
self.assertEqual(expected, result, self.assertEqual(expected, result,
"documentation for missing module found") "documentation for missing module found")
def test_badimport(self):
# This tests the fix for issue 5230, where if pydoc found the module
# but the module had an internal import error pydoc would report no doc
# found.
modname = 'testmod_xyzzy'
testpairs = (
('i_am_not_here', 'i_am_not_here'),
('test.i_am_not_here_either', 'i_am_not_here_either'),
('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
('test.{}'.format(modname), modname),
)
@contextmanager
def newdirinpath(dir):
os.mkdir(dir)
sys.path.insert(0, dir)
yield
sys.path.pop(0)
rmtree(dir)
with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
env['PYTHONPATH'] = TESTFN
fullmodname = os.path.join(TESTFN, modname)
sourcefn = fullmodname + os.extsep + "py"
for importstring, expectedinmsg in testpairs:
f = open(sourcefn, 'w')
f.write("import {}\n".format(importstring))
f.close()
try:
result = run_pydoc(modname)
finally:
forget(modname)
expected = badimport_pattern % (modname, expectedinmsg)
self.assertEqual(expected, result)
def test_input_strip(self): def test_input_strip(self):
missing_module = " test.i_am_not_here " missing_module = " test.i_am_not_here "
result = run_pydoc(missing_module) result = run_pydoc(missing_module)
...@@ -317,6 +278,55 @@ class PyDocDocTest(unittest.TestCase): ...@@ -317,6 +278,55 @@ class PyDocDocTest(unittest.TestCase):
"<type 'exceptions.Exception'>") "<type 'exceptions.Exception'>")
class PydocImportTest(unittest.TestCase):
def setUp(self):
self.test_dir = os.mkdir(TESTFN)
self.addCleanup(rmtree, TESTFN)
def test_badimport(self):
# This tests the fix for issue 5230, where if pydoc found the module
# but the module had an internal import error pydoc would report no doc
# found.
modname = 'testmod_xyzzy'
testpairs = (
('i_am_not_here', 'i_am_not_here'),
('test.i_am_not_here_either', 'i_am_not_here_either'),
('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
('i_am_not_here.{}'.format(modname),
'i_am_not_here.{}'.format(modname)),
('test.{}'.format(modname), modname),
)
sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py"
for importstring, expectedinmsg in testpairs:
with open(sourcefn, 'w') as f:
f.write("import {}\n".format(importstring))
result = run_pydoc(modname, PYTHONPATH=TESTFN)
expected = badimport_pattern % (modname, expectedinmsg)
self.assertEqual(expected, result)
def test_apropos_with_bad_package(self):
# Issue 7425 - pydoc -k failed when bad package on path
pkgdir = os.path.join(TESTFN, "syntaxerr")
os.mkdir(pkgdir)
badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py"
with open(badsyntax, 'w') as f:
f.write("invalid python syntax = $1\n")
result = run_pydoc('nothing', '-k', PYTHONPATH=TESTFN)
self.assertEqual('', result)
def test_apropos_with_unreadable_dir(self):
# Issue 7367 - pydoc -k failed when unreadable dir on path
self.unreadable_dir = os.path.join(TESTFN, "unreadable")
os.mkdir(self.unreadable_dir, 0)
self.addCleanup(os.rmdir, self.unreadable_dir)
# Note, on Windows the directory appears to be still
# readable so this is not really testing the issue there
result = run_pydoc('nothing', '-k', PYTHONPATH=TESTFN)
self.assertEqual('', result)
class TestDescriptions(unittest.TestCase): class TestDescriptions(unittest.TestCase):
def test_module(self): def test_module(self):
...@@ -376,9 +386,13 @@ class TestHelper(unittest.TestCase): ...@@ -376,9 +386,13 @@ class TestHelper(unittest.TestCase):
def test_main(): def test_main():
test.test_support.run_unittest(PyDocDocTest, try:
TestDescriptions, test.test_support.run_unittest(PyDocDocTest,
TestHelper) PydocImportTest,
TestDescriptions,
TestHelper)
finally:
reap_children()
if __name__ == "__main__": if __name__ == "__main__":
test_main() test_main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment