Skip to content

Commit

Permalink
Tags Filtering Fix
Browse files Browse the repository at this point in the history
Filtering now properly handles both verbose and non-verbose output modes
while checking for tag matches.
without changing the existing behavior that users expect when using
'avocado list -t <tag>'

Reference:      #6066
Signed-off-by: Harvey Lynden <[email protected]>
  • Loading branch information
harvey0100 committed Dec 13, 2024
1 parent 91752d4 commit d441d2b
Showing 1 changed file with 73 additions and 23 deletions.
96 changes: 73 additions & 23 deletions avocado/plugins/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _prepare_matrix_for_display(matrix, verbose=False):
colored_matrix.append((type_label, item[1]))
return colored_matrix

def _display(self, suite, matrix):
def _display(self, suite, matrix, resolution_matrix=None):
header = None
verbose = suite.config.get("core.verbose")
if verbose:
Expand All @@ -76,14 +76,24 @@ def _display(self, suite, matrix):
TERM_SUPPORT.header_str("Tag(s)"),
)

# Any kind of color, string format and term specific should be applied
# only during output/display phase. So this seems to be a better place
# for this:
# Display test matrix
matrix = self._prepare_matrix_for_display(matrix, verbose)

for line in iter_tabular_output(matrix, header=header, strip=True):
LOG_UI.debug(line)

# Display resolution matrix if available
if resolution_matrix:
LOG_UI.info("")
resolution_header = (
TERM_SUPPORT.header_str("Resolver"),
TERM_SUPPORT.header_str("Reference"),
TERM_SUPPORT.header_str("Info"),
)
for line in iter_tabular_output(
resolution_matrix, header=resolution_header, strip=True
):
LOG_UI.info(line)

self._display_extra(suite, verbose)

@staticmethod
Expand Down Expand Up @@ -133,31 +143,71 @@ def _display_extra(suite, verbose=True):
for key in sorted(suite.tags_stats):
LOG_UI.info("%s: %s", key, suite.tags_stats[key])

@staticmethod
def _matches_tag_filter(runnable, filter_by_tags):
"""Check if runnable matches the tag filter criteria.
:param runnable: The runnable to check
:param filter_by_tags: List of tags to filter by
:returns: True if runnable matches filter, False otherwise
"""
if not filter_by_tags:
return True
if not runnable.tags:
return False
return any(tag in runnable.tags for tag in filter_by_tags)

@staticmethod
def _build_matrix_entry(runnable, resolution, verbose):
"""Build a matrix entry for the given runnable.
:param runnable: The runnable to create entry for
:param resolution: The resolution containing the runnable
:param verbose: Whether to include verbose information
:returns: Tuple containing matrix entry
"""
if verbose:
return (
runnable.kind,
runnable.identifier,
runnable.uri,
resolution.origin,
runnable.tags or {},
)
return (runnable.kind, runnable.identifier)

@staticmethod
def _get_resolution_matrix(suite):
"""Used for resolver.
:returns: a list of tuples with either (kind, uri, tags) or
(kind, uri) depending on whether verbose mode is on
:returns: a tuple of (test_matrix, resolution_matrix) where test_matrix
contains test information and resolution_matrix contains
resolution results
"""
test_matrix = []
resolution_matrix = []
verbose = suite.config.get("core.verbose")
filter_by_tags = suite.config.get("filter.by_tags.tags")

for resolution in suite.resolutions:
for runnable in resolution.resolutions:
if verbose:
tags = runnable.tags or {}
test_matrix.append(
(
runnable.kind,
runnable.identifier,
runnable.uri,
resolution.origin,
tags,
)
if resolution.result != ReferenceResolutionResult.SUCCESS:
resolution_matrix.append(
(
TERM_SUPPORT.warn_header_str(resolution.origin),
resolution.reference,
resolution.info or "",
)
else:
test_matrix.append((runnable.kind, runnable.identifier))
return test_matrix
)
continue

for runnable in resolution.resolutions:
if not List._matches_tag_filter(runnable, filter_by_tags):
continue

entry = List._build_matrix_entry(runnable, resolution, verbose)
test_matrix.append(entry)

return test_matrix, resolution_matrix

@staticmethod
def _save_to_json(matrix, filename, verbose=False):
Expand Down Expand Up @@ -259,8 +309,8 @@ def run(self, config):
config["run.ignore_missing_references"] = True
try:
suite = TestSuite.from_config(config)
matrix = self._get_resolution_matrix(suite)
self._display(suite, matrix)
matrix, resolution_matrix = self._get_resolution_matrix(suite)
self._display(suite, matrix, resolution_matrix)

directory = config.get("list.recipes.write_to_directory")
if directory is not None:
Expand Down

0 comments on commit d441d2b

Please sign in to comment.