From 59f8b9311ca5dc1a540558101b05f479c9ca4207 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 25 Sep 2018 13:00:27 +0200 Subject: [PATCH] pyimport: add support for PY_IGNORE_IMPORTMISMATCH Fixes https://github.com/pytest-dev/pytest/issues/2042. --- py/_path/local.py | 4 +++- testing/path/test_local.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/py/_path/local.py b/py/_path/local.py index 79dc6284..41271106 100644 --- a/py/_path/local.py +++ b/py/_path/local.py @@ -683,7 +683,9 @@ def pyimport(self, modname=None, ensuresyspath=True): except py.error.ENOENT: issame = False if not issame: - raise self.ImportMismatchError(modname, modfile, self) + ignore = os.getenv('PY_IGNORE_IMPORTMISMATCH') + if ignore != '1': + raise self.ImportMismatchError(modname, modfile, self) return mod else: try: diff --git a/testing/path/test_local.py b/testing/path/test_local.py index ee4b9bde..af64086d 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -475,13 +475,22 @@ def test_pyimport(self, path1): assert obj.x == 42 assert obj.__name__ == 'execfile' - def test_pyimport_renamed_dir_creates_mismatch(self, tmpdir): + def test_pyimport_renamed_dir_creates_mismatch(self, tmpdir, monkeypatch): p = tmpdir.ensure("a", "test_x123.py") p.pyimport() tmpdir.join("a").move(tmpdir.join("b")) with pytest.raises(tmpdir.ImportMismatchError): tmpdir.join("b", "test_x123.py").pyimport() + # Errors can be ignored. + monkeypatch.setenv('PY_IGNORE_IMPORTMISMATCH', '1') + tmpdir.join("b", "test_x123.py").pyimport() + + # PY_IGNORE_IMPORTMISMATCH=0 does not ignore error. + monkeypatch.setenv('PY_IGNORE_IMPORTMISMATCH', '0') + with pytest.raises(tmpdir.ImportMismatchError): + tmpdir.join("b", "test_x123.py").pyimport() + def test_pyimport_messy_name(self, tmpdir): # http://bitbucket.org/hpk42/py-trunk/issue/129 path = tmpdir.ensure('foo__init__.py')