Skip to content

Commit

Permalink
Add logging of applied adjust rules
Browse files Browse the repository at this point in the history
  • Loading branch information
happz committed Aug 21, 2023
1 parent 69f3d57 commit 79725a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
34 changes: 32 additions & 2 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,32 @@ def _export(
)


def create_adjust_callback(logger: tmt.log.Logger) -> fmf.base.AdjustCallback:
"""
Create a custom callback for fmf's adjust.
Given the ``adjust`` rules are applied on many places, for
proper logging they need their own specific logger. Create
a callback closure with the given logger.
"""

def callback(
node: fmf.Tree,
rule: _RawAdjustRule,
applied: bool) -> None:
if applied is False:
return

logger.verbose(
f"Adjust rule applied to '{node.name}'",
tmt.utils.format_value(rule, key_color='green'),
color='cyan',
level=3,
topic=tmt.log.Topic.ADJUST_RULINGS)

return callback


# Types describing content accepted by various require-like keys: strings, fmf ids,
# paths, or lists mixing various types.
#
Expand Down Expand Up @@ -2587,7 +2613,9 @@ def tree(self) -> fmf.Tree:
except fmf.utils.FileError as error:
raise tmt.utils.GeneralError(f"Invalid yaml syntax: {error}")
# Adjust metadata for current fmf context
self._tree.adjust(fmf.context.Context(**self._fmf_context))
self._tree.adjust(
fmf.context.Context(**self._fmf_context),
on_rule=create_adjust_callback(self._logger))
return self._tree

@tree.setter
Expand Down Expand Up @@ -3876,7 +3904,9 @@ def resolve_dynamic_ref(
reference_tree = fmf.Tree(data=data)
if not plan:
raise tmt.utils.FileError("Cannot get plan fmf context to evaluate dynamic ref.")
reference_tree.adjust(fmf.context.Context(**plan._fmf_context))
reference_tree.adjust(
fmf.context.Context(**plan._fmf_context),
on_rule=create_adjust_callback(logger))
# Also temporarily build a plan so that env and context variables are expanded
Plan(logger=logger, node=reference_tree, run=plan.my_run, skip_validation=True)
ref = reference_tree.get("ref")
Expand Down
1 change: 1 addition & 0 deletions tmt/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
class Topic(enum.Enum):
KEY_NORMALIZATION = 'key-normalization'
CLI_INVOCATIONS = 'cli-invocations'
ADJUST_RULINGS = 'adjust-rulings'


DEFAULT_TOPICS: Set[Topic] = set()
Expand Down

0 comments on commit 79725a7

Please sign in to comment.