Skip to content

Commit 0f9d4e4

Browse files
author
Ömer Faruk Korkmaz
committed
feat(#16): raise error when fuzzy detected
1 parent c4721aa commit 0f9d4e4

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ build/
1010
dist/
1111
msgcheck.egg-info/
1212
nose-*.egg/
13+
.ruff_cache/
14+
.pytest_cache/
15+
.ty_cache/
16+
.coverage/
17+
.tox
18+
.venv/
19+
venv
20+
__pycache__/

src/msgcheck/msgcheck.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ def msgcheck_parser() -> argparse.ArgumentParser:
8282
action="store_true",
8383
help="check fuzzy strings",
8484
)
85+
parser.add_argument(
86+
"-F",
87+
"--error-on-fuzzy",
88+
action="store_true",
89+
help="raise an error if fuzzy strings are found",
90+
)
8591
parser.add_argument(
8692
"-n",
8793
"--check-noqa",
@@ -197,6 +203,7 @@ def msgcheck_check_files(args: argparse.Namespace) -> list[PoFileReport]:
197203
"no_punct",
198204
"no_whitespace",
199205
"no_whitespace_eol",
206+
"error_on_fuzzy",
200207
):
201208
if args.__dict__[option]:
202209
po_check.set_check(option.lstrip("no_"), not option.startswith("no_"))

src/msgcheck/po.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def __init__(self) -> None:
515515
"whitespace": True,
516516
"whitespace_eol": True,
517517
"extract": False,
518+
"error_on_fuzzy": False,
518519
}
519520
# spelling options
520521
self.spelling: str | None = None
@@ -614,6 +615,26 @@ def check_pofile(self, po_file: PoFile) -> list[PoReport]:
614615
"""
615616
reports: list[PoReport] = []
616617

618+
# check for fuzzy strings if error_on_fuzzy is enabled
619+
if self.checks["error_on_fuzzy"]:
620+
fuzzy_msgs = [msg for msg in po_file.msgs if msg.fuzzy]
621+
if fuzzy_msgs:
622+
for msg in fuzzy_msgs:
623+
# Get the first message for display
624+
mid = msg.messages[0][0] if msg.messages else ""
625+
mstr = msg.messages[0][1] if msg.messages else ""
626+
reports.append(
627+
PoReport(
628+
"fuzzy string found",
629+
"fuzzy",
630+
po_file.filename,
631+
msg.line,
632+
mid,
633+
mstr,
634+
fuzzy=True,
635+
),
636+
)
637+
617638
# build list of checkers (if spelling is enabled)
618639
checker = self._get_language_checker(po_file, reports)
619640

tests/test_msgcheck.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,42 @@ def test_checks_fuzzy() -> None:
155155
assert len(result[0]) == 10
156156

157157

158+
def test_error_on_fuzzy() -> None:
159+
"""Test error_on_fuzzy option that raises an error when fuzzy strings are found."""
160+
po_check = PoCheck()
161+
po_check.set_check("error_on_fuzzy")
162+
result = po_check.check_files([local_path("fr_errors.po")])
163+
164+
# be sure we have one file in result
165+
assert len(result) == 1
166+
167+
# the file should have an error for the fuzzy string
168+
assert len(result[0]) == 1
169+
170+
# check the error report
171+
report = result[0][0]
172+
assert report.idmsg == "fuzzy"
173+
assert report.message == "fuzzy string found"
174+
assert report.fuzzy is True
175+
assert "fr_errors.po" in report.filename
176+
assert report.line == 58 # Line where the fuzzy string starts
177+
assert report.mid == "Tested 3"
178+
assert report.mstr == "Testé 3."
179+
180+
181+
def test_error_on_fuzzy_no_fuzzy_strings() -> None:
182+
"""Test error_on_fuzzy option when there are no fuzzy strings."""
183+
po_check = PoCheck()
184+
po_check.set_check("error_on_fuzzy")
185+
result = po_check.check_files([local_path("fr.po")])
186+
187+
# be sure we have one file in result
188+
assert len(result) == 1
189+
190+
# the file should have no errors
191+
assert len(result[0]) == 0
192+
193+
158194
def test_checks_noqa() -> None:
159195
"""Test checks on a gettext file including `noqa`-commented lines."""
160196
po_check = PoCheck()

0 commit comments

Comments
 (0)