Repository: pytest-dev/pytest
File: testing/test_assertion.py (Python)

Code before:

    @pytest.mark.parametrize("mode", ["plain", "rewrite"])
    @pytest.mark.parametrize("disable_plugin_autoload", ["env_var", "cli", ""])
    @pytest.mark.parametrize("explicit_specify", ["env_var", "cli", ""])
    def test_installed_plugin_rewrite(
        self,
        pytester: Pytester,
        mode: str,
        monkeypatch: pytest.MonkeyPatch,
        disable_plugin_autoload: str,
        explicit_specify: str,
    ) -> None:
        args = ["mainwrapper.py", "-s", f"--assert={mode}"]
        if disable_plugin_autoload == "env_var":
            monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
        elif disable_plugin_autoload == "cli":
            monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
            args.append("--disable-plugin-autoload")
        else:
            assert disable_plugin_autoload == ""
            monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)

        # FIXME: if it's already loaded then you get a ValueError: "Plugin already
        # registered under a different name."
        # vaguely related to https://github.com/pytest-dev/pytest/issues/5661
        name = "spamplugin" if disable_plugin_autoload else "spam"
        # is there a single dotted name that can be used either way? idk

        if explicit_specify == "env_var":
            monkeypatch.setenv("PYTEST_PLUGINS", name)
        elif explicit_specify == "cli":
            args.append("-p")
            args.append(name)
        else:
            assert explicit_specify == ""

        # Make sure the hook is installed early enough so that plugins
        # installed via distribution package are rewritten.
        pytester.mkdir("hampkg")
        contents = {
            "hampkg/__init__.py": """\
                import pytest

                @pytest.fixture
                def check_first2():
                    def check(values, value):
                        assert values.pop(0) == value
                    return check
            """,
            "spamplugin.py": """\
            import pytest

Review comment (suggestion):
The difference here is that when loading via entry point, the plugin can choose its own name (`name = 'spam'`), but when loading via other methods the name of the module is used (`spamplugin`).

I have simplified things here by always using `spamplugin` as the name of the plugin (b157cefed).

Code after:

    @pytest.mark.parametrize("mode", ["plain", "rewrite"])
    @pytest.mark.parametrize("disable_plugin_autoload", ["env_var", "cli", ""])
    @pytest.mark.parametrize("explicit_specify", ["env_var", "cli", ""])
    def test_installed_plugin_rewrite(
        self,
        pytester: Pytester,
        mode: str,
        monkeypatch: pytest.MonkeyPatch,
        disable_plugin_autoload: str,
        explicit_specify: str,
    ) -> None:
        args = ["mainwrapper.py", "-s", f"--assert={mode}"]
        if disable_plugin_autoload == "env_var":
            monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
        elif disable_plugin_autoload == "cli":
            monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
            args.append("--disable-plugin-autoload")
        else:
            assert disable_plugin_autoload == ""
            monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)

        name = "spamplugin"

        if explicit_specify == "env_var":
            monkeypatch.setenv("PYTEST_PLUGINS", name)
        elif explicit_specify == "cli":
            args.append("-p")
            args.append(name)
        else:
            assert explicit_specify == ""

        # Make sure the hook is installed early enough so that plugins
        # installed via distribution package are rewritten.
        pytester.mkdir("hampkg")
        contents = {
            "hampkg/__init__.py": """\
                import pytest

                @pytest.fixture
                def check_first2():
                    def check(values, value):
                        assert values.pop(0) == value
                    return check
            """,
            "spamplugin.py": """\
            import pytest
            from hampkg import check_first2

            @pytest.fixture
            def check_first():