Skip to content

Commit

Permalink
Merge pull request #4532 from Flamefire/fix_partial_template_resolve
Browse files Browse the repository at this point in the history
Fix resolved (template) values in case of failure
  • Loading branch information
boegel authored May 30, 2024
2 parents ae29588 + 7323bae commit 431adef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions easybuild/framework/easyconfig/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2026,12 +2026,14 @@ def resolve_template(value, tmpl_dict):
# '%(name)s' -> '%(name)s'
# '%%(name)s' -> '%%(name)s'
if '%' in value:
orig_value = value
value = re.sub(re.compile(r'(%)(?!%*\(\w+\)s)'), r'\1\1', value)

try:
value = value % tmpl_dict
except KeyError:
_log.warning("Unable to resolve template value %s with dict %s", value, tmpl_dict)
value = orig_value # Undo "%"-escaping
else:
# this block deals with references to objects and returns other references
# for reading this is ok, but for self['x'] = {}
Expand Down
24 changes: 24 additions & 0 deletions test/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,30 @@ def test_resolve_template(self):
# '%(name)' is not a correct template spec (missing trailing 's')
self.assertEqual(resolve_template('%(name)', tmpl_dict), '%(name)')

# Correct (un)escaping
values = (
('10%', '10%'),
('%of', '%of'),
('10%of', '10%of'),
('%s', '%s'),
('%%(name)s', '%(name)s'),
('%%%(name)s', '%FooBar'),
('%%%%(name)s', '%%(name)s'),
# It doesn't matter what is resolved
('%%(invalid)s', '%(invalid)s'),
('%%%%(invalid)s', '%%(invalid)s'),
)
for value, expected in values:
self.assertEqual(resolve_template(value, tmpl_dict), expected)
# Templates are resolved
value += ' %(name)s'
expected += ' FooBar'
self.assertEqual(resolve_template(value, tmpl_dict), expected)

# On unknown values the value is returned unchanged
for value in ('%(invalid)s', '%(name)s %(invalid)s', '%%%(invalid)s', '% %(invalid)s', '%s %(invalid)s'):
self.assertEqual(resolve_template(value, tmpl_dict), value)

def test_det_subtoolchain_version(self):
"""Test det_subtoolchain_version function"""
_, all_tc_classes = search_toolchain('')
Expand Down

0 comments on commit 431adef

Please sign in to comment.