test_idf_tools_python_env.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import inspect
  4. import os
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. import unittest
  10. from typing import List
  11. try:
  12. import idf_tools
  13. except ImportError:
  14. sys.path.append('..')
  15. import idf_tools
  16. IDF_PATH = os.environ.get('IDF_PATH', '../..')
  17. TOOLS_DIR = os.environ.get('IDF_TOOLS_PATH') or os.path.expanduser(idf_tools.IDF_TOOLS_PATH_DEFAULT)
  18. PYTHON_DIR = os.path.join(TOOLS_DIR, 'python_env')
  19. REQ_SATISFIED = 'Python requirements are satisfied'
  20. REQ_CORE = '- {}'.format(os.path.join(IDF_PATH, 'tools', 'requirements', 'requirements.core.txt'))
  21. REQ_GDBGUI = '- {}'.format(os.path.join(IDF_PATH, 'tools', 'requirements', 'requirements.gdbgui.txt'))
  22. CONSTR = 'Constraint file: {}/espidf.constraints'.format(TOOLS_DIR)
  23. class TestPythonInstall(unittest.TestCase):
  24. def setUp(self): # type: () -> None
  25. if os.path.isdir(PYTHON_DIR):
  26. shutil.rmtree(PYTHON_DIR)
  27. if os.path.isfile(os.path.join(TOOLS_DIR, 'idf-env.json')):
  28. os.remove(os.path.join(TOOLS_DIR, 'idf-env.json'))
  29. def run_idf_tools(self, extra_args): # type: (List[str]) -> str
  30. args = [sys.executable, '../idf_tools.py'] + extra_args
  31. ret = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=300)
  32. decoded_output = ret.stdout.decode('utf-8', 'ignore')
  33. with open(os.path.join(IDF_PATH, 'tools', 'test_idf_tools', 'test_python_env_logs.txt'), 'a+') as w:
  34. # stack() returns list of callers frame records. [1] represent caller of this function
  35. w.write('============================= ' + inspect.stack()[1].function + ' =============================\n')
  36. w.write(decoded_output)
  37. return decoded_output
  38. def test_default_arguments(self): # type: () -> None
  39. output = self.run_idf_tools(['check-python-dependencies'])
  40. self.assertNotIn(REQ_SATISFIED, output)
  41. self.assertIn('bin/python doesn\'t exist', output)
  42. output = self.run_idf_tools(['install-python-env'])
  43. self.assertIn(CONSTR, output)
  44. self.assertIn(REQ_CORE, output)
  45. self.assertNotIn(REQ_GDBGUI, output)
  46. output = self.run_idf_tools(['check-python-dependencies'])
  47. self.assertIn(REQ_SATISFIED, output)
  48. def test_opt_argument(self): # type: () -> None
  49. output = self.run_idf_tools(['install-python-env', '--features', 'gdbgui'])
  50. self.assertIn(CONSTR, output)
  51. self.assertIn(REQ_CORE, output)
  52. self.assertIn(REQ_GDBGUI, output)
  53. output = self.run_idf_tools(['install-python-env'])
  54. # The gdbgui should be installed as well because the feature is is stored in the JSON file
  55. self.assertIn(CONSTR, output)
  56. self.assertIn(REQ_CORE, output)
  57. self.assertIn(REQ_GDBGUI, output)
  58. # Argument that begins with '-' can't stand alone to be parsed as value
  59. output = self.run_idf_tools(['install-python-env', '--features=-gdbgui'])
  60. # After removing the gdbgui should not be present
  61. self.assertIn(CONSTR, output)
  62. self.assertIn(REQ_CORE, output)
  63. self.assertNotIn(REQ_GDBGUI, output)
  64. def test_no_constraints(self): # type: () -> None
  65. output = self.run_idf_tools(['install-python-env', '--no-constraints'])
  66. self.assertNotIn(CONSTR, output)
  67. self.assertIn(REQ_CORE, output)
  68. class TestCustomPythonPathInstall(TestPythonInstall):
  69. def setUp(self): # type: () -> None
  70. self.CUSTOM_PYTHON_DIR = tempfile.mkdtemp()
  71. self.addCleanup(shutil.rmtree, self.CUSTOM_PYTHON_DIR)
  72. os.environ['IDF_PYTHON_ENV_PATH'] = self.CUSTOM_PYTHON_DIR
  73. def test_default_arguments(self): # type: () -> None
  74. output = self.run_idf_tools(['check-python-dependencies'])
  75. self.assertIn(f"{self.CUSTOM_PYTHON_DIR}/bin/python doesn't exist", output)
  76. self.assertNotIn(PYTHON_DIR, output)
  77. output = self.run_idf_tools(['install-python-env'])
  78. self.assertIn(self.CUSTOM_PYTHON_DIR, output)
  79. self.assertNotIn(PYTHON_DIR, output)
  80. output = self.run_idf_tools(['check-python-dependencies'])
  81. self.assertIn(self.CUSTOM_PYTHON_DIR, output)
  82. if __name__ == '__main__':
  83. unittest.main()