Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Fix compounding error causing false negatives, hope it won't raise fa…
Browse files Browse the repository at this point in the history
…lse positives.
  • Loading branch information
JulienPalard committed Oct 11, 2020
1 parent 863b7a4 commit e7bc8f8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
16 changes: 9 additions & 7 deletions pospell.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ def parse_args():


def spell_check(
po_files, personal_dict, language, drop_capitalized=False, debug_only=False
po_files,
personal_dict=None,
language="en_EN",
drop_capitalized=False,
debug_only=False,
):
"""Check for spelling mistakes in the files po_files (po format,
containing restructuredtext), for the given language.
Expand All @@ -273,12 +277,10 @@ def spell_check(
if not output.stdout:
continue # No errors :)
line_of_words = defaultdict(set)
for line, text in enumerate(text_for_hunspell.split("\n"), start=1):
for word in text.split():
line_of_words[word].add(line)
for misspelled_word in set(output.stdout.split("\n")):
for line_number in line_of_words[misspelled_word]:
errors.append((po_file, line_number, misspelled_word))
for misspelled_word in {word for word in output.stdout.split("\n") if word}:
for line_number, line in enumerate(text_for_hunspell.split("\n"), start=1):
if misspelled_word in line:
errors.append((po_file, line_number, misspelled_word))
errors.sort()
for error in errors:
print(":".join(str(token) for token in error))
Expand Down
68 changes: 67 additions & 1 deletion tests/test_pospell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pospell import clear, strip_rst
from types import SimpleNamespace
from pathlib import Path

from pospell import clear, strip_rst, spell_check


def test_clear():
Expand Down Expand Up @@ -70,3 +73,66 @@ def test_clear_accronyms():
assert "HTTP" not in clear("Yes HTTP is great.", drop_capitalized)

assert "PEPs" not in clear("Ho. PEPs good.", drop_capitalized)


def test_with_an_error(tmp_path, capsys, monkeypatch):
import subprocess

tmp_path = Path(tmp_path)
monkeypatch.setattr(
subprocess,
"run",
lambda *args, **kwargs: SimpleNamespace(stdout="Pyhton\n"),
)
(tmp_path / "test.po").write_text(
"""
msgid "Python FTW!"
msgstr "Gloire à Pyhton !"
"""
)
assert spell_check([tmp_path / "test.po"]) > 0
captured = capsys.readouterr()
assert "Pyhton" in captured.out
assert not captured.err


def test_with_no_error(tmp_path, capsys, monkeypatch):
import subprocess

tmp_path = Path(tmp_path)
monkeypatch.setattr(
subprocess,
"run",
lambda *args, **kwargs: SimpleNamespace(stdout=""),
)
(tmp_path / "test.po").write_text(
"""
msgid "Python FTW!"
msgstr "Gloire à Python !"
"""
)
assert spell_check([tmp_path / "test.po"]) == 0
captured = capsys.readouterr()
assert not captured.out
assert not captured.err


def test_issue_19(tmp_path, capsys, monkeypatch):
import subprocess

tmp_path = Path(tmp_path)
monkeypatch.setattr(
subprocess,
"run",
lambda *args, **kwargs: SimpleNamespace(stdout="pubb\nsubb\n"),
)
(tmp_path / "test.po").write_text(
"""
msgid "pubb/subb yo"
msgstr "pubb/subb"
"""
)
assert spell_check([tmp_path / "test.po"]) > 0
captured = capsys.readouterr()
assert "pubb" in captured.out
assert not captured.err

0 comments on commit e7bc8f8

Please sign in to comment.