Skip to content

Commit

Permalink
[matter_yamltests] Add a SpecDefinitionsFromPath helper instead of du…
Browse files Browse the repository at this point in the history
…plicating it (#24612)
  • Loading branch information
vivien-apple authored and pull[bot] committed Aug 9, 2023
1 parent 8a39e90 commit 1267163
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
21 changes: 21 additions & 0 deletions scripts/py_matter_yamltests/matter_yamltests/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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)
22 changes: 2 additions & 20 deletions scripts/tests/chiptest/yamltest_with_chip_repl_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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',
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1267163

Please sign in to comment.