|
17 | 17 | Author: Przemyslaw Wirkus <[email protected]>
|
18 | 18 | """
|
19 | 19 |
|
20 |
| -import re |
21 |
| -import os |
22 |
| -import os.path |
23 |
| - |
24 |
| -from mbed_greentea.mbed_greentea_log import gt_logger |
25 |
| - |
26 |
| -def load_ctest_testsuite(link_target, binary_type='.bin', verbose=False): |
27 |
| - """! Loads CMake.CTest formatted data about tests from test directory |
28 |
| - @return Dictionary of { test_case : test_case_path } pairs |
29 |
| - """ |
30 |
| - result = {} |
31 |
| - if link_target is not None: |
32 |
| - ctest_path = os.path.join(link_target, 'test', 'CTestTestfile.cmake') |
33 |
| - try: |
34 |
| - with open(ctest_path) as ctest_file: |
35 |
| - for line in ctest_file: |
36 |
| - line_parse = parse_ctesttestfile_line(link_target, binary_type, line, verbose=verbose) |
37 |
| - if line_parse: |
38 |
| - test_case, test_case_path = line_parse |
39 |
| - result[test_case] = test_case_path |
40 |
| - except: |
41 |
| - pass # Return empty list if path is not found |
42 |
| - return result |
43 |
| - |
44 |
| -def parse_ctesttestfile_line(link_target, binary_type, line, verbose=False): |
45 |
| - """! Parse lines of CTestTestFile.cmake file and searches for 'add_test' |
46 |
| - @return Dictionary of { test_case : test_case_path } pairs or None if failed to parse 'add_test' line |
47 |
| - @details Example path with CTestTestFile.cmake: |
48 |
| - c:/temp/xxx/mbed-sdk-private/build/frdm-k64f-gcc/test/ |
49 |
| -
|
50 |
| - Example format of CTestTestFile.cmake: |
51 |
| - # CMake generated Testfile for |
52 |
| - # Source directory: c:/temp/xxx/mbed-sdk-private/build/frdm-k64f-gcc/test |
53 |
| - # Build directory: c:/temp/xxx/mbed-sdk-private/build/frdm-k64f-gcc/test |
54 |
| - # |
55 |
| - # This file includes the relevant testing commands required for |
56 |
| - # testing this directory and lists subdirectories to be tested as well. |
57 |
| - add_test(mbed-test-stdio "mbed-test-stdio") |
58 |
| - add_test(mbed-test-call_before_main "mbed-test-call_before_main") |
59 |
| - add_test(mbed-test-dev_null "mbed-test-dev_null") |
60 |
| - add_test(mbed-test-div "mbed-test-div") |
61 |
| - add_test(mbed-test-echo "mbed-test-echo") |
62 |
| - add_test(mbed-test-ticker "mbed-test-ticker") |
63 |
| - add_test(mbed-test-hello "mbed-test-hello") |
64 |
| - """ |
65 |
| - add_test_pattern = '[adtesADTES_]{8}\([\w\d_-]+ \"([\w\d_-]+)\"' |
66 |
| - re_ptrn = re.compile(add_test_pattern) |
67 |
| - if line.lower().startswith('add_test'): |
68 |
| - m = re_ptrn.search(line) |
69 |
| - if m and len(m.groups()) > 0: |
70 |
| - if verbose: |
71 |
| - print(m.group(1) + binary_type) |
72 |
| - test_case = m.group(1) |
73 |
| - test_case_path = os.path.join(link_target, 'test', m.group(1) + binary_type) |
74 |
| - return test_case, test_case_path |
75 |
| - return None |
76 |
| - |
77 |
| -def list_binaries_for_targets(build_dir='./build', verbose_footer=False): |
78 |
| - """! Prints tests in target directories, only if tests exist. |
79 |
| - @param build_dir Yotta default build directory where tests will be |
80 |
| - @param verbose_footer Prints additional "how to use" Greentea footer |
81 |
| - @details Skips empty / no tests for target directories. |
82 |
| - """ |
83 |
| - dir = build_dir |
84 |
| - sub_dirs = [os.path.join(dir, o) for o in os.listdir(dir) if os.path.isdir(os.path.join(dir, o))] \ |
85 |
| - if os.path.exists(dir) else [] |
86 |
| - |
87 |
| - def count_tests(): |
88 |
| - result = 0 |
89 |
| - for sub_dir in sub_dirs: |
90 |
| - test_list = load_ctest_testsuite(sub_dir, binary_type='') |
91 |
| - if len(test_list): |
92 |
| - for test in test_list: |
93 |
| - result += 1 |
94 |
| - return result |
95 |
| - |
96 |
| - if count_tests(): |
97 |
| - for sub_dir in sub_dirs: |
98 |
| - target_name = sub_dir.split(os.sep)[-1] |
99 |
| - gt_logger.gt_log("available tests for target '%s', location '%s'"% (target_name, os.path.abspath(os.path.join(build_dir, sub_dir)))) |
100 |
| - test_list = load_ctest_testsuite(sub_dir, binary_type='') |
101 |
| - if len(test_list): |
102 |
| - for test in sorted(test_list): |
103 |
| - gt_logger.gt_log_tab("test '%s'"% test) |
104 |
| - else: |
105 |
| - gt_logger.gt_log_warn("no tests found in current location") |
106 |
| - |
107 |
| - if verbose_footer: |
108 |
| - print |
109 |
| - print("Example: execute 'mbedgt -t TARGET_NAME -n TEST_NAME' to run test TEST_NAME for target TARGET_NAME") |
110 |
| - |
111 |
| -def list_binaries_for_builds(test_spec, verbose_footer=False): |
112 |
| - """! Parse test spec and list binaries (BOOTABLE) in lexicographical order |
113 |
| - @param test_spec Test specification object |
114 |
| - @param verbose_footer Prints additional "how to use" Greentea footer |
115 |
| - """ |
116 |
| - test_builds = test_spec.get_test_builds() |
117 |
| - for tb in test_builds: |
118 |
| - gt_logger.gt_log("available tests for build '%s', location '%s'"% (tb.get_name(), tb.get_path())) |
119 |
| - for tc in sorted(tb.get_tests().keys()): |
120 |
| - gt_logger.gt_log_tab("test '%s'"% tc) |
121 |
| - |
122 |
| - if verbose_footer: |
123 |
| - print |
124 |
| - print("Example: execute 'mbedgt -t BUILD_NAME -n TEST_NAME' to run test TEST_NAME for build TARGET_NAME in current test specification") |
| 20 | +from mbed_os_tools.test.cmake_handlers import ( |
| 21 | + load_ctest_testsuite, |
| 22 | + parse_ctesttestfile_line, |
| 23 | + list_binaries_for_targets, |
| 24 | + list_binaries_for_builds, |
| 25 | +) |
0 commit comments