test_idf_tools.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. output = self.run_idf_tools_with_action(['list', '--outdated'])
  162. self.assertEqual('', output)
  163. tools_json_outdated = os.path.join(self.temp_tools_dir, 'tools', 'tools.outdated.json')
  164. new_version = 'zzzzzz'
  165. self.run_idf_tools_with_action(
  166. [
  167. 'add-version',
  168. '--tool',
  169. XTENSA_ESP32_ELF,
  170. '--url-prefix',
  171. 'http://test.com',
  172. '--version',
  173. new_version,
  174. '--override',
  175. '--checksum-file',
  176. 'add_version/checksum.sha256',
  177. '--output',
  178. tools_json_outdated
  179. ])
  180. output = self.run_idf_tools_with_action(
  181. [
  182. '--tools-json',
  183. tools_json_outdated,
  184. 'list',
  185. '--outdated'
  186. ])
  187. self.assertIn((f'{XTENSA_ESP32_ELF}: version {XTENSA_ESP32_ELF_VERSION} '
  188. f'is outdated by {new_version}'), output)
  189. def test_tools_for_esp32(self):
  190. required_tools_installed = 5
  191. output = self.run_idf_tools_with_action(['install', '--targets=esp32'])
  192. self.assert_tool_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  193. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  194. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  195. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  196. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  197. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  198. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  199. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  200. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  201. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  202. self.assertEqual(required_tools_installed, output.count('Done'))
  203. output = self.run_idf_tools_with_action(['check'])
  204. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  205. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32_ELF_VERSION, output)
  206. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  207. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  208. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  209. output = self.run_idf_tools_with_action(['export'])
  210. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  211. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  212. self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  213. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  214. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  215. (self.temp_tools_dir, OPENOCD_VERSION), output)
  216. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  217. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  218. self.assertNotIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  219. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  220. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  221. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  222. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  223. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  224. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  225. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  226. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  227. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  228. def test_tools_for_esp32c3(self):
  229. required_tools_installed = 4
  230. output = self.run_idf_tools_with_action(['install', '--targets=esp32c3'])
  231. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  232. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  233. self.assert_tool_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  234. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  235. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  236. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  237. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION)
  238. self.assert_tool_not_installed(output, XTENSA_ESP_GDB_VERSION, XTENSA_ESP_GDB_VERSION)
  239. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  240. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  241. self.assertEqual(required_tools_installed, output.count('Done'))
  242. output = self.run_idf_tools_with_action(['check'])
  243. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  244. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  245. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  246. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  247. output = self.run_idf_tools_with_action(['export'])
  248. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  249. (self.temp_tools_dir, OPENOCD_VERSION), output)
  250. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  251. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  252. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  253. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  254. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  255. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  256. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  257. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  258. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  259. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  260. self.assertNotIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  261. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  262. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  263. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  264. def test_tools_for_esp32s2(self):
  265. required_tools_installed = 6
  266. output = self.run_idf_tools_with_action(['install', '--targets=esp32s2'])
  267. self.assert_tool_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  268. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  269. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  270. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  271. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  272. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  273. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  274. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  275. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  276. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  277. self.assertEqual(required_tools_installed, output.count('Done'))
  278. output = self.run_idf_tools_with_action(['check'])
  279. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  280. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  281. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  282. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  283. output = self.run_idf_tools_with_action(['export'])
  284. self.assertIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  285. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  286. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  287. (self.temp_tools_dir, OPENOCD_VERSION), output)
  288. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  289. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  290. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  291. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  292. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  293. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  294. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  295. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  296. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  297. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  298. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  299. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  300. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  301. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  302. def test_tools_for_esp32s3(self):
  303. required_tools_installed = 6
  304. output = self.run_idf_tools_with_action(['install', '--targets=esp32s3'])
  305. self.assert_tool_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  306. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  307. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  308. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION)
  309. self.assert_tool_installed(output, XTENSA_ESP_GDB, XTENSA_ESP_GDB_VERSION)
  310. self.assert_tool_installed(output, ESP_ROM_ELFS, ESP_ROM_ELFS_VERSION)
  311. self.assert_tool_not_installed(output, RISCV_ESP_GDB, RISCV_ESP_GDB_VERSION)
  312. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  313. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  314. self.assertIn('Destination: {}'.format(os.path.join(self.temp_tools_dir, 'dist')), output)
  315. self.assertEqual(required_tools_installed, output.count('Done'))
  316. output = self.run_idf_tools_with_action(['check'])
  317. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  318. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S3_ELF_VERSION, output)
  319. self.assertIn('version installed in tools directory: ' + XTENSA_ESP_GDB_VERSION, output)
  320. self.assertIn('version installed in tools directory: ' + RISCV_ESP_GDB_VERSION, output)
  321. self.assertIn('version installed in tools directory: ' + ESP_ROM_ELFS_VERSION, output)
  322. output = self.run_idf_tools_with_action(['export'])
  323. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  324. (self.temp_tools_dir, OPENOCD_VERSION), output)
  325. self.assertIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  326. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  327. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf/bin' %
  328. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  329. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  330. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  331. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  332. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  333. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  334. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  335. self.assertIn('%s/tools/xtensa-esp-elf-gdb/%s/xtensa-esp-elf-gdb/bin' %
  336. (self.temp_tools_dir, XTENSA_ESP_GDB_VERSION), output)
  337. self.assertNotIn('%s/tools/riscv32-esp-elf-gdb/%s/riscv32-esp-elf-gdb/bin' %
  338. (self.temp_tools_dir, RISCV_ESP_GDB_VERSION), output)
  339. self.assertIn('%s/tools/esp-rom-elfs/%s/' %
  340. (self.temp_tools_dir, ESP_ROM_ELFS_VERSION), output)
  341. def test_uninstall_option(self):
  342. self.run_idf_tools_with_action(['install', '--targets=esp32'])
  343. test_tool_name = XTENSA_ESP32_ELF
  344. test_tool_version = 'test_version'
  345. tools_json_new = os.path.join(self.temp_tools_dir, 'tools', 'tools.new.json')
  346. self.run_idf_tools_with_action(
  347. [
  348. 'add-version',
  349. '--tool',
  350. test_tool_name,
  351. '--url-prefix',
  352. 'http://test.com',
  353. '--version',
  354. test_tool_version,
  355. '--override',
  356. '--checksum-file',
  357. 'add_version/checksum.sha256',
  358. '--output',
  359. tools_json_new
  360. ])
  361. output = self.run_idf_tools_with_action(['--tools-json', tools_json_new, 'uninstall', '--dry-run'])
  362. self.assertIn('For removing old versions of ' + test_tool_name, output)
  363. output = self.run_idf_tools_with_action(['--tools-json', tools_json_new, 'uninstall'])
  364. self.assertIn(os.path.join(self.temp_tools_dir, 'tools', test_tool_name, XTENSA_ESP32_ELF_VERSION) + ' was removed.', output)
  365. output = self.run_idf_tools_with_action(['uninstall'])
  366. self.assertEqual('', output)
  367. def test_deactivate(self):
  368. self.run_idf_tools_with_action(['install'])
  369. output = self.run_idf_tools_with_action(['export'])
  370. self.assertIn('export IDF_DEACTIVATE_FILE_PATH=', output, 'No IDF_DEACTIVATE_FILE_PATH exported into environment')
  371. deactivate_file = re.findall(r'(?:IDF_DEACTIVATE_FILE_PATH=")(.*)(?:")', output)[0]
  372. self.assertTrue(os.path.isfile(deactivate_file), 'File {} was not found. '.format(deactivate_file))
  373. self.assertNotEqual(os.stat(self.idf_env_json).st_size, 0, 'File {} is empty. '.format(deactivate_file))
  374. class TestMaintainer(unittest.TestCase):
  375. @classmethod
  376. def setUpClass(cls):
  377. idf_path = os.getenv('IDF_PATH')
  378. cls.tools_old = os.path.join(idf_path, 'tools/tools.json')
  379. cls.tools_new = os.path.join(idf_path, 'tools/tools.new.json')
  380. cls.test_tool_name = 'xtensa-esp32-elf'
  381. def test_validation(self):
  382. idf_tools.main(['validate'])
  383. def test_json_rewrite(self):
  384. idf_tools.main(['rewrite'])
  385. idf_path = os.getenv('IDF_PATH')
  386. if not idf_path:
  387. self.fail('IDF_PATH needs to be set to run this test')
  388. with open(self.tools_old, 'r') as f:
  389. json_old = f.read()
  390. with open(self.tools_new, 'r') as f:
  391. json_new = f.read()
  392. self.assertEqual(json_old, json_new, "Please check 'tools/tools.new.json' to find a cause!")
  393. def add_version_get_expected_json(self, addition_file, replace=False):
  394. with open(self.tools_old, 'r') as f:
  395. expected_json = json.load(f)
  396. with open(addition_file, 'r') as f:
  397. addition_json = json.load(f)
  398. for tool in expected_json['tools']:
  399. if tool['name'] == self.test_tool_name:
  400. if replace:
  401. tool['versions'] = [addition_json]
  402. else:
  403. tool['versions'].append(addition_json)
  404. return expected_json
  405. return None
  406. def test_add_version_artifact_addition(self):
  407. filenames = []
  408. with open('add_version/artifact_input.json', 'r') as f:
  409. add_tools_info = json.load(f)
  410. for tool in add_tools_info:
  411. filenames.append(tool['filename'])
  412. with open(tool['filename'], 'w') as f:
  413. self.addCleanup(os.remove, f.name)
  414. f.write('1' * tool['size'])
  415. idf_tools.main(
  416. [
  417. 'add-version',
  418. '--tool',
  419. self.test_tool_name,
  420. '--url-prefix',
  421. 'http://test.com',
  422. '--version',
  423. 'test',
  424. '--artifact-file'
  425. ] + filenames
  426. )
  427. expected_json = self.add_version_get_expected_json('add_version/artifact_expected_addition.json')
  428. with open(self.tools_new, 'r') as f1:
  429. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  430. def test_add_version_checksum_addition(self):
  431. idf_tools.main(
  432. [
  433. 'add-version',
  434. '--tool',
  435. self.test_tool_name,
  436. '--url-prefix',
  437. 'http://test.com',
  438. '--version',
  439. 'test',
  440. '--checksum-file',
  441. 'add_version/checksum.sha256',
  442. ]
  443. )
  444. expected_json = self.add_version_get_expected_json('add_version/checksum_expected_addition.json')
  445. with open(self.tools_new, 'r') as f1:
  446. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  447. def test_add_version_checksum_with_override(self):
  448. idf_tools.main(
  449. [
  450. 'add-version',
  451. '--tool',
  452. self.test_tool_name,
  453. '--url-prefix',
  454. 'http://test.com',
  455. '--version',
  456. 'test',
  457. '--override',
  458. '--checksum-file',
  459. 'add_version/checksum.sha256'
  460. ]
  461. )
  462. expected_json = self.add_version_get_expected_json('add_version/checksum_expected_override.json', True)
  463. with open(self.tools_new, 'r') as f1:
  464. self.assertEqual(json.load(f1), expected_json, "Please check 'tools/tools.new.json' to find a cause!")
  465. if __name__ == '__main__':
  466. unittest.main()