Skip to content

Commit

Permalink
Merge pull request #9 from conda-incubator/allow-dry-run-to-run
Browse files Browse the repository at this point in the history
Allow dry run to run on guarded environments
  • Loading branch information
travishathaway authored Jun 6, 2024
2 parents fb42e96 + d44c5dc commit 82bae0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions conda_protect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ def conda_guard_pre_commands_action(command: str):
TODO: This still doesn't handle `conda env update -f environment.yml`
We will have to look inside the file and pluck out the environment name
"""
# Allow things to go forward when `dry_run` is `True`
if context.dry_run:
return

known_envs = get_environment_info()
lookup_attr, value = _get_active_environment()

Expand Down
4 changes: 2 additions & 2 deletions pixi.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "conda-auth"
version = "0.2.0"
name = "conda-protect"
version = "0.5.0"
description = "Protects conda environments to avoid mistakenly modifying them"
authors = ["Travis Hathaway <travis.j.hathaway@gmail.com>"]
channels = ["conda-forge"]
Expand Down
22 changes: 21 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import pytest
from conda.testing import conda_cli # noqa: F401
from conda.exceptions import DryRunExit

from conda_protect.main import GUARDFILE_NAME, CondaProtectError, GUARD_COMMAND_NAME

Expand Down Expand Up @@ -53,7 +54,26 @@ def test_guarded_command_fails(mocker, conda_cli, conda_environment): # noqa: F
assert err == ""

with pytest.raises(CondaProtectError):
conda_cli("install", "--prefix", str(conda_environment), "python")
conda_cli("install", "--prefix", str(conda_environment), "ca-certificates")

# remove conda_protect
out, err, code = conda_cli(GUARD_COMMAND_NAME, str(conda_environment))

assert err == ""


def test_dry_run_continues(mocker, conda_cli, conda_environment):
"""
When `--dry-run` is used, the environment is not guarded
"""
mocker.patch("sys.argv", ["conda", GUARD_COMMAND_NAME, str(conda_environment)])

out, err, code = conda_cli(GUARD_COMMAND_NAME, str(conda_environment))

assert err == ""

with pytest.raises(DryRunExit):
conda_cli("install", "--dry-run", "--prefix", str(conda_environment), "ca-certificates")

# remove conda_protect
out, err, code = conda_cli(GUARD_COMMAND_NAME, str(conda_environment))
Expand Down

0 comments on commit 82bae0c

Please sign in to comment.