From 0ae27714d1940cb012827faf56245c23cb40d113 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Tue, 4 Jun 2019 10:41:58 -0700 Subject: [PATCH] Backport of #5389: fix for 'files' = None in broken metadata --- changelog/5389.bugfix.rst | 1 + src/_pytest/config/__init__.py | 2 +- testing/test_config.py | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 changelog/5389.bugfix.rst diff --git a/changelog/5389.bugfix.rst b/changelog/5389.bugfix.rst new file mode 100644 index 00000000000..debf0a9da9d --- /dev/null +++ b/changelog/5389.bugfix.rst @@ -0,0 +1 @@ +Fix regressions of `#5063 `__ for ``importlib_metadata.PathDistribution`` which have their ``files`` attribute being ``None``. diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 7a5deb13f53..6d44bf3d773 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -800,7 +800,7 @@ def _mark_plugins_for_rewrite(self, hook): str(file) for dist in importlib_metadata.distributions() if any(ep.group == "pytest11" for ep in dist.entry_points) - for file in dist.files + for file in dist.files or [] ) for name in _iter_rewritable_modules(package_files): diff --git a/testing/test_config.py b/testing/test_config.py index e588c262a9c..eefd908de59 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -586,6 +586,29 @@ def distributions(): testdir.parseconfig() +def test_importlib_metadata_broken_distribution(testdir, monkeypatch): + """Integration test for broken distributions with 'files' metadata being None (#5389)""" + monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) + + class DummyEntryPoint: + name = "mytestplugin" + group = "pytest11" + + def load(self): + return object() + + class Distribution: + version = "1.0" + files = None + entry_points = (DummyEntryPoint(),) + + def distributions(): + return (Distribution(),) + + monkeypatch.setattr(importlib_metadata, "distributions", distributions) + testdir.parseconfig() + + @pytest.mark.parametrize("block_it", [True, False]) def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch, block_it): monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)