From 5c20a2324ce26b7b0e657622ab6f88555355062a Mon Sep 17 00:00:00 2001 From: Harvey Lynden Date: Fri, 13 Dec 2024 17:16:47 +0100 Subject: [PATCH] Tags Filtering Fix 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 ' Reference: https://github.com/avocado-framework/avocado/issues/6066 Signed-off-by: Harvey Lynden --- avocado/plugins/list.py | 61 +++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/avocado/plugins/list.py b/avocado/plugins/list.py index 2cf67e26d1..23deae0f26 100644 --- a/avocado/plugins/list.py +++ b/avocado/plugins/list.py @@ -76,11 +76,7 @@ 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: matrix = self._prepare_matrix_for_display(matrix, verbose) - for line in iter_tabular_output(matrix, header=header, strip=True): LOG_UI.debug(line) @@ -133,6 +129,39 @@ 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. @@ -142,21 +171,19 @@ def _get_resolution_matrix(suite): """ test_matrix = [] verbose = suite.config.get("core.verbose") + filter_by_tags = suite.config.get("filter.by_tags.tags") + for resolution in suite.resolutions: + if resolution.result != ReferenceResolutionResult.SUCCESS: + continue + for runnable in resolution.resolutions: - if verbose: - tags = runnable.tags or {} - test_matrix.append( - ( - runnable.kind, - runnable.identifier, - runnable.uri, - resolution.origin, - tags, - ) - ) - else: - test_matrix.append((runnable.kind, runnable.identifier)) + 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 @staticmethod