From c2632253aceeb7988fdb332d2bff309b25952d9d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 27 Jun 2022 09:10:21 +0200 Subject: [PATCH] Added tests for update command --- src/cpl_cli/command/update_service.py | 7 +- unittests/unittests_cli/cli_test_suite.py | 7 +- unittests/unittests_cli/update_test_case.py | 86 ++++++++++++++++++++- unittests/unittests_shared/cli_commands.py | 4 + 4 files changed, 93 insertions(+), 11 deletions(-) diff --git a/src/cpl_cli/command/update_service.py b/src/cpl_cli/command/update_service.py index 5d082109..3b1895f5 100644 --- a/src/cpl_cli/command/update_service.py +++ b/src/cpl_cli/command/update_service.py @@ -40,6 +40,8 @@ class UpdateService(CommandABC): self._cli_settings = cli_settings self._is_simulation = False + self._project_file = f'{self._project_settings.name}.json' + @property def help_message(self) -> str: return textwrap.dedent("""\ @@ -81,7 +83,7 @@ class UpdateService(CommandABC): new_package = Pip.get_package(name) if new_package is None: Console.error(f'Update for package {package} failed') - return + continue self._project_json_update_dependency(package, new_package) @@ -151,8 +153,7 @@ class UpdateService(CommandABC): BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings) } - with open(os.path.join(self._env.working_directory, f'{self._config.get_configuration("ProjectName")}.json'), - 'w') as project: + with open(os.path.join(self._env.working_directory, self._project_file), 'w') as project: project.write(json.dumps(config, indent=2)) project.close() diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index 6cfcc39f..f26f3f3a 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -30,17 +30,15 @@ class CLITestSuite(unittest.TestSuite): self._is_online = True active_tests = [ - # # nothing needed + # nothing needed VersionTestCase, GenerateTestCase, NewTestCase, - # # project needed + # project needed BuildTestCase, PublishTestCase, RunTestCase, StartTestCase, - # check in project settings if package is updated - # UpdateTestCase, # workspace needed AddTestCase, RemoveTestCase @@ -49,6 +47,7 @@ class CLITestSuite(unittest.TestSuite): if self._is_online: active_tests.append(InstallTestCase) active_tests.append(UninstallTestCase) + active_tests.append(UpdateTestCase) for test in active_tests: self.addTests(loader.loadTestsFromTestCase(test)) diff --git a/unittests/unittests_cli/update_test_case.py b/unittests/unittests_cli/update_test_case.py index 35e6d8f3..36c6fd4f 100644 --- a/unittests/unittests_cli/update_test_case.py +++ b/unittests/unittests_cli/update_test_case.py @@ -1,10 +1,88 @@ +import json +import os +import shutil +import subprocess +import sys import unittest +from cpl_core.utils import String +from unittests_cli.constants import PLAYGROUND_PATH +from unittests_shared.cli_commands import CLICommands + class UpdateTestCase(unittest.TestCase): - def setUp(self): - pass + def __init__(self, methodName: str): + unittest.TestCase.__init__(self, methodName) + self._source = 'install-test-source' + self._project_file = f'src/{String.convert_to_snake_case(self._source)}/{self._source}.json' + + self._old_version = '1.7.1' + self._old_package_name = 'discord.py' + self._old_package = f'{self._old_package_name}=={self._old_version}' + + self._new_version = '1.7.3' + self._new_package_name = 'discord.py' + self._new_package = f'{self._new_package_name}=={self._new_version}' + + def _get_project_settings(self): + with open(os.path.join(os.getcwd(), self._project_file), 'r', encoding='utf-8') as cfg: + # load json + project_json = json.load(cfg) + cfg.close() + + return project_json + + def _save_project_settings(self, settings: dict): + with open(os.path.join(os.getcwd(), self._project_file), 'w', encoding='utf-8') as project_file: + project_file.write(json.dumps(settings, indent=2)) + project_file.close() + + def setUp(self): + CLICommands.uninstall(self._old_package) + CLICommands.uninstall(self._new_package) + os.chdir(os.path.abspath(PLAYGROUND_PATH)) + # create projects + CLICommands.new('console', self._source, '--ab', '--s') + os.chdir(os.path.join(os.getcwd(), self._source)) + CLICommands.install(self._old_package) + + def cleanUp(self): + # remove projects + if not os.path.exists(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))): + return + + shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))) + + def _get_installed_packages(self) -> dict: + reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']) + return dict([tuple(r.decode().split('==')) for r in reqs.split()]) + + def test_install_package(self): + settings = self._get_project_settings() + self.assertNotEqual(settings, {}) + self.assertIn('ProjectSettings', settings) + self.assertIn('Dependencies', settings['ProjectSettings']) + self.assertIn( + self._old_package, + settings['ProjectSettings']['Dependencies'] + ) + packages = self._get_installed_packages() + self.assertIn(self._old_package_name, packages) + self.assertEqual(self._old_version, packages[self._old_package_name]) + + CLICommands.update() + + settings = self._get_project_settings() + self.assertNotEqual(settings, {}) + self.assertIn('ProjectSettings', settings) + self.assertIn('Dependencies', settings['ProjectSettings']) + self.assertIn( + self._new_package, + settings['ProjectSettings']['Dependencies'] + ) + packages = self._get_installed_packages() + self.assertIn(self._new_package_name, packages) + self.assertEqual(self._new_version, packages[self._new_package_name]) + - def test_equal(self): - pass diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index 2e30bf82..a458390c 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -78,6 +78,10 @@ class CLICommands: def uninstall(cls, package: str, output=False): cls._run('uninstall', package, output=output) + @classmethod + def update(cls, output=False): + cls._run('update', output=output) + @classmethod def version(cls) -> str: return cls._run_with_output('version')