From 828fccd56561d73e50623abda432b062a10c3eb7 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 24 Jan 2023 11:27:55 +0100 Subject: [PATCH] [matter_yamltests] Add a SpecDefinitionsFromPath helper instead of duplicating it --- .../matter_yamltests/definitions.py | 21 ++++++++++++++++++ .../yamltest_with_chip_repl_tester.py | 22 ++----------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/scripts/py_matter_yamltests/matter_yamltests/definitions.py b/scripts/py_matter_yamltests/matter_yamltests/definitions.py index 98599072f3c9a2..728ec05a8bb4df 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/definitions.py +++ b/scripts/py_matter_yamltests/matter_yamltests/definitions.py @@ -14,6 +14,8 @@ # limitations under the License. import enum +import functools +import glob from typing import List from matter_idl.matter_idl_types import * @@ -204,3 +206,22 @@ def __get_by_id(self, cluster_id: int, target_id: int, target_type: str): return None return targets.get(target_id) + + +def SpecDefinitionsFromPath(path: str): + def sort_with_global_attribute_first(a, b): + if a.endswith('global-attributes.xml'): + return -1 + elif b.endswith('global-attributes.xml'): + return 1 + elif a > b: + return 1 + elif a == b: + return 0 + elif a < b: + return -1 + + filenames = glob.glob(path, recursive=False) + filenames.sort(key=functools.cmp_to_key(sort_with_global_attribute_first)) + sources = [ParseSource(source=name) for name in filenames] + return SpecDefinitions(sources) diff --git a/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py b/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py index ce1db0330d18d8..9b3021756f5308 100644 --- a/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py +++ b/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import functools -import glob import os import tempfile @@ -31,7 +29,7 @@ import click from chip.ChipStack import * from chip.yaml.runner import ReplTestRunner -from matter_yamltests.definitions import ParseSource, SpecDefinitions +from matter_yamltests.definitions import SpecDefinitionsFromPath from matter_yamltests.parser import TestParser _DEFAULT_CHIP_ROOT = os.path.abspath( @@ -40,19 +38,6 @@ os.path.join(_DEFAULT_CHIP_ROOT, "src/app/zap-templates/zcl/data-model/")) -def _sort_with_global_attribute_first(a, b): - if a.endswith('global-attributes.xml'): - return -1 - elif b.endswith('global-attributes.xml'): - return 1 - elif a > b: - return 1 - elif a == b: - return 0 - elif a < b: - return -1 - - @click.command() @click.option( '--setup-code', @@ -89,10 +74,7 @@ def main(setup_code, yaml_path, node_id): dev_ctrl.CommissionWithCode(setup_code, node_id) # Creating Cluster definition. - cluster_xml_filenames = glob.glob(_CLUSTER_XML_DIRECTORY_PATH + '/*/*.xml', recursive=False) - cluster_xml_filenames.sort(key=functools.cmp_to_key(_sort_with_global_attribute_first)) - sources = [ParseSource(source=name) for name in cluster_xml_filenames] - clusters_definitions = SpecDefinitions(sources) + clusters_definitions = SpecDefinitionsFromPath(_CLUSTER_XML_DIRECTORY_PATH + '/*/*.xml') # Parsing YAML test and setting up chip-repl yamltests runner. yaml = TestParser(yaml_path, None, clusters_definitions)