From 3232e6aeee433daaea0fed115c7326aab0671139 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 10 Jul 2023 12:18:29 -0300 Subject: [PATCH] Add CONDA_DEVENV_USE_LOCKS env var to configure the default for --use-locks --- CHANGELOG.rst | 6 ++++++ docs/usage.rst | 10 +++++++++- src/conda_devenv/devenv.py | 9 ++++++--- tests/test_locking.py | 20 ++++++++++++++++---- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 25eee21..4c5c7b3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ CHANGELOG ========= +3.2.0 (2023-07-11) +------------------ + +* The ``CONDA_DEVENV_USE_LOCKS`` can be used to change the default value of ``--use-locks``. Useful to set on CI to ensure + lock files are being used. + 3.1.1 (2023-07-04) ------------------ diff --git a/docs/usage.rst b/docs/usage.rst index c4390d7..3fe8045 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -381,6 +381,12 @@ No other changes are required. You can control if ``devenv`` should use lock files using the ``--use-locks {auto,yes,no}`` command-line option. +.. versionadded:: 3.2.0 + +The default value for ``--use-locks`` can be set via ``CONDA_DEVENV_USE_LOCKS``, which is useful on CI +to change the behavior when no explicit option is present in the command line. + + Updating -------- @@ -482,7 +488,9 @@ Options --use-locks {auto,yes,no} How to use lock files: 'auto' will use them if available, 'yes' will try to use and fail if not - available, 'no' skip lockfiles always. + available, 'no' skip lock files always. Can also be + configured via CONDA_DEVENV_USE_LOCKS environment + variable. --update-locks PACKAGE Update the given package in all lock files, while still obeying the pins in the devenv.yml file. Can be diff --git a/src/conda_devenv/devenv.py b/src/conda_devenv/devenv.py index 2659c00..2f6e18d 100644 --- a/src/conda_devenv/devenv.py +++ b/src/conda_devenv/devenv.py @@ -772,9 +772,12 @@ def parse_args(argv: list[str] | None) -> argparse.Namespace: group.add_argument( "--use-locks", choices=["auto", "yes", "no"], - default="auto", - help="How to use lock files: 'auto' will use them if available, 'yes' " - "will try to use and fail if not available, 'no' skip lockfiles always.", + default=os.environ.get("CONDA_DEVENV_USE_LOCKS", "auto"), + help=( + "How to use lock files: 'auto' will use them if available, 'yes' " + "will try to use and fail if not available, 'no' skip lock files always. " + "Can also be configured via CONDA_DEVENV_USE_LOCKS environment variable." + ), ) group.add_argument( "--update-locks", diff --git a/tests/test_locking.py b/tests/test_locking.py index c739afb..f83255f 100644 --- a/tests/test_locking.py +++ b/tests/test_locking.py @@ -1,6 +1,7 @@ import sys from pathlib import Path from textwrap import dedent +from typing import Literal import pytest @@ -219,7 +220,7 @@ def test_auto_use_lock_files( ) ) - # Create the env directory because we need it to exist to create the activate/deactive scripts. + # Create the env directory because we need it to exist to create the activate/deactivate scripts. env_dir = tmp_path / "envs" env_dir.joinpath(f"{env_name}/conda-meta").mkdir(parents=True) env_dirs = [env_dir] @@ -242,7 +243,7 @@ def test_auto_use_lock_files( ), ] - # Use locks again but create an environment different than the one defined + # Use locks again but create an environment different from the one defined # in environment.devenv.yml. subprocess_call_mock.reset_mock() env_dir2 = tmp_path / "envs" @@ -269,9 +270,20 @@ def test_auto_use_lock_files( ] +@pytest.mark.parametrize("mode", ["cmd-line", "env-var"]) def test_use_locks_is_yes_but_no_lock_file( - tmp_path: Path, monkeypatch, patch_conda_calls: None, capsys + tmp_path: Path, + monkeypatch, + patch_conda_calls: None, + capsys, + mode: Literal["cmd-line", "env-var"], ) -> None: + if mode == "cmd-line": + cmdline = ["--use-locks=yes"] + else: + assert mode == "env-var" + monkeypatch.setenv("CONDA_DEVENV_USE_LOCKS", "yes") + cmdline = [] monkeypatch.chdir(tmp_path) env_file = tmp_path / "environment.devenv.yml" env_file.write_text( @@ -289,7 +301,7 @@ def test_use_locks_is_yes_but_no_lock_file( ) ) - assert devenv.main(["--use-locks=yes"]) == 2 + assert devenv.main(cmdline) == 2 out, err = capsys.readouterr() platform = CondaPlatform.current().value assert (