Skip to content

Commit

Permalink
Revert "Cherry-pick python_test_parser.py from origin/main"
Browse files Browse the repository at this point in the history
This reverts commit 5d65ce5.
  • Loading branch information
swan-amazon committed Dec 20, 2024
1 parent 5d65ce5 commit ff8943b
Showing 1 changed file with 80 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#
import ast
import json
import re
from pathlib import Path
from typing import Any, List, Optional, Union
Expand All @@ -36,6 +37,7 @@
"TC_IDM_10_2",
"TC_IDM_10_3",
"TC_IDM_10_4",
"TC_IDM_10_5",
"TC_IDM_12_1",
]

Expand All @@ -58,20 +60,71 @@ def parse_python_script(path: Path) -> list[PythonTest]:
Example: file TC_ACE_1_3.py has the methods test_TC_ACE_1_3, desc_TC_ACE_1_3,
pics_TC_ACE_1_3 and steps_TC_ACE_1_3.
"""
with open(path, "r") as python_file:
parsed_python_file = ast.parse(python_file.read())

test_classes = __test_classes(parsed_python_file)

test_cases: list[PythonTest] = []
for c in test_classes:
test_methods = __test_methods(c)
test_names = __test_case_names(test_methods)
python_tests: list[PythonTest] = []

with open(path, "r") as json_file:
parsed_scripts = json.load(json_file)

if len(parsed_scripts) == 0:
return python_tests

for script_info in parsed_scripts["tests"]:
test_function = script_info["function"]
test_name = __test_case_name(test_function)
if test_name is None:
logger.info(f"Failed to parse test name [{test_function}].")
continue

test_description = script_info["desc"]
test_pics = script_info["pics"]
test_path = script_info["path"]
test_class_name = script_info["class_name"]
parsed_steps = script_info["steps"]

is_commssioning = False
test_steps: list[MatterTestStep] = []
for index, step in enumerate(parsed_steps):
step_description = step["description"]

if index == 0:
is_commssioning = step["is_commissioning"]

test_steps.append(
MatterTestStep(
label=step_description,
is_commissioning=is_commssioning,
)
)

for test_name in test_names:
test_cases.append(__parse_test_case(test_name, test_methods, c.name, path))
# - PythonTestType.MANDATORY: Mandatory test cases
# - PythonTestType.LEGACY: Tests that have only one step and with this
# name: "Run entire test"
# - PythonTestType.COMMISSIONING: Test cases flagged as commissioning
# - PythonTestType.NO_COMMISSIONING: Test cases flagged as no commissioning
if test_name in mandatory_python_tcs_public_id:
python_test_type = PythonTestType.MANDATORY
elif len(test_steps) == 1 and test_steps[0].label == "Run entire test":
python_test_type = PythonTestType.LEGACY
elif is_commssioning:
python_test_type = PythonTestType.COMMISSIONING
else:
python_test_type = PythonTestType.NO_COMMISSIONING

python_tests.append(
PythonTest(
name=test_name,
description=test_description,
steps=test_steps,
config={}, # Currently config is not configured in Python Testing
PICS=test_pics,
path=test_path,
type=MatterTestType.AUTOMATED,
class_name=test_class_name,
python_test_type=python_test_type,
)
)

return test_cases
return python_tests


def __test_classes(module: ast.Module) -> list[ast.ClassDef]:
Expand Down Expand Up @@ -122,24 +175,20 @@ def __test_methods(class_def: ast.ClassDef) -> list[FunctionDefType]:
return all_methods


def __test_case_names(methods: list[FunctionDefType]) -> list[str]:
"""Extract test case names from methods that match the pattern "test_TC_[\\S]+".
def __test_case_name(function_name: str) -> Optional[str]:
"""Extract test case name from methods that match the pattern "test_TC_[\\S]+".
Args:
methods (list[FunctionDefType]): List of methods to search from.
methods (str): Function name.
Returns:
list[str]: List of test case names.
str: Test case name.
"""
test_names: list[str] = []
if match := re.match(TC_TEST_FUNCTION_PATTERN, function_name):
if name := match["title"]:
return name

for m in methods:
if isinstance(m.name, str):
if match := re.match(TC_TEST_FUNCTION_PATTERN, m.name):
if name := match["title"]:
test_names.append(name)

return test_names
return None


def __parse_test_case(
Expand All @@ -161,8 +210,8 @@ def __parse_test_case(
try:
tc_desc = __retrieve_description(desc_method)
except Exception as e:
logger.error(
f"Error while parsing description for {tc_name}, Error:{str(e)}"
logger.warning(
f"Failed parsing description method for {tc_name}, Error:{str(e)}"
)

# If the python test does not implement the steps template method,
Expand All @@ -173,14 +222,14 @@ def __parse_test_case(
try:
tc_steps = __retrieve_steps(steps_method)
except Exception as e:
logger.error(f"Error while parsing steps for {tc_name}, Error:{str(e)}")
logger.warning(f"Failed parsing steps method for {tc_name}, Error:{str(e)}")

pics_method = __get_method_by_name(pics_method_name, methods)
if pics_method:
try:
tc_pics = __retrieve_pics(pics_method)
except Exception as e:
logger.error(f"Error while parsing PICS for {tc_name}, Error:{str(e)}")
logger.warning(f"Failed parsing PICS method for {tc_name}, Error:{str(e)}")

# - PythonTestType.COMMISSIONING: test cases that have a commissioning first step
# - PythonTestType.NO_COMMISSIONING: test cases that follow the expected template
Expand Down Expand Up @@ -240,7 +289,9 @@ def __retrieve_steps(method: FunctionDefType) -> List[MatterTestStep]:
step_name = step.args[ARG_STEP_DESCRIPTION_INDEX].value
parsed_step_name = step_name
except Exception as e:
logger.error(f"Error while parsing step from {method.name}, Error:{str(e)}")
logger.warning(
f"Failed parsing step name from {method.name}, Error:{str(e)}"
)
parsed_step_name = "UNABLE TO PARSE TEST STEP NAME"

python_steps.append(
Expand Down

0 comments on commit ff8943b

Please sign in to comment.