|
1 | 1 | import sys
|
2 | 2 | import os
|
| 3 | +import os.path |
3 | 4 | import difflib
|
4 | 5 | import subprocess
|
5 | 6 | import re
|
6 | 7 | import pydoc
|
7 | 8 | import inspect
|
8 | 9 | import unittest
|
9 | 10 | import test.support
|
| 11 | +from contextlib import contextmanager |
| 12 | +from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard |
10 | 13 |
|
11 | 14 | from test import pydoc_mod
|
12 | 15 |
|
@@ -183,6 +186,9 @@ class B(builtins.object)
|
183 | 186 | # output pattern for missing module
|
184 | 187 | missing_pattern = "no Python documentation found for '%s'"
|
185 | 188 |
|
| 189 | +# output pattern for module with bad imports |
| 190 | +badimport_pattern = "problem in %s - ImportError: No module named %s" |
| 191 | + |
186 | 192 | def run_pydoc(module_name, *args):
|
187 | 193 | """
|
188 | 194 | Runs pydoc on the specified module. Returns the stripped
|
@@ -254,6 +260,42 @@ def test_not_here(self):
|
254 | 260 | self.assertEqual(expected, result,
|
255 | 261 | "documentation for missing module found")
|
256 | 262 |
|
| 263 | + 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 | + |
257 | 299 | def test_input_strip(self):
|
258 | 300 | missing_module = " test.i_am_not_here "
|
259 | 301 | result = str(run_pydoc(missing_module), 'ascii')
|
|
0 commit comments