test_idf_tools_python_env.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = '- {}'.format(os.path.join(IDF_PATH, 'tools', 'requirements', 'requirements.core.txt'))
  19. REQ_GDBGUI = '- {}'.format(os.path.join(IDF_PATH, 'tools', 'requirements', 'requirements.gdbgui.txt'))
  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. # Argument that begins with '-' can't stand alone to be parsed as value
  52. output = self.run_idf_tools(['install-python-env', '--features=-gdbgui'])
  53. # After removing the gdbgui should not be present
  54. self.assertIn(CONSTR, output)
  55. self.assertIn(REQ_CORE, output)
  56. self.assertNotIn(REQ_GDBGUI, output)
  57. def test_no_constraints(self): # type: () -> None
  58. output = self.run_idf_tools(['install-python-env', '--no-constraints'])
  59. self.assertNotIn(CONSTR, output)
  60. self.assertIn(REQ_CORE, output)
  61. if __name__ == '__main__':
  62. unittest.main()