Skip to content

Commit

Permalink
Add functional test for spelling checker
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed May 11, 2022
1 parent c6022aa commit 4c287ba
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
8 changes: 4 additions & 4 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ def open(self) -> None:

def _check_spelling(self, msgid: str, line: str, line_num: int) -> None:
original_line = line
try:
# The mypy warning is caught by the except statement
initial_space = re.search(r"^\s+", line).regs[0][1] # type: ignore[union-attr]
except (IndexError, AttributeError):
result = re.search(r"^[^\S]\s*", line)
if result is None:
initial_space = 0
else:
initial_space = result.regs[0][1]
if line.strip().startswith("#") and "docstring" not in msgid:
line = line.strip()[1:]
# A ``Filter`` cannot determine if the directive is at the beginning of a line,
Expand Down
34 changes: 31 additions & 3 deletions pylint/testutils/lint_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from _pytest.config import Config

from pylint import checkers
from pylint.config.config_file_parser import _ConfigurationFileParser
from pylint.config.config_initialization import _config_initialization
from pylint.constants import IS_PYPY
from pylint.lint import PyLinter
Expand Down Expand Up @@ -99,9 +100,36 @@ def __init__(
"--exclude_from_minimal_messages_config", default=False
)

_config_initialization(
self._linter, args_list=args, config_file=rc_file, reporter=_test_reporter
)
try:
_config_initialization(
self._linter,
args_list=args,
config_file=rc_file,
reporter=_test_reporter,
)
except SystemExit as exc:
# If the exit occurs because of a missing spelling dictionary
# we just skip the test
config_file_parser = _ConfigurationFileParser(False, self._linter)
config_data, _ = config_file_parser.parse_config_file(
file_path=Path(rc_file)
)
if "spelling-dict" in config_data:
# Check if the exit came from the spelling-dict option
try:
self._linter._arg_parser.parse_known_args(
["--spelling-dict", config_data["spelling-dict"]]
)
except SystemExit as exc_two:
if exc_two.code == 2:
pytest.skip(
f"Need the {config_data['spelling-dict']}"
"enchant dictionary installed for this test."
)
else:
raise exc # pylint: disable=raise-missing-from
else:
raise exc

self._check_end_position = (
sys.version_info >= self._linter.config.min_pyver_end_position
Expand Down
1 change: 1 addition & 0 deletions tests/functional/s/spelling/.test_spelling_dict.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aweirdallowedword
11 changes: 11 additions & 0 deletions tests/functional/s/spelling/spelling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Functional test for speling.""" # [wrong-spelling-in-docstring, wrong-spelling-in-comment]

# mispeled text # [wrong-spelling-in-comment]


def function_name():
"""mispeled text.""" # [wrong-spelling-in-docstring, wrong-spelling-in-comment]


def func():
"""aweirdallowedword."""
5 changes: 5 additions & 0 deletions tests/functional/s/spelling/spelling.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This test will be skipped if there is no en_GB dictionary installed

[SPELLING]
spelling-dict=en_GB
spelling-private-dict-file=tests/functional/s/spelling/.test_spelling_dict.txt
20 changes: 20 additions & 0 deletions tests/functional/s/spelling/spelling.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
wrong-spelling-in-comment:1:0:None:None::"Wrong spelling of a word 'docstring' in a comment:
# [wrong-spelling-in-docstring, wrong-spelling-in-comment]
^^^^^^^^^
Did you mean: ''doc string' or 'doc-string' or 'doctoring' or 'doctrine''?":UNDEFINED
wrong-spelling-in-docstring:1:0:None:None::"Wrong spelling of a word 'speling' in a docstring:
Functional test for speling.
^^^^^^^
Did you mean: ''spelling' or 'spieling' or 'sapling' or 'spewing''?":UNDEFINED
wrong-spelling-in-comment:3:0:None:None::"Wrong spelling of a word 'mispeled' in a comment:
# mispeled text # [wrong-spelling-in-comment]
^^^^^^^^
Did you mean: ''misspelled' or 'misapplied' or 'misled' or 'dispelled''?":UNDEFINED
wrong-spelling-in-comment:7:0:None:None::"Wrong spelling of a word 'docstring' in a comment:
# [wrong-spelling-in-docstring, wrong-spelling-in-comment]
^^^^^^^^^
Did you mean: ''doc string' or 'doc-string' or 'doctoring' or 'doctrine''?":UNDEFINED
wrong-spelling-in-docstring:7:0:None:None::"Wrong spelling of a word 'mispeled' in a docstring:
mispeled text.
^^^^^^^^
Did you mean: ''misspelled' or 'misapplied' or 'misled' or 'dispelled''?":UNDEFINED

0 comments on commit 4c287ba

Please sign in to comment.