diff --git a/.gitignore b/.gitignore index c87b890..9959510 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,11 @@ build/ dist/ msgcheck.egg-info/ nose-*.egg/ +.ruff_cache/ +.pytest_cache/ +.ty_cache/ +.coverage/ +.tox +.venv/ +venv +__pycache__/ \ No newline at end of file diff --git a/src/msgcheck/msgcheck.py b/src/msgcheck/msgcheck.py index 3f3997f..367c997 100644 --- a/src/msgcheck/msgcheck.py +++ b/src/msgcheck/msgcheck.py @@ -82,6 +82,12 @@ def msgcheck_parser() -> argparse.ArgumentParser: action="store_true", help="check fuzzy strings", ) + parser.add_argument( + "-F", + "--error-on-fuzzy", + action="store_true", + help="raise an error if fuzzy strings are found", + ) parser.add_argument( "-n", "--check-noqa", @@ -197,6 +203,7 @@ def msgcheck_check_files(args: argparse.Namespace) -> list[PoFileReport]: "no_punct", "no_whitespace", "no_whitespace_eol", + "error_on_fuzzy", ): if args.__dict__[option]: po_check.set_check(option.lstrip("no_"), not option.startswith("no_")) diff --git a/src/msgcheck/po.py b/src/msgcheck/po.py index 17752c4..6453c0a 100644 --- a/src/msgcheck/po.py +++ b/src/msgcheck/po.py @@ -515,6 +515,7 @@ def __init__(self) -> None: "whitespace": True, "whitespace_eol": True, "extract": False, + "error_on_fuzzy": False, } # spelling options self.spelling: str | None = None @@ -614,6 +615,26 @@ def check_pofile(self, po_file: PoFile) -> list[PoReport]: """ reports: list[PoReport] = [] + # check for fuzzy strings if error_on_fuzzy is enabled + if self.checks["error_on_fuzzy"]: + fuzzy_msgs = [msg for msg in po_file.msgs if msg.fuzzy] + if fuzzy_msgs: + for msg in fuzzy_msgs: + # Get the first message for display + mid = msg.messages[0][0] if msg.messages else "" + mstr = msg.messages[0][1] if msg.messages else "" + reports.append( + PoReport( + "fuzzy string found", + "fuzzy", + po_file.filename, + msg.line, + mid, + mstr, + fuzzy=True, + ), + ) + # build list of checkers (if spelling is enabled) checker = self._get_language_checker(po_file, reports) diff --git a/tests/test_msgcheck.py b/tests/test_msgcheck.py index 361a2b8..66aa200 100644 --- a/tests/test_msgcheck.py +++ b/tests/test_msgcheck.py @@ -155,6 +155,42 @@ def test_checks_fuzzy() -> None: assert len(result[0]) == 10 +def test_error_on_fuzzy() -> None: + """Test error_on_fuzzy option that raises an error when fuzzy strings are found.""" + po_check = PoCheck() + po_check.set_check("error_on_fuzzy") + result = po_check.check_files([local_path("fr_errors.po")]) + + # be sure we have one file in result + assert len(result) == 1 + + # the file should have an error for the fuzzy string + assert len(result[0]) == 1 + + # check the error report + report = result[0][0] + assert report.idmsg == "fuzzy" + assert report.message == "fuzzy string found" + assert report.fuzzy is True + assert "fr_errors.po" in report.filename + assert report.line == 58 # Line where the fuzzy string starts + assert report.mid == "Tested 3" + assert report.mstr == "Testé 3." + + +def test_error_on_fuzzy_no_fuzzy_strings() -> None: + """Test error_on_fuzzy option when there are no fuzzy strings.""" + po_check = PoCheck() + po_check.set_check("error_on_fuzzy") + result = po_check.check_files([local_path("fr.po")]) + + # be sure we have one file in result + assert len(result) == 1 + + # the file should have no errors + assert len(result[0]) == 0 + + def test_checks_noqa() -> None: """Test checks on a gettext file including `noqa`-commented lines.""" po_check = PoCheck()