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 3 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
44 changes: 44 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,50 @@ def pytest_addoption(parser):
assert result.ret == ExitCode.USAGE_ERROR

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


def test_help_and_version_verbose_after_argument_error(testdir):
Copy link
Member

Choose a reason for hiding this comment

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

Thanks @debugduck!

This is not producing the output because there's special handling when there's an usage error:

def pytest_cmdline_parse(self, pluginmanager, args):
try:
self.parse(args)
except UsageError:
# Handle --version and --help here in a minimal fashion.
# This gets done via helpconfig normally, but its
# pytest_cmdline_main is not called in case of errors.
if getattr(self.option, "version", False) or "--version" in args:
from _pytest.helpconfig import showversion
showversion(self)
elif (
getattr(self.option, "help", False) or "--help" in args or "-h" in args
):
self._parser._getparser().print_help()
sys.stdout.write(
"\nNOTE: displaying only minimal help due to UsageError.\n\n"
)
raise
return self

When that happens, it is best to just stick to showing the minimal version information. 👍

So while your instincts were correct in adding this test, I think we can just remove it.

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!

Okay, I removed it. I believe I resolved the git metadata and I also added the documentation for the change as well.

Thanks! :)

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", "--version")
result.stderr.fnmatch_lines(
["*pytest*{}*imported from*".format(pytest.__version__)]
)
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