diff --git a/src/cookiecutter_python/cli.py b/src/cookiecutter_python/cli.py index 97b598cc..6df563f3 100644 --- a/src/cookiecutter_python/cli.py +++ b/src/cookiecutter_python/cli.py @@ -2,6 +2,7 @@ import os import sys +import typing as t import click @@ -17,10 +18,10 @@ def version_msg(): """Message about Python Generator version, location and Python version.""" - python_version = sys.version[:3] + python_version: t.Tuple[int, int, int] = sys.version_info[:3] message = u'Python Generator %(version)s from {} (Python {})' location = os.path.dirname(this_file_location) - return message.format(location, python_version) + return message.format(location, ".".join(map(str, python_version))) @click.command(context_settings=dict(help_option_names=[u'-h', u'--help'])) diff --git a/tests/test_build_backend_sdist.py b/tests/test_build_backend_sdist.py index dcf3baa6..2cdfdf82 100644 --- a/tests/test_build_backend_sdist.py +++ b/tests/test_build_backend_sdist.py @@ -146,6 +146,7 @@ def sdist_expected_correct_file_structure(): # 'src/stubs/requests_futures/sessions.pyi', ) TESTS = ( + 'tests/test_version_string.py', 'tests/test_git_sdk.py', 'tests/test_git_porcelain.py', 'tests/test_is_repo_clean_function.py', diff --git a/tests/test_version_string.py b/tests/test_version_string.py new file mode 100644 index 00000000..c867e871 --- /dev/null +++ b/tests/test_version_string.py @@ -0,0 +1,50 @@ +from pathlib import Path + +import pytest + + +def test_version_msg_function_returns_expected_string(distro_loc: Path): + # GIVEN the version_msg function + from cookiecutter_python.cli import version_msg + + # WHEN the version_msg function is called + result: str = version_msg() + + # THEN it should return the expected string + import sys + + EXPECTED_PYTHON_VERSION: str = ".".join(map(str, sys.version_info[:3])) + + EXPECTED_PARENT_DIR_OF_COOKIECUTTER_PYTHON: Path = distro_loc.parent + + assert result == ( + f'Python Generator %(version)s from {EXPECTED_PARENT_DIR_OF_COOKIECUTTER_PYTHON} (Python {EXPECTED_PYTHON_VERSION})' + ) + + +@pytest.mark.runner_setup(mix_stderr=False) +def test_cli_version_flag_returns_expected_string( + distro_loc: Path, + isolated_cli_runner, +): + from cookiecutter_python import __version__ + from cookiecutter_python.cli import main + + result = isolated_cli_runner.invoke( + main, + args=['--version'], + input=None, + env=None, + catch_exceptions=False, + ) + assert result.exit_code == 0 + + import sys + + EXPECTED_PYTHON_VERSION: str = ".".join(map(str, sys.version_info[:3])) + + EXPECTED_PARENT_DIR_OF_COOKIECUTTER_PYTHON: Path = distro_loc.parent + + assert result.stdout == ( + f'Python Generator {__version__} from {EXPECTED_PARENT_DIR_OF_COOKIECUTTER_PYTHON} (Python {EXPECTED_PYTHON_VERSION})\n' + )