From b15f9d011ca1bf7f24ebed80fac2027042d87a81 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 3 Jan 2024 17:44:59 +0100 Subject: [PATCH] Spec.format: error on old style format strings (#41934) --- lib/spack/spack/spec.py | 14 ++++++++++++++ lib/spack/spack/test/spec_semantics.py | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 20e5c3ffa3..8c15a873af 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -213,6 +213,19 @@ def __call__(self, match): return clr.colorize(re.sub(_SEPARATORS, insert_color(), str(spec)) + "@.") +OLD_STYLE_FMT_RE = re.compile(r"\${[A-Z]+}") + + +def ensure_modern_format_string(fmt: str) -> None: + """Ensure that the format string does not contain old ${...} syntax.""" + result = OLD_STYLE_FMT_RE.search(fmt) + if result: + raise SpecFormatStringError( + f"Format string `{fmt}` contains old syntax `{result.group(0)}`. " + "This is no longer supported." + ) + + @lang.lazy_lexicographic_ordering class ArchSpec: """Aggregate the target platform, the operating system and the target microarchitecture.""" @@ -4360,6 +4373,7 @@ def format(self, format_string=DEFAULT_FORMAT, **kwargs): that accepts a string and returns another one """ + ensure_modern_format_string(format_string) color = kwargs.get("color", False) transform = kwargs.get("transform", {}) diff --git a/lib/spack/spack/test/spec_semantics.py b/lib/spack/spack/test/spec_semantics.py index 87ed1e4b3f..08caba1518 100644 --- a/lib/spack/spack/test/spec_semantics.py +++ b/lib/spack/spack/test/spec_semantics.py @@ -1517,3 +1517,9 @@ def test_edge_equality_does_not_depend_on_virtual_order(): assert edge1 == edge2 assert tuple(sorted(edge1.virtuals)) == edge1.virtuals assert tuple(sorted(edge2.virtuals)) == edge1.virtuals + + +def test_old_format_strings_trigger_error(default_mock_concretization): + s = Spec("a").concretized() + with pytest.raises(SpecFormatStringError): + s.format("${PACKAGE}-${VERSION}-${HASH}")