Skip to content

Commit

Permalink
Warn when an non-existent prefix path is specified for rename (cond…
Browse files Browse the repository at this point in the history
…a#13387)

* Disallow protected directory paths from being able to be specified as a new conda environment prefix

* Update check_prefix function, add tests

* Relax dir pattern matching in install.py and update failing test

* Update install.py check_prefix function to check for protected dirs and provide a warning vs prevent the env from being created

* Add helpful error message to main_rename.py for nonexistent prefix

* Revert changes to install.py and main_create.py

* Add news file
  • Loading branch information
beeankha authored Jan 22, 2024
1 parent 7f79d7c commit 6ec61a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
14 changes: 9 additions & 5 deletions conda/cli/main_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,20 @@ def configure_parser(sub_parsers: _SubParsersAction, **kwargs) -> ArgumentParser
@deprecated.argument("24.3", "24.9", "prefix")
def validate_src() -> str:
"""
Validate that we are receiving at least one value for --name or --prefix
and ensure that the "base" environment is not being renamed
Validate that we are receiving at least one valid value for --name or
--prefix and ensure that the "base" environment is not being renamed
"""
from ..base.context import context
from ..exceptions import CondaEnvException

if Path(context.target_prefix).samefile(context.root_prefix):
prefix = Path(context.target_prefix)
if not prefix.exists():
raise CondaEnvException(
"The environment you are trying to rename does not exist."
)
if prefix.samefile(context.root_prefix):
raise CondaEnvException("The 'base' environment cannot be renamed")

if Path(context.target_prefix).samefile(context.active_prefix):
if prefix.samefile(context.active_prefix):
raise CondaEnvException("Cannot rename the active environment")

return context.target_prefix
Expand Down
19 changes: 19 additions & 0 deletions news/13387-warn-when-renaming-nonexistent-prefix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* Provide a more useful warning when attempting to rename a non-existent prefix. (#13387)

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
14 changes: 14 additions & 0 deletions tests/cli/test_main_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ def test_cannot_rename_active_env_by_name(
locate_prefix_by_name(env_rename)


def test_cannot_rename_nonexistent_env(conda_cli: CondaCLIFixture, env_rename: str):
"""Show a useful error message when trying to rename a non-existing env"""
with pytest.raises(
CondaEnvException,
match="The environment you are trying to rename does not exist",
):
env_dir = Path(context.root_prefix) / "foo"
conda_cli("rename", "--prefix", env_dir, env_rename)

assert Path(env_dir).exists() is False
with pytest.raises(EnvironmentNameNotFound):
locate_prefix_by_name(env_rename)


def test_rename_with_force(conda_cli: CondaCLIFixture, env_one: str, env_two: str):
"""
Runs a test where we specify the --force flag to remove an existing directory.
Expand Down

0 comments on commit 6ec61a8

Please sign in to comment.