2022-06-21 09:24:56 +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 09:24:56 +02:00
|
|
|
from cpl_core.utils import String
|
|
|
|
from unittests_cli.constants import PLAYGROUND_PATH
|
|
|
|
from unittests_shared.cli_commands import CLICommands
|
|
|
|
|
2022-05-26 13:49:31 +02:00
|
|
|
|
|
|
|
class UninstallTestCase(unittest.TestCase):
|
|
|
|
|
2022-06-21 09:24:56 +02:00
|
|
|
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 _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
|
|
|
|
|
2022-05-26 13:49:31 +02:00
|
|
|
def setUp(self):
|
2022-06-21 09:24:56 +02:00
|
|
|
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)))
|
2022-05-26 13:49:31 +02:00
|
|
|
|
2022-06-23 23:39:41 +02:00
|
|
|
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()])
|
|
|
|
|
2022-06-21 09:24:56 +02:00
|
|
|
def test_uninstall(self):
|
2022-06-27 20:16:37 +02:00
|
|
|
CLICommands.install(self._package)
|
2022-06-21 09:24:56 +02:00
|
|
|
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']
|
|
|
|
)
|
2022-06-27 20:16:37 +02:00
|
|
|
self.assertNotIn(
|
|
|
|
self._package,
|
|
|
|
settings['ProjectSettings']['DevDependencies']
|
|
|
|
)
|
|
|
|
packages = self._get_installed_packages()
|
|
|
|
self.assertNotIn(self._package_name, packages)
|
|
|
|
|
|
|
|
def test_dev_uninstall(self):
|
|
|
|
CLICommands.install(self._package, is_dev=True)
|
|
|
|
CLICommands.uninstall(self._package, is_dev=True)
|
|
|
|
settings = self._get_project_settings()
|
|
|
|
self.assertNotEqual(settings, {})
|
|
|
|
self.assertIn('ProjectSettings', settings)
|
|
|
|
self.assertIn('Dependencies', settings['ProjectSettings'])
|
|
|
|
self.assertIn('DevDependencies', settings['ProjectSettings'])
|
|
|
|
self.assertNotIn(
|
|
|
|
self._package,
|
|
|
|
settings['ProjectSettings']['Dependencies']
|
|
|
|
)
|
|
|
|
self.assertNotIn(
|
|
|
|
self._package,
|
|
|
|
settings['ProjectSettings']['DevDependencies']
|
|
|
|
)
|
2022-06-23 23:39:41 +02:00
|
|
|
packages = self._get_installed_packages()
|
2022-06-21 09:24:56 +02:00
|
|
|
self.assertNotIn(self._package_name, packages)
|