fix issue116 : --doctestmodules also works in the presence of __init__.py files, done by fixing the underlyingly used path.pyimport()

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-09-04 09:21:35 +02:00
parent d8fcc96563
commit 95bafbccd1
4 changed files with 17 additions and 6 deletions

View File

@ -1,3 +1,8 @@
Changes between 1.3.3 and XXX
==================================================
- fix issue116: --doctestmodules works in the presence of __init__.py files as well
Changes between 1.3.2 and 1.3.3
==================================================

View File

@ -519,6 +519,8 @@ class LocalPath(FSBase):
pkg = __import__(pkgpath.basename, None, None, [])
names = self.new(ext='').relto(pkgpath.dirpath())
names = names.split(self.sep)
if names and names[-1] == "__init__":
names.pop()
modname = ".".join(names)
else:
# no package scope, still make it possible
@ -532,7 +534,8 @@ class LocalPath(FSBase):
elif modfile.endswith('$py.class'):
modfile = modfile[:-9] + '.py'
if modfile.endswith("__init__.py"):
modfile = modfile[:-12]
if self.basename != "__init__.py":
modfile = modfile[:-12]
if not self.samefile(modfile):
raise self.ImportMismatchError(modname, modfile, self)
return mod

View File

@ -306,9 +306,11 @@ class TestImport:
def test_pyimport_dir(self, tmpdir):
p = tmpdir.join("hello_123")
p.ensure("__init__.py")
p_init = p.ensure("__init__.py")
m = p.pyimport()
assert m.__name__ == "hello_123"
m = p_init.pyimport()
assert m.__name__ == "hello_123"
def test_pyimport_execfile_different_name(self, path1):
obj = path1.join('execfile.py').pyimport(modname="0x.y.z")

View File

@ -1,4 +1,5 @@
from py._plugin.pytest_doctest import DoctestModule, DoctestTextfile
import py
pytest_plugins = ["pytest_doctest"]
@ -73,16 +74,16 @@ class TestDoctests:
reprec = testdir.inline_run(p, "--doctest-modules")
reprec.assertoutcome(failed=1)
def test_doctestmodule_external(self, testdir):
p = testdir.makepyfile("""
#
def test_doctestmodule_external_and_issue116(self, testdir):
p = testdir.mkpydir("hello")
p.join("__init__.py").write(py.code.Source("""
def somefunc():
'''
>>> i = 0
>>> i + 1
2
'''
""")
"""))
result = testdir.runpytest(p, "--doctest-modules")
result.stdout.fnmatch_lines([
'004 *>>> i = 0',