summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHarmen Stoppels <harmenstoppels@gmail.com>2021-03-30 18:47:36 +0200
committerGitHub <noreply@github.com>2021-03-30 18:47:36 +0200
commitb848fab3ec1a135ec49980ed46c2632ee96dd57b (patch)
treeeb75580bad6f1d05bc79ab03bd338733a1faa194 /lib
parentc3bab11ee1a26bb7074db8490e2155c598db18ea (diff)
downloadspack-b848fab3ec1a135ec49980ed46c2632ee96dd57b.tar.gz
spack-b848fab3ec1a135ec49980ed46c2632ee96dd57b.tar.bz2
spack-b848fab3ec1a135ec49980ed46c2632ee96dd57b.tar.xz
spack-b848fab3ec1a135ec49980ed46c2632ee96dd57b.zip
SpackCommand objects can set global args (#22318)
This commit extends the API of the __call__ method of the SpackCommand class to permit passing global arguments like those interposed between the main "spack" command and the subsequent subcommand. The functionality is used to fix an issue where running ```spack -e . location -b some_package``` ends up printing the name of the environment instead of the build directory of the package, because the location arg parser also stores this value as `arg.env`.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/cmd/location.py8
-rw-r--r--lib/spack/spack/main.py6
-rw-r--r--lib/spack/spack/test/cmd/location.py22
3 files changed, 31 insertions, 5 deletions
diff --git a/lib/spack/spack/cmd/location.py b/lib/spack/spack/cmd/location.py
index 2461074d88..3d0db9ab2a 100644
--- a/lib/spack/spack/cmd/location.py
+++ b/lib/spack/spack/cmd/location.py
@@ -56,7 +56,7 @@ def setup_parser(subparser):
help="build directory for a spec "
"(requires it to be staged first)")
directories.add_argument(
- '-e', '--env', action='store',
+ '-e', '--env', action='store', dest='location_env',
help="location of an environment managed by spack")
arguments.add_common_arguments(subparser, ['spec'])
@@ -71,10 +71,10 @@ def location(parser, args):
print(spack.paths.prefix)
return
- if args.env:
- path = spack.environment.root(args.env)
+ if args.location_env:
+ path = spack.environment.root(args.location_env)
if not os.path.isdir(path):
- tty.die("no such environment: '%s'" % args.env)
+ tty.die("no such environment: '%s'" % args.location_env)
print(path)
return
diff --git a/lib/spack/spack/main.py b/lib/spack/spack/main.py
index 334311fcf6..be07aa3e5c 100644
--- a/lib/spack/spack/main.py
+++ b/lib/spack/spack/main.py
@@ -528,6 +528,8 @@ class SpackCommand(object):
Keyword Args:
fail_on_error (optional bool): Don't raise an exception on error
+ global_args (optional list): List of global spack arguments:
+ simulates ``spack [global_args] [command] [*argv]``
Returns:
(str): combined output and error as a string
@@ -540,8 +542,10 @@ class SpackCommand(object):
self.returncode = None
self.error = None
+ prepend = kwargs['global_args'] if 'global_args' in kwargs else []
+
args, unknown = self.parser.parse_known_args(
- [self.command_name] + list(argv))
+ prepend + [self.command_name] + list(argv))
fail_on_error = kwargs.get('fail_on_error', True)
diff --git a/lib/spack/spack/test/cmd/location.py b/lib/spack/spack/test/cmd/location.py
index 72c3912503..00fe46f8f0 100644
--- a/lib/spack/spack/test/cmd/location.py
+++ b/lib/spack/spack/test/cmd/location.py
@@ -19,6 +19,7 @@ pytestmark = pytest.mark.usefixtures('config', 'database')
# location prints out "locations of packages and spack directories"
location = SpackCommand('location')
+env = SpackCommand('env')
@pytest.fixture
@@ -84,6 +85,27 @@ def test_location_env(mock_test_env):
assert location('--env', test_env_name).strip() == env_dir
+def test_location_env_flag_interference(mutable_mock_env_path, tmpdir):
+ """
+ Tests that specifying an active environment using `spack -e x location ...`
+ does not interfere with the location command flags.
+ """
+
+ # create two environments
+ env('create', 'first_env')
+ env('create', 'second_env')
+
+ global_args = ['-e', 'first_env']
+
+ # `spack -e first_env location -e second_env` should print the env
+ # path of second_env
+ assert 'first_env' not in location('-e', 'second_env', global_args=global_args)
+
+ # `spack -e first_env location --packages` should not print
+ # the environment path of first_env.
+ assert 'first_env' not in location('--packages', global_args=global_args)
+
+
def test_location_env_missing():
"""Tests spack location --env."""
missing_env_name = 'missing-env'