From 0e05a4fbcfa40aef921471c63ee494b83c7a3333 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Thu, 1 Sep 2011 16:19:16 +0200 Subject: [PATCH 1/3] Improve --pyargs. Don't evaluate modules and do nto show 'module not found' if ImportError is thrown in the module. --- _pytest/main.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/_pytest/main.py b/_pytest/main.py index ac306a867..d0d7c2dc3 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -2,7 +2,7 @@ import py import pytest, _pytest -import os, sys +import os, sys, imp tracebackcutdir = py.path.local(_pytest.__file__).dirpath() # exitcodes for the command line @@ -469,16 +469,22 @@ class Session(FSCollector): return True def _tryconvertpyarg(self, x): - try: - mod = __import__(x, None, None, ['__doc__']) - except (ValueError, ImportError): - return x - p = py.path.local(mod.__file__) - if p.purebasename == "__init__": - p = p.dirpath() - else: - p = p.new(basename=p.purebasename+".py") - return str(p) + mod = None + path = [os.path.abspath('.')] + sys.path + for name in x.split('.'): + try: + fd, mod, type_ = imp.find_module(name, path) + except ImportError: + return x + else: + if fd is not None: + fd.close() + + if type_[2] != imp.PKG_DIRECTORY: + path = [os.path.dirname(mod)] + else: + path = [mod] + return mod def _parsearg(self, arg): """ return (fspath, names) tuple after checking the file exists. """ From 25711a08794394fd50501cf3b919e31785ec9ad6 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 5 Sep 2011 17:38:22 +0200 Subject: [PATCH 2/3] Add acceptance test for new --pyargs behavior. --- testing/acceptance_test.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 6b815e4d7..3e68d30ff 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -374,6 +374,17 @@ class TestInvocationVariants: out, err = capsys.readouterr() assert "--myopt" in out + def test_pyargs_importerror(self, testdir, monkeypatch): + monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False) + path = testdir.mkpydir("tpkg") + path.join("test_hello.py").write('raise ImportError') + + result = testdir.runpytest("--pyargs", "tpkg.test_hello") + assert result.ret != 0 + result.stdout.fnmatch_lines([ + "*collected 0 items*" + ]) + def test_cmdline_python_package(self, testdir, monkeypatch): monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False) path = testdir.mkpydir("tpkg") From 2315de8321deca4a742b91732f4ed0e592323cb2 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 5 Sep 2011 22:01:50 +0200 Subject: [PATCH 3/3] Add FIXME. --- testing/acceptance_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 3e68d30ff..d6a0db473 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -381,6 +381,8 @@ class TestInvocationVariants: result = testdir.runpytest("--pyargs", "tpkg.test_hello") assert result.ret != 0 + # FIXME: It would be more natural to match NOT + # "ERROR*file*or*package*not*found*". result.stdout.fnmatch_lines([ "*collected 0 items*" ])