test_idf_tools.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 sys
  18. import unittest
  19. import tempfile
  20. import shutil
  21. try:
  22. from contextlib import redirect_stdout
  23. except ImportError:
  24. import contextlib
  25. @contextlib.contextmanager
  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. class TestUsage(unittest.TestCase):
  43. def test_usage_basic(self):
  44. old_tools_dir = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT)
  45. mirror_prefix_map = None
  46. if os.path.exists(old_tools_dir):
  47. mirror_prefix_map = 'https://dl.espressif.com/dl/toolchains/preview,file://' + os.path.join(old_tools_dir, 'dist')
  48. mirror_prefix_map += ';https://dl.espressif.com/dl,file://' + os.path.join(old_tools_dir, 'dist')
  49. mirror_prefix_map += ';https://github.com/espressif/.*/releases/download/.*/,file://' + os.path.join(old_tools_dir, 'dist', '')
  50. if mirror_prefix_map:
  51. print('Using IDF_MIRROR_PREFIX_MAP={}'.format(mirror_prefix_map))
  52. os.environ['IDF_MIRROR_PREFIX_MAP'] = mirror_prefix_map
  53. temp_tools_dir = tempfile.mkdtemp(prefix='idf_tools_tmp')
  54. print('Using IDF_TOOLS_PATH={}'.format(temp_tools_dir))
  55. os.environ['IDF_TOOLS_PATH'] = temp_tools_dir
  56. self.addCleanup(shutil.rmtree, temp_tools_dir)
  57. output_stream = StringIO()
  58. with redirect_stdout(output_stream):
  59. idf_tools.main(['list'])
  60. output = output_stream.getvalue()
  61. xtensa_esp32_elf_version = 'esp-2020r3-8.4.0'
  62. esp32ulp_version = '2.28.51-esp-20191205'
  63. self.assertIn('* xtensa-esp32-elf:', output)
  64. self.assertIn('- %s (recommended)' % xtensa_esp32_elf_version, output)
  65. self.assertIn('* esp32ulp-elf', output)
  66. self.assertIn('- %s (recommended)' % esp32ulp_version, output)
  67. output_stream = StringIO()
  68. with redirect_stdout(output_stream):
  69. idf_tools.main(['install'])
  70. output = output_stream.getvalue()
  71. self.assertIn('Installing esp32ulp-elf@' + esp32ulp_version, output)
  72. self.assertIn('Downloading binutils-esp32ulp', output)
  73. self.assertIn('Installing xtensa-esp32-elf@' + xtensa_esp32_elf_version, output)
  74. self.assertIn('Downloading xtensa-esp32-elf', output)
  75. self.assertIn('to ' + os.path.join(temp_tools_dir, 'dist'), output)
  76. output_stream = StringIO()
  77. with redirect_stdout(output_stream):
  78. idf_tools.main(['check'])
  79. output = output_stream.getvalue()
  80. self.assertIn('version installed in tools directory: ' + esp32ulp_version, output)
  81. self.assertIn('version installed in tools directory: ' + xtensa_esp32_elf_version, output)
  82. output_stream = StringIO()
  83. with redirect_stdout(output_stream):
  84. idf_tools.main(['export'])
  85. output = output_stream.getvalue()
  86. self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/bin' %
  87. (temp_tools_dir, esp32ulp_version), output)
  88. self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' %
  89. (temp_tools_dir, xtensa_esp32_elf_version), output)
  90. class TestMaintainer(unittest.TestCase):
  91. def test_validation(self):
  92. idf_tools.main(['validate'])
  93. def test_json_rewrite(self):
  94. idf_tools.main(['rewrite'])
  95. idf_path = os.getenv('IDF_PATH')
  96. if not idf_path:
  97. self.fail('IDF_PATH needs to be set to run this test')
  98. with open(os.path.join(idf_path, 'tools/tools.json'), 'r') as f:
  99. json_old = f.read()
  100. with open(os.path.join(idf_path, 'tools/tools.new.json'), 'r') as f:
  101. json_new = f.read()
  102. self.assertEqual(json_old, json_new)
  103. if __name__ == '__main__':
  104. unittest.main()