Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ build/
dist/
msgcheck.egg-info/
nose-*.egg/
.ruff_cache/
.pytest_cache/
.ty_cache/
.coverage/
.tox
.venv/
venv
__pycache__/
7 changes: 7 additions & 0 deletions src/msgcheck/msgcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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_"))
Expand Down
21 changes: 21 additions & 0 deletions src/msgcheck/po.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
36 changes: 36 additions & 0 deletions tests/test_msgcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading