tests: Check if all registered languages properly exist

This commit is contained in:
WerWolv 2025-08-17 17:04:06 +02:00
parent 682aae1497
commit 599b339e5d
2 changed files with 34 additions and 6 deletions

View File

@ -30,7 +30,7 @@
"name": "Italian",
"code": "it-IT",
"native_name": "Italiano",
"path": "lang/it_IT.json",
"git ch": "lang/it_IT.json",
"fallback": "en-US"
},
{

View File

@ -7,14 +7,16 @@ import sys
SHOW_UNUSED_LANGS = "--unused" in sys.argv
# use a regex on code to get all "hex.lang.id"_lang occurences
def get_lang_occurences_in_code(path):
def get_lang_occurrences_in_code(path):
for dir, _, files in os.walk(path):
for file in files:
if not os.path.splitext(file)[1] in (".cpp", ".c", ".hpp", ".h"):
continue
filepath = os.path.join(dir, file)
with open(filepath) as file:
with open(filepath, encoding="utf8") as file:
for line_num, line in enumerate(file):
for m in re.finditer('"([^"]*?)"_lang', line):
yield (filepath, line_num+1, m.group(1))
@ -27,7 +29,7 @@ def get_langs(filepath) -> list[str]:
print(f"Warning: no langs file found at {filepath}")
return []
with open(filepath, "r") as file:
with open(filepath, "r", encoding="utf8") as file:
data = json.loads(file.read())
existing_langs = []
@ -37,13 +39,13 @@ def get_langs(filepath) -> list[str]:
return existing_langs
def check_langs(code_path, bonus_langs, specific_langs_path):
print(f"\n--- Checking langs at {code_path}")
print(f"--- Checking langs at {code_path}")
specific_langs = get_langs(specific_langs_path)
unused_langs = specific_langs.copy()
ret = True
for filepath, line, match in get_lang_occurences_in_code(code_path):
for filepath, line, match in get_lang_occurrences_in_code(code_path):
try:
unused_langs.remove(match)
except ValueError:
@ -59,6 +61,31 @@ def check_langs(code_path, bonus_langs, specific_langs_path):
print(unused_lang)
return ret
def check_languages_exist(languages_file_path: str):
languages_folder = os.path.dirname(languages_file_path)
if not os.path.exists(languages_folder):
return True
if not os.path.exists(languages_file_path):
print(f"Error: Languages file '{languages_file_path}' does not exist.")
return False
with open(languages_file_path, "r", encoding="utf8") as file:
try:
data = json.load(file)
if not isinstance(data, list):
print(f"Error: Languages file '{languages_file_path}' is not a valid JSON object.")
return False
for lang in data:
if not os.path.exists(os.path.join(languages_folder, "..", lang['path'])):
print(f"Error: Language file '{lang['path']}' does not exist in '{languages_folder}'.")
return False
return True
except json.JSONDecodeError as e:
print(f"Error: Languages file '{languages_file_path}' is not a valid JSON file. {e}")
return False
ui_langs = get_langs("./plugins/ui/romfs/lang/en_US.json")
@ -72,5 +99,6 @@ for plugin in os.listdir("./plugins"):
if not os.path.isdir(path): continue
exit_ok &= check_langs(path, ui_langs, f"./plugins/{plugin}/romfs/lang/en_US.json")
exit_ok &= check_languages_exist(f"./plugins/{plugin}/romfs/lang/languages.json")
sys.exit(0 if exit_ok else 1)