Skip to content

Added support for less verbose version information #7169

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

Merged
merged 6 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Christian Tismer
Christoph Buelter
Christopher Dignam
Christopher Gilling
Claire Cecil
Claudio Madotto
CrazyMerlyn
Cyrus Maden
Expand Down
1 change: 1 addition & 0 deletions changelog/7128.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`pytest --version` now displays just the pytest version, while `pytest --version --version` displays more verbose information including plugins.
28 changes: 17 additions & 11 deletions src/_pytest/helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ def pytest_addoption(parser):
group.addoption(
"--version",
"-V",
action="store_true",
help="display pytest version and information about plugins.",
action="count",
default=0,
dest="version",
help="display pytest version and information about plugins."
"When given twice, also display information about plugins.",
)
group._addoption(
"-h",
Expand Down Expand Up @@ -116,19 +119,22 @@ def unset_tracing():


def showversion(config):
sys.stderr.write(
"This is pytest version {}, imported from {}\n".format(
pytest.__version__, pytest.__file__
if config.option.version > 1:
sys.stderr.write(
"This is pytest version {}, imported from {}\n".format(
pytest.__version__, pytest.__file__
)
)
)
plugininfo = getpluginversioninfo(config)
if plugininfo:
for line in plugininfo:
sys.stderr.write(line + "\n")
plugininfo = getpluginversioninfo(config)
if plugininfo:
for line in plugininfo:
sys.stderr.write(line + "\n")
else:
sys.stderr.write("pytest {}\n".format(pytest.__version__))


def pytest_cmdline_main(config):
if config.option.version:
if config.option.version > 0:
showversion(config)
return 0
elif config.option.help:
Expand Down
46 changes: 0 additions & 46 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,52 +1200,6 @@ def test_help_via_addopts(testdir):
)


def test_help_and_version_after_argument_error(testdir):
Copy link
Member

@nicoddemus nicoddemus May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry I don't think I was clear: this test you should keep, I meant to remove the test you have added before (test_help_and_version_verbose_after_argument_error).

This test (and the adjustment you made to it) is necessary: when we get a parser error, we need to ensure we still display at least the pytest version.

I asked you to remove the "verbose" kind of this same test because the output should actually be the same as the "non-verbose" kind: "--verbose --verbose" output normally includes plugins, which are not loaded when a parser error happens anyway, so we regardless fall back to the "non-verbose version" information.

Copy link
Member

@nicoddemus nicoddemus May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IOW: please revert your dfcec18 commit and just delete test_help_and_version_verbose_after_argument_error, leaving test_help_and_version_after_argument_error as is. 👍

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there--so I checked this today and realized that I accidentally deleted the original test test_help_and_version_after_argument_error while trying to delete onlytest_help_and_version_verbose_after_argument_error.

I'm really sorry for that confusing mistake, but I've pushed another commit to correct it.

testdir.makeconftest(
"""
def validate(arg):
raise argparse.ArgumentTypeError("argerror")

def pytest_addoption(parser):
group = parser.getgroup('cov')
group.addoption(
"--invalid-option-should-allow-for-help",
type=validate,
)
"""
)
testdir.makeini(
"""
[pytest]
addopts = --invalid-option-should-allow-for-help
"""
)
result = testdir.runpytest("--help")
result.stdout.fnmatch_lines(
[
"usage: *",
"positional arguments:",
"NOTE: displaying only minimal help due to UsageError.",
]
)
result.stderr.fnmatch_lines(
[
"ERROR: usage: *",
"%s: error: argument --invalid-option-should-allow-for-help: expected one argument"
% (testdir.request.config._parser.optparser.prog,),
]
)
# Does not display full/default help.
assert "to see available markers type: pytest --markers" not in result.stdout.lines
assert result.ret == ExitCode.USAGE_ERROR

result = testdir.runpytest("--version")
result.stderr.fnmatch_lines(
["*pytest*{}*imported from*".format(pytest.__version__)]
)
assert result.ret == ExitCode.USAGE_ERROR


def test_help_formatter_uses_py_get_terminal_width(monkeypatch):
from _pytest.config.argparsing import DropShorterLongHelpFormatter

Expand Down
13 changes: 10 additions & 3 deletions testing/test_helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
from _pytest.config import ExitCode


def test_version(testdir, pytestconfig):
def test_version_verbose(testdir, pytestconfig):
testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
result = testdir.runpytest("--version")
result = testdir.runpytest("--version", "--version")
assert result.ret == 0
# p = py.path.local(py.__file__).dirpath()
result.stderr.fnmatch_lines(
["*pytest*{}*imported from*".format(pytest.__version__)]
)
if pytestconfig.pluginmanager.list_plugin_distinfo():
result.stderr.fnmatch_lines(["*setuptools registered plugins:", "*at*"])


def test_version_less_verbose(testdir, pytestconfig):
testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
result = testdir.runpytest("--version")
assert result.ret == 0
# p = py.path.local(py.__file__).dirpath()
result.stderr.fnmatch_lines(["pytest {}".format(pytest.__version__)])


def test_help(testdir):
result = testdir.runpytest("--help")
assert result.ret == 0
Expand Down