From 2925788d019ebe066007afbfcb6ebf27af62caf5 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 21 Jun 2022 09:24:56 +0200 Subject: [PATCH] Added tests for uninstall command --- src/cpl_cli/command/uninstall_service.py | 6 +- unittests/unittests_cli/cli_test_suite.py | 16 +++++- .../unittests_cli/uninstall_test_case.py | 56 +++++++++++++++++-- unittests/unittests_shared/cli_commands.py | 4 ++ 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/src/cpl_cli/command/uninstall_service.py b/src/cpl_cli/command/uninstall_service.py index c5c132c4..f2b88d5f 100644 --- a/src/cpl_cli/command/uninstall_service.py +++ b/src/cpl_cli/command/uninstall_service.py @@ -35,6 +35,7 @@ class UninstallService(CommandABC): self._is_simulating = False self._is_virtual = False + self._project_file = f'{self._project_settings.name}.json' @property def help_message(self) -> str: @@ -59,8 +60,7 @@ class UninstallService(CommandABC): Console.error(f'Expected package') Console.error(f'Usage: cpl uninstall ') return - - + if '--virtual' in args: self._is_virtual = True args.remove('--virtual') @@ -110,7 +110,7 @@ class UninstallService(CommandABC): ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings), 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_file: + with open(os.path.join(self._env.working_directory, self._project_file), 'w') as project_file: project_file.write(json.dumps(config, indent=2)) project_file.close() diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index 626902b4..a0e49f09 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -11,6 +11,7 @@ from unittests_cli.generate_test_case import GenerateTestCase from unittests_cli.install_test_case import InstallTestCase from unittests_cli.new_test_case import NewTestCase from unittests_cli.remove_test_case import RemoveTestCase +from unittests_cli.uninstall_test_case import UninstallTestCase class CLITestSuite(unittest.TestSuite): @@ -23,15 +24,28 @@ class CLITestSuite(unittest.TestSuite): # nothing needed self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) self.addTests(loader.loadTestsFromTestCase(NewTestCase)) + + # compare console output # self.addTests(loader.loadTestsFromTestCase(VersionTestCase)) # project needed + # compare two file states/directory content # self.addTests(loader.loadTestsFromTestCase(BuildTestCase)) + self.addTests(loader.loadTestsFromTestCase(InstallTestCase)) + + # compare two file states/directory content # self.addTests(loader.loadTestsFromTestCase(PublishTestCase)) + + # check if application was executed properly # self.addTests(loader.loadTestsFromTestCase(RunTestCase)) + + # check if application was executed properly and file watcher is working # self.addTests(loader.loadTestsFromTestCase(StartTestCase)) - # self.addTests(loader.loadTestsFromTestCase(UninstallTestCase)) + + self.addTests(loader.loadTestsFromTestCase(UninstallTestCase)) + + # check in project settings if package is updated # self.addTests(loader.loadTestsFromTestCase(UpdateTestCase)) # workspace needed diff --git a/unittests/unittests_cli/uninstall_test_case.py b/unittests/unittests_cli/uninstall_test_case.py index f3d1ae15..66bde537 100644 --- a/unittests/unittests_cli/uninstall_test_case.py +++ b/unittests/unittests_cli/uninstall_test_case.py @@ -1,10 +1,58 @@ +import json +import os +import shutil +import time import unittest +import pkg_resources + +from cpl_core.utils import String + +from unittests_cli.constants import PLAYGROUND_PATH +from unittests_shared.cli_commands import CLICommands + class UninstallTestCase(unittest.TestCase): - def setUp(self): - pass + def __init__(self, methodName: str): + unittest.TestCase.__init__(self, methodName) + self._source = 'uninstall-test-source' + self._project_file = f'src/{String.convert_to_snake_case(self._source)}/{self._source}.json' + self._version = '1.7.3' + self._package_name = 'discord.py' + self._package = f'{self._package_name}=={self._version}' - def test_equal(self): - pass + 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 setUp(self): + 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._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 test_uninstall(self): + CLICommands.uninstall(self._package) + settings = self._get_project_settings() + self.assertNotEqual(settings, {}) + self.assertIn('ProjectSettings', settings) + self.assertIn('Dependencies', settings['ProjectSettings']) + self.assertNotIn( + self._package, + settings['ProjectSettings']['Dependencies'] + ) + packages = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set) + self.assertNotIn(self._package_name, packages) diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index cccfb675..41ab4b4c 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -36,3 +36,7 @@ class CLICommands: @classmethod def remove(cls, project: str): cls._run('remove', project) + + @classmethod + def uninstall(cls, package: str, output=False): + cls._run('uninstall', package, output=output)