test_idf_tools.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 json
  17. import os
  18. import sys
  19. import unittest
  20. import tempfile
  21. import shutil
  22. try:
  23. from contextlib import redirect_stdout
  24. except ImportError:
  25. import contextlib
  26. @contextlib.contextmanager
  27. def redirect_stdout(target):
  28. original = sys.stdout
  29. sys.stdout = target
  30. yield
  31. sys.stdout = original
  32. try:
  33. from cStringIO import StringIO
  34. except ImportError:
  35. from io import StringIO
  36. # Need to do this before importing idf_tools.py
  37. os.environ['IDF_MAINTAINER'] = '1'
  38. try:
  39. import idf_tools
  40. except ImportError:
  41. sys.path.append('..')
  42. import idf_tools
  43. ESP32ULP = 'esp32ulp-elf'
  44. ESP32S2ULP = 'esp32s2ulp-elf'
  45. OPENOCD = 'openocd-esp32'
  46. XTENSA_ESP32_ELF = 'xtensa-esp32-elf'
  47. XTENSA_ESP32S2_ELF = 'xtensa-esp32s2-elf'
  48. def get_version_dict():
  49. '''
  50. Return a dictionary with tool name to tool version mapping.
  51. It works with tools.json directly and not through idf_tools.py in order to bypass the script under test. This is
  52. a little hacky but thanks to this, versions are not required to be updated here every time a tool is updated.
  53. '''
  54. with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'tools.json')) as f:
  55. tools_obj = json.loads(f.read())
  56. return dict((tool['name'], tool['versions'][0]['name']) for tool in tools_obj['tools'])
  57. version_dict = get_version_dict()
  58. ESP32ULP_VERSION = version_dict[ESP32ULP]
  59. ESP32S2ULP_VERSION = version_dict[ESP32S2ULP]
  60. OPENOCD_VERSION = version_dict[OPENOCD]
  61. XTENSA_ESP32_ELF_VERSION = version_dict[XTENSA_ESP32_ELF]
  62. XTENSA_ESP32S2_ELF_VERSION = version_dict[XTENSA_ESP32S2_ELF]
  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, 'dist')
  70. mirror_prefix_map += ';https://dl.espressif.com/dl,file://' + os.path.join(old_tools_dir, 'dist')
  71. mirror_prefix_map += ';https://github.com/espressif/.*/releases/download/.*/,file://' + os.path.join(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. @classmethod
  79. def tearDownClass(cls):
  80. shutil.rmtree(cls.temp_tools_dir)
  81. def tearDown(self):
  82. if os.path.isdir(os.path.join(self.temp_tools_dir, 'dist')):
  83. shutil.rmtree(os.path.join(self.temp_tools_dir, 'dist'))
  84. if os.path.isdir(os.path.join(self.temp_tools_dir, 'tools')):
  85. shutil.rmtree(os.path.join(self.temp_tools_dir, 'tools'))
  86. if os.path.isfile(os.path.join(self.temp_tools_dir, 'idf-env.json')):
  87. os.remove(os.path.join(self.temp_tools_dir, 'idf-env.json'))
  88. def check_install_tool(self,tool,tool_version,output,assertIn=True):
  89. if assertIn:
  90. self.assertIn('Installing %s@' % tool + tool_version, output)
  91. self.assertIn('Downloading %s' % tool, output)
  92. else:
  93. self.assertNotIn('Installing %s@' % tool + tool_version, output)
  94. self.assertNotIn('Downloading %s' % tool, output)
  95. def check_install_esp32_ulp(self,output,assertIn=True):
  96. if assertIn:
  97. self.assertIn('Installing esp32ulp-elf@' + ESP32ULP_VERSION, output)
  98. self.assertIn('Downloading binutils-esp32ulp', output)
  99. else:
  100. self.assertNotIn('Installing esp32ulp-elf@' + ESP32ULP_VERSION, output)
  101. self.assertNotIn('Downloading binutils-esp32ulp', output)
  102. def check_install_esp32s2_ulp(self,output,assertIn=True):
  103. if assertIn:
  104. self.assertIn('Installing esp32s2ulp-elf@' + ESP32S2ULP_VERSION, output)
  105. self.assertIn('Downloading binutils-esp32s2ulp', output)
  106. else:
  107. self.assertNotIn('Installing esp32s2ulp-elf@' + ESP32S2ULP_VERSION, output)
  108. self.assertNotIn('Downloading binutils-esp32s2ulp', output)
  109. def run_idf_tools_with_action(self,action):
  110. output_stream = StringIO()
  111. with redirect_stdout(output_stream):
  112. idf_tools.main(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:' % ESP32S2ULP, output)
  120. self.assertIn('- %s (recommended)' % ESP32S2ULP_VERSION, output)
  121. self.assertIn('* %s:' % OPENOCD, output)
  122. self.assertIn('- %s (recommended)' % OPENOCD_VERSION, output)
  123. self.assertIn('* %s:' % XTENSA_ESP32_ELF, output)
  124. self.assertIn('- %s (recommended)' % XTENSA_ESP32_ELF_VERSION, output)
  125. self.assertIn('* %s:' % XTENSA_ESP32S2_ELF, output)
  126. self.assertIn('- %s (recommended)' % XTENSA_ESP32S2_ELF_VERSION, output)
  127. required_tools_installed = 5
  128. output = self.run_idf_tools_with_action(['install'])
  129. self.check_install_tool(OPENOCD,OPENOCD_VERSION,output)
  130. self.check_install_tool(XTENSA_ESP32_ELF,XTENSA_ESP32_ELF_VERSION,output)
  131. self.check_install_tool(XTENSA_ESP32S2_ELF,XTENSA_ESP32S2_ELF_VERSION,output)
  132. self.check_install_esp32_ulp(output)
  133. self.check_install_esp32s2_ulp(output)
  134. self.assertIn('to ' + 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: ' + XTENSA_ESP32_ELF_VERSION, output)
  141. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  142. output = self.run_idf_tools_with_action(['export'])
  143. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/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/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  150. (self.temp_tools_dir, ESP32S2ULP_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. def test_tools_for_esp32(self):
  154. required_tools_installed = 3
  155. output = self.run_idf_tools_with_action(['install', '--targets=esp32'])
  156. self.check_install_tool(XTENSA_ESP32_ELF,XTENSA_ESP32_ELF_VERSION,output)
  157. self.check_install_tool(OPENOCD,OPENOCD_VERSION,output)
  158. self.check_install_esp32_ulp(output)
  159. self.check_install_tool(XTENSA_ESP32S2_ELF,XTENSA_ESP32S2_ELF_VERSION,output,assertIn=False)
  160. self.check_install_esp32s2_ulp(output,assertIn=False)
  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/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  175. (self.temp_tools_dir, ESP32S2ULP_VERSION), output)
  176. self.assertNotIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  177. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  178. def test_tools_for_esp32s2(self):
  179. required_tools_installed = 3
  180. output = self.run_idf_tools_with_action(['install', '--targets=esp32s2'])
  181. self.check_install_tool(XTENSA_ESP32S2_ELF,XTENSA_ESP32S2_ELF_VERSION,output)
  182. self.check_install_tool(OPENOCD,OPENOCD_VERSION,output)
  183. self.check_install_esp32s2_ulp(output)
  184. self.check_install_tool(XTENSA_ESP32_ELF,XTENSA_ESP32_ELF_VERSION,output,assertIn=False)
  185. self.check_install_esp32_ulp(output,assertIn=False)
  186. self.assertIn('to ' + os.path.join(self.temp_tools_dir, 'dist'), output)
  187. self.assertEqual(required_tools_installed,output.count('Done'))
  188. output = self.run_idf_tools_with_action(['check'])
  189. self.assertIn('version installed in tools directory: ' + ESP32S2ULP_VERSION, output)
  190. self.assertIn('version installed in tools directory: ' + OPENOCD_VERSION, output)
  191. self.assertIn('version installed in tools directory: ' + XTENSA_ESP32S2_ELF_VERSION, output)
  192. output = self.run_idf_tools_with_action(['export'])
  193. self.assertIn('%s/tools/esp32s2ulp-elf/%s/esp32s2ulp-elf-binutils/bin' %
  194. (self.temp_tools_dir, ESP32S2ULP_VERSION), output)
  195. self.assertIn('%s/tools/xtensa-esp32s2-elf/%s/xtensa-esp32s2-elf/bin' %
  196. (self.temp_tools_dir, XTENSA_ESP32S2_ELF_VERSION), output)
  197. self.assertIn('%s/tools/openocd-esp32/%s/openocd-esp32/bin' %
  198. (self.temp_tools_dir, OPENOCD_VERSION), output)
  199. self.assertNotIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/bin' %
  200. (self.temp_tools_dir, ESP32ULP_VERSION), output)
  201. self.assertNotIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  202. (self.temp_tools_dir, XTENSA_ESP32_ELF_VERSION), output)
  203. class TestMaintainer(unittest.TestCase):
  204. def test_validation(self):
  205. idf_tools.main(['validate'])
  206. def test_json_rewrite(self):
  207. idf_tools.main(['rewrite'])
  208. idf_path = os.getenv('IDF_PATH')
  209. if not idf_path:
  210. self.fail('IDF_PATH needs to be set to run this test')
  211. with open(os.path.join(idf_path, 'tools/tools.json'), 'r') as f:
  212. json_old = f.read()
  213. with open(os.path.join(idf_path, 'tools/tools.new.json'), 'r') as f:
  214. json_new = f.read()
  215. self.assertEqual(json_old, json_new)
  216. if __name__ == '__main__':
  217. unittest.main()