fix: narrow OSError catch and clean up test mock

Agent-Logs-Url: https://github.com/rommapp/romm/sessions/303f2c27-6b65-41a9-b201-c055142b1edb

Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-03 17:31:14 +00:00
committed by GitHub
parent 394799d7c3
commit 674061c1d3
2 changed files with 11 additions and 12 deletions

View File

@@ -1483,19 +1483,19 @@ async def delete_roms(
await fs_rom_handler.remove_file(file_path=rom_path)
# Clean up empty parent directory if it becomes empty
parent = full_path.parent
try:
if (
parent != fs_rom_handler.base_path
and parent.is_dir()
and not any(parent.iterdir())
):
if (
parent != fs_rom_handler.base_path
and parent.is_dir()
and not any(parent.iterdir())
):
try:
await fs_rom_handler.remove_directory(
str(parent.relative_to(fs_rom_handler.base_path))
)
except OSError as dir_err:
log.warning(
f"Couldn't clean up empty parent directory for {hl(rom.fs_name)}: {dir_err}"
)
except OSError as dir_err:
log.warning(
f"Couldn't clean up empty parent directory for {hl(rom.fs_name)}: {dir_err}"
)
except FileNotFoundError:
error = f"Rom file {hl(rom.fs_name)} not found for platform {hl(rom.platform_display_name, color=BLUE)}[{hl(rom.platform_slug)}]"
log.error(error)

View File

@@ -191,8 +191,7 @@ def test_delete_roms_from_fs_flat_cleans_empty_parent(
mock_path = MagicMock(spec=Path)
mock_path.is_dir.return_value = False
# Parent is not the base_path, is a dir, and is empty
mock_path.parent.__ne__ = lambda self, other: True
# Parent is not the base_path (a MagicMock will not equal a real Path), is a dir, and is empty
mock_path.parent.is_dir.return_value = True
mock_path.parent.__iter__ = lambda self: iter([]) # empty directory
mock_validate_path.return_value = mock_path