diff --git a/AUTHORS b/AUTHORS index ab6938652..2e487e772 100644 --- a/AUTHORS +++ b/AUTHORS @@ -80,6 +80,7 @@ Lukas Bednar Maciek Fijalkowski Maho Marc Schlaich +Marcin Bachry Mark Abramowitz Markus Unterwaditzer Martijn Faassen diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 13c61c923..d571a2bf3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,11 +6,15 @@ * +* Fix loader error when running ``pytest`` embedded in a zipfile. + Thanks `@mbachry`_ for the PR. + * * +.. _@mbachry: https://github.com/mbachry .. _#1822: https://github.com/pytest-dev/pytest/issues/1822 diff --git a/_pytest/main.py b/_pytest/main.py index 48f5bb6f2..5771a1699 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -687,7 +687,7 @@ class Session(FSCollector): # This method is sometimes invoked when AssertionRewritingHook, which # does not define a get_filename method, is already in place: try: - path = loader.get_filename() + path = loader.get_filename(x) except AttributeError: # Retrieve path from AssertionRewritingHook: path = loader.modules[x][0].co_filename diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 82b131dcc..b03f7fe4c 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -763,3 +763,21 @@ class TestDurationWithFixture: * call *test_1* """) + +def test_zipimport_hook(testdir, tmpdir): + """Test package loader is being used correctly (see #1837).""" + zipapp = pytest.importorskip('zipapp') + testdir.tmpdir.join('app').ensure(dir=1) + testdir.makepyfile(**{ + 'app/foo.py': """ + import pytest + def main(): + pytest.main(['--pyarg', 'foo']) + """, + }) + target = tmpdir.join('foo.zip') + zipapp.create_archive(str(testdir.tmpdir.join('app')), str(target), main='foo:main') + result = testdir.runpython(target) + assert result.ret == 0 + result.stderr.fnmatch_lines(['*not found*foo*']) + assert 'INTERNALERROR>' not in result.stdout.str()