diff --git a/anta/catalog.py b/anta/catalog.py index 9b752fa05..bc95104fc 100644 --- a/anta/catalog.py +++ b/anta/catalog.py @@ -298,13 +298,11 @@ def __init__( self.indexes_built: bool self.tag_to_tests: defaultdict[str | None, set[AntaTestDefinition]] - self._tests_without_tags: set[AntaTestDefinition] self._init_indexes() def _init_indexes(self) -> None: """Init indexes related variables.""" self.tag_to_tests = defaultdict(set) - self._tests_without_tags = set() self.indexes_built = False @property @@ -486,11 +484,7 @@ def build_indexes(self, filtered_tests: set[str] | None = None) -> None: If a `filtered_tests` set is provided, only the tests in this set will be indexed. - This method populates two attributes: - - - tag_to_tests: A dictionary mapping each tag to a set of tests that contain it. - - - _tests_without_tags: A set of tests that do not have any tags. + This method populates the tag_to_tests attribute, which is a dictionary mapping tags to sets of tests. Once the indexes are built, the `indexes_built` attribute is set to True. """ @@ -504,9 +498,8 @@ def build_indexes(self, filtered_tests: set[str] | None = None) -> None: for tag in test_tags: self.tag_to_tests[tag].add(test) else: - self._tests_without_tags.add(test) + self.tag_to_tests[None].add(test) - self.tag_to_tests[None] = self._tests_without_tags self.indexes_built = True def clear_indexes(self) -> None: diff --git a/anta/runner.py b/anta/runner.py index dcb2d962e..0147c3ccd 100644 --- a/anta/runner.py +++ b/anta/runner.py @@ -146,8 +146,7 @@ def prepare_tests( # Using a set to avoid inserting duplicate tests device_to_tests: defaultdict[AntaDevice, set[AntaTestDefinition]] = defaultdict(set) - # Create AntaTestRunner tuples from the tags - final_tests_count = 0 + # Create the device to tests mapping from the tags for device in inventory.devices: if tags: if not any(tag in device.tags for tag in tags): @@ -160,8 +159,6 @@ def prepare_tests( # Add the tests with matching tags from device tags device_to_tests[device].update(catalog.get_tests_by_tags(device.tags)) - final_tests_count += len(device_to_tests[device]) - if len(device_to_tests.values()) == 0: msg = ( f"There are no tests{f' matching the tags {tags} ' if tags else ' '}to run in the current test catalog and device inventory, please verify your inputs." diff --git a/tests/units/test_catalog.py b/tests/units/test_catalog.py index ca78a870c..57a8e2f3b 100644 --- a/tests/units/test_catalog.py +++ b/tests/units/test_catalog.py @@ -260,10 +260,10 @@ def test_build_indexes_all(self) -> None: """Test AntaCatalog.build_indexes().""" catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog_with_tags.yml") catalog.build_indexes() - assert len(catalog._tests_without_tags) == 6 + assert len(catalog.tag_to_tests[None]) == 6 assert "leaf" in catalog.tag_to_tests assert len(catalog.tag_to_tests["leaf"]) == 3 - all_unique_tests = catalog._tests_without_tags + all_unique_tests = catalog.tag_to_tests[None] for tests in catalog.tag_to_tests.values(): all_unique_tests.update(tests) assert len(all_unique_tests) == 11 @@ -275,8 +275,8 @@ def test_build_indexes_filtered(self) -> None: catalog.build_indexes({"VerifyUptime", "VerifyCoredump", "VerifyL3MTU"}) assert "leaf" in catalog.tag_to_tests assert len(catalog.tag_to_tests["leaf"]) == 1 - assert len(catalog._tests_without_tags) == 1 - all_unique_tests = catalog._tests_without_tags + assert len(catalog.tag_to_tests[None]) == 1 + all_unique_tests = catalog.tag_to_tests[None] for tests in catalog.tag_to_tests.values(): all_unique_tests.update(tests) assert len(all_unique_tests) == 4