[core] Improve clean-all with no arguments (#15184)

This commit is contained in:
Jonathan Swoboda
2026-03-26 13:39:19 -04:00
committed by GitHub
parent 02e23eb386
commit c2456409bd
2 changed files with 51 additions and 0 deletions
+10
View File
@@ -476,6 +476,16 @@ def clean_all(configuration: list[str]):
data_dirs.append(Path(env_data_dir))
if env_build_path := os.environ.get("ESPHOME_BUILD_PATH"):
data_dirs.append(Path(env_build_path))
if not data_dirs:
# No config files or known data dirs, check current directory
cwd_esphome = Path.cwd() / ".esphome"
if cwd_esphome.is_dir():
data_dirs.append(cwd_esphome)
else:
_LOGGER.warning(
"No configuration files specified and no .esphome directory found in current directory. "
"Pass YAML files or a configuration directory to clean build artifacts."
)
# Clean build dir
for dir in data_dirs:
+41
View File
@@ -990,6 +990,47 @@ def test_clean_all_ignores_empty_env_vars(
assert marker.exists()
@patch("esphome.writer.CORE")
def test_clean_all_no_args_with_esphome_dir(
mock_core: MagicMock,
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test clean_all with no args cleans .esphome in cwd."""
esphome_dir = tmp_path / ".esphome"
esphome_dir.mkdir()
(esphome_dir / "dummy.txt").write_text("x")
from esphome.writer import clean_all
with (
caplog.at_level("INFO"),
patch("esphome.writer.Path.cwd", return_value=tmp_path),
):
clean_all([])
assert esphome_dir.exists()
assert not (esphome_dir / "dummy.txt").exists()
@patch("esphome.writer.CORE")
def test_clean_all_no_args_no_esphome_dir(
mock_core: MagicMock,
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test clean_all with no args and no .esphome dir warns."""
from esphome.writer import clean_all
with (
caplog.at_level("WARNING"),
patch("esphome.writer.Path.cwd", return_value=tmp_path),
):
clean_all([])
assert "No configuration files specified" in caplog.text
@patch("esphome.writer.CORE")
def test_clean_all(
mock_core: MagicMock,