test_idf_tools.py 26 KB

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