test_idf_tools.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 os
  6. import shutil
  7. import sys
  8. import tempfile
  9. import unittest
  10. try:
  11. from contextlib import redirect_stdout
  12. except ImportError:
  13. import contextlib
  14. @contextlib.contextmanager # type: ignore
  15. def redirect_stdout(target):
  16. original = sys.stdout
  17. sys.stdout = target
  18. yield
  19. sys.stdout = original
  20. try:
  21. from cStringIO import StringIO
  22. except ImportError:
  23. from io import StringIO
  24. # Need to do this before importing idf_tools.py
  25. os.environ['IDF_MAINTAINER'] = '1'
  26. try:
  27. import idf_tools
  28. except ImportError:
  29. sys.path.append('..')
  30. import idf_tools
  31. ESP32ULP = 'esp32ulp-elf'
  32. ESP32ULP_ARCHIVE = 'binutils-esp32ulp'
  33. ESP32S2ULP = 'esp32s2ulp-elf'
  34. ESP32S2ULP_ARCHIVE = 'binutils-esp32s2ulp'
  35. OPENOCD = 'openocd-esp32'
  36. RISCV_ELF = 'riscv32-esp-elf'
  37. XTENSA_ESP32_ELF = 'xtensa-esp32-elf'
  38. XTENSA_ESP32S2_ELF = 'xtensa-esp32s2-elf'
  39. XTENSA_ESP32S3_ELF = 'xtensa-esp32s3-elf'
  40. ESP32ULP_VERSION = '2.28.51-esp-20191205'
  41. ESP32S2ULP_VERSION = '2.28.51-esp-20191205'
  42. OPENOCD_VERSION = 'v0.10.0-esp32-20211111'
  43. RISCV_ELF_VERSION = 'esp-2021r2-8.4.0'
  44. XTENSA_ESP32_ELF_VERSION = 'esp-2021r2-8.4.0'
  45. XTENSA_ESP32S2_ELF_VERSION = 'esp-2021r2-8.4.0'
  46. XTENSA_ESP32S3_ELF_VERSION = 'esp-2021r2-8.4.0'
  47. class TestUsage(unittest.TestCase):
  48. @classmethod
  49. def setUpClass(cls):
  50. old_tools_dir = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT)
  51. mirror_prefix_map = None
  52. if os.path.exists(old_tools_dir):
  53. mirror_prefix_map = 'https://dl.espressif.com/dl/toolchains/preview,file://' + os.path.join(old_tools_dir,
  54. 'dist')
  55. mirror_prefix_map += ';https://dl.espressif.com/dl,file://' + os.path.join(old_tools_dir, 'dist')
  56. mirror_prefix_map += ';https://github.com/espressif/.*/releases/download/.*/,file://' + os.path.join(
  57. old_tools_dir, 'dist', '')
  58. if mirror_prefix_map:
  59. print('Using IDF_MIRROR_PREFIX_MAP={}'.format(mirror_prefix_map))
  60. os.environ['IDF_MIRROR_PREFIX_MAP'] = mirror_prefix_map
  61. cls.temp_tools_dir = tempfile.mkdtemp(prefix='idf_tools_tmp')
  62. print('Using IDF_TOOLS_PATH={}'.format(cls.temp_tools_dir))
  63. os.environ['IDF_TOOLS_PATH'] = cls.temp_tools_dir
  64. @classmethod
  65. def tearDownClass(cls):
  66. shutil.rmtree(cls.temp_tools_dir)
  67. def tearDown(self):
  68. if os.path.isdir(os.path.join(self.temp_tools_dir, 'dist')):
  69. shutil.rmtree(os.path.join(self.temp_tools_dir, 'dist'))
  70. if os.path.isdir(os.path.join(self.temp_tools_dir, 'tools')):
  71. shutil.rmtree(os.path.join(self.temp_tools_dir, 'tools'))
  72. if os.path.isfile(os.path.join(self.temp_tools_dir, 'idf-env.json')):
  73. os.remove(os.path.join(self.temp_tools_dir, 'idf-env.json'))
  74. def assert_tool_installed(self, output, tool, tool_version, tool_archive_name=None):
  75. if tool_archive_name is None:
  76. tool_archive_name = tool
  77. self.assertIn('Installing %s@' % tool + tool_version, output)
  78. self.assertIn('Downloading %s' % tool_archive_name, output)
  79. def assert_tool_not_installed(self, output, tool, tool_version, tool_archive_name=None):
  80. if tool_archive_name is None:
  81. tool_archive_name = tool
  82. self.assertNotIn('Installing %s@' % tool + tool_version, output)
  83. self.assertNotIn('Downloading %s' % tool_archive_name, output)
  84. def run_idf_tools_with_action(self, action):
  85. output_stream = StringIO()
  86. with redirect_stdout(output_stream):
  87. idf_tools.main(['--non-interactive'] + action)
  88. output = output_stream.getvalue()
  89. return output
  90. def test_usage_basic(self):
  91. output = self.run_idf_tools_with_action(['list'])
  92. self.assertIn('* %s:' % ESP32ULP, output)
  93. self.assertIn('- %s (recommended)' % ESP32ULP_VERSION, output)
  94. self.assertIn('* %s:' % ESP32S2ULP, output)
  95. self.assertIn('- %s (recommended)' % ESP32S2ULP_VERSION, output)
  96. self.assertIn('* %s:' % OPENOCD, output)
  97. self.assertIn('- %s (recommended)' % OPENOCD_VERSION, output)
  98. self.assertIn('* %s:' % RISCV_ELF, output)
  99. self.assertIn('- %s (recommended)' % RISCV_ELF_VERSION, output)
  100. self.assertIn('* %s:' % XTENSA_ESP32_ELF, output)
  101. self.assertIn('- %s (recommended)' % XTENSA_ESP32_ELF_VERSION, output)
  102. self.assertIn('* %s:' % XTENSA_ESP32S2_ELF, output)
  103. self.assertIn('- %s (recommended)' % XTENSA_ESP32S2_ELF_VERSION, output)
  104. self.assertIn('* %s:' % XTENSA_ESP32S3_ELF, output)
  105. self.assertIn('- %s (recommended)' % XTENSA_ESP32S3_ELF_VERSION, output)
  106. required_tools_installed = 7
  107. output = self.run_idf_tools_with_action(['install'])
  108. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  109. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  110. self.assert_tool_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  111. self.assert_tool_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  112. self.assert_tool_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  113. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION, ESP32ULP_ARCHIVE)
  114. self.assert_tool_installed(output, ESP32S2ULP, ESP32S2ULP_VERSION, ESP32S2ULP_ARCHIVE)
  115. self.assertIn('to ' + os.path.join(self.temp_tools_dir, 'dist'), output)
  116. self.assertEqual(required_tools_installed, output.count('Done'))
  117. output = self.run_idf_tools_with_action(['check'])
  118. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  119. self.assertIn('version installed in tools directory: ' + ESP32S2ULP_VERSION, output)
  120. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  121. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  122. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32_ELF_VERSION, output)
  123. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  124. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S3_ELF_VERSION, output)
  125. output = self.run_idf_tools_with_action(['export'])
  126. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/bin' %
  127. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  128. self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  129. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  130. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  131. (self.temp_tools_dir, OPENOCD_VERSION), output)
  132. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  133. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  134. self.assertIn('%s/tools/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  135. (self.temp_tools_dir, ESP32S2ULP_VERSION), output)
  136. self.assertIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  137. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  138. self.assertIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  139. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  140. def test_tools_for_esp32(self):
  141. required_tools_installed = 3
  142. output = self.run_idf_tools_with_action(['install', '--targets=esp32'])
  143. self.assert_tool_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  144. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  145. self.assert_tool_installed(output, ESP32ULP, ESP32ULP_VERSION, ESP32ULP_ARCHIVE)
  146. self.assert_tool_not_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  147. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  148. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  149. self.assert_tool_not_installed(output, ESP32S2ULP, ESP32S2ULP_VERSION, ESP32S2ULP_ARCHIVE)
  150. self.assertIn('to ' + os.path.join(self.temp_tools_dir, 'dist'), output)
  151. self.assertEqual(required_tools_installed, output.count('Done'))
  152. output = self.run_idf_tools_with_action(['check'])
  153. self.assertIn('version installed in tools directory: ' + ESP32ULP_VERSION, output)
  154. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32_ELF_VERSION, output)
  155. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  156. output = self.run_idf_tools_with_action(['export'])
  157. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/bin' %
  158. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  159. self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  160. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  161. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  162. (self.temp_tools_dir, OPENOCD_VERSION), output)
  163. self.assertNotIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  164. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  165. self.assertNotIn('%s/tools/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  166. (self.temp_tools_dir, ESP32S2ULP_VERSION), output)
  167. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  168. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  169. self.assertNotIn('%s/tools/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  170. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  171. def test_tools_for_esp32c3(self):
  172. required_tools_installed = 2
  173. output = self.run_idf_tools_with_action(['install', '--targets=esp32c3'])
  174. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  175. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  176. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  177. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  178. self.assert_tool_not_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  179. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION, ESP32ULP_ARCHIVE)
  180. self.assert_tool_not_installed(output, ESP32S2ULP, ESP32S2ULP_VERSION, ESP32S2ULP_ARCHIVE)
  181. self.assertIn('to ' + os.path.join(self.temp_tools_dir, 'dist'), output)
  182. self.assertEqual(required_tools_installed, output.count('Done'))
  183. output = self.run_idf_tools_with_action(['check'])
  184. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  185. self.assertIn('version installed in tools directory: ' + RISCV_ELF_VERSION, output)
  186. output = self.run_idf_tools_with_action(['export'])
  187. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  188. (self.temp_tools_dir, OPENOCD_VERSION), output)
  189. self.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  190. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  191. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/bin' %
  192. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  193. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  194. (self.temp_tools_dir, XTENSA_ESP32_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. def test_tools_for_esp32s2(self):
  202. required_tools_installed = 4
  203. output = self.run_idf_tools_with_action(['install', '--targets=esp32s2'])
  204. self.assert_tool_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  205. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  206. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  207. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_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, ESP32ULP_ARCHIVE)
  210. self.assert_tool_installed(output, ESP32S2ULP, ESP32S2ULP_VERSION, ESP32S2ULP_ARCHIVE)
  211. self.assertIn('to ' + os.path.join(self.temp_tools_dir, 'dist'), output)
  212. self.assertEqual(required_tools_installed, output.count('Done'))
  213. output = self.run_idf_tools_with_action(['check'])
  214. self.assertIn('version installed in tools directory: ' + ESP32S2ULP_VERSION, output)
  215. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  216. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  217. output = self.run_idf_tools_with_action(['export'])
  218. self.assertIn('%s/tools/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  219. (self.temp_tools_dir, ESP32S2ULP_VERSION), output)
  220. self.assertIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  221. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  222. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  223. (self.temp_tools_dir, OPENOCD_VERSION), output)
  224. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/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.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  229. (self.temp_tools_dir, RISCV_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. def test_tools_for_esp32s3(self):
  233. required_tools_installed = 4
  234. output = self.run_idf_tools_with_action(['install', '--targets=esp32s3'])
  235. self.assert_tool_installed(output, XTENSA_ESP32S3_ELF, XTENSA_ESP32S3_ELF_VERSION)
  236. self.assert_tool_installed(output, OPENOCD, OPENOCD_VERSION)
  237. self.assert_tool_installed(output, RISCV_ELF, RISCV_ELF_VERSION)
  238. self.assert_tool_not_installed(output, XTENSA_ESP32_ELF, XTENSA_ESP32_ELF_VERSION)
  239. self.assert_tool_not_installed(output, XTENSA_ESP32S2_ELF, XTENSA_ESP32S2_ELF_VERSION)
  240. self.assert_tool_not_installed(output, ESP32ULP, ESP32ULP_VERSION, ESP32ULP_ARCHIVE)
  241. self.assert_tool_installed(output, ESP32S2ULP, ESP32S2ULP_VERSION, ESP32S2ULP_ARCHIVE)
  242. self.assertIn('to ' + os.path.join(self.temp_tools_dir, 'dist'), output)
  243. self.assertEqual(required_tools_installed, output.count('Done'))
  244. output = self.run_idf_tools_with_action(['check'])
  245. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  246. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S3_ELF_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/xtensa-esp32s3-elf/%s/xtensa-esp32s3-elf/bin' %
  251. (self.temp_tools_dir, XTENSA_ESP32S3_ELF_VERSION), output)
  252. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/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.assertIn('%s/tools/riscv32-esp-elf/%s/riscv32-esp-elf/bin' %
  257. (self.temp_tools_dir, RISCV_ELF_VERSION), output)
  258. self.assertIn('%s/tools/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  259. (self.temp_tools_dir, ESP32S2ULP_VERSION), output)
  260. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  261. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  262. class TestMaintainer(unittest.TestCase):
  263. def test_validation(self):
  264. idf_tools.main(['validate'])
  265. def test_json_rewrite(self):
  266. idf_tools.main(['rewrite'])
  267. idf_path = os.getenv('IDF_PATH')
  268. if not idf_path:
  269. self.fail('IDF_PATH needs to be set to run this test')
  270. with open(os.path.join(idf_path, 'tools/tools.json'), 'r') as f:
  271. json_old = f.read()
  272. with open(os.path.join(idf_path, 'tools/tools.new.json'), 'r') as f:
  273. json_new = f.read()
  274. self.assertEqual(json_old, json_new, "Please check 'tools/tools.new.json' to find a cause!")
  275. if __name__ == '__main__':
  276. unittest.main()