test_idf_tools_python_env.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. import unittest
  8. from typing import List
  9. try:
  10. import idf_tools
  11. except ImportError:
  12. sys.path.append('..')
  13. import idf_tools
  14. IDF_PATH = os.environ.get('IDF_PATH', '../..')
  15. TOOLS_DIR = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT)
  16. PYTHON_DIR = os.path.join(TOOLS_DIR, 'python_env')
  17. REQ_SATISFIED = 'Python requirements are satisfied'
  18. REQ_CORE = '- {}/requirements.core.txt'.format(IDF_PATH)
  19. REQ_GDBGUI = '- {}/requirements.gdbgui.txt'.format(IDF_PATH)
  20. CONSTR = 'Constraint file: {}/espidf.constraints'.format(TOOLS_DIR)
  21. class TestPythonInstall(unittest.TestCase):
  22. def setUp(self): # type: () -> None
  23. if os.path.isdir(PYTHON_DIR):
  24. shutil.rmtree(PYTHON_DIR)
  25. if os.path.isfile(os.path.join(TOOLS_DIR, 'idf-env.json')):
  26. os.remove(os.path.join(TOOLS_DIR, 'idf-env.json'))
  27. def run_idf_tools(self, extra_args): # type: (List[str]) -> str
  28. args = [sys.executable, '../idf_tools.py'] + extra_args
  29. ret = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=120)
  30. return ret.stdout.decode('utf-8', 'ignore')
  31. def test_default_arguments(self): # type: () -> None
  32. output = self.run_idf_tools(['check-python-dependencies'])
  33. self.assertNotIn(REQ_SATISFIED, output)
  34. self.assertIn('bin/python doesn\'t exist', output)
  35. output = self.run_idf_tools(['install-python-env'])
  36. self.assertIn(CONSTR, output)
  37. self.assertIn(REQ_CORE, output)
  38. self.assertNotIn(REQ_GDBGUI, output)
  39. output = self.run_idf_tools(['check-python-dependencies'])
  40. self.assertIn(REQ_SATISFIED, output)
  41. def test_opt_argument(self): # type: () -> None
  42. output = self.run_idf_tools(['install-python-env', '--features', 'gdbgui'])
  43. self.assertIn(CONSTR, output)
  44. self.assertIn(REQ_CORE, output)
  45. self.assertIn(REQ_GDBGUI, output)
  46. output = self.run_idf_tools(['install-python-env'])
  47. # The gdbgui should be installed as well because the feature is is stored in the JSON file
  48. self.assertIn(CONSTR, output)
  49. self.assertIn(REQ_CORE, output)
  50. self.assertIn(REQ_GDBGUI, output)
  51. def test_no_constraints(self): # type: () -> None
  52. output = self.run_idf_tools(['install-python-env', '--no-constraints'])
  53. self.assertNotIn(CONSTR, output)
  54. self.assertIn(REQ_CORE, output)
  55. if __name__ == '__main__':
  56. unittest.main()