test_idf_tools.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import json
  6. import os
  7. import re
  8. import shutil
  9. import sys
  10. import tempfile
  11. import unittest
  12. try:
  13. from contextlib import redirect_stdout
  14. except ImportError:
  15. import contextlib
  16. @contextlib.contextmanager # type: ignore
  17. def redirect_stdout(target):
  18. original = sys.stdout
  19. sys.stdout = target
  20. yield
  21. sys.stdout = original
  22. try:
  23. from cStringIO import StringIO
  24. except ImportError:
  25. from io import StringIO
  26. # Need to do this before importing idf_tools.py
  27. os.environ['IDF_MAINTAINER'] = '1'
  28. try:
  29. import idf_tools
  30. except ImportError:
  31. sys.path.append('..')
  32. import idf_tools
  33. ESP32ULP = 'esp32ulp-elf'
  34. OPENOCD = 'openocd-esp32'
  35. RISCV_ELF = 'riscv32-esp-elf'
  36. XTENSA_ESP32_ELF = 'xtensa-esp32-elf'
  37. XTENSA_ESP32S2_ELF = 'xtensa-esp32s2-elf'
  38. XTENSA_ESP32S3_ELF = 'xtensa-esp32s3-elf'
  39. XTENSA_ESP_GDB = 'xtensa-esp-elf-gdb'
  40. RISCV_ESP_GDB = 'riscv32-esp-elf-gdb'
  41. ESP_ROM_ELFS = 'esp-rom-elfs'
  42. def get_version_dict():
  43. '''
  44. Return a dictionary with tool name to tool version mapping.
  45. It works with tools.json directly and not through idf_tools.py in order to bypass the script under test. This is
  46. a little hacky but thanks to this, versions are not required to be updated here every time a tool is updated.
  47. '''
  48. with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'tools.json')) as f:
  49. tools_obj = json.loads(f.read())
  50. return dict((tool['name'], tool['versions'][0]['name']) for tool in tools_obj['tools'])
  51. version_dict = get_version_dict()
  52. ESP32ULP_VERSION = version_dict[ESP32ULP]
  53. OPENOCD_VERSION = version_dict[OPENOCD]
  54. RISCV_ELF_VERSION = version_dict[RISCV_ELF]
  55. XTENSA_ESP32_ELF_VERSION = version_dict[XTENSA_ESP32_ELF]
  56. XTENSA_ESP32S2_ELF_VERSION = version_dict[XTENSA_ESP32S2_ELF]
  57. XTENSA_ESP32S3_ELF_VERSION = version_dict[XTENSA_ESP32S3_ELF]
  58. XTENSA_ESP_GDB_VERSION = version_dict[XTENSA_ESP_GDB]
  59. RISCV_ESP_GDB_VERSION = version_dict[RISCV_ESP_GDB]
  60. ESP_ROM_ELFS_VERSION = version_dict[ESP_ROM_ELFS]
  61. class TestUsage(unittest.TestCase):
  62. @classmethod
  63. def setUpClass(cls):
  64. old_tools_dir = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT)
  65. mirror_prefix_map = None
  66. if os.path.exists(old_tools_dir):
  67. mirror_prefix_map = 'https://dl.espressif.com/dl/toolchains/preview,file://' + os.path.join(old_tools_dir,
  68. 'dist')
  69. mirror_prefix_map += ';https://dl.espressif.com/dl,file://' + os.path.join(old_tools_dir, 'dist')
  70. mirror_prefix_map += ';https://github.com/espressif/.*/releases/download/.*/,file://' + os.path.join(
  71. old_tools_dir, 'dist', '')
  72. if mirror_prefix_map:
  73. print('Using IDF_MIRROR_PREFIX_MAP={}'.format(mirror_prefix_map))
  74. os.environ['IDF_MIRROR_PREFIX_MAP'] = mirror_prefix_map
  75. cls.temp_tools_dir = tempfile.mkdtemp(prefix='idf_tools_tmp')
  76. print('Using IDF_TOOLS_PATH={}'.format(cls.temp_tools_dir))
  77. os.environ['IDF_TOOLS_PATH'] = cls.temp_tools_dir
  78. cls.idf_env_json = os.path.join(cls.temp_tools_dir, 'idf-env.json')
  79. @classmethod
  80. def tearDownClass(cls):
  81. shutil.rmtree(cls.temp_tools_dir)
  82. def tearDown(self):
  83. if os.path.isdir(os.path.join(self.temp_tools_dir, 'dist')):
  84. shutil.rmtree(os.path.join(self.temp_tools_dir, 'dist'))
  85. if os.path.isdir(os.path.join(self.temp_tools_dir, 'tools')):
  86. shutil.rmtree(os.path.join(self.temp_tools_dir, 'tools'))
  87. if os.path.isfile(self.idf_env_json):
  88. os.remove(self.idf_env_json)
  89. def assert_tool_installed(self, output, tool, tool_version, tool_archive_name=None):
  90. if tool_archive_name is None:
  91. tool_archive_name = tool
  92. self.assertIn('Installing %s@' % tool + tool_version, output)
  93. self.assertRegex(output, re.compile(rf'Downloading \S+/{tool_archive_name}'))
  94. def assert_tool_not_installed(self, output, tool, tool_version, tool_archive_name=None):
  95. if tool_archive_name is None:
  96. tool_archive_name = tool
  97. self.assertNotIn('Installing %s@' % tool + tool_version, output)
  98. self.assertNotRegex(output, re.compile(rf'Downloading \S+/{tool_archive_name}'))
  99. def run_idf_tools_with_action(self, action):
  100. output_stream = StringIO()
  101. with redirect_stdout(output_stream):
  102. idf_tools.main(['--non-interactive'] + action)
  103. output = output_stream.getvalue()
  104. return output
  105. def test_usage_basic(self):
  106. output = self.run_idf_tools_with_action(['list'])
  107. self.assertIn('* %s:' % ESP32ULP, output)
  108. self.assertIn('- %s (recommended)' % ESP32ULP_VERSION, output)
  109. self.assertIn('* %s:' % OPENOCD, output)
  110. self.assertIn('- %s (recommended)' % OPENOCD_VERSION, output)
  111. self.assertIn('* %s:' % RISCV_ELF, output)
  112. self.assertIn('- %s (recommended)' % RISCV_ELF_VERSION, output)
  113. self.assertIn('* %s:' % XTENSA_ESP32_ELF, output)
  114. self.assertIn('- %s (recommended)' % XTENSA_ESP32_ELF_VERSION, output)
  115. self.assertIn('* %s:' % XTENSA_ESP32S2_ELF, output)
  116. self.assertIn('- %s (recommended)' % XTENSA_ESP32S2_ELF_VERSION, output)
  117. self.assertIn('* %s:' % XTENSA_ESP32S3_ELF, output)
  118. self.assertIn('- %s (recommended)' % XTENSA_ESP32S3_ELF_VERSION, output)
  119. required_tools_installed = 9
  120. output = self.run_idf_tools_with_action(['install'])
  121. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  122. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  123. self.assert_tool_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  124. self.assert_tool_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  125. self.assert_tool_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  126. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  127. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  128. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  129. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  130. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  131. self.assertEqual(required_tools_installed, output.count('Done'))
  132. output = self.run_idf_tools_with_action(['check'])
  133. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  134. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  135. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  136. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32_ELF_VERSION, output)
  137. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  138. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S3_ELF_VERSION, output)
  139. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  140. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  141. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  142. output = self.run_idf_tools_with_action(['export'])
  143. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  144. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  145. self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  146. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  147. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  148. (self.temp_tools_dir, OPENOCD_VERSION), output)
  149. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  150. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  151. self.assertIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  152. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  153. self.assertIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  154. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  155. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  156. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  157. self.assertIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  158. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  159. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  160. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  161. def test_tools_for_esp32(self):
  162. required_tools_installed = 5
  163. output = self.run_idf_tools_with_action(['install', '--targets=esp32'])
  164. self.assert_tool_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  165. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  166. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  167. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  168. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  169. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  170. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  171. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  172. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  173. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  174. self.assertEqual(required_tools_installed, output.count('Done'))
  175. output = self.run_idf_tools_with_action(['check'])
  176. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  177. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32_ELF_VERSION, output)
  178. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  179. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  180. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  181. output = self.run_idf_tools_with_action(['export'])
  182. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  183. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  184. self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  185. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  186. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  187. (self.temp_tools_dir, OPENOCD_VERSION), output)
  188. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  189. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  190. self.assertNotIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  191. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  192. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  193. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  194. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  195. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  196. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  197. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  198. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  199. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  200. def test_tools_for_esp32c3(self):
  201. required_tools_installed = 4
  202. output = self.run_idf_tools_with_action(['install', '--targets=esp32c3'])
  203. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  204. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  205. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  206. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  207. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  208. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  209. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION)
  210. self.assert_tool_not_installed(output, XTENSA_ESP_GDB_VERSION, XTENSA_ESP_GDB_VERSION)
  211. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  212. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  213. self.assertEqual(required_tools_installed, output.count('Done'))
  214. output = self.run_idf_tools_with_action(['check'])
  215. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  216. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  217. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  218. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  219. output = self.run_idf_tools_with_action(['export'])
  220. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  221. (self.temp_tools_dir, OPENOCD_VERSION), output)
  222. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  223. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  224. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  225. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  226. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  227. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  228. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  229. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  230. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  231. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  232. self.assertNotIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  233. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  234. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  235. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  236. def test_tools_for_esp32s2(self):
  237. required_tools_installed = 6
  238. output = self.run_idf_tools_with_action(['install', '--targets=esp32s2'])
  239. self.assert_tool_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  240. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  241. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  242. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  243. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  244. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  245. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  246. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  247. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  248. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  249. self.assertEqual(required_tools_installed, output.count('Done'))
  250. output = self.run_idf_tools_with_action(['check'])
  251. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  252. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  253. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  254. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  255. output = self.run_idf_tools_with_action(['export'])
  256. self.assertIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  257. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  258. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  259. (self.temp_tools_dir, OPENOCD_VERSION), output)
  260. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  261. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  262. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  263. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  264. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  265. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  266. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  267. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  268. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  269. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  270. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  271. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  272. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  273. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  274. def test_tools_for_esp32s3(self):
  275. required_tools_installed = 6
  276. output = self.run_idf_tools_with_action(['install', '--targets=esp32s3'])
  277. self.assert_tool_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  278. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  279. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  280. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  281. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  282. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  283. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  284. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  285. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  286. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  287. self.assertEqual(required_tools_installed, output.count('Done'))
  288. output = self.run_idf_tools_with_action(['check'])
  289. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  290. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S3_ELF_VERSION, output)
  291. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  292. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  293. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  294. output = self.run_idf_tools_with_action(['export'])
  295. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  296. (self.temp_tools_dir, OPENOCD_VERSION), output)
  297. self.assertIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  298. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  299. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  300. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  301. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  302. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  303. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  304. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  305. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  306. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  307. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  308. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  309. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  310. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  311. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  312. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  313. def test_uninstall_option(self):
  314. self.run_idf_tools_with_action(['install', '--targets=esp32'])
  315. test_tool_name = XTENSA_ESP32_ELF
  316. test_tool_version = 'test_version'
  317. tools_json_new = os.path.join(self.temp_tools_dir, 'tools', 'tools.new.json')
  318. self.run_idf_tools_with_action(
  319. [
  320. 'add-version',
  321. '--tool',
  322. test_tool_name,
  323. '--url-prefix',
  324. 'http://test.com',
  325. '--version',
  326. test_tool_version,
  327. '--override',
  328. '--checksum-file',
  329. 'add_version/checksum.sha256',
  330. '--output',
  331. tools_json_new
  332. ])
  333. output = self.run_idf_tools_with_action(['--tools-json', tools_json_new, 'uninstall', '--dry-run'])
  334. self.assertIn('For removing old versions of ' + test_tool_name, output)
  335. output = self.run_idf_tools_with_action(['--tools-json', tools_json_new, 'uninstall'])
  336. self.assertIn(os.path.join(self.temp_tools_dir, 'tools', test_tool_name, XTENSA_ESP32_ELF_VERSION) + ' was removed.', output)
  337. output = self.run_idf_tools_with_action(['uninstall'])
  338. self.assertEqual('', output)
  339. def test_deactivate(self):
  340. self.run_idf_tools_with_action(['install'])
  341. output = self.run_idf_tools_with_action(['export'])
  342. self.assertIn('export IDF_DEACTIVATE_FILE_PATH=', output, 'No IDF_DEACTIVATE_FILE_PATH exported into environment')
  343. deactivate_file = re.findall(r'(?:IDF_DEACTIVATE_FILE_PATH=")(.*)(?:")', output)[0]
  344. self.assertTrue(os.path.isfile(deactivate_file), 'File {} was not found. '.format(deactivate_file))
  345. self.assertNotEqual(os.stat(self.idf_env_json).st_size, 0, 'File {} is empty. '.format(deactivate_file))
  346. class TestMaintainer(unittest.TestCase):
  347. @classmethod
  348. def setUpClass(cls):
  349. idf_path = os.getenv('IDF_PATH')
  350. cls.tools_old = os.path.join(idf_path, 'tools/tools.json')
  351. cls.tools_new = os.path.join(idf_path, 'tools/tools.new.json')
  352. cls.test_tool_name = 'xtensa-esp32-elf'
  353. def test_validation(self):
  354. idf_tools.main(['validate'])
  355. def test_json_rewrite(self):
  356. idf_tools.main(['rewrite'])
  357. idf_path = os.getenv('IDF_PATH')
  358. if not idf_path:
  359. self.fail('IDF_PATH needs to be set to run this test')
  360. with open(self.tools_old, 'r') as f:
  361. json_old = f.read()
  362. with open(self.tools_new, 'r') as f:
  363. json_new = f.read()
  364. self.assertEqual(json_old, json_new, "Please check 'tools/tools.new.json' to find a cause!")
  365. def add_version_get_expected_json(self, addition_file, replace=False):
  366. with open(self.tools_old, 'r') as f:
  367. expected_json = json.load(f)
  368. with open(addition_file, 'r') as f:
  369. addition_json = json.load(f)
  370. for tool in expected_json['tools']:
  371. if tool['name'] == self.test_tool_name:
  372. if replace:
  373. tool['versions'] = [addition_json]
  374. else:
  375. tool['versions'].append(addition_json)
  376. return expected_json
  377. return None
  378. def test_add_version_artifact_addition(self):
  379. filenames = []
  380. with open('add_version/artifact_input.json', 'r') as f:
  381. add_tools_info = json.load(f)
  382. for tool in add_tools_info:
  383. filenames.append(tool['filename'])
  384. with open(tool['filename'], 'w') as f:
  385. self.addCleanup(os.remove, f.name)
  386. f.write('1' * tool['size'])
  387. idf_tools.main(
  388. [
  389. 'add-version',
  390. '--tool',
  391. self.test_tool_name,
  392. '--url-prefix',
  393. 'http://test.com',
  394. '--version',
  395. 'test',
  396. '--artifact-file'
  397. ] + filenames
  398. )
  399. expected_json = self.add_version_get_expected_json('add_version/artifact_expected_addition.json')
  400. with open(self.tools_new, 'r') as f1:
  401. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  402. def test_add_version_checksum_addition(self):
  403. idf_tools.main(
  404. [
  405. 'add-version',
  406. '--tool',
  407. self.test_tool_name,
  408. '--url-prefix',
  409. 'http://test.com',
  410. '--version',
  411. 'test',
  412. '--checksum-file',
  413. 'add_version/checksum.sha256',
  414. ]
  415. )
  416. expected_json = self.add_version_get_expected_json('add_version/checksum_expected_addition.json')
  417. with open(self.tools_new, 'r') as f1:
  418. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  419. def test_add_version_checksum_with_override(self):
  420. idf_tools.main(
  421. [
  422. 'add-version',
  423. '--tool',
  424. self.test_tool_name,
  425. '--url-prefix',
  426. 'http://test.com',
  427. '--version',
  428. 'test',
  429. '--override',
  430. '--checksum-file',
  431. 'add_version/checksum.sha256'
  432. ]
  433. )
  434. expected_json = self.add_version_get_expected_json('add_version/checksum_expected_override.json', True)
  435. with open(self.tools_new, 'r') as f1:
  436. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  437. if __name__ == '__main__':
  438. unittest.main()