Skip to content

Commit

Permalink
Allow configuring default_venv_backend with an environment variable (
Browse files Browse the repository at this point in the history
…#780)

Closes #776
  • Loading branch information
edgarrmondragon committed Feb 25, 2024
1 parent 5a097cd commit 93bacbd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
15 changes: 11 additions & 4 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,25 @@ 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 <backend>`` or ``--default-venv-backend <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::

The ``uv``, ``conda``, and ``mamba`` backends require their respective
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.

Expand Down
1 change: 1 addition & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,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"
Expand Down
29 changes: 27 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,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):
Expand Down

0 comments on commit 93bacbd

Please sign in to comment.