summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJordan Galby <67924449+Jordan474@users.noreply.github.com>2024-09-09 10:42:04 +0200
committerGitHub <noreply@github.com>2024-09-09 10:42:04 +0200
commit2502a3c2b647e71b1643eab0e11b4b8eeb1b1122 (patch)
tree545d763aba03a4ec13979493f05fb6b074173442 /lib
parent9bdc97043e021245611d0e62a2c3609c1b388dcb (diff)
downloadspack-2502a3c2b647e71b1643eab0e11b4b8eeb1b1122.tar.gz
spack-2502a3c2b647e71b1643eab0e11b4b8eeb1b1122.tar.bz2
spack-2502a3c2b647e71b1643eab0e11b4b8eeb1b1122.tar.xz
spack-2502a3c2b647e71b1643eab0e11b4b8eeb1b1122.zip
Fix regression in spec format string for indiviual variants (#46206)
Fix a regression in {variants.X} and {variants.X.value} spec format strings.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/spec.py7
-rw-r--r--lib/spack/spack/test/spec_semantics.py14
2 files changed, 19 insertions, 2 deletions
diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py
index 0c869febb1..e420eaada9 100644
--- a/lib/spack/spack/spec.py
+++ b/lib/spack/spack/spec.py
@@ -3914,9 +3914,12 @@ class Spec:
if part.startswith("_"):
raise SpecFormatStringError("Attempted to format private attribute")
else:
- if part == "variants" and isinstance(current, VariantMap):
+ if isinstance(current, VariantMap):
# subscript instead of getattr for variant names
- current = current[part]
+ try:
+ current = current[part]
+ except KeyError:
+ raise SpecFormatStringError(f"Variant '{part}' does not exist")
else:
# aliases
if part == "arch":
diff --git a/lib/spack/spack/test/spec_semantics.py b/lib/spack/spack/test/spec_semantics.py
index cd0f930acd..0f3b58e486 100644
--- a/lib/spack/spack/test/spec_semantics.py
+++ b/lib/spack/spack/test/spec_semantics.py
@@ -741,6 +741,13 @@ class TestSpecSemantics:
("{/hash}", "/", lambda s: "/" + s.dag_hash()),
]
+ variants_segments = [
+ ("{variants.debug}", spec, "debug"),
+ ("{variants.foo}", spec, "foo"),
+ ("{^pkg-a.variants.bvv}", spec["pkg-a"], "bvv"),
+ ("{^pkg-a.variants.foo}", spec["pkg-a"], "foo"),
+ ]
+
other_segments = [
("{spack_root}", spack.paths.spack_root),
("{spack_install}", spack.store.STORE.layout.root),
@@ -768,6 +775,12 @@ class TestSpecSemantics:
callpath, fmt_str = depify("callpath", named_str, sigil)
assert spec.format(fmt_str) == getter(callpath)
+ for named_str, test_spec, variant_name in variants_segments:
+ assert test_spec.format(named_str) == str(test_spec.variants[variant_name])
+ assert test_spec.format(named_str[:-1] + ".value}") == str(
+ test_spec.variants[variant_name].value
+ )
+
for named_str, expected in other_segments:
actual = spec.format(named_str)
assert expected == actual
@@ -827,6 +840,7 @@ class TestSpecSemantics:
r"{dag_hash}",
r"{foo}",
r"{+variants.debug}",
+ r"{variants.this_variant_does_not_exist}",
],
)
def test_spec_formatting_bad_formats(self, default_mock_concretization, fmt_str):