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
  • Loading branch information
vivien-apple committed Jan 24, 2023
1 parent 95c1888 commit 54e7a8f
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 @@ -223,3 +225,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

Expand All @@ -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(
Expand All @@ -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',
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 54e7a8f

Please sign in to comment.