From 79701c65ed47d9fd9dbc8949a49136f25506d9e0 Mon Sep 17 00:00:00 2001 From: Claire Cecil Date: Sat, 23 May 2020 10:27:58 -0400 Subject: [PATCH] Added support for less verbose version information (#7169) --- AUTHORS | 1 + changelog/7128.improvement.rst | 1 + src/_pytest/helpconfig.py | 28 +++++++++++++++++----------- testing/test_config.py | 4 +--- testing/test_helpconfig.py | 13 ++++++++++--- 5 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 changelog/7128.improvement.rst diff --git a/AUTHORS b/AUTHORS index 5822c74f2..64907c1d8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -63,6 +63,7 @@ Christian Tismer Christoph Buelter Christopher Dignam Christopher Gilling +Claire Cecil Claudio Madotto CrazyMerlyn Cyrus Maden diff --git a/changelog/7128.improvement.rst b/changelog/7128.improvement.rst new file mode 100644 index 000000000..9d24d567a --- /dev/null +++ b/changelog/7128.improvement.rst @@ -0,0 +1 @@ +`pytest --version` now displays just the pytest version, while `pytest --version --version` displays more verbose information including plugins. diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index 11fd02462..402ffae66 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -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", @@ -116,19 +119,22 @@ def pytest_cmdline_parse(): 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: diff --git a/testing/test_config.py b/testing/test_config.py index 7d553e63b..17385dc17 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1243,9 +1243,7 @@ def test_help_and_version_after_argument_error(testdir): assert result.ret == ExitCode.USAGE_ERROR result = testdir.runpytest("--version") - result.stderr.fnmatch_lines( - ["*pytest*{}*imported from*".format(pytest.__version__)] - ) + result.stderr.fnmatch_lines(["pytest {}".format(pytest.__version__)]) assert result.ret == ExitCode.USAGE_ERROR diff --git a/testing/test_helpconfig.py b/testing/test_helpconfig.py index 5e4f85228..24590dd3b 100644 --- a/testing/test_helpconfig.py +++ b/testing/test_helpconfig.py @@ -2,11 +2,10 @@ import pytest 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__)] ) @@ -14,6 +13,14 @@ def test_version(testdir, pytestconfig): 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