From 4e535f6580bd8b841b3709ae57a6d5a212d042e4 Mon Sep 17 00:00:00 2001 From: bacon Date: Sun, 17 Mar 2024 21:34:03 +0300 Subject: [PATCH] updated lib_Cog test --- bot.py | 2 +- lib/CogsPrep.py | 16 ++++++++++------ tests/__init__.py | 0 tests/test_lib_Cog.py | 29 ++++++++++++++--------------- 4 files changed, 25 insertions(+), 22 deletions(-) delete mode 100644 tests/__init__.py diff --git a/bot.py b/bot.py index d21e819..56373df 100755 --- a/bot.py +++ b/bot.py @@ -36,7 +36,7 @@ bot = Bot(command_prefix=determine_prefix, bot.i18n.load("locale/") -asyncio.run(work_with_cogs('load', bot, asyncio.run(cog_list()))) +asyncio.run(work_with_cogs('load', bot, cog_list())) @bot.event diff --git a/lib/CogsPrep.py b/lib/CogsPrep.py index a699a8c..9976ac4 100644 --- a/lib/CogsPrep.py +++ b/lib/CogsPrep.py @@ -8,6 +8,8 @@ work_with_cogs: loads, reloads and unloads cogs files """ from os import listdir, rename +from disnake.ext.commands.interaction_bot_base import InteractionBotBase + from .Logger import logger @@ -20,27 +22,29 @@ def cog_list(fold: str = './cogs') -> list: async def work_with_cogs(what_do: str, - bot, + bot: InteractionBotBase, cog: str | list): if isinstance(cog, str): cog = cog.split() for _filename in cog: + if _filename.endswith('.py'): + _filename = _filename.split('.')[0] if what_do == "load": - await bot.load_extension(f'cogs.{_filename}') + bot.load_extension(f'cogs.{_filename}') logger.info(f'Cog {_filename} loaded') elif what_do == 'unload': - await bot.unload_extension(f'cogs.{_filename}') + bot.unload_extension(f'cogs.{_filename}') logger.info(f'Cog {_filename} unloaded') elif what_do == 'reload': - await bot.reload_extension(f'cogs.{_filename}') + bot.reload_extension(f'cogs.{_filename}') logger.info(f'Cog {_filename} reloaded') elif what_do == 'disable': - await bot.unload_extension(f'cogs.{_filename}') + bot.unload_extension(f'cogs.{_filename}') rename(f'cogs/{_filename}.py', f'cogs/disabled/{_filename}.py') logger.info(f'Cog {_filename} stopped and disabled') elif what_do == 'enable': rename(f'cogs/disabled/{_filename}.py', f'cogs/{_filename}.py') - await bot.load_extension(f'cogs.{_filename}') + bot.load_extension(f'cogs.{_filename}') logger.info(f'Cog {_filename} started and enabled') diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_lib_Cog.py b/tests/test_lib_Cog.py index 62e9fcf..4095fd7 100644 --- a/tests/test_lib_Cog.py +++ b/tests/test_lib_Cog.py @@ -1,29 +1,28 @@ import pytest -from disnake.ext.commands import Bot +from disnake.ext.commands.common_bot_base import CommonBotBase from mock import mock -from ..lib.CogsPrep import cog_list, work_with_cogs +from lib.CogsPrep import cog_list, work_with_cogs -@mock.patch('discord_bot.lib.CogsPrep.cog_list') -def test_cog_list(mock_cog_list): - with mock.patch('discord_bot.lib.CogsPrep.listdir') as MockClass: +def test_cog_list(): + with mock.patch('lib.CogsPrep.listdir') as MockClass: MockClass.return_value = ['cog1.py', 'cog2.py'] result = cog_list() assert result == ['cog1', 'cog2'] -# @pytest.mark.skip(reason=None) - @pytest.mark.asyncio @pytest.mark.parametrize("cog", ["cog1.py", "cog2"]) @pytest.mark.parametrize("what_do", ['load', 'unload', 'reload', 'disable', 'enable']) async def test_work_with_cogs(what_do, cog): - mock_logger = mock.AsyncMock() - with (mock.patch('discord_bot.lib.CogsPrep.logger.info') as mocked_logger): - mock_bot = mock.AsyncMock() - await mock_bot(spec=Bot) - with mock.patch('discord_bot.lib.CogsPrep.rename') as mock_rename: - mock_rename.return_value = True - result = await work_with_cogs(what_do, mock_bot, cog) - assert len(mocked_logger.call_args_list) == 1, f"Not listed what_do {what_do}" + with (mock.patch('lib.CogsPrep.rename') as mock_rename): + mock_rename.return_value = None + mock_bot = mock.MagicMock(spec=CommonBotBase) + result = await work_with_cogs(what_do, mock_bot, cog) + if what_do in ['load', 'enable']: + assert mock_bot.load_extension.called + elif what_do in ['unload', 'disable']: + assert mock_bot.unload_extension.called + elif what_do == 'reload': + assert mock_bot.reload_extension.called