X Tutup
Skip to content

Commit b848fab

Browse files
authored
SpackCommand objects can set global args (spack#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`.
1 parent c3bab11 commit b848fab

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/spack/spack/cmd/location.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def setup_parser(subparser):
5656
help="build directory for a spec "
5757
"(requires it to be staged first)")
5858
directories.add_argument(
59-
'-e', '--env', action='store',
59+
'-e', '--env', action='store', dest='location_env',
6060
help="location of an environment managed by spack")
6161

6262
arguments.add_common_arguments(subparser, ['spec'])
@@ -71,10 +71,10 @@ def location(parser, args):
7171
print(spack.paths.prefix)
7272
return
7373

74-
if args.env:
75-
path = spack.environment.root(args.env)
74+
if args.location_env:
75+
path = spack.environment.root(args.location_env)
7676
if not os.path.isdir(path):
77-
tty.die("no such environment: '%s'" % args.env)
77+
tty.die("no such environment: '%s'" % args.location_env)
7878
print(path)
7979
return
8080

lib/spack/spack/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ def __call__(self, *argv, **kwargs):
528528
529529
Keyword Args:
530530
fail_on_error (optional bool): Don't raise an exception on error
531+
global_args (optional list): List of global spack arguments:
532+
simulates ``spack [global_args] [command] [*argv]``
531533
532534
Returns:
533535
(str): combined output and error as a string
@@ -540,8 +542,10 @@ def __call__(self, *argv, **kwargs):
540542
self.returncode = None
541543
self.error = None
542544

545+
prepend = kwargs['global_args'] if 'global_args' in kwargs else []
546+
543547
args, unknown = self.parser.parse_known_args(
544-
[self.command_name] + list(argv))
548+
prepend + [self.command_name] + list(argv))
545549

546550
fail_on_error = kwargs.get('fail_on_error', True)
547551

lib/spack/spack/test/cmd/location.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
# location prints out "locations of packages and spack directories"
2121
location = SpackCommand('location')
22+
env = SpackCommand('env')
2223

2324

2425
@pytest.fixture
@@ -84,6 +85,27 @@ def test_location_env(mock_test_env):
8485
assert location('--env', test_env_name).strip() == env_dir
8586

8687

88+
def test_location_env_flag_interference(mutable_mock_env_path, tmpdir):
89+
"""
90+
Tests that specifying an active environment using `spack -e x location ...`
91+
does not interfere with the location command flags.
92+
"""
93+
94+
# create two environments
95+
env('create', 'first_env')
96+
env('create', 'second_env')
97+
98+
global_args = ['-e', 'first_env']
99+
100+
# `spack -e first_env location -e second_env` should print the env
101+
# path of second_env
102+
assert 'first_env' not in location('-e', 'second_env', global_args=global_args)
103+
104+
# `spack -e first_env location --packages` should not print
105+
# the environment path of first_env.
106+
assert 'first_env' not in location('--packages', global_args=global_args)
107+
108+
87109
def test_location_env_missing():
88110
"""Tests spack location --env."""
89111
missing_env_name = 'missing-env'

0 commit comments

Comments
 (0)
X Tutup