Skip to content

Commit e8ba963

Browse files
committed
Use implementation from mbed-os-tools in greentea package
1 parent 8cf6ce5 commit e8ba963

13 files changed

+175
-3637
lines changed

legacy/mbed-greentea/mbed_greentea/cmake_handlers.py

Lines changed: 6 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -17,108 +17,9 @@
1717
Author: Przemyslaw Wirkus <[email protected]>
1818
"""
1919

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+
)

legacy/mbed-greentea/mbed_greentea/mbed_common_api.py

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,7 @@
1717
Author: Przemyslaw Wirkus <[email protected]>
1818
"""
1919

20-
from subprocess import call, Popen, PIPE
21-
22-
23-
def run_cli_command(cmd, shell=True, verbose=False):
24-
"""! Runs command from command line
25-
@param shell Shell command (e.g. ls, ps)
26-
@param verbose Verbose mode flag
27-
@return Returns (True, 0) if command was executed successfully else return (False, error code)
28-
"""
29-
result = True
30-
ret = 0
31-
try:
32-
ret = call(cmd, shell=shell)
33-
if ret:
34-
result = False
35-
if verbose:
36-
print("mbedgt: [ret=%d] Command: %s"% (int(ret), cmd))
37-
except OSError as e:
38-
result = False
39-
if verbose:
40-
print("mbedgt: [ret=%d] Command: %s"% (int(ret), cmd))
41-
print(str(e))
42-
return (result, ret)
43-
44-
def run_cli_process(cmd):
45-
"""! Runs command as a process and return stdout, stderr and ret code
46-
@param cmd Command to execute
47-
@return Tuple of (stdout, stderr, returncode)
48-
"""
49-
try:
50-
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
51-
_stdout, _stderr = p.communicate()
52-
except OSError as e:
53-
print("mbedgt: Command: %s"% (cmd))
54-
print(str(e))
55-
print("mbedgt: traceback...")
56-
print(e.child_traceback)
57-
return str(), str(), -1
58-
return _stdout, _stderr, p.returncode
20+
from mbed_os_tools.test.mbed_common_api import (
21+
run_cli_command,
22+
run_cli_process,
23+
)

legacy/mbed-greentea/mbed_greentea/mbed_coverage_api.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,4 @@
1717
Author: Przemyslaw Wirkus <[email protected]>
1818
"""
1919

20-
import os
21-
22-
"""
23-
def __default_coverage_start_callback(self, key, value, timestamp):
24-
# {{__coverage_start;PATH;PAYLOAD}}
25-
# PAYLAODED is HEX coded string
26-
lcov_path, lcov_payload = value.split(';')
27-
try:
28-
bin_payload = coverage_pack_hex_payload(lcov_payload)
29-
coverage_dump_file(lcov_path, bin_payload)
30-
31-
self.log("dumped %d bytes to '%s'"% (len(bin_payload), lcov_path))
32-
except Exception as e:
33-
self.log("LCOV:" + str(e))
34-
"""
35-
36-
def coverage_pack_hex_payload(payload):
37-
"""! Convert a block of hex string data back to binary and return the binary data
38-
@param payload String with hex encoded ascii data, e.g.: '6164636772...'
39-
@return bytearray with payload with data
40-
"""
41-
# This payload might be packed with dot compression
42-
# where byte value 0x00 is coded as ".", and not as "00"
43-
payload = payload.replace('.', '00')
44-
45-
hex_pairs = map(''.join, zip(*[iter(payload)] * 2)) # ['61', '64', '63', '67', '72', ... ]
46-
bin_payload = bytearray([int(s, 16) for s in hex_pairs])
47-
return bin_payload
48-
49-
50-
def coverage_dump_file(build_path, path, payload):
51-
"""! Creates file and dumps payload to it on specified path (even if path doesn't exist)
52-
@param path Path to file
53-
@param payload Binary data to store in a file
54-
@return True if operation was completed
55-
"""
56-
result = True
57-
try:
58-
d, filename = os.path.split(path)
59-
if not os.path.isabs(d) and not os.path.exists(d):
60-
# For a relative path that do not exist. Try adding ./build/<yotta target> prefix
61-
d = build_path
62-
path = os.path.join(d, filename)
63-
if not os.path.exists(d):
64-
os.makedirs(d)
65-
with open(path, "wb") as f:
66-
f.write(payload)
67-
except IOError as e:
68-
print(str(e))
69-
result = False
70-
return result
20+
from mbed_os_tools.test.mbed_coverage_api import *

0 commit comments

Comments
 (0)