test_git.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import os
  5. import re
  6. import shutil
  7. import subprocess
  8. import typing
  9. from pathlib import Path
  10. from test_build_system_helpers import EnvDict, IdfPyFunc, run_idf_py
  11. def run_git_cmd(*args: str,
  12. workdir: Path,
  13. env: typing.Optional[EnvDict] = None) -> subprocess.CompletedProcess:
  14. cmd = ['git'] + list(args)
  15. env_dict = dict(**os.environ)
  16. if env:
  17. env_dict.update(env)
  18. logging.debug('running {} in {}'.format(' '.join(cmd), workdir))
  19. return subprocess.run(cmd, cwd=workdir, env=env_dict,
  20. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  21. def test_get_version_from_git_describe(test_git_template_app: Path, idf_py: IdfPyFunc) -> None:
  22. logging.info('Get the version of app from git describe. Project is not inside IDF and do not have a tag only a hash commit.')
  23. idf_ret = idf_py('reconfigure')
  24. git_ret = run_git_cmd('describe', '--always', '--tags', '--dirty', workdir=test_git_template_app)
  25. assert f'App "app-template" version: {git_ret.stdout.decode("utf-8")}' in idf_ret.stdout, 'Project version should have a hash commit'
  26. # In this test, the action needs to be performed in ESP-IDF that is valid git directory
  27. # Copying ESP-IDF is not possible
  28. def test_git_custom_tag() -> None:
  29. try:
  30. env_dict = dict(**os.environ)
  31. idf_build_test_app_path = Path(env_dict['IDF_PATH'], 'tools', 'test_build_system', 'build_test_app')
  32. logging.info('Project is in ESP-IDF which has a custom tag')
  33. env_dict['GIT_COMMITTER_NAME'] = 'No One'
  34. env_dict['GIT_COMMITTER_EMAIL'] = 'noone@espressif.com'
  35. run_git_cmd('tag', 'mytag', '-a', '-m', 'mytag', workdir=idf_build_test_app_path, env=env_dict)
  36. idf_ret = run_idf_py('reconfigure', workdir=idf_build_test_app_path)
  37. assert 'App "build_test_app" version: mytag' in idf_ret.stdout, 'Project version should be the custom tag'
  38. version = run_idf_py('--version', workdir=idf_build_test_app_path)
  39. assert 'mytag' not in version.stdout, 'IDF version should not contain mytag'
  40. finally:
  41. run_git_cmd('tag', '-d', 'mytag', workdir=idf_build_test_app_path)
  42. shutil.rmtree(idf_build_test_app_path / 'build')
  43. os.unlink(idf_build_test_app_path / 'sdkconfig')
  44. def test_support_git_worktree(test_git_template_app: Path) -> None:
  45. logging.info('Supports git worktree')
  46. run_git_cmd('branch', 'test_build_system', workdir=test_git_template_app)
  47. run_git_cmd('worktree', 'add', '../esp-idf-worktree-app', 'test_build_system', workdir=test_git_template_app)
  48. try:
  49. idf_ret_template_app = run_idf_py('reconfigure', workdir=test_git_template_app)
  50. idf_ret_worktree_app = run_idf_py('reconfigure', workdir=os.path.join(os.path.dirname(test_git_template_app), 'esp-idf-worktree-app'))
  51. assert (re.search(r'-- App \"app-template\".*', idf_ret_template_app.stdout).group() == # type: ignore
  52. re.search(r'-- App \"app-template\".*', idf_ret_worktree_app.stdout).group()) # type: ignore
  53. finally:
  54. run_git_cmd('worktree', 'remove', '../esp-idf-worktree-app', workdir=test_git_template_app)
  55. run_git_cmd('branch', '-d', 'test_build_system', workdir=test_git_template_app)