Skip to content

Commit 0a9debf

Browse files
committed
feat(#16): raise error when fuzzy detected
1 parent c4721aa commit 0a9debf

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/msgcheck/po.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,31 @@ def check_spelling(self, spelling: str, checkers: list[SpellChecker]) -> list[Po
352352
)
353353
return errors
354354

355+
def check_fuzzy_string(self) -> list[PoReport]:
356+
"""Check if message is marked as fuzzy.
357+
358+
Return a list with errors detected.
359+
"""
360+
if not self.fuzzy:
361+
return []
362+
errors: list[PoReport] = []
363+
# Report fuzzy strings (skip header entries with empty msgid)
364+
for mid, mstr in self.messages:
365+
if mid: # Skip header entries (empty msgid)
366+
errors.append(
367+
PoReport(
368+
"fuzzy string found",
369+
"fuzzy",
370+
self.filename,
371+
self.line,
372+
mid,
373+
mstr,
374+
fuzzy=True,
375+
),
376+
)
377+
break
378+
return errors
379+
355380

356381
class Checker:
357382
"""Messages checker."""
@@ -596,6 +621,8 @@ def check_msg(
596621
if mid and mstr:
597622
reports.append(PoReport(mstr, "extract"))
598623
else:
624+
if self.checks["fuzzy"]:
625+
reports += msg.check_fuzzy_string()
599626
if self.checks["lines"]:
600627
reports += msg.check_lines()
601628
if self.checks["punct"]:
@@ -629,7 +656,7 @@ def check_pofile(self, po_file: PoFile) -> list[PoReport]:
629656

630657
return reports
631658

632-
def check_file(self, filename: str) -> PoFileReport :
659+
def check_file(self, filename: str) -> PoFileReport:
633660
"""Check compilation and translations in a PO file."""
634661
po_file = PoFile(filename)
635662
report = PoFileReport(po_file.filename)
@@ -660,11 +687,13 @@ def check_files(self, files: list[str]) -> list[PoFileReport]:
660687
for path in files:
661688
if Path(path).is_dir():
662689
for root, _, filenames in os.walk(path):
663-
result.extend([
664-
self.check_file(str(Path(root) / filename))
665-
for filename in filenames
666-
if filename.endswith(".po")
667-
])
690+
result.extend(
691+
[
692+
self.check_file(str(Path(root) / filename))
693+
for filename in filenames
694+
if filename.endswith(".po")
695+
],
696+
)
668697
else:
669698
result.append(self.check_file(path))
670699
return result

tests/test_msgcheck.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,15 @@ def test_checks_fuzzy() -> None:
151151
# be sure we have one file in result
152152
assert len(result) == 1
153153

154-
# the file has 11 errors (with the fuzzy string)
155-
assert len(result[0]) == 10
154+
# the file has 11 errors (10 regular + 1 fuzzy string error)
155+
assert len(result[0]) == 11
156+
157+
# verify that fuzzy string is reported as an error
158+
fuzzy_errors = [report for report in result[0] if report.idmsg == "fuzzy"]
159+
assert len(fuzzy_errors) == 1
160+
assert fuzzy_errors[0].message == "fuzzy string found"
161+
assert fuzzy_errors[0].fuzzy is True
162+
assert fuzzy_errors[0].line == 58 # Line number of msgid (fuzzy comment is at line 57)
156163

157164

158165
def test_checks_noqa() -> None:

0 commit comments

Comments
 (0)