diff --git a/esphome/espidf/size_summary.py b/esphome/espidf/size_summary.py index cab632defe..d5d345b7e6 100644 --- a/esphome/espidf/size_summary.py +++ b/esphome/espidf/size_summary.py @@ -124,32 +124,36 @@ def print_summary(size_json: Path, partitions_csv: Path, firmware_elf: Path) -> for entry in data.get("layout", []) if isinstance(entry, dict) } - ram_region = regions.get("DRAM") or regions.get("DIRAM") or {} - ram_used = ram_region.get("used") - ram_total = ram_region.get("total") - if ram_total and ram_used is not None: + # Every chip has a DRAM or DIRAM region, so a warning here usually + # means the esp_idf_size json schema changed + ram_region = regions.get("DRAM") or regions.get("DIRAM") + if ram_region is None: + _LOGGER.warning("Skipping RAM summary: no DRAM/DIRAM region in %s", size_json) + elif ( + isinstance(ram_total := ram_region.get("total"), int) + and ram_total > 0 + and isinstance(ram_used := ram_region.get("used"), int) + ): print_size_line("RAM", ram_used, ram_total) else: - # Every chip has a DRAM or DIRAM region, so this firing usually - # means the esp_idf_size json schema changed - _LOGGER.warning("Skipping RAM summary: no DRAM/DIRAM region in %s", size_json) + _LOGGER.warning( + "Skipping RAM summary: unusable region %s in %s", ram_region, size_json + ) # esp-idf-size >= 2.1 (IDF >= 6.0) reports the exact image size in # json2; older 1.x omits it, so derive the same figure from the ELF. flash_used = data.get("total_size") - try: - if flash_used is None: - _LOGGER.debug( - "No total_size in %s, deriving from %s", size_json, firmware_elf - ) + if not (isinstance(flash_used, int) and flash_used > 0): + _LOGGER.debug("No total_size in %s, deriving from %s", size_json, firmware_elf) + try: flash_used = _image_size_from_elf(firmware_elf) + except (OSError, ValueError) as e: + # The ELF must be present and well formed after a successful build + _LOGGER.warning("Skipping Flash summary: %s", e) + return + try: app_size = _find_app_partition_size(partitions_csv) - except FileNotFoundError as e: - # The ELF must exist after a successful build; a missing - # partitions.csv raises ValueError and stays at debug level. - _LOGGER.warning("Skipping Flash summary: %s", e) - return - except (OSError, ValueError) as e: + except ValueError as e: _LOGGER.debug("Skipping Flash summary: %s", e) return print_size_line("Flash", flash_used, app_size) diff --git a/tests/unit_tests/test_size_summary.py b/tests/unit_tests/test_size_summary.py index cebf16eca5..be4b96f1ba 100644 --- a/tests/unit_tests/test_size_summary.py +++ b/tests/unit_tests/test_size_summary.py @@ -134,7 +134,9 @@ def test_print_summary_s3_falls_back_to_diram( def test_print_summary_skips_when_diram_total_collapses( - tmp_path: Path, capsys: pytest.CaptureFixture[str] + tmp_path: Path, + capsys: pytest.CaptureFixture[str], + caplog: pytest.LogCaptureFixture, ) -> None: """A zero-size region drops the RAM line rather than divide by zero.""" size_json = _write_size_json( @@ -147,6 +149,7 @@ def test_print_summary_skips_when_diram_total_collapses( _print_summary_ram_only(tmp_path, size_json) out = capsys.readouterr().out assert "RAM:" not in out + assert "unusable region" in caplog.text def test_print_summary_handles_missing_json( @@ -158,12 +161,18 @@ def test_print_summary_handles_missing_json( def test_print_summary_handles_no_layout( - tmp_path: Path, capsys: pytest.CaptureFixture[str] + tmp_path: Path, + capsys: pytest.CaptureFixture[str], + caplog: pytest.LogCaptureFixture, ) -> None: - """A size json without ``layout`` still doesn't crash.""" + """A size json without ``layout`` warns so schema drift is visible.""" size_json = _write_size_json(tmp_path, {"version": "1.1"}) _print_summary_ram_only(tmp_path, size_json) assert capsys.readouterr().out == "" + assert any( + r.levelname == "WARNING" and "no DRAM/DIRAM region" in r.message + for r in caplog.records + ) def test_print_summary_flash_line_prefers_total_size( @@ -202,6 +211,22 @@ def test_print_summary_flash_line_derives_from_elf( assert "(used 724215 bytes from 1835008 bytes)" in out +def test_print_summary_flash_falls_back_on_bad_total_size( + tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """A zero or non-int total_size falls back to the ELF instead of + printing a used-0-bytes line CI would read as a real measurement.""" + data = _s3_size_data() + data["total_size"] = 0 + size_json = _write_size_json(tmp_path, data) + partitions = _write_partitions(tmp_path) + firmware_elf = tmp_path / "firmware.elf" + firmware_elf.write_bytes(_elf_bytes([(1, 0x2, 4096)])) + print_summary(size_json, partitions, firmware_elf) + out = capsys.readouterr().out + assert "(used 4096 bytes from 1835008 bytes)" in out + + _GOOD_ELF = _elf_bytes([(1, 0x2, 1024)]) @@ -224,6 +249,7 @@ def test_print_summary_skips_flash_on_bad_input( with_partitions: bool, tmp_path: Path, capsys: pytest.CaptureFixture[str], + caplog: pytest.LogCaptureFixture, ) -> None: """An unusable ELF or missing partitions.csv skips the Flash line, not the RAM line.""" size_json = _write_size_json(tmp_path, _s3_size_data()) @@ -236,3 +262,10 @@ def test_print_summary_skips_flash_on_bad_input( out = capsys.readouterr().out assert "RAM:" in out assert "Flash:" not in out + # ELF problems warn (anomaly after a successful build); a missing + # partitions.csv stays at debug + warned = any( + r.levelname == "WARNING" and "Skipping Flash summary" in r.message + for r in caplog.records + ) + assert warned == with_partitions