test_idf_tools.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2019-2023 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_ELF = 'xtensa-esp-elf'
  37. XTENSA_ESP_GDB = 'xtensa-esp-elf-gdb'
  38. RISCV_ESP_GDB = 'riscv32-esp-elf-gdb'
  39. ESP_ROM_ELFS = 'esp-rom-elfs'
  40. QEMU_RISCV = 'qemu-riscv32'
  41. QEMU_XTENSA = 'qemu-xtensa'
  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_ELF_VERSION = version_dict[XTENSA_ELF]
  56. XTENSA_ESP_GDB_VERSION = version_dict[XTENSA_ESP_GDB]
  57. RISCV_ESP_GDB_VERSION = version_dict[RISCV_ESP_GDB]
  58. ESP_ROM_ELFS_VERSION = version_dict[ESP_ROM_ELFS]
  59. QEMU_RISCV_VERSION = version_dict[QEMU_RISCV]
  60. QEMU_XTENSA_VERSION = version_dict[QEMU_XTENSA]
  61. # There are some complex search patterns to detect download snippets
  62. # Avoiding an ambiguity with a substring 'riscv32-esp-elf' in the `riscv32-esp-elf-gdb`
  63. # (removing esp- prefix from version)
  64. RISCV_ELF_ARCHIVE_PATTERN = RISCV_ELF + '-' \
  65. + (RISCV_ELF_VERSION[len('esp-'):] if RISCV_ELF_VERSION.startswith('esp-') else RISCV_ELF_VERSION)
  66. # The same like above
  67. XTENSA_ELF_ARCHIVE_PATTERN = XTENSA_ELF + '-' \
  68. + (XTENSA_ELF_VERSION[len('esp-'):] if XTENSA_ELF_VERSION.startswith('esp-') else XTENSA_ELF_VERSION)
  69. QEMU_RISCV_ARCHIVE_PATTERN = 'esp-' + QEMU_RISCV
  70. QEMU_XTENSA_ARCHIVE_PATTERN = 'esp-' + QEMU_XTENSA
  71. class TestUsage(unittest.TestCase):
  72. @classmethod
  73. def setUpClass(cls):
  74. old_tools_dir = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT)
  75. mirror_prefix_map = None
  76. if os.path.exists(old_tools_dir):
  77. mirror_prefix_map = 'https://dl.espressif.com/dl/toolchains/preview,file://' + os.path.join(old_tools_dir,
  78. 'dist')
  79. mirror_prefix_map += ';https://dl.espressif.com/dl,file://' + os.path.join(old_tools_dir, 'dist')
  80. mirror_prefix_map += ';https://github.com/espressif/.*/releases/download/.*/,file://' + os.path.join(
  81. old_tools_dir, 'dist', '')
  82. if mirror_prefix_map:
  83. print('Using IDF_MIRROR_PREFIX_MAP={}'.format(mirror_prefix_map))
  84. os.environ['IDF_MIRROR_PREFIX_MAP'] = mirror_prefix_map
  85. cls.temp_tools_dir = tempfile.mkdtemp(prefix='idf_tools_tmp')
  86. print('Using IDF_TOOLS_PATH={}'.format(cls.temp_tools_dir))
  87. os.environ['IDF_TOOLS_PATH'] = cls.temp_tools_dir
  88. cls.idf_env_json = os.path.join(cls.temp_tools_dir, 'idf-env.json')
  89. @classmethod
  90. def tearDownClass(cls):
  91. shutil.rmtree(cls.temp_tools_dir)
  92. def tearDown(self):
  93. if os.path.isdir(os.path.join(self.temp_tools_dir, 'dist')):
  94. shutil.rmtree(os.path.join(self.temp_tools_dir, 'dist'))
  95. if os.path.isdir(os.path.join(self.temp_tools_dir, 'tools')):
  96. shutil.rmtree(os.path.join(self.temp_tools_dir, 'tools'))
  97. if os.path.isfile(self.idf_env_json):
  98. os.remove(self.idf_env_json)
  99. def assert_tool_installed(self, output, tool, tool_version, tool_archive_name=None):
  100. if tool_archive_name is None:
  101. tool_archive_name = tool
  102. self.assertIn('Installing %s@' % tool + tool_version, output)
  103. self.assertRegex(output, re.compile(rf'Downloading \S+/{tool_archive_name}'))
  104. def assert_tool_not_installed(self, output, tool, tool_version, tool_archive_name=None):
  105. if tool_archive_name is None:
  106. tool_archive_name = tool
  107. self.assertNotIn('Installing %s@' % tool + tool_version, output)
  108. self.assertNotRegex(output, re.compile(rf'Downloading \S+/{tool_archive_name}'))
  109. def run_idf_tools_with_action(self, action):
  110. output_stream = StringIO()
  111. with redirect_stdout(output_stream):
  112. idf_tools.main(['--non-interactive'] + action)
  113. output = output_stream.getvalue()
  114. return output
  115. def test_usage_basic(self):
  116. output = self.run_idf_tools_with_action(['list'])
  117. self.assertIn('* %s:' % ESP32ULP, output)
  118. self.assertIn('- %s (recommended)' % ESP32ULP_VERSION, output)
  119. self.assertIn('* %s:' % OPENOCD, output)
  120. self.assertIn('- %s (recommended)' % OPENOCD_VERSION, output)
  121. self.assertIn('* %s:' % RISCV_ELF, output)
  122. self.assertIn('- %s (recommended)' % RISCV_ELF_VERSION, output)
  123. self.assertIn('* %s:' % XTENSA_ELF, output)
  124. self.assertIn('- %s (recommended)' % XTENSA_ELF_VERSION, output)
  125. required_tools_installed = 7
  126. output = self.run_idf_tools_with_action(['install'])
  127. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  128. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  129. self.assert_tool_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  130. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  131. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  132. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  133. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  134. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  135. self.assertEqual(required_tools_installed, output.count('Done'))
  136. output = self.run_idf_tools_with_action(['check'])
  137. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  138. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  139. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  140. self.assertIn('version installed in tools directory: ' + XTENSA_ELF_VERSION, output)
  141. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  142. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  143. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  144. output = self.run_idf_tools_with_action(['export'])
  145. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  146. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  147. self.assertIn('%s/tools/xtensa-esp-elf/%s/xtensa-esp-elf/bin' %
  148. (self.temp_tools_dir, XTENSA_ELF_VERSION), output)
  149. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  150. (self.temp_tools_dir, OPENOCD_VERSION), output)
  151. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  152. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  153. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  154. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  155. self.assertIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  156. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  157. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  158. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  159. output = self.run_idf_tools_with_action(['list', '--outdated'])
  160. self.assertEqual('', output)
  161. tools_json_outdated = os.path.join(self.temp_tools_dir, 'tools', 'tools.outdated.json')
  162. new_version = 'zzzzzz'
  163. self.run_idf_tools_with_action(
  164. [
  165. 'add-version',
  166. '--tool',
  167. XTENSA_ELF,
  168. '--url-prefix',
  169. 'http://test.com',
  170. '--version',
  171. new_version,
  172. '--override',
  173. '--checksum-file',
  174. 'add_version/checksum.sha256',
  175. '--output',
  176. tools_json_outdated
  177. ])
  178. output = self.run_idf_tools_with_action(
  179. [
  180. '--tools-json',
  181. tools_json_outdated,
  182. 'list',
  183. '--outdated'
  184. ])
  185. self.assertIn((f'{XTENSA_ELF}: version {XTENSA_ELF_VERSION} '
  186. f'is outdated by {new_version}'), output)
  187. def test_tools_for_esp32(self):
  188. required_tools_installed = 5
  189. output = self.run_idf_tools_with_action(['install', '--targets=esp32'])
  190. self.assert_tool_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  191. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  192. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  193. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  194. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  195. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  196. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  197. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  198. self.assertEqual(required_tools_installed, output.count('Done'))
  199. output = self.run_idf_tools_with_action(['check'])
  200. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  201. self.assertIn('version installed in tools directory: ' + XTENSA_ELF_VERSION, output)
  202. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  203. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  204. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  205. output = self.run_idf_tools_with_action(['export'])
  206. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  207. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  208. self.assertIn('%s/tools/xtensa-esp-elf/%s/xtensa-esp-elf/bin' %
  209. (self.temp_tools_dir, XTENSA_ELF_VERSION), output)
  210. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  211. (self.temp_tools_dir, OPENOCD_VERSION), output)
  212. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  213. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  214. self.assertNotIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  215. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  216. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  217. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  218. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  219. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  220. def test_tools_for_esp32c3(self):
  221. required_tools_installed = 4
  222. output = self.run_idf_tools_with_action(['install', '--targets=esp32c3'])
  223. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  224. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  225. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  226. self.assert_tool_not_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  227. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION)
  228. self.assert_tool_not_installed(output, XTENSA_ESP_GDB_VERSION, XTENSA_ESP_GDB_VERSION)
  229. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  230. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  231. self.assertEqual(required_tools_installed, output.count('Done'))
  232. output = self.run_idf_tools_with_action(['check'])
  233. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  234. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  235. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  236. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  237. output = self.run_idf_tools_with_action(['export'])
  238. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  239. (self.temp_tools_dir, OPENOCD_VERSION), output)
  240. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  241. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  242. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  243. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  244. self.assertNotIn('%s/tools/xtensa-esp-elf/%s/xtensa-esp-elf/bin' %
  245. (self.temp_tools_dir, XTENSA_ELF_VERSION), output)
  246. self.assertNotIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  247. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  248. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  249. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  250. def test_tools_for_esp32s2(self):
  251. required_tools_installed = 6
  252. output = self.run_idf_tools_with_action(['install', '--targets=esp32s2'])
  253. self.assert_tool_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  254. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  255. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  256. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  257. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  258. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  259. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  260. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  261. self.assertEqual(required_tools_installed, output.count('Done'))
  262. output = self.run_idf_tools_with_action(['check'])
  263. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  264. self.assertIn('version installed in tools directory: ' + XTENSA_ELF_VERSION, output)
  265. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  266. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  267. output = self.run_idf_tools_with_action(['export'])
  268. self.assertIn('%s/tools/xtensa-esp-elf/%s/xtensa-esp-elf/bin' %
  269. (self.temp_tools_dir, XTENSA_ELF_VERSION), output)
  270. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  271. (self.temp_tools_dir, OPENOCD_VERSION), output)
  272. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  273. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  274. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  275. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  276. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  277. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  278. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  279. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  280. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  281. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  282. def test_tools_for_esp32s3(self):
  283. required_tools_installed = 6
  284. output = self.run_idf_tools_with_action(['install', '--targets=esp32s3'])
  285. self.assert_tool_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  286. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  287. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  288. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  289. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  290. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  291. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  292. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  293. self.assertEqual(required_tools_installed, output.count('Done'))
  294. output = self.run_idf_tools_with_action(['check'])
  295. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  296. self.assertIn('version installed in tools directory: ' + XTENSA_ELF_VERSION, output)
  297. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  298. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  299. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  300. output = self.run_idf_tools_with_action(['export'])
  301. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  302. (self.temp_tools_dir, OPENOCD_VERSION), output)
  303. self.assertIn('%s/tools/xtensa-esp-elf/%s/xtensa-esp-elf/bin' %
  304. (self.temp_tools_dir, XTENSA_ELF_VERSION), output)
  305. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  306. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  307. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  308. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  309. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  310. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  311. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  312. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  313. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  314. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  315. # a different test for qemu because of "on_request"
  316. def test_tools_for_qemu_with_required(self):
  317. required_tools_installed = 9
  318. output = self.run_idf_tools_with_action(['install', 'required', 'qemu-xtensa', 'qemu-riscv32'])
  319. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  320. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  321. self.assert_tool_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  322. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  323. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  324. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  325. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  326. self.assert_tool_installed(output, QEMU_RISCV, QEMU_RISCV_VERSION, QEMU_RISCV_ARCHIVE_PATTERN)
  327. self.assert_tool_installed(output, QEMU_XTENSA, QEMU_XTENSA_VERSION, QEMU_XTENSA_ARCHIVE_PATTERN)
  328. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  329. self.assertEqual(required_tools_installed, output.count('Done'))
  330. def test_tools_for_wildcards1(self):
  331. required_tools_installed = 2
  332. output = self.run_idf_tools_with_action(['install', '*gdb*'])
  333. self.assert_tool_not_installed(output, OPENOCD, OPENOCD_VERSION)
  334. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION,RISCV_ELF_ARCHIVE_PATTERN)
  335. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  336. self.assert_tool_not_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  337. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION)
  338. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  339. self.assert_tool_not_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  340. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  341. self.assertEqual(required_tools_installed, output.count('Done'))
  342. def test_tools_for_wildcards2(self):
  343. required_tools_installed = 1
  344. output = self.run_idf_tools_with_action(['install', '*gdb*', '--targets=esp32c3'])
  345. self.assert_tool_not_installed(output, OPENOCD, OPENOCD_VERSION)
  346. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  347. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  348. self.assert_tool_not_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  349. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION)
  350. self.assert_tool_not_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  351. self.assert_tool_not_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  352. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  353. self.assertEqual(required_tools_installed, output.count('Done'))
  354. def test_tools_for_wildcards3(self):
  355. required_tools_installed = 1
  356. output = self.run_idf_tools_with_action(['install', '*gdb*', '--targets=esp32s3'])
  357. self.assert_tool_not_installed(output, OPENOCD, OPENOCD_VERSION)
  358. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION, RISCV_ELF_ARCHIVE_PATTERN)
  359. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  360. self.assert_tool_not_installed(output, XTENSA_ELF, XTENSA_ELF_VERSION, XTENSA_ELF_ARCHIVE_PATTERN)
  361. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION)
  362. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  363. self.assert_tool_not_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  364. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  365. self.assertEqual(required_tools_installed, output.count('Done'))
  366. def test_uninstall_option(self):
  367. self.run_idf_tools_with_action(['install', '--targets=esp32'])
  368. test_tool_name = XTENSA_ELF
  369. test_tool_version = 'test_version'
  370. tools_json_new = os.path.join(self.temp_tools_dir, 'tools', 'tools.new.json')
  371. self.run_idf_tools_with_action(
  372. [
  373. 'add-version',
  374. '--tool',
  375. test_tool_name,
  376. '--url-prefix',
  377. 'http://test.com',
  378. '--version',
  379. test_tool_version,
  380. '--override',
  381. '--checksum-file',
  382. 'add_version/checksum.sha256',
  383. '--output',
  384. tools_json_new
  385. ])
  386. output = self.run_idf_tools_with_action(['--tools-json', tools_json_new, 'uninstall', '--dry-run'])
  387. self.assertIn('For removing old versions of ' + test_tool_name, output)
  388. output = self.run_idf_tools_with_action(['--tools-json', tools_json_new, 'uninstall'])
  389. self.assertIn(os.path.join(self.temp_tools_dir, 'tools', test_tool_name, XTENSA_ELF_VERSION) + ' was removed.', output)
  390. output = self.run_idf_tools_with_action(['uninstall'])
  391. self.assertEqual('', output)
  392. def test_deactivate(self):
  393. self.run_idf_tools_with_action(['install'])
  394. output = self.run_idf_tools_with_action(['export'])
  395. self.assertIn('export IDF_DEACTIVATE_FILE_PATH=', output, 'No IDF_DEACTIVATE_FILE_PATH exported into environment')
  396. deactivate_file = re.findall(r'(?:IDF_DEACTIVATE_FILE_PATH=")(.*)(?:")', output)[0]
  397. self.assertTrue(os.path.isfile(deactivate_file), 'File {} was not found. '.format(deactivate_file))
  398. self.assertNotEqual(os.stat(self.idf_env_json).st_size, 0, 'File {} is empty. '.format(deactivate_file))
  399. class TestMaintainer(unittest.TestCase):
  400. @classmethod
  401. def setUpClass(cls):
  402. idf_path = os.getenv('IDF_PATH')
  403. assert idf_path, 'IDF_PATH needs to be set to run this test'
  404. cls.tools_old = os.path.join(idf_path, 'tools/tools.json')
  405. cls.tools_new = os.path.join(idf_path, 'tools/tools.new.json')
  406. cls.test_tool_name = 'xtensa-esp-elf'
  407. def test_validation(self):
  408. idf_tools.main(['validate'])
  409. def test_json_rewrite(self):
  410. idf_tools.main(['rewrite'])
  411. with open(self.tools_old, 'r') as f:
  412. json_old = f.read()
  413. with open(self.tools_new, 'r') as f:
  414. json_new = f.read()
  415. self.assertEqual(json_old, json_new, "Please check 'tools/tools.new.json' to find a cause!")
  416. def add_version_get_expected_json(self, addition_file, replace=False):
  417. with open(self.tools_old, 'r') as f:
  418. expected_json = json.load(f)
  419. with open(addition_file, 'r') as f:
  420. addition_json = json.load(f)
  421. for tool in expected_json['tools']:
  422. if tool['name'] == self.test_tool_name:
  423. if replace:
  424. tool['versions'] = [addition_json]
  425. else:
  426. tool['versions'].append(addition_json)
  427. return expected_json
  428. return None
  429. def test_add_version_artifact_addition(self):
  430. filenames = []
  431. with open('add_version/artifact_input.json', 'r') as f:
  432. add_tools_info = json.load(f)
  433. for tool in add_tools_info:
  434. filenames.append(tool['filename'])
  435. with open(tool['filename'], 'w') as f:
  436. self.addCleanup(os.remove, f.name)
  437. f.write('1' * tool['size'])
  438. idf_tools.main(
  439. [
  440. 'add-version',
  441. '--tool',
  442. self.test_tool_name,
  443. '--url-prefix',
  444. 'http://test.com',
  445. '--version',
  446. 'test',
  447. '--artifact-file'
  448. ] + filenames
  449. )
  450. expected_json = self.add_version_get_expected_json('add_version/artifact_expected_addition.json')
  451. with open(self.tools_new, 'r') as f1:
  452. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  453. def test_add_version_checksum_addition(self):
  454. idf_tools.main(
  455. [
  456. 'add-version',
  457. '--tool',
  458. self.test_tool_name,
  459. '--url-prefix',
  460. 'http://test.com',
  461. '--version',
  462. 'test',
  463. '--checksum-file',
  464. 'add_version/checksum.sha256',
  465. ]
  466. )
  467. expected_json = self.add_version_get_expected_json('add_version/checksum_expected_addition.json')
  468. with open(self.tools_new, 'r') as f1:
  469. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  470. def test_add_version_checksum_with_override(self):
  471. idf_tools.main(
  472. [
  473. 'add-version',
  474. '--tool',
  475. self.test_tool_name,
  476. '--url-prefix',
  477. 'http://test.com',
  478. '--version',
  479. 'test',
  480. '--override',
  481. '--checksum-file',
  482. 'add_version/checksum.sha256'
  483. ]
  484. )
  485. expected_json = self.add_version_get_expected_json('add_version/checksum_expected_override.json', True)
  486. with open(self.tools_new, 'r') as f1:
  487. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  488. if __name__ == '__main__':
  489. unittest.main()