Skip to content

Commit a13f027

Browse files
Fixed the script as per comments added to the code review
1 parent db6c35b commit a13f027

File tree

2 files changed

+76
-57
lines changed

2 files changed

+76
-57
lines changed

.github/workflows/tests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ jobs:
496496
run: |
497497
mkdir -p out/trace_data
498498
scripts/run_in_python_env.sh out/venv './scripts/tests/run_python_test.py --load-from-env /tmp/test_env.yaml --script src/controller/python/test/test_scripts/mobile-device-test.py'
499-
scripts/run_in_python_env.sh out/venv 'python3 ./src/python_testing/execute_python_tests.py'
499+
scripts/run_in_python_env.sh out/venv 'python3 ./src/python_testing/execute_python_tests.py --env-file /tmp/test_env.yaml --search-directory src/python_testing'
500500
scripts/run_in_python_env.sh out/venv './scripts/tests/run_python_test.py --script "src/python_testing/TestConformanceSupport.py" --script-args "--trace-to json:out/trace_data/test-{SCRIPT_BASE_NAME}.json --trace-to perfetto:out/trace_data/test-{SCRIPT_BASE_NAME}.perfetto"'
501501
scripts/run_in_python_env.sh out/venv './scripts/tests/run_python_test.py --script "src/python_testing/TestMatterTestingSupport.py" --script-args "--trace-to json:out/trace_data/test-{SCRIPT_BASE_NAME}.json --trace-to perfetto:out/trace_data/test-{SCRIPT_BASE_NAME}.perfetto"'
502502
scripts/run_in_python_env.sh out/venv './scripts/tests/run_python_test.py --script "src/python_testing/TestSpecParsingSupport.py" --script-args "--trace-to json:out/trace_data/test-{SCRIPT_BASE_NAME}.json --trace-to perfetto:out/trace_data/test-{SCRIPT_BASE_NAME}.perfetto"'

src/python_testing/execute_python_tests.py

+75-56
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,94 @@
1818
import glob
1919
import os
2020
import subprocess
21+
import argparse
2122

2223
# Function to load environment variables from a YAML file
23-
24-
2524
def load_env_from_yaml(file_path):
25+
"""
26+
Load environment variables from the specified YAML file.
27+
28+
The YAML file contains key-value pairs that define --app environment variables
29+
required for the test scripts to run. These variables configurations needed during the test execution.
30+
31+
This function reads the YAML file and sets the environment variables
32+
in the current process's environment using os.environ.
33+
34+
Args:
35+
file_path (str): The path to the YAML file containing the environment variables.
36+
"""
2637
with open(file_path, 'r') as file:
2738
for line in file:
2839
if line.strip(): # Skip empty lines
2940
key, value = line.strip().split(': ', 1)
3041
os.environ[key] = value
3142

43+
def main(search_directory, env_file):
44+
# Determine the root directory of the CHIP project
45+
chip_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
46+
47+
# Load environment variables from the specified file
48+
load_env_from_yaml(env_file)
3249

33-
# Load environment variables from test_env.yaml
34-
load_env_from_yaml('/tmp/test_env.yaml')
50+
# Define the base command to run tests
51+
base_command = os.path.join(chip_root, "scripts/tests/run_python_test.py")
3552

36-
# Define the base command to run tests
37-
base_command = "./scripts/tests/run_python_test.py"
53+
# Define the files and patterns to exclude
54+
excluded_patterns = {
55+
"MinimalRepresentation.py",
56+
"TC_ACL_2_2.py",
57+
"TC_CNET_4_4.py",
58+
"TC_DGGEN_3_2.py",
59+
"TC_EEVSE_Utils.py",
60+
"TC_EnergyReporting_Utils.py",
61+
"TC_OpstateCommon.py",
62+
"TC_TMP_2_1.py",
63+
"TC_pics_checker.py",
64+
"TestCommissioningTimeSync.py",
65+
"TestConformanceSupport.py",
66+
"TestIdChecks.py",
67+
"TestMatterTestingSupport.py",
68+
"TestSpecParsingSupport.py",
69+
"TestTimeSyncTrustedTimeSource.py",
70+
"basic_composition_support.py",
71+
"conformance_support.py",
72+
"drlk_2_x_common.py",
73+
"execute_python_tests.py",
74+
"global_attribute_ids.py",
75+
"hello_external_runner.py",
76+
"hello_test.py",
77+
"matter_testing_support.py",
78+
"pics_support.py",
79+
"spec_parsing_support.py",
80+
"taglist_and_topology_test_support.py",
81+
"test_plan_support.py",
82+
"test_plan_table_generator.py"
83+
}
3884

