test_idf_tools.py 4.5 KB

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