From 674061c1d36ea9ba4ef5e5073d12dc51e749e183 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Apr 2026 17:31:14 +0000 Subject: [PATCH] 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> --- backend/endpoints/roms/__init__.py | 20 ++++++++++---------- backend/tests/endpoints/roms/test_rom.py | 3 +-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index 6d8a536ba..560cb9e30 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -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) diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index 50c12a80e..fc45acba4 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -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