diff --git a/app/tasks/minetestcheck/translation.py b/app/tasks/minetestcheck/translation.py index e1100594..86448a8e 100644 --- a/app/tasks/minetestcheck/translation.py +++ b/app/tasks/minetestcheck/translation.py @@ -29,7 +29,6 @@ class Translation: self.entries = entries - def parse_tr(filepath: str) -> Translation: entries = {} filename = os.path.basename(filepath) @@ -40,13 +39,13 @@ def parse_tr(filepath: str) -> Translation: language = filename_parts[-2] textdomain = ".".join(filename_parts[0:-2]) - with open(filepath, "r", encoding='utf-8') as existing_file: + with open(filepath, "r", encoding="utf-8") as existing_file: lines = existing_file.readlines() line_index = 0 while line_index < len(lines): line = lines[line_index].rstrip('\n') - if line == "": + if line.strip() == "": pass # Comment lines @@ -63,6 +62,7 @@ def parse_tr(filepath: str) -> Translation: had_equals = False source = "" current_part = "" + next_variable = 1 while i < len(line): if line[i] == "@": if i + 1 < len(line): @@ -76,6 +76,17 @@ def parse_tr(filepath: str) -> Translation: current_part += "\n" elif code.isdigit(): current_part += "@" + code + if had_equals: + if int(code) >= next_variable: + raise SyntaxError( + f"Line {line_index + 1}: Unknown argument @{code} in translated string") + else: + if int(code) != next_variable: + raise SyntaxError( + f"Line {line_index + 1}: Arguments out of order in source, found @{code} and expected @{next_variable}." + + "Arguments in source must be in increasing order, without gaps or repetitions, starting from 1") + + next_variable += 1 else: raise SyntaxError(f"Line {line_index + 1}: Unknown escape character: {code}") diff --git a/app/tests/unit/bad_args.fr.tr b/app/tests/unit/bad_args.fr.tr new file mode 100644 index 00000000..358f6d65 --- /dev/null +++ b/app/tests/unit/bad_args.fr.tr @@ -0,0 +1 @@ +Some @1 args @5=Some @1 args @5 diff --git a/app/tests/unit/test_translation.py b/app/tests/unit/test_translation.py index bcb9211f..c34f58bd 100644 --- a/app/tests/unit/test_translation.py +++ b/app/tests/unit/test_translation.py @@ -59,3 +59,23 @@ def test_parses_tr_error_on_bad_escape(): parse_tr(filepath) assert str(e.value) == "Line 1: Unknown escape character: x" + + +def test_parses_tr_error_on_bad_args(): + dirname = os.path.dirname(__file__) + filepath = os.path.join(dirname, "bad_args.fr.tr") + + with pytest.raises(SyntaxError) as e: + parse_tr(filepath) + + assert "Line 1: Arguments out of order in source, found @5 and expected @2." in str(e.value) + + +def test_parses_tr_error_on_unknown_arg(): + dirname = os.path.dirname(__file__) + filepath = os.path.join(dirname, "unknown_arg.fr.tr") + + with pytest.raises(SyntaxError) as e: + parse_tr(filepath) + + assert str(e.value) == "Line 1: Unknown argument @2 in translated string" diff --git a/app/tests/unit/unknown_arg.fr.tr b/app/tests/unit/unknown_arg.fr.tr new file mode 100644 index 00000000..efe6fcdb --- /dev/null +++ b/app/tests/unit/unknown_arg.fr.tr @@ -0,0 +1 @@ +Some @1 arg=Some @2 arg