From ae8dabc41adca202cd7f6ac380672ddc05123fe5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Mar 2026 22:25:18 -1000 Subject: [PATCH] Add test for run_external_command with line_callbacks --- tests/unit_tests/test_util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/unit_tests/test_util.py b/tests/unit_tests/test_util.py index 0f006c9185..74d18eb677 100644 --- a/tests/unit_tests/test_util.py +++ b/tests/unit_tests/test_util.py @@ -523,3 +523,28 @@ def test_redirect_text_incomplete_line_buffered() -> None: redirect.write(" line\n") assert len(results) == 1 assert results[0] == "partial line" + + +def test_run_external_command_line_callbacks(capsys: pytest.CaptureFixture) -> None: + """Test that run_external_command passes line_callbacks to RedirectText.""" + results: list[str] = [] + + def callback(line: str) -> str | None: + results.append(line) + if "hello" in line: + return "CALLBACK FIRED\n" + return None + + def fake_main() -> int: + print("hello world") + return 0 + + with patch("esphome.core.CORE") as mock_core: + mock_core.dashboard = False + rc = util.run_external_command(fake_main, "fake", line_callbacks=[callback]) + + assert rc == 0 + assert len(results) == 1 + assert "hello world" in results[0] + captured = capsys.readouterr() + assert "CALLBACK FIRED" in captured.out