From ab58f1080a3d51034503eb5668fcec8a172f55d8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 20 Aug 2026 12:50:56 -0500 Subject: [PATCH] Cover the tidy-project idedata assembly and hoist its helper import --- esphome/espidf/clang_tidy.py | 4 +-- tests/unit_tests/test_espidf_clang_tidy.py | 41 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/esphome/espidf/clang_tidy.py b/esphome/espidf/clang_tidy.py index d60e544412..fc98d2c21f 100644 --- a/esphome/espidf/clang_tidy.py +++ b/esphome/espidf/clang_tidy.py @@ -23,6 +23,8 @@ from dataclasses import dataclass import os from pathlib import Path +from esphome.build_helpers.idedata import _get_toolchain_includes, _parse_entry + TIDY_PROJECT_NAME = "esphome_tidy" # A do-nothing C++ app: just enough for IDF to configure a valid project. It's @@ -415,8 +417,6 @@ def _idedata_from_tidy_project(compile_commands: Path) -> dict: """ import json - from esphome.build_helpers.idedata import _get_toolchain_includes, _parse_entry - entries = json.loads(Path(compile_commands).read_text(encoding="utf-8")) entry = next((e for e in entries if e["file"].endswith("tidy.cpp")), None) if entry is None: diff --git a/tests/unit_tests/test_espidf_clang_tidy.py b/tests/unit_tests/test_espidf_clang_tidy.py index cb25535d8d..88195e4872 100644 --- a/tests/unit_tests/test_espidf_clang_tidy.py +++ b/tests/unit_tests/test_espidf_clang_tidy.py @@ -64,3 +64,44 @@ def test_setup_core_sets_arduino_env( _setup_core(tmp_path / "proj", _settings(target_framework=target_framework)) assert os.environ["ESPHOME_ARDUINO_COMPONENT"] == expected + + +def test_idedata_from_tidy_project(tmp_path) -> None: + """The tidy TU's compile entry is assembled into consumer-shaped idedata.""" + import json + from unittest.mock import patch + + from esphome.espidf import clang_tidy + + compile_commands = tmp_path / "compile_commands.json" + compile_commands.write_text( + json.dumps( + [ + { + "directory": str(tmp_path), + "file": str(tmp_path / "main" / "tidy.cpp"), + "command": "/tc/xtensa-esp32-elf-g++ -DUSE_ESP32 " + f"-I{tmp_path}/inc -c main/tidy.cpp -o tidy.o", + } + ] + ) + ) + with patch( + "esphome.espidf.clang_tidy._get_toolchain_includes", return_value=["/tc/inc"] + ): + data = clang_tidy._idedata_from_tidy_project(compile_commands) + assert data["cxx_path"] == "/tc/xtensa-esp32-elf-g++" + assert data["defines"] == ["USE_ESP32"] + assert data["includes"]["toolchain"] == ["/tc/inc"] + assert any(inc.endswith("/inc") for inc in data["includes"]["build"]) + + +def test_idedata_from_tidy_project_missing_tu_raises(tmp_path) -> None: + import json + + from esphome.espidf import clang_tidy + + compile_commands = tmp_path / "compile_commands.json" + compile_commands.write_text(json.dumps([])) + with pytest.raises(RuntimeError, match="tidy.cpp not found"): + clang_tidy._idedata_from_tidy_project(compile_commands)