From 0a9debf901a4b74be1f2f985e49238c63b353375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Korkmaz?= Date: Mon, 19 Jan 2026 02:55:00 +0300 Subject: [PATCH] feat(#16): raise error when fuzzy detected --- src/msgcheck/po.py | 41 +++++++++++++++++++++++++++++++++++------ tests/test_msgcheck.py | 11 +++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/msgcheck/po.py b/src/msgcheck/po.py index 17752c4..9f97c63 100644 --- a/src/msgcheck/po.py +++ b/src/msgcheck/po.py @@ -352,6 +352,31 @@ def check_spelling(self, spelling: str, checkers: list[SpellChecker]) -> list[Po ) return errors + def check_fuzzy_string(self) -> list[PoReport]: + """Check if message is marked as fuzzy. + + Return a list with errors detected. + """ + if not self.fuzzy: + return [] + errors: list[PoReport] = [] + # Report fuzzy strings (skip header entries with empty msgid) + for mid, mstr in self.messages: + if mid: # Skip header entries (empty msgid) + errors.append( + PoReport( + "fuzzy string found", + "fuzzy", + self.filename, + self.line, + mid, + mstr, + fuzzy=True, + ), + ) + break + return errors + class Checker: """Messages checker.""" @@ -596,6 +621,8 @@ def check_msg( if mid and mstr: reports.append(PoReport(mstr, "extract")) else: + if self.checks["fuzzy"]: + reports += msg.check_fuzzy_string() if self.checks["lines"]: reports += msg.check_lines() if self.checks["punct"]: @@ -629,7 +656,7 @@ def check_pofile(self, po_file: PoFile) -> list[PoReport]: return reports - def check_file(self, filename: str) -> PoFileReport : + def check_file(self, filename: str) -> PoFileReport: """Check compilation and translations in a PO file.""" po_file = PoFile(filename) report = PoFileReport(po_file.filename) @@ -660,11 +687,13 @@ def check_files(self, files: list[str]) -> list[PoFileReport]: for path in files: if Path(path).is_dir(): for root, _, filenames in os.walk(path): - result.extend([ - self.check_file(str(Path(root) / filename)) - for filename in filenames - if filename.endswith(".po") - ]) + result.extend( + [ + self.check_file(str(Path(root) / filename)) + for filename in filenames + if filename.endswith(".po") + ], + ) else: result.append(self.check_file(path)) return result diff --git a/tests/test_msgcheck.py b/tests/test_msgcheck.py index 361a2b8..4e64bf1 100644 --- a/tests/test_msgcheck.py +++ b/tests/test_msgcheck.py @@ -151,8 +151,15 @@ def test_checks_fuzzy() -> None: # be sure we have one file in result assert len(result) == 1 - # the file has 11 errors (with the fuzzy string) - assert len(result[0]) == 10 + # the file has 11 errors (10 regular + 1 fuzzy string error) + assert len(result[0]) == 11 + + # verify that fuzzy string is reported as an error + fuzzy_errors = [report for report in result[0] if report.idmsg == "fuzzy"] + assert len(fuzzy_errors) == 1 + assert fuzzy_errors[0].message == "fuzzy string found" + assert fuzzy_errors[0].fuzzy is True + assert fuzzy_errors[0].line == 58 # Line number of msgid (fuzzy comment is at line 57) def test_checks_noqa() -> None: