Skip to content

Commit 7edf3ef

Browse files
Ömer Faruk Korkmazflashcode
authored andcommitted
Add option -F (or --error-on-fuzzy) to report fuzzy strings
1 parent c4721aa commit 7edf3ef

4 files changed

Lines changed: 72 additions & 1 deletion

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: 16 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
@@ -618,9 +619,24 @@ def check_pofile(self, po_file: PoFile) -> list[PoReport]:
618619
checker = self._get_language_checker(po_file, reports)
619620

620621
# check all messages
622+
check_error_on_fuzzy = self.checks["error_on_fuzzy"]
621623
check_fuzzy = self.checks["fuzzy"]
622624
check_noqa = self.checks["check_noqa"]
623625
for msg in po_file.msgs:
626+
if check_error_on_fuzzy and msg.fuzzy:
627+
mid = msg.messages[0][0] if msg.messages else ""
628+
mstr = msg.messages[0][1] if msg.messages else ""
629+
reports.append(
630+
PoReport(
631+
"fuzzy string",
632+
"fuzzy",
633+
po_file.filename,
634+
msg.line,
635+
mid,
636+
mstr,
637+
fuzzy=True,
638+
),
639+
)
624640
if msg.noqa and not check_noqa:
625641
continue
626642
if msg.fuzzy and not check_fuzzy:

tests/test_msgcheck.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,50 @@ 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)
154+
# the file has 10 errors (with the fuzzy string)
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+
# disable all tests and enable only "error_on_fuzzy"
162+
po_check.checks = dict.fromkeys(po_check.checks, False)
163+
po_check.set_check("error_on_fuzzy")
164+
result = po_check.check_files([local_path("fr_errors.po")])
165+
166+
# be sure we have one file in result
167+
assert len(result) == 1
168+
169+
# the file has 1 fuzzy string
170+
assert len(result[0]) == 1
171+
172+
# check the error report
173+
report = result[0][0]
174+
assert report.idmsg == "fuzzy"
175+
assert report.message == "fuzzy string"
176+
assert report.fuzzy is True
177+
assert "fr_errors.po" in report.filename
178+
assert report.line == 58 # Line where the fuzzy string starts
179+
assert report.mid == "Tested 3"
180+
assert report.mstr == "Testé 3."
181+
182+
183+
def test_error_on_fuzzy_no_fuzzy_strings() -> None:
184+
"""Test error_on_fuzzy option when there are no fuzzy strings."""
185+
po_check = PoCheck()
186+
# disable all tests and enable only "error_on_fuzzy"
187+
po_check.checks = dict.fromkeys(po_check.checks, False)
188+
po_check.set_check("error_on_fuzzy")
189+
result = po_check.check_files([local_path("fr.po")])
190+
191+
# be sure we have one file in result
192+
assert len(result) == 1
193+
194+
# the file has 10 errors (no fuzzy strings)
195+
assert len(result[0]) == 0
196+
197+
158198
def test_checks_noqa() -> None:
159199
"""Test checks on a gettext file including `noqa`-commented lines."""
160200
po_check = PoCheck()

0 commit comments

Comments
 (0)