test_idf_tools.py 18 KB

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