39-
# Define the directory to search for Python scripts
40-
search_directory = "src/python_testing"
85+
"""
86+
Explanation for excluded files:
87+
The above list of files are excluded from the tests as they are either not app-specific tests
88+
or are run through a different block of tests mentioned in tests.yaml.
89+
This is to ensure that only relevant test scripts are executed, avoiding redundancy and ensuring
90+
the correctness of the test suite.
91+
"""
4192

42-
# Define the files and patterns to exclude
43-
excluded_patterns = [
44-
"MinimalRepresentation.py",
45-
"TC_ACL_2_2.py",
46-
"TC_CNET_4_4.py",
47-
"TC_DGGEN_3_2.py",
48-
"TC_EEVSE_Utils.py",
49-
"TC_EnergyReporting_Utils.py",
50-
"TC_OpstateCommon.py",
51-
"TC_TMP_2_1.py",
52-
"TC_pics_checker.py",
53-
"TestCommissioningTimeSync.py",
54-
"TestConformanceSupport.py",
55-
"TestIdChecks.py",
56-
"TestMatterTestingSupport.py",
57-
"TestSpecParsingSupport.py",
58-
"TestTimeSyncTrustedTimeSource.py",
59-
"basic_composition_support.py",
60-
"conformance_support.py",
61-
"drlk_2_x_common.py",
62-
"execute_python_tests.py",
63-
"global_attribute_ids.py",
64-
"hello_external_runner.py",
65-
"hello_test.py",
66-
"matter_testing_support.py",
67-
"pics_support.py",
68-
"spec_parsing_support.py",
69-
"taglist_and_topology_test_support.py",
70-
"test_plan_support.py",
71-
"test_plan_table_generator.py"
72-
]
93+
# Get all .py files in the directory
94+
all_python_files = glob.glob(os.path.join(search_directory, "*.py"))
7395

74-
# Get all .py files in the directory
75-
all_python_files = glob.glob(os.path.join(search_directory, "*.py"))
96+
# Filter out the files matching the excluded patterns
97+
python_files = [file for file in all_python_files if os.path.basename(file) not in excluded_patterns]
7698

77-
# Filter out the files matching the excluded patterns
78-
python_files = []
79-
for file in all_python_files:
80-
exclude = False
81-
for pattern in excluded_patterns:
82-
if glob.fnmatch.fnmatch(os.path.basename(file), pattern):
83-
exclude = True
84-
break
85-
if not exclude:
86-
python_files.append(file)
99+
# Run each script with the base command
100+
for script in python_files:
101+
full_command = f"{base_command} --load-from-env {env_file} --script {script}"
102+
print(f"Running command: {full_command}")
103+
subprocess.run(full_command, shell=True, check=True)
87104

88-
# Run each script with the base command
89-
for script in python_files:
90-
full_command = f"{base_command} --load-from-env /tmp/test_env.yaml --script {script}"
91-
print(f"Running command: {full_command}")
92-
subprocess.run(full_command, shell=True)
105+
if __name__ == "__main__":
106+
parser = argparse.ArgumentParser(description="Run Python test scripts.")
107+
parser.add_argument("--search-directory", type=str, default="src/python_testing", help="Directory to search for Python scripts.")
108+
parser.add_argument("--env-file", type=str, default="/tmp/test_env.yaml", help="Path to the environment variables file.")
109+
110+
args = parser.parse_args()
111+
main(args.search_directory, args.env_file)

0 commit comments

Comments
 (0)