sh_cpl/unittests/unittests_cli/version_test_case.py

97 lines
3.2 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):
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
2023-02-20 15:55:20 +01:00
for line in version.split("\n"):
2022-06-26 19:51:18 +02:00
if line == "":
continue
if index <= 5:
2023-02-20 15:55:20 +01:00
self._block_banner += f"{line}\n"
2022-06-26 19:51:18 +02:00
if 7 <= index <= 9:
2023-02-20 15:55:20 +01:00
self._block_version += f"{line}\n"
2022-06-26 19:51:18 +02:00
if 10 <= index <= 16:
2023-02-20 15:55:20 +01:00
self._block_cpl_packages += f"{line}\n"
2022-06-26 19:51:18 +02:00
if index >= 18:
2023-02-20 15:55:20 +01:00
self._block_packages += f"{line}\n"
2022-06-26 19:51:18 +02:00
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:
2023-02-20 15:55:20 +01:00
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)
2023-02-20 15:55:20 +01:00
reference_banner = colored(text2art(self._name), ForegroundColorEnum.yellow.value).split("\n")
reference_banner = "\n".join(reference_banner[: len(reference_banner) - 1]) + "\n"
2023-02-20 15:55:20 +01:00
with self.subTest(msg="Block banner"):
self.assertEqual(reference_banner, self._block_banner)
2022-06-26 19:51:18 +02:00
reference_version = [
colored(f'{colored("Common Python library CLI: ")}{colored(cpl_cli.__version__)}'),
2023-02-20 15:55:20 +01:00
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",
2022-06-26 19:51:18 +02:00
]
2023-02-20 15:55:20 +01:00
with self.subTest(msg="Block version"):
self.assertEqual("\n".join(reference_version), self._block_version)
2022-06-26 19:51:18 +02:00
reference_cpl_packages = [
2023-02-20 15:55:20 +01:00
colored(colored(f"CPL packages:")),
colored(f'{tabulate(cpl_packages, headers=["Name", "Version"])}') + "\n",
2022-06-26 19:51:18 +02:00
]
2023-02-20 15:55:20 +01:00
with self.subTest(msg="Block cpl packages"):
self.assertEqual("\n".join(reference_cpl_packages), self._block_cpl_packages)
2022-06-26 19:51:18 +02:00
reference_packages = [
2023-02-20 15:55:20 +01:00
colored(colored(f"Python packages:")),
2022-06-26 19:51:18 +02:00
colored(f'{tabulate(packages, headers=["Name", "Version"])}'),
2023-02-20 15:55:20 +01:00
"\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\n", # fix colored codes
2022-06-26 19:51:18 +02:00
]
self.maxDiff = None
2023-02-20 15:55:20 +01:00
with self.subTest(msg="Block packages"):
ref_packages = "\n".join(reference_packages)
self.assertEqual(ref_packages, self._block_packages)