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 One of the positive user-facing effects is that the "request" object
can now be used in closures. can now be used in closures.
- fixed version comparison in pytest.importskip(modname, minverstring)
Changes between 2.4.1 and 2.4.2 Changes between 2.4.1 and 2.4.2
----------------------------------- -----------------------------------

View File

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

View File

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