test_idf_tools_python_env.py 3.4 KB

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