Skip to content

Commit

Permalink
Fixed errors when renaming without an active env (conda#11915)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelisNivalis authored Jan 31, 2023
1 parent d2d1208 commit 5a9deaf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion conda/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def default_prefix(self):

@property
def active_prefix(self):
return os.getenv('CONDA_PREFIX')
return os.getenv("CONDA_PREFIX")

@property
def shlvl(self):
Expand Down
4 changes: 3 additions & 1 deletion conda/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def ensure_name_or_prefix(args, command):
raise CondaValueError('either -n NAME or -p PREFIX option required,\n'
'try "conda %s -h" for more details' % command)

def is_active_prefix(prefix):
def is_active_prefix(prefix: str) -> bool:
"""
Determines whether the args we pass in are pointing to the active prefix.
Can be used a validation step to make sure operations are not being
performed on the active prefix.
"""
if context.active_prefix is None:
return False
return (
paths_equal(prefix, context.active_prefix)
# normcasing our prefix check for Windows, for case insensitivity
Expand Down
19 changes: 19 additions & 0 deletions news/11915-no-active-env-renaming-error
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fixed errors when renaming without an active env (#11915)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
10 changes: 8 additions & 2 deletions tests/cli/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

from conda.auxlib.collection import AttrDict
from conda.base.context import conda_tests_ctxt_mgmt_def_pol, context
from conda.cli.common import check_non_admin, confirm, confirm_yn
from conda.cli.common import check_non_admin, confirm, confirm_yn, is_active_prefix
from conda.common.compat import on_win
from conda.common.io import captured, env_var
from conda.exceptions import CondaSystemExit, DryRunExit, OperationNotAllowed


def test_check_non_admin_enabled_false():
with env_var('CONDA_NON_ADMIN_ENABLED', 'false', stack_callback=conda_tests_ctxt_mgmt_def_pol):
if on_win:
Expand Down Expand Up @@ -77,3 +76,10 @@ def test_always_yes():
with env_var('CONDA_DRY_RUN', 'false', stack_callback=conda_tests_ctxt_mgmt_def_pol):
choice = confirm_yn()
assert choice is True


@pytest.mark.parametrize("prefix,active", [("", False), ("active_prefix", True)])
def test_is_active_prefix(prefix, active):
if prefix == "active_prefix":
prefix = context.active_prefix
assert is_active_prefix(prefix) is active

0 comments on commit 5a9deaf

Please sign in to comment.