(fixes issue83) don't try to import conftest from an invalid package path, refine path.pyimport() logic

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-04-27 15:49:13 +02:00
parent c8d78177b9
commit 8131f5bdc0
4 changed files with 23 additions and 1 deletions

View File

@ -456,7 +456,8 @@ class LocalPath(FSBase):
def pypkgpath(self, pkgname=None): def pypkgpath(self, pkgname=None):
""" return the path's package path by looking for the given """ return the path's package path by looking for the given
pkgname. If pkgname is None then look for the last pkgname. If pkgname is None then look for the last
directory upwards which still contains an __init__.py. directory upwards which still contains an __init__.py
and whose basename is python-importable.
Return None if a pkgpath can not be determined. Return None if a pkgpath can not be determined.
""" """
pkgpath = None pkgpath = None
@ -464,6 +465,8 @@ class LocalPath(FSBase):
if pkgname is None: if pkgname is None:
if parent.check(file=1): if parent.check(file=1):
continue continue
if not isimportable(parent.basename):
break
if parent.join('__init__.py').check(): if parent.join('__init__.py').check():
pkgpath = parent pkgpath = parent
continue continue
@ -797,3 +800,6 @@ def autopath(globs=None):
ret.pkgdir = pkgdir ret.pkgdir = pkgdir
return ret return ret
def isimportable(name):
return name[0].isalpha() and name.isalnum()

View File

@ -355,6 +355,14 @@ def test_pypkgdir(tmpdir):
assert pkg.pypkgpath() == pkg assert pkg.pypkgpath() == pkg
assert pkg.join('subdir', '__init__.py').pypkgpath() == pkg assert pkg.join('subdir', '__init__.py').pypkgpath() == pkg
def test_pypkgdir_unimportable(tmpdir):
pkg = tmpdir.ensure('pkg1-1', dir=1) # unimportable
pkg.ensure("__init__.py")
subdir = pkg.ensure("subdir/__init__.py").dirpath()
assert subdir.pypkgpath() == subdir
assert subdir.ensure("xyz.py").pypkgpath() == subdir
assert not pkg.pypkgpath()
def test_homedir(): def test_homedir():
homedir = py.path.local._gethomedir() homedir = py.path.local._gethomedir()
assert homedir.check(dir=1) assert homedir.check(dir=1)

View File

@ -169,6 +169,7 @@ class TestConfigApi_getinitialnodes:
assert col.config is config assert col.config is config
def test_pkgfile(self, testdir, tmpdir): def test_pkgfile(self, testdir, tmpdir):
tmpdir = tmpdir.join("subdir")
x = tmpdir.ensure("x.py") x = tmpdir.ensure("x.py")
tmpdir.ensure("__init__.py") tmpdir.ensure("__init__.py")
config = testdir.reparseconfig([x]) config = testdir.reparseconfig([x])

View File

@ -90,6 +90,13 @@ class TestConftestValueAccessGlobal:
assert path.dirpath() == basedir.join("adir", "b") assert path.dirpath() == basedir.join("adir", "b")
assert path.purebasename == "conftest" assert path.purebasename == "conftest"
def test_conftest_in_nonpkg_with_init(tmpdir):
tmpdir.ensure("adir-1.0/conftest.py").write("a=1 ; Directory = 3")
tmpdir.ensure("adir-1.0/b/conftest.py").write("b=2 ; a = 1.5")
tmpdir.ensure("adir-1.0/b/__init__.py")
tmpdir.ensure("adir-1.0/__init__.py")
conftest = ConftestWithSetinitial(tmpdir.join("adir-1.0", "b"))
def test_conftestcutdir(testdir): def test_conftestcutdir(testdir):
conf = testdir.makeconftest("") conf = testdir.makeconftest("")
p = testdir.mkdir("x") p = testdir.mkdir("x")