sh_cpl/unittests/unittests_cli/version_test_case.py

88 lines
2.9 KiB
Python
Raw Normal View History

2022-06-26 19:51:18 +02:00
import pkgutil
import platform
import sys
import textwrap
2022-05-26 13:49:31 +02:00
import unittest
2022-06-26 19:51:18 +02:00
import pkg_resources
from art import text2art
from tabulate import tabulate
import cpl_cli
from cpl_core.console import ForegroundColorEnum
from termcolor import colored
2022-12-01 23:23:48 +01:00
from unittests_cli.abc.command_test_case import CommandTestCase
2022-06-26 19:51:18 +02:00
from unittests_shared.cli_commands import CLICommands
2022-05-26 13:49:31 +02:00
2022-12-01 23:23:48 +01:00
class VersionTestCase(CommandTestCase):
2022-05-26 13:49:31 +02:00
2022-12-01 23:23:48 +01:00
def __init__(self, method_name: str):
CommandTestCase.__init__(self, method_name)
2022-06-26 19:51:18 +02:00
self._block_banner = ""
self._block_version = ""
self._block_package_header = ""
self._block_cpl_packages = ""
self._block_packages = ""
self._name = "CPL CLI"
2022-05-26 13:49:31 +02:00
def setUp(self):
pass
2022-06-26 19:51:18 +02:00
def _get_version_output(self, version: str):
index = 0
for line in version.split('\n'):
if line == "":
continue
if index <= 5:
self._block_banner += f'{line}\n'
if 7 <= index <= 9:
self._block_version += f'{line}\n'
if 10 <= index <= 16:
2022-06-26 19:51:18 +02:00
self._block_cpl_packages += f'{line}\n'
if index >= 18:
2022-06-26 19:51:18 +02:00
self._block_packages += f'{line}\n'
index += 1
def test_version(self):
packages = []
cpl_packages = []
dependencies = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
for p in dependencies:
if str(p).startswith('cpl-'):
cpl_packages.append([p, dependencies[p]])
continue
packages.append([p, dependencies[p]])
2022-06-26 19:51:18 +02:00
version = CLICommands.version()
self._get_version_output(version)
reference_banner = colored(text2art(self._name), ForegroundColorEnum.yellow.value).split('\n')
reference_banner = "\n".join(reference_banner[:len(reference_banner) - 1]) + '\n'
self.assertEqual(reference_banner, self._block_banner)
reference_version = [
colored(f'{colored("Common Python library CLI: ")}{colored(cpl_cli.__version__)}'),
colored(f'{colored("Python: ")}{colored(f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")}'),
colored(f'OS: {colored(f"{platform.system()} {platform.processor()}")}') + '\n'
]
self.assertEqual('\n'.join(reference_version), self._block_version)
reference_cpl_packages = [
colored(colored(f'CPL packages:')),
colored(f'{tabulate(cpl_packages, headers=["Name", "Version"])}') + '\n'
2022-06-26 19:51:18 +02:00
]
self.assertEqual('\n'.join(reference_cpl_packages), self._block_cpl_packages)
reference_packages = [
colored(colored(f'Python packages:')),
colored(f'{tabulate(packages, headers=["Name", "Version"])}'),
'\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\n' # fix colored codes
]
self.assertEqual('\n'.join(reference_packages), self._block_packages)