diff --git a/esphome/helpers.py b/esphome/helpers.py index 8a7c1c70f9..7aa1a9a88c 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -555,10 +555,8 @@ def write_file_if_changed(path: Path, text: str) -> bool: try: src_content = path.read_text(encoding="utf-8") except UnicodeDecodeError as err: - # A damaged existing file must be replaced, not abort the very - # regeneration that would fix it. Only decode failures recover: - # an OSError (permissions, I/O) may hide an intact file and - # propagates below like it always did. + # Replace a damaged file rather than abort the regeneration that + # fixes it; an OSError may hide an intact file, so it still raises _LOGGER.warning("Replacing damaged file %s: %s", path, err) with suppress(OSError): path.unlink(missing_ok=True) diff --git a/tests/unit_tests/test_helpers.py b/tests/unit_tests/test_helpers.py index 088aa4bc97..eaa7d5a8dc 100644 --- a/tests/unit_tests/test_helpers.py +++ b/tests/unit_tests/test_helpers.py @@ -256,8 +256,7 @@ class Test_write_file_if_changed: def test_damaged_existing_file_is_replaced( self, tmp_path: Path, caplog: pytest.LogCaptureFixture ): - """A non-UTF-8 existing file is logged and overwritten; aborting - would block the very regeneration that fixes it.""" + """A non-UTF-8 existing file is logged and overwritten.""" dst = tmp_path / "generated.txt" dst.write_bytes(b"\xff\xfe") @@ -267,8 +266,7 @@ class Test_write_file_if_changed: assert "Replacing damaged file" in caplog.text def test_unreadable_existing_file_still_raises(self, tmp_path: Path): - """An OSError on the comparison read may hide an intact file, so it - propagates as EsphomeError instead of unlinking.""" + """An OSError on the comparison read still raises EsphomeError.""" dst = tmp_path / "generated.txt" dst.write_text("intact")