From 16b83fcb74c53f87dd1f0597aceb6b22ad7d028b Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 21 Jul 2023 14:32:32 +0200 Subject: [PATCH] =?UTF-8?q?[matter=5Fyamltests]=20Add=20an=20implicit=20ru?= =?UTF-8?q?le=20that=20the=20key=20'value'=20in=20argum=E2=80=A6=20(#28068?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [matter_yamltests] Add an implicit rule that the key 'value' in arguments can not be used if the command is not a 'writeAttribute' * Update scripts/py_matter_yamltests/matter_yamltests/errors.py --------- Co-authored-by: Boris Zbarsky --- .../py_matter_yamltests/matter_yamltests/errors.py | 12 ++++++++++++ .../matter_yamltests/yaml_loader.py | 14 +++++++++++--- scripts/py_matter_yamltests/test_yaml_loader.py | 6 ++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/scripts/py_matter_yamltests/matter_yamltests/errors.py b/scripts/py_matter_yamltests/matter_yamltests/errors.py index e38cc3a4875ff1..fb525bae6c8850 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/errors.py +++ b/scripts/py_matter_yamltests/matter_yamltests/errors.py @@ -198,3 +198,15 @@ def __init__(self, content): super().__init__(message) self.tag_key_with_error(content, 'response') + + +class TestStepArgumentsValueError(TestStepError): + """Raise when a test step arguments use the 'value' keyword but the command is not trying to write to an attribute""" + + def __init__(self, content): + message = 'The "value" key can not be used in conjuction with a command that is not "writeAttribute"' + super().__init__(message) + + self.tag_key_with_error(content, 'command') + arguments = content.get('arguments') + self.tag_key_with_error(arguments, 'value') diff --git a/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py b/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py index 6dabfdd164ee1a..50a9217ee18b1a 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py +++ b/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py @@ -15,9 +15,9 @@ from typing import Tuple, Union -from .errors import (TestStepError, TestStepGroupEndPointError, TestStepGroupResponseError, TestStepInvalidTypeError, - TestStepKeyError, TestStepNodeIdAndGroupIdError, TestStepResponseVariableError, TestStepValueAndValuesError, - TestStepVerificationStandaloneError, TestStepWaitResponseError) +from .errors import (TestStepArgumentsValueError, TestStepError, TestStepGroupEndPointError, TestStepGroupResponseError, + TestStepInvalidTypeError, TestStepKeyError, TestStepNodeIdAndGroupIdError, TestStepResponseVariableError, + TestStepValueAndValuesError, TestStepVerificationStandaloneError, TestStepWaitResponseError) from .fixes import add_yaml_support_for_scientific_notation_without_dot try: @@ -120,6 +120,7 @@ def __check_test_step(self, config: dict, content): content) self.__rule_wait_should_not_expect_a_response(content) self.__rule_response_variable_should_exist_in_config(config, content) + self.__rule_argument_value_is_only_when_writing_attributes(content) if 'arguments' in content: arguments = content.get('arguments') @@ -260,3 +261,10 @@ def __rule_response_variable_should_exist_in_config(self, config, content): response = content.get('response') if isinstance(response, str) and response not in config: raise TestStepResponseVariableError(content) + + def __rule_argument_value_is_only_when_writing_attributes(self, content): + if 'arguments' in content: + command = content.get('command') + arguments = content.get('arguments') + if 'value' in arguments and command != 'writeAttribute': + raise TestStepArgumentsValueError(content) diff --git a/scripts/py_matter_yamltests/test_yaml_loader.py b/scripts/py_matter_yamltests/test_yaml_loader.py index 9c1868f3bf3cb4..d0078a8bea7b98 100644 --- a/scripts/py_matter_yamltests/test_yaml_loader.py +++ b/scripts/py_matter_yamltests/test_yaml_loader.py @@ -267,7 +267,8 @@ def test_key_tests_step_dict_keys(self): load = YamlLoader().load content = ('tests:\n' - ' - {key}: {value}') + ' - command: writeAttribute\n' + ' {key}: {value}') keys = [ 'arguments', ] @@ -278,7 +279,8 @@ def test_key_tests_step_dict_keys(self): for key in keys: _, _, _, _, tests = load( content.format(key=key, value=valid_value)) - self.assertEqual(tests, [{key: {'value': True}}]) + self.assertEqual( + tests, [{'command': 'writeAttribute', key: {'value': True}}]) for value in wrong_values: x = content.format(key=key, value=value)