From e7bc8f86ee1af39a9f2536ff25cca04dbec51686 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Sun, 11 Oct 2020 16:04:26 +0200 Subject: [PATCH] Fix compounding error causing false negatives, hope it won't raise false positives. --- pospell.py | 16 +++++----- tests/test_pospell.py | 68 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/pospell.py b/pospell.py index fa8ab56..57c3018 100644 --- a/pospell.py +++ b/pospell.py @@ -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. @@ -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)) diff --git a/tests/test_pospell.py b/tests/test_pospell.py index 3b9f0d2..592c3d2 100644 --- a/tests/test_pospell.py +++ b/tests/test_pospell.py @@ -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(): @@ -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