diff --git a/docs/usage.rst b/docs/usage.rst index dc1e03d3..bd05b998 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -128,10 +128,17 @@ Changing the sessions default backend By default Nox uses ``virtualenv`` as the virtual environment backend for the sessions, but it also supports ``uv``, ``conda``, ``mamba``, and ``venv`` as well as no backend (passthrough to whatever python environment Nox is running on). You can change the default behaviour by using ``-db `` or ``--default-venv-backend ``. Supported names are ``('none', 'uv', 'virtualenv', 'conda', 'mamba', 'venv')``. -.. code-block:: console - nox -db conda - nox --default-venv-backend conda +.. tabs:: + + .. code-tab:: console CLI options + + nox -db conda + nox --default-venv-backend conda + + .. code-tab:: console Environment variables + + NOX_DEFAULT_VENV_BACKEND=conda .. note:: @@ -139,7 +146,7 @@ By default Nox uses ``virtualenv`` as the virtual environment backend for the se programs be pre-installed. ``uv`` is distributed as a Python package and can be installed with the ``nox[uv]`` extra. -You can also set this option in the Noxfile with ``nox.options.default_venv_backend``. In case both are provided, the commandline argument takes precedence. +You can also set this option with the ``NOX_DEFAULT_VENV_BACKEND`` environment variable, or in the Noxfile with ``nox.options.default_venv_backend``. In case more than one is provided, the command line argument overrides the environment variable, which in turn overrides the Noxfile configuration. Note that using this option does not change the backend for sessions where ``venv_backend`` is explicitly set. diff --git a/nox/_options.py b/nox/_options.py index 7c4b89e8..87b5a1db 100644 --- a/nox/_options.py +++ b/nox/_options.py @@ -380,6 +380,7 @@ def _tag_completer( "--default-venv-backend", group=options.groups["environment"], noxfile=True, + default=lambda: os.environ.get("NOX_DEFAULT_VENV_BACKEND"), merge_func=_default_venv_backend_merge_func, help=( "Virtual environment backend to use by default for Nox sessions, this is" diff --git a/tests/test_main.py b/tests/test_main.py index e33277b3..e55dd033 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -256,8 +256,33 @@ def test_main_list_option_from_nox_env_var(monkeypatch, var, option, env, values config = execute.call_args[1]["global_config"] config_values = getattr(config, option) assert len(config_values) == len(values) - for value in values: - assert value in config_values + assert all(value in config_values for value in values) + + +@pytest.mark.parametrize( + "options,env,expected", + [ + (["--default-venv-backend", "conda"], "", "conda"), + ([], "mamba", "mamba"), + (["--default-venv-backend", "conda"], "mamba", "conda"), + ], + ids=["option", "env", "option_over_env"], +) +def test_default_venv_backend_option(monkeypatch, options, env, expected): + monkeypatch.setenv("NOX_DEFAULT_VENV_BACKEND", env) + monkeypatch.setattr(sys, "argv", [sys.executable, *options]) + with mock.patch("nox.workflow.execute") as execute: + execute.return_value = 0 + + # Call the main function. + with mock.patch.object(sys, "exit") as exit: + nox.__main__.main() + exit.assert_called_once_with(0) + assert execute.called + + # Verify that the default venv backend is set in the config. + config = execute.call_args[1]["global_config"] + assert config.default_venv_backend == expected def test_main_positional_args(capsys, monkeypatch):