Make 'spack location -e' print the current env, and 'spack cd -e' go to the current env (#26654)

This commit is contained in:
Harmen Stoppels 2021-10-21 13:19:48 +02:00 committed by GitHub
parent 7f4bf2b2e1
commit 3fa0654d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 21 deletions

View File

@ -56,8 +56,8 @@ def setup_parser(subparser):
help="build directory for a spec "
"(requires it to be staged first)")
directories.add_argument(
'-e', '--env', action='store', dest='location_env',
help="location of an environment managed by spack")
'-e', '--env', action='store', dest='location_env', nargs='?', metavar="name",
default=False, help="location of the named or current environment")
arguments.add_common_arguments(subparser, ['spec'])
@ -71,10 +71,17 @@ def location(parser, args):
print(spack.paths.prefix)
return
if args.location_env:
path = ev.root(args.location_env)
if not os.path.isdir(path):
tty.die("no such environment: '%s'" % args.location_env)
# no -e corresponds to False, -e without arg to None, -e name to the string name.
if args.location_env is not False:
if args.location_env is None:
# Get current environment path
spack.cmd.require_active_env('location -e')
path = ev.active_environment().path
else:
# Get named environment path
if not ev.exists(args.location_env):
tty.die("no such environment: '%s'" % args.location_env)
path = ev.root(args.location_env)
print(path)
return

View File

@ -23,17 +23,6 @@
env = SpackCommand('env')
@pytest.fixture
def mock_test_env():
test_env_name = 'test'
env_dir = ev.root(test_env_name)
mkdirp(env_dir)
yield test_env_name, env_dir
# Remove the temporary test environment directory created above
shutil.rmtree(env_dir)
@pytest.fixture
def mock_spec():
spec = spack.spec.Spec('externaltest').concretized()
@ -82,10 +71,19 @@ def test_location_cmd_error(options):
location(*options)
def test_location_env(mock_test_env):
"""Tests spack location --env."""
test_env_name, env_dir = mock_test_env
assert location('--env', test_env_name).strip() == env_dir
def test_location_env_exists(mutable_mock_env_path):
"""Tests spack location --env <name> for an existing environment."""
e = ev.create("example")
e.write()
assert location('--env', "example").strip() == e.path
def test_location_with_active_env(mutable_mock_env_path):
"""Tests spack location --env with active env"""
e = ev.create("example")
e.write()
with e:
assert location('--env').strip() == e.path
def test_location_env_flag_interference(mutable_mock_env_path, tmpdir):