From 3fa0654d0ba6ec4ff7aec48b719c0303db267927 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Thu, 21 Oct 2021 13:19:48 +0200 Subject: Make 'spack location -e' print the current env, and 'spack cd -e' go to the current env (#26654) --- lib/spack/spack/cmd/location.py | 19 +++++++++++++------ lib/spack/spack/test/cmd/location.py | 28 +++++++++++++--------------- 2 files changed, 26 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/cmd/location.py b/lib/spack/spack/cmd/location.py index f844f51ed6..35cbf0754c 100644 --- a/lib/spack/spack/cmd/location.py +++ b/lib/spack/spack/cmd/location.py @@ -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 diff --git a/lib/spack/spack/test/cmd/location.py b/lib/spack/spack/test/cmd/location.py index db7c9d526a..b4cbf897b5 100644 --- a/lib/spack/spack/test/cmd/location.py +++ b/lib/spack/spack/test/cmd/location.py @@ -23,17 +23,6 @@ location = SpackCommand('location') 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 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): -- cgit v1.2.3-60-g2f50