(
pytester: Pytester,
monkeypatch: MonkeyPatch,
enable_plugin_method: str,
disable_plugin_method: str,
)
| 1622 | @pytest.mark.parametrize("disable_plugin_method", ["env_var", "flag", ""]) |
| 1623 | @pytest.mark.parametrize("enable_plugin_method", ["env_var", "flag", ""]) |
| 1624 | def test_disable_plugin_autoload( |
| 1625 | pytester: Pytester, |
| 1626 | monkeypatch: MonkeyPatch, |
| 1627 | enable_plugin_method: str, |
| 1628 | disable_plugin_method: str, |
| 1629 | ) -> None: |
| 1630 | class DummyEntryPoint: |
| 1631 | project_name = name = "mytestplugin" |
| 1632 | group = "pytest11" |
| 1633 | version = "1.0" |
| 1634 | |
| 1635 | def load(self): |
| 1636 | return sys.modules[self.name] |
| 1637 | |
| 1638 | class Distribution: |
| 1639 | metadata = {"name": "foo"} |
| 1640 | entry_points = (DummyEntryPoint(),) |
| 1641 | files = () |
| 1642 | |
| 1643 | class PseudoPlugin: |
| 1644 | x = 42 |
| 1645 | |
| 1646 | attrs_used = [] |
| 1647 | |
| 1648 | def __getattr__(self, name): |
| 1649 | assert name in ("__loader__", "__spec__") |
| 1650 | self.attrs_used.append(name) |
| 1651 | return object() |
| 1652 | |
| 1653 | def distributions(): |
| 1654 | return (Distribution(),) |
| 1655 | |
| 1656 | parse_args: list[str] = [] |
| 1657 | |
| 1658 | if disable_plugin_method == "env_var": |
| 1659 | monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") |
| 1660 | elif disable_plugin_method == "flag": |
| 1661 | monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 1662 | parse_args.append("--disable-plugin-autoload") |
| 1663 | else: |
| 1664 | assert disable_plugin_method == "" |
| 1665 | monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 1666 | |
| 1667 | if enable_plugin_method == "env_var": |
| 1668 | monkeypatch.setenv("PYTEST_PLUGINS", "mytestplugin") |
| 1669 | elif enable_plugin_method == "flag": |
| 1670 | parse_args.extend(["-p", "mytestplugin"]) |
| 1671 | else: |
| 1672 | assert enable_plugin_method == "" |
| 1673 | |
| 1674 | monkeypatch.setattr(importlib.metadata, "distributions", distributions) |
| 1675 | monkeypatch.setitem(sys.modules, "mytestplugin", PseudoPlugin()) |
| 1676 | config = pytester.parseconfig(*parse_args) |
| 1677 | |
| 1678 | has_loaded = config.pluginmanager.get_plugin("mytestplugin") is not None |
| 1679 | # it should load if it's enabled, or we haven't disabled autoloading |
| 1680 | assert has_loaded == (bool(enable_plugin_method) or not disable_plugin_method) |
| 1681 |
nothing calls this directly
no test coverage detected