sh_cpl/unittests/unittests_cli/install_test_case.py

131 lines
4.9 KiB
Python
Raw Normal View History

2022-06-21 08:56:26 +02:00
import json
import os
import shutil
2022-06-23 23:39:41 +02:00
import subprocess
import sys
2022-05-26 13:49:31 +02:00
import unittest
2022-06-21 08:56:26 +02:00
from cpl_core.utils import String
2022-12-01 23:23:48 +01:00
from unittests_cli.abc.command_test_case import CommandTestCase
2022-06-21 08:56:26 +02:00
from unittests_cli.constants import PLAYGROUND_PATH
from unittests_shared.cli_commands import CLICommands
2022-05-26 13:49:31 +02:00
2022-12-01 23:23:48 +01:00
class InstallTestCase(CommandTestCase):
2022-05-26 13:49:31 +02:00
2022-12-01 23:23:48 +01:00
def __init__(self, method_name: str):
CommandTestCase.__init__(self, method_name)
2022-06-21 08:56:26 +02:00
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()
2022-05-26 13:49:31 +02:00
def setUp(self):
2022-12-01 17:00:17 +01:00
if not os.path.exists(PLAYGROUND_PATH):
os.makedirs(PLAYGROUND_PATH)
2022-12-01 17:00:17 +01:00
os.chdir(PLAYGROUND_PATH)
2022-06-21 08:56:26 +02:00
# create projects
CLICommands.new('console', self._source, '--ab', '--s', '--venv')
2022-06-21 08:56:26 +02:00
os.chdir(os.path.join(os.getcwd(), self._source))
2022-06-23 23:39:41 +02:00
def _get_installed_packages(self) -> dict:
reqs = subprocess.check_output([os.path.join(os.getcwd(), 'venv/bin/python'), '-m', 'pip', 'freeze'])
2022-06-23 23:39:41 +02:00
return dict([tuple(r.decode().split('==')) for r in reqs.split()])
2022-06-21 08:56:26 +02:00
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()
with self.subTest(msg='Project deps'):
self.assertNotEqual(settings, {})
self.assertIn('ProjectSettings', settings)
self.assertIn('Dependencies', settings['ProjectSettings'])
self.assertIn(
package,
settings['ProjectSettings']['Dependencies']
)
with self.subTest(msg='PIP'):
packages = self._get_installed_packages()
self.assertIn(package_name, packages)
self.assertEqual(version, packages[package_name])
2022-06-21 08:56:26 +02:00
2022-06-27 20:16:37 +02:00
def test_dev_install_package(self):
version = '1.7.3'
package_name = 'discord.py'
package = f'{package_name}=={version}'
CLICommands.install(package, is_dev=True)
settings = self._get_project_settings()
with self.subTest(msg='Project deps'):
self.assertNotEqual(settings, {})
self.assertIn('ProjectSettings', settings)
self.assertIn('Dependencies', settings['ProjectSettings'])
self.assertIn('DevDependencies', settings['ProjectSettings'])
self.assertNotIn(
package,
settings['ProjectSettings']['Dependencies']
)
self.assertIn(
package,
settings['ProjectSettings']['DevDependencies']
)
with self.subTest(msg='PIP'):
packages = self._get_installed_packages()
self.assertIn(package_name, packages)
self.assertEqual(version, packages[package_name])
2022-06-27 20:16:37 +02:00
2022-06-23 23:39:41 +02:00
def _test_install_all(self):
2022-06-21 08:56:26 +02:00
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'])
2022-06-27 20:16:37 +02:00
self.assertIn('DevDependencies', settings['ProjectSettings'])
self.assertNotIn(
package,
settings['ProjectSettings']['Dependencies']
)
self.assertIn('DevDependencies', settings['ProjectSettings'])
2022-06-21 08:56:26 +02:00
self.assertNotIn(
package,
settings['ProjectSettings']['Dependencies']
)
settings['ProjectSettings']['Dependencies'].append(package)
2022-06-27 20:16:37 +02:00
settings['ProjectSettings']['DevDependencies'].append(package)
2022-06-21 08:56:26 +02:00
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'])
2022-06-27 20:16:37 +02:00
self.assertIn('DevDependencies', new_settings['ProjectSettings'])
2022-06-21 08:56:26 +02:00
self.assertIn(
package,
new_settings['ProjectSettings']['Dependencies']
)
2022-06-27 20:16:37 +02:00
self.assertIn(
package,
new_settings['ProjectSettings']['DevDependencies']
)
2022-06-23 23:39:41 +02:00
packages = self._get_installed_packages()
self.assertIn(package_name, packages)
self.assertEqual(version, packages[package_name])