Compare commits

...
2 changed files with 59 additions and 5 deletions
+3 -2
View File
@@ -337,12 +337,13 @@ def copy_src_tree():
else:
try:
existing = json.loads(build_info_json_path.read_text(encoding="utf-8"))
if (
if not isinstance(existing, dict) or (
existing.get("config_hash") != config_hash
or existing.get("esphome_version") != __version__
):
# Non-object JSON is stale like every other damage case
sources_changed = True
except (json.JSONDecodeError, KeyError, OSError):
except (json.JSONDecodeError, OSError):
sources_changed = True
# Write build_info header and JSON metadata
+56 -3
View File
@@ -2072,9 +2072,10 @@ def test_copy_src_tree_detects_config_hash_change(
)
)
# Create existing build_info_data.h
# Both sources must exist to reach the JSON comparison branch
build_info_h_path = esphome_core_path / "build_info_data.h"
build_info_h_path.write_text("// old build_info_data.h")
(esphome_core_path / "build_info_data.cpp").write_text("// old")
# Setup mocks
mock_core.relative_src_path.side_effect = src_path.joinpath
@@ -2136,9 +2137,10 @@ def test_copy_src_tree_detects_version_change(
)
)
# Create existing build_info_data.h
# Both sources must exist to reach the JSON comparison branch
build_info_h_path = esphome_core_path / "build_info_data.h"
build_info_h_path.write_text("// old build_info_data.h")
(esphome_core_path / "build_info_data.cpp").write_text("// old")
# Setup mocks
mock_core.relative_src_path.side_effect = src_path.joinpath
@@ -2186,9 +2188,60 @@ def test_copy_src_tree_handles_invalid_build_info_json(
build_info_json_path = build_path / "build_info.json"
build_info_json_path.write_text("invalid json {{{")
# Create existing build_info_data.h
# Both sources must exist to reach the JSON comparison branch
build_info_h_path = esphome_core_path / "build_info_data.h"
build_info_h_path.write_text("// old build_info_data.h")
(esphome_core_path / "build_info_data.cpp").write_text("// old")
# Setup mocks
mock_core.relative_src_path.side_effect = src_path.joinpath
mock_core.relative_build_path.side_effect = build_path.joinpath
mock_core.defines = []
mock_core.config_hash = 0xDEADBEEF
mock_core.comment = ""
mock_core.target_platform = "test_platform"
mock_core.config = {}
mock_iter_components.return_value = []
mock_walk_files.return_value = []
with (
patch("esphome.writer.__version__", "2025.1.0-dev"),
patch("esphome.writer.importlib.import_module") as mock_import,
):
mock_import.side_effect = AttributeError
copy_src_tree()
# Verify build_info files were created despite invalid JSON
assert build_info_h_path.exists()
new_json = json.loads(build_info_json_path.read_text())
assert new_json["config_hash"] == 0xDEADBEEF
@patch("esphome.writer.CORE")
@patch("esphome.writer.iter_components")
@patch("esphome.writer.walk_files")
def test_copy_src_tree_handles_non_dict_build_info_json(
mock_walk_files: MagicMock,
mock_iter_components: MagicMock,
mock_core: MagicMock,
tmp_path: Path,
) -> None:
"""Valid JSON that is not an object (no .get) is treated as stale."""
# Setup directory structure
src_path = tmp_path / "src"
src_path.mkdir()
esphome_core_path = src_path / "esphome" / "core"
esphome_core_path.mkdir(parents=True)
build_path = tmp_path / "build"
build_path.mkdir()
build_info_json_path = build_path / "build_info.json"
build_info_json_path.write_text("[]")
# Both sources must exist to reach the JSON comparison branch
build_info_h_path = esphome_core_path / "build_info_data.h"
build_info_h_path.write_text("// old build_info_data.h")
(esphome_core_path / "build_info_data.cpp").write_text("// old")
# Setup mocks
mock_core.relative_src_path.side_effect = src_path.joinpath