2021-09-25 14:48:33 +00:00
|
|
|
import asyncio
|
2022-04-14 23:22:45 +00:00
|
|
|
import glob
|
|
|
|
import os
|
2021-09-24 19:57:25 +00:00
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2021-09-24 21:37:46 +00:00
|
|
|
from unittest.mock import Mock
|
2021-09-24 19:57:25 +00:00
|
|
|
|
2021-09-24 21:07:32 +00:00
|
|
|
import pytest
|
|
|
|
from pytest_mock import MockerFixture
|
|
|
|
|
2021-09-24 19:57:25 +00:00
|
|
|
# Add source to PATH such that imports within src modules are properly resolved.
|
|
|
|
SRC_PATH = str(Path(__file__).parent / '..' / 'src')
|
|
|
|
sys.path.insert(0, SRC_PATH)
|
2021-09-24 21:07:32 +00:00
|
|
|
|
2021-09-25 01:49:46 +00:00
|
|
|
from bot import Music
|
|
|
|
|
2021-09-24 21:07:32 +00:00
|
|
|
|
2022-04-14 23:22:45 +00:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
2021-11-04 00:07:42 +00:00
|
|
|
def global_teardown():
|
2021-11-03 23:29:54 +00:00
|
|
|
yield
|
|
|
|
logs = glob.glob('*.log')
|
|
|
|
for log in logs:
|
|
|
|
os.remove(log)
|
|
|
|
|
|
|
|
|
2021-09-24 21:07:32 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def bot(mocker: MockerFixture):
|
2021-09-24 21:37:46 +00:00
|
|
|
bot_mock = mocker.patch('discord.ext.commands.Bot', autospec=True)
|
|
|
|
bot_mock.loop = Mock()
|
|
|
|
yield bot_mock
|
2021-09-24 21:07:32 +00:00
|
|
|
|
|
|
|
|
2021-09-25 01:49:46 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mbot(bot):
|
|
|
|
mbot_mock = Music(bot)
|
|
|
|
yield mbot_mock
|
2021-09-25 14:48:33 +00:00
|
|
|
mbot_mock.cog_unload()
|
2021-09-25 01:49:46 +00:00
|
|
|
|
|
|
|
|
2021-09-24 21:07:32 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def ctx(mocker: MockerFixture):
|
2021-09-25 18:58:09 +00:00
|
|
|
ctx_mock = mocker.patch('discord.ext.commands.Context', autospec=True)
|
2022-04-14 23:22:45 +00:00
|
|
|
ctx_mock.voice_client.stop = lambda: ctx_mock.voice_client.play.call_args.kwargs['after'](None)
|
2021-09-25 18:58:09 +00:00
|
|
|
return ctx_mock
|
2021-09-25 14:48:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def event_loop():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
yield loop
|
|
|
|
pending = asyncio.all_tasks(loop=loop)
|
|
|
|
asyncio.ensure_future(asyncio.gather(*pending))
|