Make --version write to stdout rather than stderr

Fix #8246
This commit is contained in:
Bruno Oliveira 2021-01-17 08:43:36 -03:00
parent 2623ee2e0e
commit 109312ba86
4 changed files with 8 additions and 7 deletions

View File

@ -0,0 +1 @@
``--version`` now writes version information to ``stdout`` rather than ``stderr``.

View File

@ -127,7 +127,7 @@ def pytest_cmdline_parse():
def showversion(config: Config) -> None: def showversion(config: Config) -> None:
if config.option.version > 1: if config.option.version > 1:
sys.stderr.write( sys.stdout.write(
"This is pytest version {}, imported from {}\n".format( "This is pytest version {}, imported from {}\n".format(
pytest.__version__, pytest.__file__ pytest.__version__, pytest.__file__
) )
@ -135,9 +135,9 @@ def showversion(config: Config) -> None:
plugininfo = getpluginversioninfo(config) plugininfo = getpluginversioninfo(config)
if plugininfo: if plugininfo:
for line in plugininfo: for line in plugininfo:
sys.stderr.write(line + "\n") sys.stdout.write(line + "\n")
else: else:
sys.stderr.write(f"pytest {pytest.__version__}\n") sys.stdout.write(f"pytest {pytest.__version__}\n")
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]: def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:

View File

@ -1756,7 +1756,7 @@ def test_help_and_version_after_argument_error(pytester: Pytester) -> None:
assert result.ret == ExitCode.USAGE_ERROR assert result.ret == ExitCode.USAGE_ERROR
result = pytester.runpytest("--version") result = pytester.runpytest("--version")
result.stderr.fnmatch_lines([f"pytest {pytest.__version__}"]) result.stdout.fnmatch_lines([f"pytest {pytest.__version__}"])
assert result.ret == ExitCode.USAGE_ERROR assert result.ret == ExitCode.USAGE_ERROR

View File

@ -7,16 +7,16 @@ def test_version_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None:
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
result = pytester.runpytest("--version", "--version") result = pytester.runpytest("--version", "--version")
assert result.ret == 0 assert result.ret == 0
result.stderr.fnmatch_lines([f"*pytest*{pytest.__version__}*imported from*"]) result.stdout.fnmatch_lines([f"*pytest*{pytest.__version__}*imported from*"])
if pytestconfig.pluginmanager.list_plugin_distinfo(): if pytestconfig.pluginmanager.list_plugin_distinfo():
result.stderr.fnmatch_lines(["*setuptools registered plugins:", "*at*"]) result.stdout.fnmatch_lines(["*setuptools registered plugins:", "*at*"])
def test_version_less_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None: def test_version_less_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None:
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
result = pytester.runpytest("--version") result = pytester.runpytest("--version")
assert result.ret == 0 assert result.ret == 0
result.stderr.fnmatch_lines([f"pytest {pytest.__version__}"]) result.stdout.fnmatch_lines([f"pytest {pytest.__version__}"])
def test_versions(): def test_versions():