Skip to content

Commit

Permalink
fix: --config-file not refreshed
Browse files Browse the repository at this point in the history
  • Loading branch information
hfudev committed Jan 10, 2025
1 parent d8f4bea commit 66ccb67
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
37 changes: 25 additions & 12 deletions idf_build_apps/args.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

import argparse
Expand Down Expand Up @@ -115,8 +115,9 @@ def get_meta(f: FieldInfo) -> t.Optional[FieldMetadata]:
class BaseArguments(BaseSettings):
"""Base settings class for all settings classes"""

CONFIG_FILE_PATH: t.ClassVar[t.Optional[Path]] = None

model_config = SettingsConfigDict(
toml_file=IDF_BUILD_APPS_TOML_FN,
# these below two are supported in pydantic 2.6
pyproject_toml_table_header=('tool', 'idf-build-apps'),
pyproject_toml_depth=sys.maxsize,
Expand All @@ -132,11 +133,15 @@ def settings_customise_sources(
dotenv_settings: PydanticBaseSettingsSource, # noqa: ARG003
file_secret_settings: PydanticBaseSettingsSource, # noqa: ARG003
) -> t.Tuple[PydanticBaseSettingsSource, ...]:
return (
init_settings,
TomlConfigSettingsSource(settings_cls),
PyprojectTomlConfigSettingsSource(settings_cls),
)
sources: t.Tuple[PydanticBaseSettingsSource, ...] = (init_settings,)
if cls.CONFIG_FILE_PATH is None:
sources += (TomlConfigSettingsSource(settings_cls, toml_file=Path(IDF_BUILD_APPS_TOML_FN)),)
sources += (PyprojectTomlConfigSettingsSource(settings_cls, toml_file=Path('pyproject.toml')),)
else:
sources += (TomlConfigSettingsSource(settings_cls, toml_file=Path(cls.CONFIG_FILE_PATH)),)
sources += (PyprojectTomlConfigSettingsSource(settings_cls, toml_file=Path(cls.CONFIG_FILE_PATH)),)

return sources

@field_validator('*', mode='before')
@classmethod
Expand Down Expand Up @@ -874,12 +879,20 @@ def add_args_to_obj_doc_as_params(argument_cls: t.Type[GlobalArguments], obj: t.
_obj.__doc__ = _doc_str


def apply_config_file(config_file: t.Optional[str]) -> None:
def apply_config_file(config_file: t.Optional[str] = None, reset: bool = False) -> None:
def _subclasses(klass: t.Type[T]) -> t.Set[t.Type[T]]:
return set(klass.__subclasses__()).union([s for c in klass.__subclasses__() for s in _subclasses(c)])

if config_file:
BaseArguments.model_config['toml_file'] = str(config_file)
# modify all subclasses
if reset:
BaseArguments.CONFIG_FILE_PATH = None
for cls in _subclasses(BaseArguments):
cls.model_config['toml_file'] = str(config_file)
cls.CONFIG_FILE_PATH = None

if config_file:
if os.path.isfile(config_file):
p = Path(config_file)
BaseArguments.CONFIG_FILE_PATH = p
for cls in _subclasses(BaseArguments):
cls.CONFIG_FILE_PATH = p
else:
LOGGER.warning(f'Config file {config_file} does not exist. Ignoring...')
12 changes: 7 additions & 5 deletions idf_build_apps/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PYTHON_ARGCOMPLETE_OK

# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

import argparse
Expand Down Expand Up @@ -68,7 +68,8 @@ def find_apps(
:return: list of found apps
"""
apply_config_file(config_file)
if config_file:
apply_config_file(config_file)

# compatible with old usage
## `preserve`
Expand Down Expand Up @@ -132,7 +133,8 @@ def build_apps(
:return: exit code
"""
apply_config_file(config_file)
if config_file:
apply_config_file(config_file)

# compatible with old usage
## `check_app_dependencies`
Expand Down Expand Up @@ -385,8 +387,8 @@ def main():
kwargs = vars(args)
action = kwargs.pop('action')
config_file = kwargs.pop('config_file')

apply_config_file(config_file)
if config_file:
apply_config_file(config_file)

if action == 'dump-manifest-sha':
arguments = DumpManifestShaArguments(**drop_none_kwargs(kwargs))
Expand Down
4 changes: 3 additions & 1 deletion idf_build_apps/vendors/pydantic_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def _pick_toml_file(provided: Optional[Path], depth: int, filename: str) -> Opti
:param depth: Number of directories up the tree to check of a pyproject.toml.
"""
if provided and Path(provided).is_file():
return provided.resolve()
fp = provided.resolve()
print(f'Loading config file: {fp}')
return fp

rv = Path.cwd()
count = -1
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
setup_logging,
)
from idf_build_apps.args import apply_config_file
from idf_build_apps.constants import IDF_BUILD_APPS_TOML_FN, SUPPORTED_TARGETS
from idf_build_apps.constants import SUPPORTED_TARGETS
from idf_build_apps.manifest.manifest import FolderRule


Expand All @@ -19,7 +19,7 @@ def clean_cls_attr(tmp_path):
App.MANIFEST = None
FolderRule.DEFAULT_BUILD_TARGETS = SUPPORTED_TARGETS
idf_build_apps.SESSION_ARGS.clean()
apply_config_file(IDF_BUILD_APPS_TOML_FN)
apply_config_file(reset=True)
os.chdir(tmp_path)


Expand Down
39 changes: 38 additions & 1 deletion tests/test_args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import os
from tempfile import NamedTemporaryFile
from xml.etree import ElementTree

import pytest
Expand Down Expand Up @@ -323,3 +324,39 @@ def test_config_file(self, tmp_path, monkeypatch):
assert test_suite.attrib['skipped'] == '2'
assert test_suite.findall('testcase')[0].attrib['name'] == 'foo/build_esp32'
assert test_suite.findall('testcase')[1].attrib['name'] == 'foo/build_esp32s2'

def test_config_file_by_cli(self, tmp_path, monkeypatch):
create_project('foo', tmp_path)
create_project('bar', tmp_path)

with open(IDF_BUILD_APPS_TOML_FN, 'w') as fw:
fw.write('paths = ["foo"]')

with NamedTemporaryFile(mode='w', suffix='.toml') as ft:
ft.write('paths = ["bar"]')
ft.flush()

with monkeypatch.context() as m:
m.setattr(
'sys.argv',
[
'idf-build-apps',
'build',
'-t',
'esp32',
'--config-file',
ft.name,
'--junitxml',
'test.xml',
'--dry-run',
],
)
main()

with open('test.xml') as f:
xml = ElementTree.fromstring(f.read())
test_suite = xml.findall('testsuite')[0]
assert test_suite.attrib['failures'] == '0'
assert test_suite.attrib['errors'] == '0'
assert test_suite.attrib['skipped'] == '1'
assert test_suite.findall('testcase')[0].attrib['name'] == 'bar/build'

0 comments on commit 66ccb67

Please sign in to comment.