semihost_vfs_example_test.py 2.3 KB

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