diff options
author | Harmen Stoppels <harmenstoppels@gmail.com> | 2021-10-21 13:19:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-21 13:19:48 +0200 |
commit | 3fa0654d0ba6ec4ff7aec48b719c0303db267927 (patch) | |
tree | 40fa08822394cd531b5c11043121820b565ab8ed /lib | |
parent | 7f4bf2b2e1263d2c0900c0c15e1f65b15a79fa49 (diff) | |
download | spack-3fa0654d0ba6ec4ff7aec48b719c0303db267927.tar.gz spack-3fa0654d0ba6ec4ff7aec48b719c0303db267927.tar.bz2 spack-3fa0654d0ba6ec4ff7aec48b719c0303db267927.tar.xz spack-3fa0654d0ba6ec4ff7aec48b719c0303db267927.zip |
Make 'spack location -e' print the current env, and 'spack cd -e' go to the current env (#26654)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/cmd/location.py | 19 | ||||
-rw-r--r-- | lib/spack/spack/test/cmd/location.py | 28 |
2 files changed, 26 insertions, 21 deletions
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 @@ -24,17 +24,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() pkg = spack.repo.get(spec) @@ -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): |