From e1f4c62428b59db3d2cf7e0a122a98cb45ee9b95 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Wed, 31 May 2023 12:29:08 +0200 Subject: [PATCH] Add conan version as separated command (#13999) * Add conan version command Signed-off-by: Uilian Ries * Print python version as well Signed-off-by: Uilian Ries * don't reinvent the wheel - print_serial Signed-off-by: Uilian Ries * adjust tests Signed-off-by: Uilian Ries * strip new lines Signed-off-by: Uilian Ries * better format for testing conan version Signed-off-by: Uilian Ries --------- Signed-off-by: Uilian Ries --- conan/cli/commands/version.py | 21 ++++++++ .../integration/command_v2/test_version.py | 52 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 conan/cli/commands/version.py create mode 100644 conans/test/integration/command_v2/test_version.py diff --git a/conan/cli/commands/version.py b/conan/cli/commands/version.py new file mode 100644 index 00000000000..507f0f7d16e --- /dev/null +++ b/conan/cli/commands/version.py @@ -0,0 +1,21 @@ +from conan.cli.commands.list import print_serial +from conan.cli.command import conan_command +from conan.cli.formatters import default_json_formatter +from conan import conan_version +import platform +import sys + + +@conan_command(group="Consumer", formatters={"text": print_serial, "json": default_json_formatter}) +def version(conan_api, parser, *args): + """ + Give information about the Conan client version. + """ + + return {'version': str(conan_version), + 'python': { + 'version': platform.python_version().replace('\n', ''), + 'sys_version': sys.version.replace('\n', ''), + } + } + diff --git a/conans/test/integration/command_v2/test_version.py b/conans/test/integration/command_v2/test_version.py new file mode 100644 index 00000000000..6ddc7aff12d --- /dev/null +++ b/conans/test/integration/command_v2/test_version.py @@ -0,0 +1,52 @@ +from conans.test.utils.tools import TestClient +from conan import __version__ +import json +import platform +import sys + + +def _python_version(): + return platform.python_version().replace("\n", "") + + +def _sys_version(): + return sys.version.replace("\n", "") + + +def test_version_json(): + """ + Conan version command should be able to output a json with the version and python version. + """ + t = TestClient() + t.run("version --format=json") + js = json.loads(t.stdout) + assert js["version"] == __version__ + assert js["python"]["version"] == _python_version() + assert js["python"]["sys_version"] == _sys_version() + + +def test_version_text(): + """ + Conan version command should be able to output a raw text with the version and python version. + """ + t = TestClient() + t.run("version --format=text") + _validate_text_output(t.out) + + +def test_version_raw(): + """ + Conan version command should be able to output a raw text with the version and python version, + when no format is specified. + """ + t = TestClient() + t.run("version") + _validate_text_output(t.out) + + +def _validate_text_output(output): + lines = output.splitlines() + assert f'version: {__version__}' in lines + assert 'python' in lines + assert f' version: {_python_version()}' in lines + assert f' sys_version: {_sys_version()}' in lines