Make env (de)activate error with -e, -E, -D flags (#25625)

* Make sure that spack -e. env activate b and spack -e. env deactivate error
* Add a test
This commit is contained in:
Harmen Stoppels 2021-08-27 01:54:58 +02:00 committed by GitHub
parent f5d4f5bdac
commit 74389472ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -88,6 +88,11 @@ def env_activate(args):
)
return 1
# Error out when -e, -E, -D flags are given, cause they are ambiguous.
if args.env or args.no_env or args.env_dir:
tty.die('Calling spack env activate with --env, --env-dir and --no-env '
'is ambiguous')
if ev.exists(env) and not args.dir:
spack_env = ev.root(env)
short_name = env
@ -137,6 +142,11 @@ def env_deactivate(args):
)
return 1
# Error out when -e, -E, -D flags are given, cause they are ambiguous.
if args.env or args.no_env or args.env_dir:
tty.die('Calling spack env deactivate with --env, --env-dir and --no-env '
'is ambiguous')
if 'SPACK_ENV' not in os.environ:
tty.die('No environment is currently active.')

View File

@ -4,6 +4,7 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import glob
import os
from argparse import Namespace
import pytest
from six import StringIO
@ -11,6 +12,7 @@
import llnl.util.filesystem as fs
import llnl.util.link_tree
import spack.cmd.env
import spack.environment as ev
import spack.hash_types as ht
import spack.modules
@ -2632,3 +2634,24 @@ def test_query_develop_specs():
assert e.is_develop(Spec('mpich'))
assert not e.is_develop(Spec('mpileaks'))
@pytest.mark.parametrize('method', [
spack.cmd.env.env_activate,
spack.cmd.env.env_deactivate
])
@pytest.mark.parametrize(
'env,no_env,env_dir',
[
('b', False, None),
(None, True, None),
(None, False, 'path/'),
])
def test_activation_and_deactiviation_ambiguities(method, env, no_env, env_dir, capsys):
"""spack [-e x | -E | -D x/] env [activate | deactivate] y are ambiguous"""
args = Namespace(shell='sh', activate_env='a',
env=env, no_env=no_env, env_dir=env_dir)
with pytest.raises(SystemExit):
method(args)
_, err = capsys.readouterr()
assert 'is ambiguous' in err