diff --git a/scripts/py_matter_yamltests/matter_yamltests/definitions.py b/scripts/py_matter_yamltests/matter_yamltests/definitions.py index 9f1960a1ae815a..35e546bb64af24 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 * @@ -228,3 +230,22 @@ def __enforce_casing(self, target_name: str, targets: list): if name.lower() == target_name.lower(): raise KeyError( f'Unknown target {target_name}. Did you mean {name} ?') + + +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 2cf03caf9a858b..dbfba4d9988fca 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 import traceback @@ -32,7 +30,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( @@ -41,19 +39,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', @@ -91,10 +76,7 @@ def main(setup_code, yaml_path, node_id): try: # 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)