test_components.py 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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'