From e540d7c1a283c7e64a48dcbbe9b55ef22eadec6f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Apr 2026 17:26:22 +0000 Subject: [PATCH 2/5] fix: handle nested ROM directories in delete endpoint and reorder FS/DB operations 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 | 38 +++++++---- backend/tests/endpoints/roms/test_rom.py | 86 ++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 12 deletions(-) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index 8178eb920..bceb04d62 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -1472,6 +1472,32 @@ async def delete_roms( continue try: + if id in delete_from_fs: + log.info(f"Deleting {hl(rom.fs_name)} from filesystem") + try: + rom_path = f"{rom.fs_path}/{rom.fs_name}" + full_path = fs_rom_handler.validate_path(rom_path) + if full_path.is_dir(): + await fs_rom_handler.remove_directory(rom_path) + else: + await fs_rom_handler.remove_file(file_path=rom_path) + # Clean up empty parent directory if it becomes empty + parent = full_path.parent + if ( + parent != fs_rom_handler.base_path + and parent.is_dir() + and not any(parent.iterdir()) + ): + await fs_rom_handler.remove_directory( + str(parent.relative_to(fs_rom_handler.base_path)) + ) + 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) + errors.append(error) + failed_items += 1 + continue + log.info( f"Deleting {hl(str(rom.name or 'ROM'), color=BLUE)} [{hl(rom.fs_name)}] from database" ) @@ -1484,18 +1510,6 @@ async def delete_roms( f"Couldn't find resources to delete for {hl(str(rom.name or 'ROM'), color=BLUE)}" ) - if id in delete_from_fs: - log.info(f"Deleting {hl(rom.fs_name)} from filesystem") - try: - file_path = f"{rom.fs_path}/{rom.fs_name}" - await fs_rom_handler.remove_file(file_path=file_path) - 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) - errors.append(error) - failed_items += 1 - continue - successful_items += 1 except Exception as e: failed_items += 1 diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index 181ffb445..15664f70c 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -124,6 +124,92 @@ def test_delete_roms(client: TestClient, access_token: str, rom: Rom): assert body["successful_items"] == 1 +@patch( + "endpoints.roms.fs_rom_handler.remove_file", + new_callable=AsyncMock, +) +@patch( + "endpoints.roms.fs_rom_handler.validate_path", +) +def test_delete_roms_from_fs_flat( + mock_validate_path, + mock_remove_file, + client: TestClient, + access_token: str, + rom: Rom, +): + """Test that flat (non-directory) ROM files are deleted from filesystem.""" + from pathlib import Path + from unittest.mock import MagicMock + + mock_path = MagicMock(spec=Path) + mock_path.is_dir.return_value = False + mock_path.parent.is_dir.return_value = False + mock_validate_path.return_value = mock_path + + response = client.post( + "/api/roms/delete", + headers={"Authorization": f"Bearer {access_token}"}, + json={"roms": [rom.id], "delete_from_fs": [rom.id]}, + ) + assert response.status_code == status.HTTP_200_OK + + body = response.json() + assert body["successful_items"] == 1 + assert body["failed_items"] == 0 + mock_remove_file.assert_called_once() + + +@patch( + "endpoints.roms.fs_rom_handler.remove_directory", + new_callable=AsyncMock, +) +@patch( + "endpoints.roms.fs_rom_handler.validate_path", +) +def test_delete_roms_from_fs_nested( + mock_validate_path, + mock_remove_directory, + client: TestClient, + access_token: str, + platform: Platform, +): + """Test that nested (directory) ROMs are deleted using remove_directory.""" + from pathlib import Path + from unittest.mock import MagicMock + + from handler.database import db_rom_handler + from models.rom import Rom + + nested_rom = Rom( + platform_id=platform.id, + name="Nested Game", + slug="nested-game", + fs_name="Nested Game", + fs_name_no_tags="Nested Game", + fs_name_no_ext="Nested Game", + fs_extension="", + fs_path=f"{platform.slug}/roms", + ) + nested_rom = db_rom_handler.add_rom(nested_rom) + + mock_path = MagicMock(spec=Path) + mock_path.is_dir.return_value = True + mock_validate_path.return_value = mock_path + + response = client.post( + "/api/roms/delete", + headers={"Authorization": f"Bearer {access_token}"}, + json={"roms": [nested_rom.id], "delete_from_fs": [nested_rom.id]}, + ) + assert response.status_code == status.HTTP_200_OK + + body = response.json() + assert body["successful_items"] == 1 + assert body["failed_items"] == 0 + mock_remove_directory.assert_called_once() + + def test_update_rom_user_props_with_data_envelope( client: TestClient, access_token: str, rom: Rom ): From 394799d7c3206222bfb58ad39af284d922184119 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Apr 2026 17:28:50 +0000 Subject: [PATCH 3/5] fix: wrap empty parent dir cleanup in try-except and add test coverage for it 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 | 19 +++++---- backend/tests/endpoints/roms/test_rom.py | 52 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index bceb04d62..6d8a536ba 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -1483,13 +1483,18 @@ 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 - if ( - parent != fs_rom_handler.base_path - and parent.is_dir() - and not any(parent.iterdir()) - ): - await fs_rom_handler.remove_directory( - str(parent.relative_to(fs_rom_handler.base_path)) + try: + if ( + parent != fs_rom_handler.base_path + and parent.is_dir() + and not any(parent.iterdir()) + ): + 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 FileNotFoundError: error = f"Rom file {hl(rom.fs_name)} not found for platform {hl(rom.platform_display_name, color=BLUE)}[{hl(rom.platform_slug)}]" diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index 15664f70c..50c12a80e 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -124,6 +124,10 @@ def test_delete_roms(client: TestClient, access_token: str, rom: Rom): assert body["successful_items"] == 1 +@patch( + "endpoints.roms.fs_rom_handler.remove_directory", + new_callable=AsyncMock, +) @patch( "endpoints.roms.fs_rom_handler.remove_file", new_callable=AsyncMock, @@ -134,6 +138,7 @@ def test_delete_roms(client: TestClient, access_token: str, rom: Rom): def test_delete_roms_from_fs_flat( mock_validate_path, mock_remove_file, + mock_remove_directory, client: TestClient, access_token: str, rom: Rom, @@ -158,6 +163,53 @@ def test_delete_roms_from_fs_flat( assert body["successful_items"] == 1 assert body["failed_items"] == 0 mock_remove_file.assert_called_once() + mock_remove_directory.assert_not_called() + + +@patch( + "endpoints.roms.fs_rom_handler.remove_directory", + new_callable=AsyncMock, +) +@patch( + "endpoints.roms.fs_rom_handler.remove_file", + new_callable=AsyncMock, +) +@patch( + "endpoints.roms.fs_rom_handler.validate_path", +) +def test_delete_roms_from_fs_flat_cleans_empty_parent( + mock_validate_path, + mock_remove_file, + mock_remove_directory, + client: TestClient, + access_token: str, + rom: Rom, +): + """Test that empty parent directories are cleaned up after flat ROM file deletion.""" + from pathlib import Path + from unittest.mock import MagicMock + + 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 + mock_path.parent.is_dir.return_value = True + mock_path.parent.__iter__ = lambda self: iter([]) # empty directory + mock_validate_path.return_value = mock_path + + response = client.post( + "/api/roms/delete", + headers={"Authorization": f"Bearer {access_token}"}, + json={"roms": [rom.id], "delete_from_fs": [rom.id]}, + ) + assert response.status_code == status.HTTP_200_OK + + body = response.json() + assert body["successful_items"] == 1 + assert body["failed_items"] == 0 + mock_remove_file.assert_called_once() + # remove_directory should be called to clean up the empty parent dir + mock_remove_directory.assert_called_once() @patch( 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 4/5] 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 From ce42957162e0615a3bbae90aeb2cae3720d9d2c8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Apr 2026 17:33:06 +0000 Subject: [PATCH 5/5] style: use consistent positional args for remove_file call 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index 560cb9e30..622091cdf 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -1480,7 +1480,7 @@ async def delete_roms( if full_path.is_dir(): await fs_rom_handler.remove_directory(rom_path) else: - await fs_rom_handler.remove_file(file_path=rom_path) + await fs_rom_handler.remove_file(rom_path) # Clean up empty parent directory if it becomes empty parent = full_path.parent if (