Skip to content

Commit

Permalink
Add conan version as separated command (#13999)
Browse files Browse the repository at this point in the history
* Add conan version command

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Print python version as well

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* don't reinvent the wheel - print_serial

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* adjust tests

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* strip new lines

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* better format for testing conan version

Signed-off-by: Uilian Ries <uilianries@gmail.com>

---------

Signed-off-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
uilianries authored May 31, 2023
1 parent 0839bc7 commit e1f4c62
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions conan/cli/commands/version.py
Original file line number Diff line number Diff line change
@@ -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', ''),
}
}

52 changes: 52 additions & 0 deletions conans/test/integration/command_v2/test_version.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e1f4c62

Please sign in to comment.