|
18 | 18 | import glob
|
19 | 19 | import os
|
20 | 20 | import subprocess
|
| 21 | +import argparse |
21 | 22 |
|
22 | 23 | # Function to load environment variables from a YAML file
|
23 |
| - |
24 |
| - |
25 | 24 | 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 | + """ |
26 | 37 | with open(file_path, 'r') as file:
|
27 | 38 | for line in file:
|
28 | 39 | if line.strip(): # Skip empty lines
|
29 | 40 | key, value = line.strip().split(': ', 1)
|
30 | 41 | os.environ[key] = value
|
31 | 42 |
|
| 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) |
32 | 49 |
|
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") |
35 | 52 |
|
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 | + } |
38 | 84 |
|
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 | + """ |
41 | 92 |
|
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")) |
73 | 95 |
|
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] |
76 | 98 |
|
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) |
87 | 104 |
|
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