mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[core] Don't remove storage in clean-all (#10921)
This commit is contained in:
		| @@ -1155,7 +1155,9 @@ def parse_args(argv): | ||||
|         "configuration", help="Your YAML configuration file(s).", nargs="+" | ||||
|     ) | ||||
|  | ||||
|     parser_clean_all = subparsers.add_parser("clean-all", help="Clean all files.") | ||||
|     parser_clean_all = subparsers.add_parser( | ||||
|         "clean-all", help="Clean all build and platform files." | ||||
|     ) | ||||
|     parser_clean_all.add_argument( | ||||
|         "configuration", help="Your YAML configuration directory.", nargs="*" | ||||
|     ) | ||||
|   | ||||
| @@ -340,10 +340,15 @@ def clean_all(configuration: list[str]): | ||||
|  | ||||
|     # Clean entire build dir | ||||
|     for dir in configuration: | ||||
|         buid_dir = Path(dir) / ".esphome" | ||||
|         if buid_dir.is_dir(): | ||||
|             _LOGGER.info("Deleting %s", buid_dir) | ||||
|             shutil.rmtree(buid_dir) | ||||
|         build_dir = Path(dir) / ".esphome" | ||||
|         if build_dir.is_dir(): | ||||
|             _LOGGER.info("Cleaning %s", build_dir) | ||||
|             # Don't remove storage as it will cause the dashboard to regenerate all configs | ||||
|             for item in build_dir.iterdir(): | ||||
|                 if item.is_file(): | ||||
|                     item.unlink() | ||||
|                 elif item.name != "storage" and item.is_dir(): | ||||
|                     shutil.rmtree(item) | ||||
|  | ||||
|     # Clean PlatformIO project files | ||||
|     try: | ||||
|   | ||||
| @@ -790,16 +790,21 @@ def test_clean_all( | ||||
|         with caplog.at_level("INFO"): | ||||
|             clean_all([str(config1_dir), str(config2_dir)]) | ||||
|  | ||||
|     # Verify deletions | ||||
|     assert not build_dir1.exists() | ||||
|     assert not build_dir2.exists() | ||||
|     # Verify deletions - .esphome directories remain but contents are cleaned | ||||
|     # The .esphome directory itself is not removed because it may contain storage | ||||
|     assert build_dir1.exists() | ||||
|     assert build_dir2.exists() | ||||
|  | ||||
|     # Verify that files in .esphome were removed | ||||
|     assert not (build_dir1 / "dummy.txt").exists() | ||||
|     assert not (build_dir2 / "dummy.txt").exists() | ||||
|     assert not pio_cache.exists() | ||||
|     assert not pio_packages.exists() | ||||
|     assert not pio_platforms.exists() | ||||
|     assert not pio_core.exists() | ||||
|  | ||||
|     # Verify logging mentions each | ||||
|     assert "Deleting" in caplog.text | ||||
|     assert "Cleaning" in caplog.text | ||||
|     assert str(build_dir1) in caplog.text | ||||
|     assert str(build_dir2) in caplog.text | ||||
|     assert "PlatformIO cache" in caplog.text | ||||
| @@ -808,6 +813,55 @@ def test_clean_all( | ||||
|     assert "PlatformIO core" in caplog.text | ||||
|  | ||||
|  | ||||
| @patch("esphome.writer.CORE") | ||||
| def test_clean_all_preserves_storage( | ||||
|     mock_core: MagicMock, | ||||
|     tmp_path: Path, | ||||
|     caplog: pytest.LogCaptureFixture, | ||||
| ) -> None: | ||||
|     """Test clean_all preserves storage directory.""" | ||||
|     # Create build directory with storage subdirectory | ||||
|     config_dir = tmp_path / "config" | ||||
|     config_dir.mkdir() | ||||
|  | ||||
|     build_dir = config_dir / ".esphome" | ||||
|     build_dir.mkdir() | ||||
|     (build_dir / "dummy.txt").write_text("x") | ||||
|     (build_dir / "other_file.txt").write_text("y") | ||||
|  | ||||
|     # Create storage directory with content | ||||
|     storage_dir = build_dir / "storage" | ||||
|     storage_dir.mkdir() | ||||
|     (storage_dir / "storage.json").write_text('{"test": "data"}') | ||||
|     (storage_dir / "other_storage.txt").write_text("storage content") | ||||
|  | ||||
|     # Call clean_all | ||||
|     from esphome.writer import clean_all | ||||
|  | ||||
|     with caplog.at_level("INFO"): | ||||
|         clean_all([str(config_dir)]) | ||||
|  | ||||
|     # Verify .esphome directory still exists | ||||
|     assert build_dir.exists() | ||||
|  | ||||
|     # Verify storage directory still exists with its contents | ||||
|     assert storage_dir.exists() | ||||
|     assert (storage_dir / "storage.json").exists() | ||||
|     assert (storage_dir / "other_storage.txt").exists() | ||||
|  | ||||
|     # Verify storage contents are intact | ||||
|     assert (storage_dir / "storage.json").read_text() == '{"test": "data"}' | ||||
|     assert (storage_dir / "other_storage.txt").read_text() == "storage content" | ||||
|  | ||||
|     # Verify other files were removed | ||||
|     assert not (build_dir / "dummy.txt").exists() | ||||
|     assert not (build_dir / "other_file.txt").exists() | ||||
|  | ||||
|     # Verify logging mentions deletion | ||||
|     assert "Cleaning" in caplog.text | ||||
|     assert str(build_dir) in caplog.text | ||||
|  | ||||
|  | ||||
| @patch("esphome.writer.CORE") | ||||
| def test_clean_all_platformio_not_available( | ||||
|     mock_core: MagicMock, | ||||
| @@ -833,8 +887,8 @@ def test_clean_all_platformio_not_available( | ||||
|     ): | ||||
|         clean_all([str(config_dir)]) | ||||
|  | ||||
|     # Build dir removed, PlatformIO dirs remain | ||||
|     assert not build_dir.exists() | ||||
|         # Build dir contents cleaned, PlatformIO dirs remain | ||||
|         assert build_dir.exists() | ||||
|     assert pio_cache.exists() | ||||
|  | ||||
|     # No PlatformIO-specific logs | ||||
| @@ -866,4 +920,68 @@ def test_clean_all_partial_exists( | ||||
|  | ||||
|         clean_all([str(config_dir)]) | ||||
|  | ||||
|     assert not build_dir.exists() | ||||
|         assert build_dir.exists() | ||||
|  | ||||
|  | ||||
| @patch("esphome.writer.CORE") | ||||
| def test_clean_all_removes_non_storage_directories( | ||||
|     mock_core: MagicMock, | ||||
|     tmp_path: Path, | ||||
|     caplog: pytest.LogCaptureFixture, | ||||
| ) -> None: | ||||
|     """Test clean_all removes directories other than storage.""" | ||||
|     # Create build directory with various subdirectories | ||||
|     config_dir = tmp_path / "config" | ||||
|     config_dir.mkdir() | ||||
|  | ||||
|     build_dir = config_dir / ".esphome" | ||||
|     build_dir.mkdir() | ||||
|  | ||||
|     # Create files | ||||
|     (build_dir / "file1.txt").write_text("content1") | ||||
|     (build_dir / "file2.txt").write_text("content2") | ||||
|  | ||||
|     # Create storage directory (should be preserved) | ||||
|     storage_dir = build_dir / "storage" | ||||
|     storage_dir.mkdir() | ||||
|     (storage_dir / "storage.json").write_text('{"test": "data"}') | ||||
|  | ||||
|     # Create other directories (should be removed) | ||||
|     cache_dir = build_dir / "cache" | ||||
|     cache_dir.mkdir() | ||||
|     (cache_dir / "cache_file.txt").write_text("cache content") | ||||
|  | ||||
|     logs_dir = build_dir / "logs" | ||||
|     logs_dir.mkdir() | ||||
|     (logs_dir / "log1.txt").write_text("log content") | ||||
|  | ||||
|     temp_dir = build_dir / "temp" | ||||
|     temp_dir.mkdir() | ||||
|     (temp_dir / "temp_file.txt").write_text("temp content") | ||||
|  | ||||
|     # Call clean_all | ||||
|     from esphome.writer import clean_all | ||||
|  | ||||
|     with caplog.at_level("INFO"): | ||||
|         clean_all([str(config_dir)]) | ||||
|  | ||||
|     # Verify .esphome directory still exists | ||||
|     assert build_dir.exists() | ||||
|  | ||||
|     # Verify storage directory and its contents are preserved | ||||
|     assert storage_dir.exists() | ||||
|     assert (storage_dir / "storage.json").exists() | ||||
|     assert (storage_dir / "storage.json").read_text() == '{"test": "data"}' | ||||
|  | ||||
|     # Verify files were removed | ||||
|     assert not (build_dir / "file1.txt").exists() | ||||
|     assert not (build_dir / "file2.txt").exists() | ||||
|  | ||||
|     # Verify non-storage directories were removed | ||||
|     assert not cache_dir.exists() | ||||
|     assert not logs_dir.exists() | ||||
|     assert not temp_dir.exists() | ||||
|  | ||||
|     # Verify logging mentions cleaning | ||||
|     assert "Cleaning" in caplog.text | ||||
|     assert str(build_dir) in caplog.text | ||||
|   | ||||
		Reference in New Issue
	
	Block a user