fixed version comparison in pytest.importskip(modname, minverstring)

This commit is contained in:
holger krekel 2013-11-21 13:53:04 +01:00
parent 2e90aaf7af
commit fc073cb81c
3 changed files with 16 additions and 14 deletions

View File

@ -69,6 +69,8 @@ Unreleased
One of the positive user-facing effects is that the "request" object
can now be used in closures.
- fixed version comparison in pytest.importskip(modname, minverstring)
Changes between 2.4.1 and 2.4.2
-----------------------------------

View File

@ -336,7 +336,7 @@ class SetupState(object):
except Exception:
# XXX Only first exception will be seen by user,
# ideally all should be reported.
if not exc:
if exc is None:
exc = sys.exc_info()
if exc:
py.builtin._reraise(*exc)
@ -459,25 +459,25 @@ fail.Exception = Failed
def importorskip(modname, minversion=None):
""" return imported module if it has a higher __version__ than the
optionally specified 'minversion' - otherwise call py.test.skip()
with a message detailing the mismatch.
""" return imported module if it has at least "minversion" as its
__version__ attribute. If no minversion is specified the a skip
is only triggered if the module can not be imported.
Note that version comparison only works with simple version strings
like "1.2.3" but not "1.2.3.dev1" or others.
"""
__tracebackhide__ = True
compile(modname, '', 'eval') # to catch syntaxerrors
try:
__import__(modname)
except ImportError:
py.test.skip("could not import %r" %(modname,))
skip("could not import %r" %(modname,))
mod = sys.modules[modname]
if minversion is None:
return mod
verattr = getattr(mod, '__version__', None)
if isinstance(minversion, str):
minver = minversion.split(".")
else:
minver = list(minversion)
if verattr is None or verattr.split(".") < minver:
py.test.skip("module %r has __version__ %r, required is: %r" %(
modname, verattr, minversion))
def intver(verstring):
return [int(x) for x in verstring.split(".")]
if verattr is None or intver(verattr) < intver(minversion):
skip("module %r has __version__ %r, required is: %r" %(
modname, verattr, minversion))
return mod

View File

@ -469,11 +469,11 @@ def test_importorskip():
assert path.purebasename == "test_runner"
pytest.raises(SyntaxError, "py.test.importorskip('x y z')")
pytest.raises(SyntaxError, "py.test.importorskip('x=y')")
path = importorskip("py", minversion=".".join(py.__version__))
path = importorskip("py", minversion=py.__version__)
mod = py.std.types.ModuleType("hello123")
mod.__version__ = "1.3"
pytest.raises(pytest.skip.Exception, """
py.test.importorskip("hello123", minversion="5.0")
py.test.importorskip("hello123", minversion="1.3.1")
""")
except pytest.skip.Exception:
print(py.code.ExceptionInfo())