mirror of
https://github.com/esphome/esphome.git
synced 2026-10-03 01:35:59 +00:00
Merge branch 'esp8266-native-library-backend' into esp8266-native-build-spec
This commit is contained in:
@@ -7,11 +7,9 @@ import pytest
|
||||
from esphome.build_helpers.size_summary import format_bar, print_size_line
|
||||
|
||||
|
||||
def test_format_bar_non_positive_total_raises() -> None:
|
||||
"""A meaningless "from 0 bytes" bar raises so every caller must skip it."""
|
||||
for total in (0, -1):
|
||||
with pytest.raises(ValueError, match="non-positive size total"):
|
||||
format_bar(0, total)
|
||||
def test_format_bar_zero_total() -> None:
|
||||
"""A zero total must not divide by zero."""
|
||||
assert format_bar(0, 0) == "[ ] 0.0% (used 0 bytes from 0 bytes)"
|
||||
|
||||
|
||||
def test_print_size_line_label_padding(capsys: pytest.CaptureFixture[str]) -> None:
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -71,18 +69,6 @@ def _s3_size_data() -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _dram_size_data(image_size: int = 100) -> dict:
|
||||
return {"memory_types": {"DRAM": {"used": 1, "size": 2}}, "image_size": image_size}
|
||||
|
||||
|
||||
def _write_partitions(
|
||||
tmp_path: Path, size: str, ptype: str = "app", subtype: str = "ota_0"
|
||||
) -> Path:
|
||||
partitions = tmp_path / "partitions.csv"
|
||||
partitions.write_text(f"app0, {ptype}, {subtype}, 0x10000, {size},\n")
|
||||
return partitions
|
||||
|
||||
|
||||
def test_print_summary_esp32_uses_dram(
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
@@ -126,15 +112,11 @@ def test_print_summary_skips_when_diram_total_collapses(
|
||||
|
||||
|
||||
def test_print_summary_handles_missing_json(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
"""Missing size json is non-fatal and prints nothing."""
|
||||
print_summary(tmp_path / "does_not_exist.json", partitions_csv=None)
|
||||
assert capsys.readouterr().out == ""
|
||||
assert "cannot read" in caplog.text
|
||||
assert "Skipping size summary for" not in caplog.text
|
||||
|
||||
|
||||
def test_print_summary_handles_no_memory_types(
|
||||
@@ -144,207 +126,3 @@ 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: Path, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
"""Valid JSON that is not an object must not raise past a linked build."""
|
||||
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: Path, capsys: pytest.CaptureFixture[str], caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""An OSError reading the partition table skips the summary, not the build."""
|
||||
size_json = _write_size_json(tmp_path, _dram_size_data())
|
||||
partitions = _write_partitions(tmp_path, "1M")
|
||||
real_read_text = Path.read_text
|
||||
|
||||
def fail_partitions_read(self: Path, *args: object, **kwargs: object) -> str:
|
||||
if self == partitions:
|
||||
raise OSError("permission denied")
|
||||
return real_read_text(self, *args, **kwargs)
|
||||
|
||||
with patch.object(Path, "read_text", fail_partitions_read):
|
||||
print_summary(size_json, partitions)
|
||||
# An impossible post-build state is the backstop's business
|
||||
out = capsys.readouterr().out
|
||||
assert "RAM:" in out and "Flash:" not in out
|
||||
assert "Skipping size summary for" in caplog.text
|
||||
|
||||
|
||||
def test_print_summary_happy_path_prints_both_bars(
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
"""A well-formed size report and partition table print both bars."""
|
||||
size_json = tmp_path / "size.json"
|
||||
size_json.write_text(
|
||||
'{"memory_types": {"DRAM": {"used": 1000, "size": 2000}}, "image_size": 100000}'
|
||||
)
|
||||
partitions = _write_partitions(tmp_path, "0x180000")
|
||||
print_summary(size_json, partitions)
|
||||
out = capsys.readouterr().out
|
||||
assert "RAM:" in out and "Flash:" in out
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"payload",
|
||||
[
|
||||
{"memory_types": []},
|
||||
{"memory_types": {"DRAM": 5}},
|
||||
{"memory_types": {"DRAM": {"used": "x", "size": "y"}}, "image_size": 1},
|
||||
{"memory_types": {"DRAM": []}},
|
||||
{"memory_types": {"DRAM": {"used": True, "size": True}}},
|
||||
],
|
||||
)
|
||||
def test_print_summary_nested_bad_shapes_never_raise(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
payload: dict,
|
||||
) -> None:
|
||||
"""Corrupt nested shapes hit the named malformed guard, not the blanket."""
|
||||
size_json = _write_size_json(tmp_path, payload)
|
||||
print_summary(size_json, None)
|
||||
# No half-formed bar for CI to scrape; every payload fails before printing
|
||||
assert capsys.readouterr().out == ""
|
||||
assert "malformed memory_types" in caplog.text
|
||||
assert "Skipping size summary for" not in caplog.text
|
||||
|
||||
|
||||
def test_print_summary_absent_region_stays_quiet(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""A well-shaped report without DRAM/DIRAM is a variant difference, not
|
||||
a broken artifact: debug, never a per-build warning."""
|
||||
size_json = _write_size_json(tmp_path, {"memory_types": {}, "image_size": 1})
|
||||
with caplog.at_level(logging.DEBUG, logger="esphome.espidf.size_summary"):
|
||||
print_summary(size_json, None)
|
||||
assert "RAM:" not in capsys.readouterr().out
|
||||
assert "no usable DRAM/DIRAM region" in caplog.text
|
||||
assert not [
|
||||
r for r in caplog.records if r.levelno >= logging.WARNING and "RAM" in r.message
|
||||
]
|
||||
|
||||
|
||||
def test_print_summary_non_numeric_image_size_warns_by_name(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""A non-numeric image_size hits the named guard, not the blanket."""
|
||||
size_json = _write_size_json(
|
||||
tmp_path,
|
||||
{"memory_types": {"DRAM": {"used": 1, "size": 2}}, "image_size": "x"},
|
||||
)
|
||||
print_summary(size_json, _write_partitions(tmp_path, "0x100000"))
|
||||
assert "Flash:" not in capsys.readouterr().out
|
||||
assert "no usable image_size" in caplog.text
|
||||
assert "Skipping size summary for" not in caplog.text
|
||||
|
||||
|
||||
def test_print_summary_blanket_guard_catches_the_rest(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""A genuinely unforeseen failure warns via the blanket backstop and
|
||||
never raises past a linked build."""
|
||||
size_json = _write_size_json(tmp_path, _dram_size_data())
|
||||
with patch(
|
||||
"esphome.espidf.size_summary._flash_bar",
|
||||
side_effect=RuntimeError("unforeseen"),
|
||||
):
|
||||
print_summary(size_json, None)
|
||||
assert "Skipping size summary for" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cell", ["1M", "1048576"], ids=["suffixed", "decimal"])
|
||||
def test_print_summary_suffixed_size_cell(
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str], cell: str
|
||||
) -> None:
|
||||
"""K/M suffixes and plain decimals parse like PlatformIO's rule."""
|
||||
size_json = _write_size_json(tmp_path, _dram_size_data())
|
||||
partitions = tmp_path / "partitions.csv"
|
||||
partitions.write_text(
|
||||
f"# comment row\nshort,row\napp0, app, ota_0, 0x10000, {cell},\n"
|
||||
)
|
||||
print_summary(size_json, partitions)
|
||||
assert "from 1048576 bytes" in capsys.readouterr().out
|
||||
|
||||
|
||||
def test_print_summary_missing_or_appless_partitions_stay_quiet(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""A table without a qualifying app row is a legitimate layout: the
|
||||
Flash line drops at debug, never at warning."""
|
||||
size_json = _write_size_json(tmp_path, _dram_size_data())
|
||||
partitions = _write_partitions(tmp_path, "0x1000", ptype="data", subtype="spiffs")
|
||||
with caplog.at_level(logging.DEBUG, logger="esphome.espidf.size_summary"):
|
||||
print_summary(size_json, partitions)
|
||||
out = capsys.readouterr().out
|
||||
assert "Flash:" not in out
|
||||
# Quiet means debug-logged, not unlogged
|
||||
assert "Skipping Flash summary: no app partition" in caplog.text
|
||||
assert not [r for r in caplog.records if r.levelno >= logging.WARNING]
|
||||
|
||||
|
||||
def test_print_summary_corrupt_size_json_warns(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""The build's own size report failing to parse is a regression signal."""
|
||||
size_json = tmp_path / "size.json"
|
||||
size_json.write_text("not json {{{")
|
||||
print_summary(size_json, None)
|
||||
size_json.write_bytes(b"\xff\xfe\x00")
|
||||
print_summary(size_json, None)
|
||||
assert capsys.readouterr().out == ""
|
||||
# The named arm, not the blanket, for both damage classes
|
||||
assert caplog.text.count("cannot read") == 2
|
||||
assert "Skipping size summary for" not in caplog.text
|
||||
|
||||
|
||||
def test_print_summary_flash_line_matches_ci_extraction(
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
"""The exact padded shape script/ci_memory_impact_extract.py greps."""
|
||||
size_json = _write_size_json(tmp_path, _dram_size_data(image_size=888511))
|
||||
print_summary(size_json, _write_partitions(tmp_path, "0x1C0000"))
|
||||
out = capsys.readouterr().out
|
||||
assert "Flash: [===== ] 48.4% (used 888511 bytes from 1835008 bytes)" in out
|
||||
|
||||
|
||||
def test_print_summary_bad_ram_region_still_prints_flash(
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str], caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""A malformed RAM region cannot suppress a computable Flash line."""
|
||||
size_json = _write_size_json(
|
||||
tmp_path, {"memory_types": {"DRAM": 5}, "image_size": 100}
|
||||
)
|
||||
print_summary(size_json, _write_partitions(tmp_path, "0x100000"))
|
||||
out = capsys.readouterr().out
|
||||
assert "Flash:" in out and "RAM:" not in out
|
||||
assert "malformed memory_types" in caplog.text
|
||||
|
||||
|
||||
def test_print_summary_missing_partitions_named_in_backstop(
|
||||
tmp_path: Path,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""A vanished table is an impossible post-build state; the backstop
|
||||
reports it by name instead of a bare FileNotFoundError."""
|
||||
size_json = _write_size_json(tmp_path, _dram_size_data())
|
||||
print_summary(size_json, tmp_path / "nope.csv")
|
||||
assert "Flash:" not in capsys.readouterr().out
|
||||
assert "partitions.csv not found" in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user