mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
fix(gamelist): drop partial results when a document is unparseable
Streaming means valid entries are consumed before the parser reaches any damage further down the file, so a truncated or corrupt gamelist returned whatever had been read so far. The previous full-document parse rejected such a file outright and imported nothing. Clear the accumulated entries when a parse error escapes both the incremental pass and the fallback, restoring that behavior. Errors raised while building an entry still keep what was read, as before. Also filter the fallback path to game/folder elements so it matches the incremental path, and hoist the lowercase filename and suffix tuple out of the per-extension loop in exclude_single_files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TLTDjv3niosCXZWtjKELK2
This commit is contained in:
@@ -272,11 +272,12 @@ class FSHandler:
|
||||
excluded_extensions = cnfg.EXCLUDED_SINGLE_EXT
|
||||
excluded_names = cnfg.EXCLUDED_SINGLE_FILES
|
||||
|
||||
# Built once rather than per file, and endswith takes the whole tuple.
|
||||
excluded_suffixes = tuple(f".{ext}" for ext in excluded_extensions)
|
||||
|
||||
def is_excluded(file_name: str) -> bool:
|
||||
# Check whether the filename ends with any excluded extension entry.
|
||||
if any(
|
||||
file_name.lower().endswith("." + ext) for ext in excluded_extensions
|
||||
):
|
||||
if file_name.lower().endswith(excluded_suffixes):
|
||||
return True
|
||||
|
||||
# Check if the file name matches a pattern in the excluded list.
|
||||
|
||||
@@ -377,7 +377,9 @@ class GamelistHandler(MetadataHandler):
|
||||
xml_content = gamelist_path.read_text(encoding="utf-8", errors="replace")
|
||||
xml_content = ALTERNATIVE_EMULATOR_SELF_CLOSING_RE.sub("", xml_content)
|
||||
xml_content = ALTERNATIVE_EMULATOR_PAIRED_RE.sub("", xml_content)
|
||||
yield from ET.fromstring(xml_content)
|
||||
for elem in ET.fromstring(xml_content):
|
||||
if elem.tag in ("game", "folder"):
|
||||
yield elem
|
||||
|
||||
def _parse_gamelist_xml(
|
||||
self, gamelist_path: Path, platform: Platform
|
||||
@@ -481,6 +483,10 @@ class GamelistHandler(MetadataHandler):
|
||||
self._gamelist_cache[cache_key] = roms_data
|
||||
except ET.ParseError as e:
|
||||
log.warning(f"Failed to parse gamelist.xml at {gamelist_path}: {e}")
|
||||
# Entries read before the document turned out to be invalid are
|
||||
# dropped, so a corrupt file yields nothing rather than a partial
|
||||
# import that silently looks complete.
|
||||
roms_data.clear()
|
||||
except Exception as e:
|
||||
log.error(f"Error reading gamelist.xml at {gamelist_path}: {e}")
|
||||
|
||||
|
||||
@@ -297,6 +297,52 @@ class TestGamelistHandler:
|
||||
assert roms_data["Tetris.gb"].get("name") == "Tetris"
|
||||
assert roms_data["Zelda.gb"].get("summary") == "Adventure"
|
||||
|
||||
def test_parse_gamelist_truncated_after_valid_entries_returns_nothing(
|
||||
self, tmp_path
|
||||
):
|
||||
"""A corrupt document imports nothing, not the part read before the error.
|
||||
|
||||
Entries are streamed, so valid games are consumed before the parser
|
||||
reaches the damage.
|
||||
"""
|
||||
gamelist_path = tmp_path / "gamelist.xml"
|
||||
gamelist_path.write_text(
|
||||
"""<?xml version="1.0"?>
|
||||
<gameList>
|
||||
<game>
|
||||
<path>./Tetris.gb</path>
|
||||
<name>Tetris</name>
|
||||
</game>
|
||||
<game>
|
||||
<path>./Zelda.gb</path>
|
||||
<name>Zelda</name>
|
||||
</game>
|
||||
<game>
|
||||
<path>./Truncated.gb</path>
|
||||
<name>Trunca""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
handler = GamelistHandler()
|
||||
platform = SimpleNamespace(id=4, fs_slug="gb")
|
||||
|
||||
with (
|
||||
patch(
|
||||
"handler.metadata.gamelist_handler.get_preferred_media_types",
|
||||
return_value=[],
|
||||
),
|
||||
patch(
|
||||
"handler.metadata.gamelist_handler.extract_metadata_from_gamelist_rom",
|
||||
return_value=dict(MOCK_METADATA),
|
||||
),
|
||||
):
|
||||
roms_data = handler._parse_gamelist_xml(
|
||||
gamelist_path, cast(Platform, platform)
|
||||
)
|
||||
|
||||
assert roms_data == {}
|
||||
assert 4 not in handler._gamelist_cache
|
||||
|
||||
def test_iter_game_elements_yields_only_game_and_folder(self, tmp_path):
|
||||
gamelist_path = tmp_path / "gamelist.xml"
|
||||
gamelist_path.write_text(
|
||||
@@ -314,3 +360,25 @@ class TestGamelistHandler:
|
||||
tags = [elem.tag for elem in handler._iter_game_elements(gamelist_path)]
|
||||
|
||||
assert tags == ["game", "folder"]
|
||||
|
||||
def test_iter_game_elements_filters_on_the_fallback_path(self, tmp_path):
|
||||
"""The ES-DE sibling forces the fallback, which must filter identically."""
|
||||
gamelist_path = tmp_path / "gamelist.xml"
|
||||
gamelist_path.write_text(
|
||||
"""<?xml version="1.0"?>
|
||||
<alternativeEmulator>
|
||||
<label>Gambatte</label>
|
||||
</alternativeEmulator>
|
||||
<gameList>
|
||||
<provider><System>gb</System></provider>
|
||||
<game><path>./Tetris.gb</path></game>
|
||||
<folder><path>./Sub</path></folder>
|
||||
</gameList>
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
handler = GamelistHandler()
|
||||
tags = [elem.tag for elem in handler._iter_game_elements(gamelist_path)]
|
||||
|
||||
assert tags == ["game", "folder"]
|
||||
|
||||
Reference in New Issue
Block a user