test_components.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import json
  4. import logging
  5. import shutil
  6. from pathlib import Path
  7. import pytest
  8. from test_build_system_helpers import EnvDict, IdfPyFunc, append_to_file, replace_in_file
  9. def test_component_extra_dirs(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  10. logging.info('Setting EXTRA_COMPONENT_DIRS works')
  11. shutil.move(test_app_copy / 'main', test_app_copy / 'different_main' / 'main')
  12. replace_in_file((test_app_copy / 'CMakeLists.txt'), '# placeholder_before_include_project_cmake',
  13. 'set(EXTRA_COMPONENT_DIRS {})'.format(Path('different_main', 'main')))
  14. ret = idf_py('reconfigure')
  15. assert str(test_app_copy / 'different_main' / 'main') in ret.stdout
  16. assert str(test_app_copy / 'main') not in ret.stdout
  17. @pytest.mark.usefixtures('test_app_copy')
  18. def test_component_nonexistent_extra_dirs_not_allowed(idf_py: IdfPyFunc) -> None:
  19. ret = idf_py('reconfigure', '-DEXTRA_COMPONENT_DIRS="nonexistent_dir"', check=False)
  20. assert ret.returncode != 0
  21. def test_component_names_contain_spaces(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  22. logging.info('Component names may contain spaces')
  23. (test_app_copy / 'extra component').mkdir()
  24. (test_app_copy / 'extra component' / 'CMakeLists.txt').write_text('idf_component_register')
  25. idf_py('-DEXTRA_COMPONENT_DIRS="extra component;main"')
  26. def test_component_can_not_be_empty_dir(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  27. logging.info('Empty directory not treated as a component')
  28. empty_component_dir = (test_app_copy / 'components' / 'esp32')
  29. empty_component_dir.mkdir(parents=True)
  30. idf_py('reconfigure')
  31. data = json.load(open(test_app_copy / 'build' / 'project_description.json', 'r'))
  32. assert str(empty_component_dir) not in data.get('build_component_paths')
  33. def test_component_subdirs_not_added_to_component_dirs(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  34. logging.info('If a component directory is added to COMPONENT_DIRS, its subdirectories are not added')
  35. (test_app_copy / 'main' / 'test').mkdir(parents=True)
  36. (test_app_copy / 'main' / 'test' / 'CMakeLists.txt').write_text('idf_component_register()')
  37. idf_py('reconfigure')
  38. data = json.load(open(test_app_copy / 'build' / 'project_description.json', 'r'))
  39. assert str(test_app_copy / 'main' / 'test') not in data.get('build_component_paths')
  40. assert str(test_app_copy / 'main') in data.get('build_component_paths')
  41. def test_component_sibling_dirs_not_added_to_component_dirs(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  42. logging.info('If a component directory is added to COMPONENT_DIRS, its sibling directories are not added')
  43. mycomponents_subdir = (test_app_copy / 'mycomponents')
  44. (mycomponents_subdir / 'mycomponent').mkdir(parents=True)
  45. (mycomponents_subdir / 'mycomponent' / 'CMakeLists.txt').write_text('idf_component_register()')
  46. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  47. (mycomponents_subdir / 'esp32').mkdir(parents=True)
  48. (mycomponents_subdir / 'esp32' / 'CMakeLists.txt').write_text('idf_component_register()')
  49. idf_py('-DEXTRA_COMPONENT_DIRS={}'.format(str(mycomponents_subdir / 'mycomponent')), 'reconfigure')
  50. data = json.load(open(test_app_copy / 'build' / 'project_description.json', 'r'))
  51. assert str(mycomponents_subdir / 'esp32') not in data.get('build_component_paths')
  52. assert str(mycomponents_subdir / 'mycomponent') in data.get('build_component_paths')
  53. shutil.rmtree(mycomponents_subdir / 'esp32')
  54. # now the same thing, but add a components directory
  55. (test_app_copy / 'esp32').mkdir()
  56. (test_app_copy / 'esp32' / 'CMakeLists.txt').write_text('idf_component_register()')
  57. idf_py('-DEXTRA_COMPONENT_DIRS={}'.format(str(mycomponents_subdir)), 'reconfigure')
  58. data = json.load(open(test_app_copy / 'build' / 'project_description.json', 'r'))
  59. assert str(test_app_copy / 'esp32') not in data.get('build_component_paths')
  60. assert str(mycomponents_subdir / 'mycomponent') in data.get('build_component_paths')
  61. def test_component_properties_are_set(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  62. logging.info('Component properties are set')
  63. append_to_file(test_app_copy / 'CMakeLists.txt', '\n'.join(['',
  64. 'idf_component_get_property(srcs main SRCS)',
  65. 'message(STATUS SRCS:${srcs})']))
  66. ret = idf_py('reconfigure')
  67. assert 'SRCS:{}'.format(test_app_copy / 'main' / 'build_test_app.c') in ret.stdout, 'Component properties should be set'
  68. def test_component_overriden_dir(idf_py: IdfPyFunc, test_app_copy: Path, default_idf_env: EnvDict) -> None:
  69. logging.info('Getting component overriden dir')
  70. (test_app_copy / 'components' / 'hal').mkdir(parents=True)
  71. (test_app_copy / 'components' / 'hal' / 'CMakeLists.txt').write_text('\n'.join([
  72. 'idf_component_get_property(overriden_dir ${COMPONENT_NAME} COMPONENT_OVERRIDEN_DIR)',
  73. 'message(STATUS overriden_dir:${overriden_dir})']))
  74. ret = idf_py('reconfigure')
  75. idf_path = Path(default_idf_env.get('IDF_PATH'))
  76. # no registration, overrides registration as well
  77. assert 'overriden_dir:{}'.format(idf_path / 'components' / 'hal') in ret.stdout, 'Failed to get overriden dir'
  78. append_to_file((test_app_copy / 'components' / 'hal' / 'CMakeLists.txt'), '\n'.join([
  79. '',
  80. 'idf_component_register(KCONFIG ${overriden_dir}/Kconfig)',
  81. 'idf_component_get_property(kconfig ${COMPONENT_NAME} KCONFIG)',
  82. 'message(STATUS kconfig:${overriden_dir}/Kconfig)']))
  83. ret = idf_py('reconfigure', check=False)
  84. assert 'kconfig:{}'.format(idf_path / 'components' / 'hal') in ret.stdout, 'Failed to verify original `main` directory'
  85. def test_components_prioritizer_over_extra_components_dir(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  86. logging.info('Project components prioritized over EXTRA_COMPONENT_DIRS')
  87. (test_app_copy / 'extra_dir' / 'my_component').mkdir(parents=True)
  88. (test_app_copy / 'extra_dir' / 'my_component' / 'CMakeLists.txt').write_text('idf_component_register()')
  89. replace_in_file(test_app_copy / 'CMakeLists.txt',
  90. '# placeholder_before_include_project_cmake',
  91. 'set(EXTRA_COMPONENT_DIRS extra_dir)')
  92. ret = idf_py('reconfigure')
  93. assert str(test_app_copy / 'extra_dir' / 'my_component') in ret.stdout, 'Unable to find component specified in EXTRA_COMPONENT_DIRS'
  94. (test_app_copy / 'components' / 'my_component').mkdir(parents=True)
  95. (test_app_copy / 'components' / 'my_component' / 'CMakeLists.txt').write_text('idf_component_register()')
  96. ret = idf_py('reconfigure')
  97. assert str(test_app_copy / 'components' / 'my_component') in ret.stdout, 'Project components should be prioritized over EXTRA_COMPONENT_DIRS'
  98. def test_exclude_components_not_passed(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  99. logging.info('Components in EXCLUDE_COMPONENTS not passed to idf_component_manager')
  100. idf_py('create-component', '-C', 'components', 'to_be_excluded')
  101. (test_app_copy / 'components' / 'to_be_excluded' / 'idf_component.yml').write_text('invalid syntax..')
  102. ret = idf_py('reconfigure', check=False)
  103. assert ret.returncode == 2, 'Reconfigure should have failed due to invalid syntax in idf_component.yml'
  104. idf_py('-DEXCLUDE_COMPONENTS=to_be_excluded', 'reconfigure')
  105. def test_version_in_component_cmakelist(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  106. logging.info('Use IDF version variables in component CMakeLists.txt file')
  107. replace_in_file((test_app_copy / 'main' / 'CMakeLists.txt'), '# placeholder_before_idf_component_register',
  108. '\n'.join(['if (NOT IDF_VERSION_MAJOR)', ' message(FATAL_ERROR "IDF version not set")', 'endif()']))
  109. idf_py('reconfigure')