1import os 2import shutil 3import tempfile 4from io import open 5 6import ttfw_idf 7 8try: 9 from itertools import izip_longest as zip_longest 10except ImportError: 11 # Python 3 12 from itertools import zip_longest 13 14 15@ttfw_idf.idf_example_test(env_tag='test_jtag_arm') 16def test_examples_semihost_vfs(env, extra_data): 17 18 rel_project_path = os.path.join('examples', 'storage', 'semihost_vfs') 19 dut = env.get_dut('semihost_vfs', rel_project_path) 20 idf_path = dut.app.get_sdk_path() 21 proj_path = os.path.join(idf_path, rel_project_path) 22 host_file_name = 'host_file.txt' 23 24 try: 25 temp_dir = tempfile.mkdtemp() 26 host_file_path = os.path.join(proj_path, 'data', host_file_name) 27 shutil.copyfile(host_file_path, os.path.join(temp_dir, host_file_name)) 28 cfg_cmds = ['set ESP_SEMIHOST_BASEDIR "{}"'.format(temp_dir)] 29 30 with ttfw_idf.OCDBackend(os.path.join(proj_path, 'openocd.log'), dut.app.target, cfg_cmds=cfg_cmds): 31 dut.start_app() 32 dut.expect_all('example: Switch to semihosted stdout', 33 'example: Switched back to UART stdout', 34 'example: Wrote 2798 bytes', 35 '====================== HOST DATA START =========================', 36 timeout=20) 37 with open(host_file_path) as f: 38 file_content = [line.strip() for line in f] 39 dut.expect_all(*file_content, timeout=20) 40 dut.expect_all('====================== HOST DATA END =========================', 41 'example: Read 6121 bytes', 42 timeout=5) 43 44 with open(os.path.join(temp_dir, 'esp32_stdout.txt')) as f: 45 def expected_content(): 46 yield 'example: Switched to semihosted stdout' 47 for i in range(100): 48 yield 'Semihosted stdout write {}'.format(i) 49 yield 'example: Switch to UART stdout' 50 51 for actual, expected in zip_longest(f, expected_content(), fillvalue='-'): 52 if expected not in actual: # "in" used because of the printed ASCII color codes 53 raise RuntimeError('"{}" != "{}"'.format(expected, actual.strip())) 54 finally: 55 shutil.rmtree(temp_dir, ignore_errors=True) 56 57 58if __name__ == '__main__': 59 test_examples_semihost_vfs() 60