-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[symilar] Migrate from getopt to argparse #9731
Changes from 3 commits
e187d5d
c7aac2c
757d123
812664a
ed1b787
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
We migrated ``symilar`` to argparse, from getopt, so the error and help output changed | ||
(for the better). We exit with 2 instead of sometime 1, sometime 2. The error output | ||
is not captured by the runner anymore. It's not possible to use a value for the | ||
boolean options anymore (``--ignore-comments 1`` should become ``--ignore-comments``). | ||
|
||
Refs #9731 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from pathlib import Path | ||
|
||
import pytest | ||
from _pytest.capture import CaptureFixture | ||
|
||
from pylint.checkers import symilar | ||
from pylint.lint import PyLinter | ||
|
@@ -364,13 +365,16 @@ def test_help() -> None: | |
pytest.fail("not system exit") | ||
|
||
|
||
def test_no_args() -> None: | ||
def test_no_args(capsys: CaptureFixture) -> None: | ||
output = StringIO() | ||
with redirect_stdout(output): | ||
try: | ||
symilar.Run([]) | ||
except SystemExit as ex: | ||
assert ex.code == 1 | ||
assert ex.code == 2 | ||
out, err = capsys.readouterr() | ||
assert not out | ||
assert "the following arguments are required: files" in err | ||
else: | ||
pytest.fail("not system exit") | ||
|
||
|
@@ -494,30 +498,30 @@ def test_set_duplicate_lines_to_zero() -> None: | |
assert output.getvalue() == "" | ||
|
||
|
||
@pytest.mark.parametrize("v", ["d"]) | ||
def test_bad_equal_short_form_option(v: str) -> None: | ||
def test_equal_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: | ||
symilar.Run([f"-{v}=0", SIMILAR1, SIMILAR2]) | ||
assert ex.value.code == 2 | ||
assert "invalid literal for int() with base 10: '=0'" in output.getvalue() | ||
symilar.Run(["-d=2", SIMILAR1, SIMILAR2]) | ||
assert ex.value.code == 0 | ||
assert "similar lines in" in output.getvalue() | ||
|
||
|
||
@pytest.mark.parametrize("v", ["i", "d"]) | ||
def test_space_short_form_option(v: str) -> None: | ||
def test_space_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: | ||
symilar.Run([f"-{v} 2", SIMILAR1, SIMILAR2]) | ||
symilar.Run(["-d 2", SIMILAR1, SIMILAR2]) | ||
assert ex.value.code == 0 | ||
assert "similar lines in" in output.getvalue() | ||
|
||
|
||
def test_bad_short_form_option() -> None: | ||
def test_bad_short_form_option(capsys: CaptureFixture) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be out of scope of this PR, but could it be helpful to add a couple more unit tests?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of those test cases are going to fail with an elegant argparse message. We had a lot of unit tests because we were using getopt and doing too much ourselves (no other choices at time of implementation, as argparse was not builtin yet). I added test previously to check that I wasn't changing the behavior with the migration. I don't know if we should remove tests because we trust argparse now, maybe later (?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Makes sense! Yeah, seeing as a lot of this error handling gets offloaded to |
||
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343""" | ||
output = StringIO() | ||
with redirect_stdout(output), pytest.raises(SystemExit) as ex: | ||
symilar.Run(["-j=0", SIMILAR1, SIMILAR2]) | ||
out, err = capsys.readouterr() | ||
assert ex.value.code == 2 | ||
assert "option -j not recognized" in output.getvalue() | ||
assert not out | ||
assert "unrecognized arguments: -j=0" in err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Not sure under what circumstances users might end up seeing the parser's "usage" explanation, but if they might,
help
statements might be helpful here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch !