Added tests for version command

This commit is contained in:
Sven Heidemann 2022-06-26 19:51:18 +02:00
parent 78ffbcc33d
commit 45be77dd10
3 changed files with 108 additions and 6 deletions

View File

@ -17,6 +17,7 @@ from unittests_cli.run_test_case import RunTestCase
from unittests_cli.start_test_case import StartTestCase from unittests_cli.start_test_case import StartTestCase
from unittests_cli.uninstall_test_case import UninstallTestCase from unittests_cli.uninstall_test_case import UninstallTestCase
from unittests_cli.update_test_case import UpdateTestCase from unittests_cli.update_test_case import UpdateTestCase
from unittests_cli.version_test_case import VersionTestCase
class CLITestSuite(unittest.TestSuite): class CLITestSuite(unittest.TestSuite):
@ -29,12 +30,11 @@ class CLITestSuite(unittest.TestSuite):
self._is_online = True self._is_online = True
active_tests = [ active_tests = [
# nothing needed # # nothing needed
VersionTestCase,
GenerateTestCase, GenerateTestCase,
NewTestCase, NewTestCase,
# compare console output # # project needed
# VersionTestCase,
# project needed
BuildTestCase, BuildTestCase,
PublishTestCase, PublishTestCase,
RunTestCase, RunTestCase,

View File

@ -1,10 +1,97 @@
import pkgutil
import platform
import sys
import textwrap
import unittest import unittest
import pkg_resources
from art import text2art
from tabulate import tabulate
import cpl_cli
from cpl_core.console import ForegroundColorEnum
from termcolor import colored
from unittests_shared.cli_commands import CLICommands
class VersionTestCase(unittest.TestCase): class VersionTestCase(unittest.TestCase):
def __init__(self, methodName: str):
unittest.TestCase.__init__(self, methodName)
self._block_banner = ""
self._block_version = ""
self._block_package_header = ""
self._block_cpl_packages = ""
self._block_packages = ""
self._name = "CPL CLI"
def setUp(self): def setUp(self):
pass pass
def test_equal(self): def _get_version_output(self, version: str):
pass 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 <= 15:
self._block_cpl_packages += f'{line}\n'
if index >= 17:
self._block_packages += f'{line}\n'
index += 1
def test_version(self):
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)
packages = []
cpl_packages = [
'cpl_core',
'cpl_cli',
'cpl_query'
]
for modname in cpl_packages:
module = pkgutil.find_loader(modname)
if module is None:
break
module = module.load_module(modname)
if '__version__' in dir(module):
packages.append([f'{modname}', module.__version__])
reference_cpl_packages = [
colored(colored(f'CPL packages:')),
colored(f'{tabulate(packages, headers=["Name", "Version"])}') + '\n'
]
self.assertEqual('\n'.join(reference_cpl_packages), self._block_cpl_packages)
packages = []
dependencies = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
for p in dependencies:
packages.append([p, dependencies[p]])
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)

View File

@ -20,6 +20,17 @@ class CLICommands:
else: else:
subprocess.run(command, env=env_vars, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL) subprocess.run(command, env=env_vars, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
@staticmethod
def _run_with_output(cmd: str, *args) -> str:
env_vars = os.environ
env_vars['CPL_IS_UNITTEST'] = 'NO'
command = ['python', CLI_PATH, cmd]
for arg in args:
command.append(arg)
return subprocess.run(command, env=env_vars, check=True, capture_output=True, text=True).stdout
@classmethod @classmethod
def add(cls, source: str, target: str, output=False): def add(cls, source: str, target: str, output=False):
cls._run('add', source, target, output=output) cls._run('add', source, target, output=output)
@ -66,3 +77,7 @@ class CLICommands:
@classmethod @classmethod
def uninstall(cls, package: str, output=False): def uninstall(cls, package: str, output=False):
cls._run('uninstall', package, output=output) cls._run('uninstall', package, output=output)
@classmethod
def version(cls) -> str:
return cls._run_with_output('version')