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 17import os 18import shutil 19import sys 20import tempfile 21import unittest 22 23try: 24 from contextlib import redirect_stdout 25except ImportError: 26 import contextlib 27 28 @contextlib.contextmanager 29 def redirect_stdout(target): 30 original = sys.stdout 31 sys.stdout = target 32 yield 33 sys.stdout = original 34 35try: 36 from cStringIO import StringIO 37except ImportError: 38 from io import StringIO 39 40# Need to do this before importing idf_tools.py 41os.environ['IDF_MAINTAINER'] = '1' 42 43try: 44 import idf_tools 45except ImportError: 46 sys.path.append('..') 47 import idf_tools 48 49 50class TestUsage(unittest.TestCase): 51 52 def test_usage_basic(self): 53 old_tools_dir = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT) 54 55 mirror_prefix_map = None 56 if os.path.exists(old_tools_dir): 57 mirror_prefix_map = 'https://dl.espressif.com/dl/toolchains/preview,file://' + os.path.join(old_tools_dir, 'dist') 58 mirror_prefix_map += ';https://dl.espressif.com/dl,file://' + os.path.join(old_tools_dir, 'dist') 59 mirror_prefix_map += ';https://github.com/espressif/.*/releases/download/.*/,file://' + os.path.join(old_tools_dir, 'dist', '') 60 if mirror_prefix_map: 61 print('Using IDF_MIRROR_PREFIX_MAP={}'.format(mirror_prefix_map)) 62 os.environ['IDF_MIRROR_PREFIX_MAP'] = mirror_prefix_map 63 64 temp_tools_dir = tempfile.mkdtemp(prefix='idf_tools_tmp') 65 print('Using IDF_TOOLS_PATH={}'.format(temp_tools_dir)) 66 os.environ['IDF_TOOLS_PATH'] = temp_tools_dir 67 68 self.addCleanup(shutil.rmtree, temp_tools_dir) 69 70 output_stream = StringIO() 71 with redirect_stdout(output_stream): 72 idf_tools.main(['list']) 73 output = output_stream.getvalue() 74 75 xtensa_esp32_elf_version = 'esp-2020r3-8.4.0' 76 esp32ulp_version = '2.28.51-esp-20191205' 77 78 self.assertIn('* xtensa-esp32-elf:', output) 79 self.assertIn('- %s (recommended)' % xtensa_esp32_elf_version, output) 80 self.assertIn('* esp32ulp-elf', output) 81 self.assertIn('- %s (recommended)' % esp32ulp_version, output) 82 83 output_stream = StringIO() 84 with redirect_stdout(output_stream): 85 idf_tools.main(['install']) 86 output = output_stream.getvalue() 87 88 self.assertIn('Installing esp32ulp-elf@' + esp32ulp_version, output) 89 self.assertIn('Downloading binutils-esp32ulp', output) 90 self.assertIn('Installing xtensa-esp32-elf@' + xtensa_esp32_elf_version, output) 91 self.assertIn('Downloading xtensa-esp32-elf', output) 92 self.assertIn('to ' + os.path.join(temp_tools_dir, 'dist'), output) 93 94 output_stream = StringIO() 95 with redirect_stdout(output_stream): 96 idf_tools.main(['check']) 97 output = output_stream.getvalue() 98 99 self.assertIn('version installed in tools directory: ' + esp32ulp_version, output) 100 self.assertIn('version installed in tools directory: ' + xtensa_esp32_elf_version, output) 101 102 output_stream = StringIO() 103 with redirect_stdout(output_stream): 104 idf_tools.main(['export']) 105 output = output_stream.getvalue() 106 107 self.assertIn('%s/tools/esp32ulp-elf/%s/esp32ulp-elf-binutils/bin' % 108 (temp_tools_dir, esp32ulp_version), output) 109 self.assertIn('%s/tools/xtensa-esp32-elf/%s/xtensa-esp32-elf/bin' % 110 (temp_tools_dir, xtensa_esp32_elf_version), output) 111 112 113class TestMaintainer(unittest.TestCase): 114 115 def test_validation(self): 116 idf_tools.main(['validate']) 117 118 def test_json_rewrite(self): 119 idf_tools.main(['rewrite']) 120 idf_path = os.getenv('IDF_PATH') 121 if not idf_path: 122 self.fail('IDF_PATH needs to be set to run this test') 123 with open(os.path.join(idf_path, 'tools/tools.json'), 'r') as f: 124 json_old = f.read() 125 with open(os.path.join(idf_path, 'tools/tools.new.json'), 'r') as f: 126 json_new = f.read() 127 self.assertEqual(json_old, json_new) 128 129 130if __name__ == '__main__': 131 unittest.main() 132