test_sdkconfig.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. from pathlib import Path
  7. from typing import Optional
  8. import pytest
  9. from test_build_system_helpers import IdfPyFunc, file_contains
  10. def idf_version_from_cmake() -> Optional[str]:
  11. version_path = os.path.join(os.environ['IDF_PATH'], 'tools/cmake/version.cmake')
  12. regex = re.compile(r'^\s*set\s*\(\s*IDF_VERSION_([A-Z]{5})\s+(\d+)')
  13. ver = {}
  14. try:
  15. with open(version_path) as f:
  16. for line in f:
  17. m = regex.match(line)
  18. if m:
  19. ver[m.group(1)] = m.group(2)
  20. return '%s.%s.%s' % (ver['MAJOR'], ver['MINOR'], ver['PATCH'])
  21. except (KeyError, OSError):
  22. pytest.fail('Cannot find ESP-IDF version in tools/cmake/version.cmake')
  23. return None
  24. def test_sdkconfig_contains_all_files(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  25. logging.info('sdkconfig should have contents of all files: sdkconfig, sdkconfig.defaults, sdkconfig.defaults.IDF_TARGET')
  26. (test_app_copy / 'sdkconfig').write_text('CONFIG_PARTITION_TABLE_TWO_OTA=y')
  27. (test_app_copy / 'sdkconfig.defaults').write_text('CONFIG_PARTITION_TABLE_OFFSET=0x10000')
  28. (test_app_copy / 'sdkconfig.defaults.esp32').write_text('CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y')
  29. idf_py('reconfigure')
  30. assert all([file_contains((test_app_copy / 'sdkconfig'), x) for x in ['CONFIG_PARTITION_TABLE_TWO_OTA=y',
  31. 'CONFIG_PARTITION_TABLE_OFFSET=0x10000',
  32. 'CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y']])
  33. def test_sdkconfig_multiple_default_files(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  34. logging.info('should be able to specify multiple sdkconfig default files')
  35. (test_app_copy / 'sdkconfig.defaults1').write_text('CONFIG_PARTITION_TABLE_OFFSET=0x10000')
  36. (test_app_copy / 'sdkconfig.defaults2').write_text('CONFIG_PARTITION_TABLE_TWO_OTA=y')
  37. idf_py('-DSDKCONFIG_DEFAULTS=sdkconfig.defaults1;sdkconfig.defaults2', 'reconfigure')
  38. assert all([file_contains((test_app_copy / 'sdkconfig'), x) for x in ['CONFIG_PARTITION_TABLE_TWO_OTA=y',
  39. 'CONFIG_PARTITION_TABLE_OFFSET=0x10000']])
  40. def test_keep_idf_init_version(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  41. logging.info('sdkconfig should contain initial IDF version, even after regenerating the config')
  42. (test_app_copy / 'sdkconfig').write_text('CONFIG_IDF_INIT_VERSION="1.2.3"')
  43. idf_py('reconfigure')
  44. assert file_contains((test_app_copy / 'sdkconfig'), 'CONFIG_IDF_INIT_VERSION="1.2.3"')
  45. def test_empty_idf_init_version(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  46. logging.info('sdkconfig should add current IDF version as initial IDF version')
  47. (test_app_copy / 'sdkconfig').write_text('CONFIG_IDF_INIT_VERSION=""')
  48. idf_py('reconfigure')
  49. version = idf_version_from_cmake()
  50. assert file_contains((test_app_copy / 'sdkconfig'), f'CONFIG_IDF_INIT_VERSION="{version}"')