[core] Add support for passing yaml files to clean-all (#12039)

This commit is contained in:
Jonathan Swoboda
2025-11-24 10:10:24 -05:00
committed by GitHub
parent b4b98505ba
commit 8607a0881d
3 changed files with 39 additions and 2 deletions

View File

@@ -1319,7 +1319,7 @@ def parse_args(argv):
"clean-all", help="Clean all build and platform files."
)
parser_clean_all.add_argument(
"configuration", help="Your YAML configuration directory.", nargs="*"
"configuration", help="Your YAML file or configuration directory.", nargs="*"
)
parser_dashboard = subparsers.add_parser(

View File

@@ -343,7 +343,13 @@ def clean_build(clear_pio_cache: bool = True):
def clean_all(configuration: list[str]):
import shutil
data_dirs = [Path(dir) / ".esphome" for dir in configuration]
data_dirs = []
for config in configuration:
item = Path(config)
if item.is_file() and item.suffix in (".yaml", ".yml"):
data_dirs.append(item.parent / ".esphome")
else:
data_dirs.append(item / ".esphome")
if is_ha_addon():
data_dirs.append(Path("/data"))
if "ESPHOME_DATA_DIR" in os.environ:

View File

@@ -737,6 +737,37 @@ def test_write_cpp_with_duplicate_markers(
write_cpp("// New code")
@patch("esphome.writer.CORE")
def test_clean_all_with_yaml_file(
mock_core: MagicMock,
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test clean_all with a .yaml file uses parent directory."""
# Create config directory with yaml file
config_dir = tmp_path / "config"
config_dir.mkdir()
yaml_file = config_dir / "test.yaml"
yaml_file.write_text("esphome:\n name: test\n")
build_dir = config_dir / ".esphome"
build_dir.mkdir()
(build_dir / "dummy.txt").write_text("x")
from esphome.writer import clean_all
with caplog.at_level("INFO"):
clean_all([str(yaml_file)])
# Verify .esphome directory still exists but contents cleaned
assert build_dir.exists()
assert not (build_dir / "dummy.txt").exists()
# Verify logging mentions the build dir
assert "Cleaning" in caplog.text
assert str(build_dir) in caplog.text
@patch("esphome.writer.CORE")
def test_clean_all(
mock_core: MagicMock,