From e5a1c606592a9a45224955d9292869502969db16 Mon Sep 17 00:00:00 2001 From: Chandan Singh Date: Thu, 14 May 2020 19:24:07 +0000 Subject: _frontend/profile: Use non-greedy search to substitute variables Use non-greedy search to ensure that we stop at the next closing brace. Otherwise, for a string like `%{variable} {variable}`, the second variable will also get substituted. Fixes #1307. --- src/buildstream/_frontend/profile.py | 2 +- tests/format/substitutions.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/format/substitutions.py diff --git a/src/buildstream/_frontend/profile.py b/src/buildstream/_frontend/profile.py index f49be5b0a..02b601c66 100644 --- a/src/buildstream/_frontend/profile.py +++ b/src/buildstream/_frontend/profile.py @@ -73,4 +73,4 @@ class Profile: return self.fmt(formatted, **kwargs) # Lazy regex, after our word, match anything that does not have '%' - return re.sub(r"%(\{(" + varname + r")[^%]*\})", subst_callback, text) + return re.sub(r"%(\{(" + varname + r")[^%]*?\})", subst_callback, text) diff --git a/tests/format/substitutions.py b/tests/format/substitutions.py new file mode 100644 index 000000000..265f13e66 --- /dev/null +++ b/tests/format/substitutions.py @@ -0,0 +1,20 @@ +# Pylint doesn't play well with fixtures and dependency injection from pytest +# pylint: disable=redefined-outer-name + +import os +import pytest +from buildstream.testing import cli # pylint: disable=unused-import + + +DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "project", "default") + + +# Test that output is formatted correctly, when there are multiple matches of a +# variable that is known to BuildStream. +# +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_match_multiple(cli, datafiles): + project = str(datafiles) + result = cli.run(project=project, args=["show", "--format", "%{name} {name} %{name}", "manual.bst"]) + result.assert_success() + assert result.output == "manual.bst {name} manual.bst\n" -- cgit v1.2.1