wntrblm / nox

Flexible test automation for Python
https://nox.thea.codes
Apache License 2.0
1.3k stars 148 forks source link

test__create_venv_options fails without conda #793

Closed mtelka closed 6 months ago

mtelka commented 6 months ago

Current Behavior

I'm running tests for nox and I found that TestSessionRunner.test__create_venv_options[nox.virtualenv.CondaEnv.create-conda-CondaEnv] fails because I've no conda installed:

_ TestSessionRunner.test__create_venv_options[nox.virtualenv.CondaEnv.create-conda-CondaEnv] _

self = <test_sessions.TestSessionRunner object at 0x7fffad2f92e0>
create_method = 'nox.virtualenv.CondaEnv.create', venv_backend = 'conda'
expected_backend = <class 'nox.virtualenv.CondaEnv'>

    @pytest.mark.parametrize(
        "create_method,venv_backend,expected_backend",
        [
            ("nox.virtualenv.VirtualEnv.create", None, nox.virtualenv.VirtualEnv),
            (
                "nox.virtualenv.VirtualEnv.create",
                "virtualenv",
                nox.virtualenv.VirtualEnv,
            ),
            ("nox.virtualenv.VirtualEnv.create", "venv", nox.virtualenv.VirtualEnv),
            ("nox.virtualenv.CondaEnv.create", "conda", nox.virtualenv.CondaEnv),
        ],
    )
    def test__create_venv_options(self, create_method, venv_backend, expected_backend):
        runner = self.make_runner()
        runner.func.python = "coolpython"
        runner.func.reuse_venv = True
        runner.func.venv_backend = venv_backend

        with mock.patch(create_method, autospec=True) as create:
>           runner._create_venv()

$(BUILD_DIR)/tests/test_sessions.py:959:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <nox.sessions.SessionRunner object at 0x7fffad054070>

    def _create_venv(self) -> None:
        reuse_existing = self.reuse_existing_venv()

        backends = (
            self.global_config.force_venv_backend
            or self.func.venv_backend
            or self.global_config.default_venv_backend
            or "virtualenv"
        ).split("|")

        # Support fallback backends
        for bk in backends:
            if bk not in nox.virtualenv.ALL_VENVS:
                msg = f"Expected venv_backend one of {list(nox.virtualenv.ALL_VENVS)!r}, but got {bk!r}."
                raise ValueError(msg)

        for bk in backends[:-1]:
            if bk not in nox.virtualenv.OPTIONAL_VENVS:
                msg = f"Only optional backends ({list(nox.virtualenv.OPTIONAL_VENVS)!r}) may have a fallback, {bk!r} is not optional."
                raise ValueError(msg)

        for bk in backends:
            if nox.virtualenv.OPTIONAL_VENVS.get(bk, True):
                backend = bk
                break
        else:
            msg = f"No backends present, looked for {backends!r}."
>           raise ValueError(msg)
E           ValueError: No backends present, looked for ['conda'].
$(BUILD_DIR)/nox/sessions.py:791: ValueError

Expected Behavior

All tests pass even without conda.

Steps To Reproduce

Run nox tests without conda installed.

Environment

- OS: OpenIndiana
- Python: 3.9.18
- Nox: 2024.3.2

Anything else?

No response

cjolowicz commented 6 months ago

Thanks for reporting! I could reproduce.

Would something like this work?

            pytest.param("nox.virtualenv.CondaEnv.create", "conda", nox.virtualenv.CondaEnv, marks=has_conda),

The has_conda marker is defined in test_virtualenv.py.

mtelka commented 6 months ago
/usr/lib/python3.9/ast.py:50: in parse
    return compile(source, filename, mode, flags,
E     File "$(BUILD_DIR)/tests/test_sessions.py", line 949
E       ("nox.virtualenv.CondaEnv.create", "conda", nox.virtualenv.CondaEnv, marks=has_conda),
E                                                                                 ^
E   SyntaxError: invalid syntax
mtelka commented 6 months ago

Above tested with this patch:

--- nox-2024.3.2/tests/test_sessions.py.orig
+++ nox-2024.3.2/tests/test_sessions.py
@@ -946,7 +946,7 @@
                 nox.virtualenv.VirtualEnv,
             ),
             ("nox.virtualenv.VirtualEnv.create", "venv", nox.virtualenv.VirtualEnv),
-            ("nox.virtualenv.CondaEnv.create", "conda", nox.virtualenv.CondaEnv),
+            ("nox.virtualenv.CondaEnv.create", "conda", nox.virtualenv.CondaEnv, marks=has_conda),
         ],
     )
     def test__create_venv_options(self, create_method, venv_backend, expected_backend):
mtelka commented 6 months ago

I looked at #794 and I had to use it differently. Sorry for mess.

mtelka commented 6 months ago

I tested with patch from #794 and now the test is properly skipped.