Skip to content

Commit

Permalink
[symilar] Fix crash when giving bad options to symilar
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jun 11, 2024
1 parent c3e2579 commit e13544f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9343.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a crash in ``symilar`` when the ``-d`` option was not properly recognized.

Closes #9343
14 changes: 11 additions & 3 deletions pylint/checkers/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import warnings
from collections import defaultdict
from collections.abc import Callable, Generator, Iterable, Sequence
from getopt import getopt
from getopt import GetoptError, getopt
from io import BufferedIOBase, BufferedReader, BytesIO
from itertools import chain
from typing import (
Expand Down Expand Up @@ -921,10 +921,18 @@ def Run(argv: Sequence[str] | None = None) -> NoReturn:
ignore_docstrings = False
ignore_imports = False
ignore_signatures = False
opts, args = getopt(list(argv), s_opts, l_opts)
try:
opts, args = getopt(list(argv), s_opts, l_opts)
except GetoptError as e:
print(e)
usage(2)
for opt, val in opts:
if opt in {"-d", "--duplicates"}:
min_lines = int(val)
try:
min_lines = int(val)
except ValueError as e:
print(e)
usage(2)
elif opt in {"-h", "--help"}:
usage()
elif opt in {"-i", "--ignore-comments"}:
Expand Down
29 changes: 29 additions & 0 deletions tests/checkers/unittest_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,32 @@ def test_set_duplicate_lines_to_zero() -> None:
similar.Run(["--duplicates=0", SIMILAR1, SIMILAR2])
assert ex.value.code == 0
assert output.getvalue() == ""


@pytest.mark.parametrize("v", ["d", "i"])
def test_bad_equal_short_form_option(v: str) -> None:
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343"""
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run([f"-{v}=0", SIMILAR1, SIMILAR2])
assert ex.value.code == 2
assert "option -= not recognized" in output.getvalue()


@pytest.mark.parametrize("v", ["i", "d"])
def test_space_short_form_option(v: str) -> None:
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343"""
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run([f"-{v} 2", SIMILAR1, SIMILAR2])
assert ex.value.code == 2
assert "option - not recognized" in output.getvalue()


def test_bad_short_form_option() -> None:
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343"""
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["-j=0", SIMILAR1, SIMILAR2])
assert ex.value.code == 2
assert "option -j not recognized" in output.getvalue()

0 comments on commit e13544f

Please sign in to comment.