From c0789cf4f7a6ed5640de4823f50375246a830f28 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 21 Jun 2022 08:56:26 +0200 Subject: [PATCH] Added tests for install command --- src/cpl_cli/command/version_service.py | 1 - unittests/unittests_cli/cli_test_suite.py | 3 +- unittests/unittests_cli/install_test_case.py | 85 +++++++++++++++++++- unittests/unittests_shared/cli_commands.py | 4 + 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/cpl_cli/command/version_service.py b/src/cpl_cli/command/version_service.py index 5e93660f..7b74b007 100644 --- a/src/cpl_cli/command/version_service.py +++ b/src/cpl_cli/command/version_service.py @@ -5,7 +5,6 @@ import pkg_resources import textwrap import cpl_cli -import cpl_core from cpl_core.console.console import Console from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_cli.command_abc import CommandABC diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index 58638afb..626902b4 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -8,6 +8,7 @@ from unittest import TestResult from unittests_cli.add_test_case import AddTestCase from unittests_cli.constants import PLAYGROUND_PATH 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 @@ -26,7 +27,7 @@ class CLITestSuite(unittest.TestSuite): # project needed # self.addTests(loader.loadTestsFromTestCase(BuildTestCase)) - # self.addTests(loader.loadTestsFromTestCase(InstallTestCase)) + self.addTests(loader.loadTestsFromTestCase(InstallTestCase)) # self.addTests(loader.loadTestsFromTestCase(PublishTestCase)) # self.addTests(loader.loadTestsFromTestCase(RunTestCase)) # self.addTests(loader.loadTestsFromTestCase(StartTestCase)) diff --git a/unittests/unittests_cli/install_test_case.py b/unittests/unittests_cli/install_test_case.py index 0271f37b..bb992206 100644 --- a/unittests/unittests_cli/install_test_case.py +++ b/unittests/unittests_cli/install_test_case.py @@ -1,10 +1,87 @@ +import json +import os +import shutil 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 InstallTestCase(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' + + 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): + 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)) + + 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_install_package(self): + version = '1.7.3' + package_name = 'discord.py' + package = f'{package_name}=={version}' + CLICommands.install(package) + settings = self._get_project_settings() + self.assertNotEqual(settings, {}) + self.assertIn('ProjectSettings', settings) + self.assertIn('Dependencies', settings['ProjectSettings']) + self.assertIn( + package, + settings['ProjectSettings']['Dependencies'] + ) + packages = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set) + self.assertIn(package_name, packages) + self.assertEqual(version, packages[package_name]) + + def test_install_all(self): + version = '1.7.3' + package_name = 'discord.py' + package = f'{package_name}=={version}' + settings = self._get_project_settings() + self.assertIn('ProjectSettings', settings) + self.assertIn('Dependencies', settings['ProjectSettings']) + self.assertNotIn( + package, + settings['ProjectSettings']['Dependencies'] + ) + settings['ProjectSettings']['Dependencies'].append(package) + self._save_project_settings(settings) + CLICommands.install() + new_settings = self._get_project_settings() + self.assertEqual(settings, new_settings) + self.assertIn('ProjectSettings', new_settings) + self.assertIn('Dependencies', new_settings['ProjectSettings']) + self.assertIn( + package, + new_settings['ProjectSettings']['Dependencies'] + ) + - def test_equal(self): - pass diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index dd1e0029..cccfb675 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -25,6 +25,10 @@ class CLICommands: def generate(cls, schematic: str, name: str, output=False): cls._run('generate', schematic, name, output=output) + @classmethod + def install(cls, package: str = '', output=False): + cls._run('install', package, output=output) + @classmethod def new(cls, project_type: str, name: str, *args, output=False): cls._run('new', project_type, name, *args, output=output)