Skip to content

Commit 2435188

Browse files
andy31415andreilitvin
authored andcommitted
Add a script capable to check for backwards compatibility differences between .matter files (#29349)
* Start defining a compatibility checker with unit tests * Enumeration validation passes * Restyle * Mark unimplemented items with TODO * Add some cluster validation tests * Add validation for bitmap statuses and unit tests for that * Mild restyle and clearer code * Take into consideration cluster sides * Update naming a bit * More detailed messages for code entry changes * Use sub-tests to mark clearly what failures occur where * Fix typing and common logic * Add implementation and tests for event backward compatibility * Restyle * Add command backwards compatibility checks and tests, better error reporting * Less noisy output for unit tests * Restyle * Fixed types * Split tests so we have better split and execution report - 23 tests instead of just 5 * Add minor unit test * Added struct handling and unit tests * Restyle * Attribute validation * Restyle * Fix typo * Add some extra logic to not detect renames in fields * Additional tests for renames of fields * Add struct tests for reorder and renames * Add cluster names for error reports * Restyle * Fix test comments * Move defs outside init into separate file, update build scripts * Move to single file for use as an import instead of a module directory * Remove unused import * Rename some global methods to pep8 * Move more methods to pep8 * Remove unused import * Make sure unit test is run as part of standard tests * Update comment * Ensure click argument for choice is list of str instead of a dictionary view (makes mypy happy) --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com>
1 parent 0ce0949 commit 2435188

6 files changed

+768
-5
lines changed
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2023 Project CHIP Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
#
17+
import logging
18+
import sys
19+
20+
import click
21+
22+
try:
23+
import coloredlogs
24+
_has_coloredlogs = True
25+
except ImportError:
26+
_has_coloredlogs = False
27+
28+
try:
29+
from matter_idl.matter_idl_parser import CreateParser
30+
except ImportError:
31+
import os
32+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'py_matter_idl')))
33+
from matter_idl.matter_idl_parser import CreateParser
34+
35+
from matter_idl.backwards_compatibility import is_backwards_compatible
36+
37+
# Supported log levels, mapping string values required for argument
38+
# parsing into logging constants
39+
__LOG_LEVELS__ = {
40+
'debug': logging.DEBUG,
41+
'info': logging.INFO,
42+
'warn': logging.WARN,
43+
'fatal': logging.FATAL,
44+
}
45+
46+
47+
@click.command()
48+
@click.option(
49+
'--log-level',
50+
default='INFO',
51+
type=click.Choice(list(__LOG_LEVELS__.keys()), case_sensitive=False),
52+
help='Determines the verbosity of script output')
53+
@click.argument(
54+
'old_idl',
55+
type=click.Path(exists=True))
56+
@click.argument(
57+
'new_idl',
58+
type=click.Path(exists=True))
59+
def main(log_level, old_idl, new_idl):
60+
"""
61+
Parses MATTER IDL files (.matter) and validates that <new_idl> is backwards compatible
62+
when compared to <old_idl>.
63+
64+
Generally additions are safe, but not deletes or id changes. Actual set of rules
65+
defined in `backwards_compatibility` module.
66+
"""
67+
if _has_coloredlogs:
68+
coloredlogs.install(level=__LOG_LEVELS__[
69+
log_level], fmt='%(asctime)s %(levelname)-7s %(message)s')
70+
else:
71+
logging.basicConfig(
72+
level=__LOG_LEVELS__[log_level],
73+
format='%(asctime)s %(levelname)-7s %(message)s',
74+
datefmt='%Y-%m-%d %H:%M:%S'
75+
)
76+
77+
logging.info("Parsing OLD idl from %s" % old_idl)
78+
old_tree = CreateParser().parse(open(old_idl, "rt").read())
79+
80+
logging.info("Parsing NEW idl from %s" % new_idl)
81+
new_tree = CreateParser().parse(open(new_idl, "rt").read())
82+
83+
if not is_backwards_compatible(original=old_tree, updated=new_tree):
84+
sys.exit(1)
85+
86+
sys.exit(0)
87+
88+
89+
if __name__ == '__main__':
90+
main(auto_envvar_prefix='CHIP')

scripts/py_matter_idl/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pw_python_package("matter_idl") {
5959
sources = matter_idl_generator_sources
6060

6161
tests = [
62+
"matter_idl/test_backwards_compatibility.py",
6263
"matter_idl/test_matter_idl_parser.py",
6364
"matter_idl/test_generators.py",
6465
"matter_idl/test_xml_parser.py",

scripts/py_matter_idl/files.gni

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ matter_idl_generator_templates = [
2121

2222
matter_idl_generator_sources = [
2323
"${chip_root}/scripts/py_matter_idl/matter_idl/__init__.py",
24+
"${chip_root}/scripts/py_matter_idl/matter_idl/backwards_compatibility.py",
2425
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/__init__.py",
2526
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/cpp/__init__.py",
2627
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py",
@@ -34,6 +35,7 @@ matter_idl_generator_sources = [
3435
"${chip_root}/scripts/py_matter_idl/matter_idl/lint/types.py",
3536
"${chip_root}/scripts/py_matter_idl/matter_idl/matter_idl_parser.py",
3637
"${chip_root}/scripts/py_matter_idl/matter_idl/matter_idl_types.py",
38+
"${chip_root}/scripts/py_matter_idl/matter_idl/test_backwards_compatibility.py",
3739
"${chip_root}/scripts/py_matter_idl/matter_idl/test_generators.py",
3840
"${chip_root}/scripts/py_matter_idl/matter_idl/test_matter_idl_parser.py",
3941
"${chip_root}/scripts/py_matter_idl/matter_idl/test_xml_parser.py",

0 commit comments

Comments
 (0)