diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 5266afe00..14a179a1b 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -665,6 +665,8 @@ class FSRomsHandler(FSHandler): entries.append((f_path, file_name, os.stat(Path(f_path, file_name)))) except OSError as exc: log.warning(f"Skipping unreadable file {f_path / file_name}: {exc}") + # Listing order is filesystem-specific; the ROM-level hash must not be. + entries.sort(key=lambda e: Path(e[0], e[1]).as_posix()) return entries async def converto_active(self, platform_slug: str) -> bool: diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index 67252893e..275b075e1 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -1387,6 +1387,26 @@ class TestFSRomsHandler: if test_file.exists(): test_file.unlink() + def test_list_rom_dir_sorts_regardless_of_listing_order( + self, handler: FSRomsHandler, config, tmp_path + ): + rom_dir = tmp_path / "Order Test (World)" + (rom_dir / "sub").mkdir(parents=True) + for name in ("a.bin", "b.bin", "sub/c.bin"): + (rom_dir / name).write_bytes(b"x") + listing = [(rom_dir, "b.bin"), (rom_dir / "sub", "c.bin"), (rom_dir, "a.bin")] + + with patch( + "handler.filesystem.roms_handler.iter_files", return_value=iter(listing) + ): + entries = handler._list_rom_dir(rom_dir, config) + + assert [(p, n) for p, n, _ in entries] == [ + (rom_dir, "a.bin"), + (rom_dir, "b.bin"), + (rom_dir / "sub", "c.bin"), + ] + def test_failed_7z_extraction_hashes_only_the_raw_archive( self, handler: FSRomsHandler, tmp_path ):