diff options
author | Chandan Singh <chandan@chandansingh.net> | 2020-05-14 19:24:07 +0000 |
---|---|---|
committer | Chandan Singh <chandan@chandansingh.net> | 2020-05-14 19:32:41 +0000 |
commit | e5a1c606592a9a45224955d9292869502969db16 (patch) | |
tree | 6fcea5fdff19a6a181046fe39c38171964712559 | |
parent | dd2eb18bfc60cef39c9aa478397a4ff9a6c87a1d (diff) | |
download | buildstream-chandan/fix-subst-non-greedy.tar.gz |
_frontend/profile: Use non-greedy search to substitute variableschandan/fix-subst-non-greedy
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.
-rw-r--r-- | src/buildstream/_frontend/profile.py | 2 | ||||
-rw-r--r-- | tests/format/substitutions.py | 20 |
2 files changed, 21 insertions, 1 deletions
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" |