diff --git a/esphome/espidf/size_summary.py b/esphome/espidf/size_summary.py index 7a5305ff0c..7e3369fc3f 100644 --- a/esphome/espidf/size_summary.py +++ b/esphome/espidf/size_summary.py @@ -94,6 +94,12 @@ def print_summary(size_json: Path, partitions_csv: Path | None) -> None: _LOGGER.debug("Skipping size summary: %s", e) return + if not isinstance(data, dict): + # Valid JSON that is not an object (truncated tool output) must + # not raise past a build that already linked + _LOGGER.debug("Skipping size summary: unexpected shape in %s", size_json) + return + memory_types = data.get("memory_types", {}) ram_region = memory_types.get("DRAM") or memory_types.get("DIRAM") or {} ram_used = ram_region.get("used") @@ -106,7 +112,12 @@ def print_summary(size_json: Path, partitions_csv: Path | None) -> None: return try: app_size = _find_app_partition_size(partitions_csv) - except ValueError as e: + except (ValueError, OSError) as e: _LOGGER.debug("Skipping Flash summary: %s", e) return + if app_size <= 0: + # A malformed partition row parses to 0; a 0% bar would feed CI's + # memory-impact extraction fabricated data + _LOGGER.debug("Skipping Flash summary: app partition size is 0") + return print(f"Flash: {_format_bar(image_size, app_size)}") diff --git a/tests/unit_tests/test_size_summary.py b/tests/unit_tests/test_size_summary.py index 933be88476..c5e0708539 100644 --- a/tests/unit_tests/test_size_summary.py +++ b/tests/unit_tests/test_size_summary.py @@ -126,3 +126,38 @@ def test_print_summary_handles_no_memory_types( size_json = _write_size_json(tmp_path, {"image_size": 0}) print_summary(size_json, partitions_csv=None) assert capsys.readouterr().out == "" + + +def test_print_summary_non_dict_json_is_skipped(tmp_path, capsys) -> None: + """Valid JSON that is not an object must not raise past a build that + already linked.""" + size_json = tmp_path / "size.json" + size_json.write_text("[]") + print_summary(size_json, tmp_path / "partitions.csv") + assert capsys.readouterr().out == "" + + +def test_print_summary_unreadable_partitions_is_skipped(tmp_path, capsys) -> None: + """An OSError reading the partition table is a skipped summary, not a + failed build.""" + size_json = tmp_path / "size.json" + size_json.write_text( + '{"memory_types": {"DRAM": {"used": 1, "size": 2}}, "image_size": 100}' + ) + print_summary(size_json, tmp_path / "missing" / "partitions.csv") + out = capsys.readouterr().out + assert "RAM:" in out and "Flash:" not in out + + +def test_print_summary_zero_app_partition_is_skipped(tmp_path, capsys) -> None: + """A malformed partition row parsing to 0 must not render a 0% bar for + CI's memory-impact extraction to ingest.""" + size_json = tmp_path / "size.json" + size_json.write_text( + '{"memory_types": {"DRAM": {"used": 1, "size": 2}}, "image_size": 100}' + ) + partitions = tmp_path / "partitions.csv" + partitions.write_text("app0, app, ota_0, 0x10000, ,\n") + print_summary(size_json, partitions) + out = capsys.readouterr().out + assert "RAM:" in out and "Flash:" not in out