From 45be77dd10206b23f3ef62d1b49bf3987cf8869c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 26 Jun 2022 19:51:18 +0200 Subject: [PATCH] Added tests for version command --- unittests/unittests_cli/cli_test_suite.py | 8 +- unittests/unittests_cli/version_test_case.py | 91 +++++++++++++++++++- unittests/unittests_shared/cli_commands.py | 15 ++++ 3 files changed, 108 insertions(+), 6 deletions(-) diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index 3b74a429..6cfcc39f 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -17,6 +17,7 @@ from unittests_cli.run_test_case import RunTestCase from unittests_cli.start_test_case import StartTestCase from unittests_cli.uninstall_test_case import UninstallTestCase from unittests_cli.update_test_case import UpdateTestCase +from unittests_cli.version_test_case import VersionTestCase class CLITestSuite(unittest.TestSuite): @@ -29,12 +30,11 @@ class CLITestSuite(unittest.TestSuite): self._is_online = True active_tests = [ - # nothing needed + # # nothing needed + VersionTestCase, GenerateTestCase, NewTestCase, - # compare console output - # VersionTestCase, - # project needed + # # project needed BuildTestCase, PublishTestCase, RunTestCase, diff --git a/unittests/unittests_cli/version_test_case.py b/unittests/unittests_cli/version_test_case.py index cd7e75e9..73a7053a 100644 --- a/unittests/unittests_cli/version_test_case.py +++ b/unittests/unittests_cli/version_test_case.py @@ -1,10 +1,97 @@ +import pkgutil +import platform +import sys +import textwrap 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): + 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): pass - def test_equal(self): - pass + 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 <= 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) diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index eeb253e1..2e30bf82 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -20,6 +20,17 @@ class CLICommands: else: 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 def add(cls, source: str, target: str, output=False): cls._run('add', source, target, output=output) @@ -66,3 +77,7 @@ class CLICommands: @classmethod def uninstall(cls, package: str, output=False): cls._run('uninstall', package, output=output) + + @classmethod + def version(cls) -> str: + return cls._run_with_output('version')