mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-20 18:06:08 -05:00
[V3] Make pytest fixtures available as a plugin (#1858)
* Move all fixtures to pytest plugin folder * Add core dunder all * Update other dunder all's * Black reformat
This commit is contained in:
@@ -2,20 +2,7 @@ from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from redbot.cogs.admin import Admin
|
||||
from redbot.cogs.admin.announcer import Announcer
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def admin(config):
|
||||
return Admin(config)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def announcer(admin):
|
||||
a = Announcer(MagicMock(), "Some message", admin.conf)
|
||||
yield a
|
||||
a.cancel()
|
||||
from redbot.pytest.admin import *
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from collections import namedtuple
|
||||
|
||||
from redbot.cogs.dataconverter import core_specs
|
||||
from redbot.pytest.dataconverter import *
|
||||
from redbot.core.utils.data_converter import DataConverter
|
||||
|
||||
|
||||
@@ -14,16 +13,9 @@ def mock_dpy_member(guildid, userid):
|
||||
return namedtuple("Member", "id guild")(int(userid), mock_dpy_object(guildid))
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def specresolver():
|
||||
here = Path(__file__)
|
||||
|
||||
resolver = core_specs.SpecResolver(here.parent)
|
||||
return resolver
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mod_nicknames(red, specresolver: core_specs.SpecResolver):
|
||||
async def test_mod_nicknames(red):
|
||||
specresolver = get_specresolver(__file__)
|
||||
filepath, converter, cogname, attr, _id = specresolver.get_conversion_info("Past Nicknames")
|
||||
conf = specresolver.get_config_object(red, cogname, attr, _id)
|
||||
|
||||
|
||||
@@ -6,69 +6,12 @@ import pytest
|
||||
from unittest.mock import MagicMock
|
||||
from raven.versioning import fetch_git_sha
|
||||
|
||||
from redbot.pytest.downloader import *
|
||||
|
||||
from redbot.cogs.downloader.repo_manager import RepoManager, Repo
|
||||
from redbot.cogs.downloader.errors import ExistingGitRepo
|
||||
|
||||
|
||||
async def fake_run(*args, **kwargs):
|
||||
fake_result_tuple = namedtuple("fake_result", "returncode result")
|
||||
res = fake_result_tuple(0, (args, kwargs))
|
||||
print(args[0])
|
||||
return res
|
||||
|
||||
|
||||
async def fake_run_noprint(*args, **kwargs):
|
||||
fake_result_tuple = namedtuple("fake_result", "returncode result")
|
||||
res = fake_result_tuple(0, (args, kwargs))
|
||||
return res
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def patch_relative_to(monkeysession):
|
||||
def fake_relative_to(self, some_path: Path):
|
||||
return self
|
||||
|
||||
monkeysession.setattr("pathlib.Path.relative_to", fake_relative_to)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repo_manager(tmpdir_factory):
|
||||
rm = RepoManager()
|
||||
# rm.repos_folder = Path(str(tmpdir_factory.getbasetemp())) / 'repos'
|
||||
return rm
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repo(tmpdir):
|
||||
repo_folder = Path(str(tmpdir)) / "repos" / "squid"
|
||||
repo_folder.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
return Repo(
|
||||
url="https://github.com/tekulvw/Squid-Plugins",
|
||||
name="squid",
|
||||
branch="rewrite_cogs",
|
||||
folder_path=repo_folder,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repo_norun(repo):
|
||||
repo._run = fake_run
|
||||
return repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bot_repo(event_loop):
|
||||
cwd = Path.cwd()
|
||||
return Repo(
|
||||
name="Red-DiscordBot",
|
||||
branch="WRONG",
|
||||
url="https://empty.com/something.git",
|
||||
folder_path=cwd,
|
||||
loop=event_loop,
|
||||
)
|
||||
|
||||
|
||||
def test_existing_git_repo(tmpdir):
|
||||
repo_folder = Path(str(tmpdir)) / "repos" / "squid" / ".git"
|
||||
repo_folder.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -3,31 +3,9 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from redbot.pytest.downloader import *
|
||||
from redbot.cogs.downloader.installable import Installable, InstallableType
|
||||
|
||||
INFO_JSON = {
|
||||
"author": ("tekulvw",),
|
||||
"bot_version": (3, 0, 0),
|
||||
"description": "A long description",
|
||||
"hidden": False,
|
||||
"install_msg": "A post-installation message",
|
||||
"required_cogs": {},
|
||||
"requirements": ("tabulate"),
|
||||
"short": "A short description",
|
||||
"tags": ("tag1", "tag2"),
|
||||
"type": "COG",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def installable(tmpdir):
|
||||
cog_path = tmpdir.mkdir("test_repo").mkdir("test_cog")
|
||||
info_path = cog_path.join("info.json")
|
||||
info_path.write_text(json.dumps(INFO_JSON), "utf-8")
|
||||
|
||||
cog_info = Installable(Path(str(cog_path)))
|
||||
return cog_info
|
||||
|
||||
|
||||
def test_process_info_file(installable):
|
||||
for k, v in INFO_JSON.items():
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from redbot.cogs.alias import Alias
|
||||
from redbot.core import Config
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def alias(config, monkeypatch):
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr(Config, "get_conf", lambda *args, **kwargs: config)
|
||||
return Alias(None)
|
||||
from redbot.pytest.alias import *
|
||||
|
||||
|
||||
def test_is_valid_alias_name(alias):
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def bank(config, monkeypatch):
|
||||
from redbot.core import Config
|
||||
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr(Config, "get_conf", lambda *args, **kwargs: config)
|
||||
from redbot.core import bank
|
||||
|
||||
bank._register_defaults()
|
||||
return bank
|
||||
from redbot.pytest.economy import *
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -1,16 +1,6 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mod(config, monkeypatch):
|
||||
from redbot.core import Config
|
||||
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr(Config, "get_conf", lambda *args, **kwargs: config)
|
||||
from redbot.core import modlog
|
||||
|
||||
modlog._register_defaults()
|
||||
return modlog
|
||||
from redbot.pytest.mod import *
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
import random
|
||||
from collections import namedtuple
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from redbot.core import Config
|
||||
from redbot.core.bot import Red
|
||||
|
||||
from redbot.core.drivers import red_json
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def monkeysession(request):
|
||||
mpatch = MonkeyPatch()
|
||||
yield mpatch
|
||||
mpatch.undo()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def override_data_path(tmpdir):
|
||||
from redbot.core import data_manager
|
||||
|
||||
data_manager.basic_config = data_manager.basic_config_default
|
||||
data_manager.basic_config["DATA_PATH"] = str(tmpdir)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def coroutine():
|
||||
async def some_coro(*args, **kwargs):
|
||||
return args, kwargs
|
||||
|
||||
return some_coro
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def json_driver(tmpdir_factory):
|
||||
import uuid
|
||||
|
||||
rand = str(uuid.uuid4())
|
||||
path = Path(str(tmpdir_factory.mktemp(rand)))
|
||||
driver = red_json.JSON("PyTest", identifier=str(uuid.uuid4()), data_path_override=path)
|
||||
return driver
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def config(json_driver):
|
||||
conf = Config(
|
||||
cog_name="PyTest", unique_identifier=json_driver.unique_cog_identifier, driver=json_driver
|
||||
)
|
||||
yield conf
|
||||
conf._defaults = {}
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def config_fr(json_driver):
|
||||
"""
|
||||
Mocked config object with force_register enabled.
|
||||
"""
|
||||
conf = Config(
|
||||
cog_name="PyTest",
|
||||
unique_identifier=json_driver.unique_cog_identifier,
|
||||
driver=json_driver,
|
||||
force_registration=True,
|
||||
)
|
||||
yield conf
|
||||
conf._defaults = {}
|
||||
|
||||
|
||||
# region Dpy Mocks
|
||||
@pytest.fixture()
|
||||
def guild_factory():
|
||||
mock_guild = namedtuple("Guild", "id members")
|
||||
|
||||
class GuildFactory:
|
||||
def get(self):
|
||||
return mock_guild(random.randint(1, 999999999), [])
|
||||
|
||||
return GuildFactory()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def empty_guild(guild_factory):
|
||||
return guild_factory.get()
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def empty_channel():
|
||||
mock_channel = namedtuple("Channel", "id")
|
||||
return mock_channel(random.randint(1, 999999999))
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def empty_role():
|
||||
mock_role = namedtuple("Role", "id")
|
||||
return mock_role(random.randint(1, 999999999))
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def member_factory(guild_factory):
|
||||
mock_member = namedtuple("Member", "id guild display_name")
|
||||
|
||||
class MemberFactory:
|
||||
def get(self):
|
||||
return mock_member(random.randint(1, 999999999), guild_factory.get(), "Testing_Name")
|
||||
|
||||
return MemberFactory()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def empty_member(member_factory):
|
||||
return member_factory.get()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_factory():
|
||||
mock_user = namedtuple("User", "id")
|
||||
|
||||
class UserFactory:
|
||||
def get(self):
|
||||
return mock_user(random.randint(1, 999999999))
|
||||
|
||||
return UserFactory()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def empty_user(user_factory):
|
||||
return user_factory.get()
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def empty_message():
|
||||
mock_msg = namedtuple("Message", "content")
|
||||
return mock_msg("No content.")
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def ctx(empty_member, empty_channel, red):
|
||||
mock_ctx = namedtuple("Context", "author guild channel message bot")
|
||||
return mock_ctx(empty_member, empty_member.guild, empty_channel, empty_message, red)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Red Mock
|
||||
@pytest.fixture()
|
||||
def red(config_fr):
|
||||
from redbot.core.cli import parse_cli_flags
|
||||
|
||||
cli_flags = parse_cli_flags(["ignore_me"])
|
||||
|
||||
description = "Red v3 - Alpha"
|
||||
|
||||
Config.get_core_conf = lambda *args, **kwargs: config_fr
|
||||
|
||||
red = Red(cli_flags=cli_flags, description=description, pm_help=None)
|
||||
|
||||
yield red
|
||||
|
||||
red.http._session.close()
|
||||
|
||||
|
||||
# endregion
|
||||
@@ -2,19 +2,10 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from redbot.pytest.cog_manager import *
|
||||
from redbot.core import cog_manager
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def cog_mgr(red):
|
||||
return red.cog_mgr
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def default_dir(red):
|
||||
return red.main_dir
|
||||
|
||||
|
||||
@pytest.mark.skip
|
||||
@pytest.mark.asyncio
|
||||
async def test_ensure_cogs_in_paths(cog_mgr, default_dir):
|
||||
|
||||
@@ -3,28 +3,10 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from redbot.pytest.data_manager import *
|
||||
from redbot.core import data_manager
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def cleanup_datamanager():
|
||||
data_manager.basic_config = None
|
||||
data_manager.jsonio = None
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def data_mgr_config(tmpdir):
|
||||
default = data_manager.basic_config_default.copy()
|
||||
default["BASE_DIR"] = str(tmpdir)
|
||||
return default
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def cog_instance():
|
||||
thing = type("CogTest", (object,), {})
|
||||
return thing()
|
||||
|
||||
|
||||
def test_no_basic(cog_instance):
|
||||
with pytest.raises(RuntimeError):
|
||||
data_manager.core_data_path()
|
||||
|
||||
@@ -1,52 +1,7 @@
|
||||
import pytest
|
||||
from redbot.core.rpc import RPC, RPCMixin, get_name
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def rpc():
|
||||
return RPC()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def rpcmixin():
|
||||
r = RPCMixin()
|
||||
r.rpc = MagicMock(spec=RPC)
|
||||
return r
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def cog():
|
||||
class Cog:
|
||||
async def cofunc(*args, **kwargs):
|
||||
pass
|
||||
|
||||
async def cofunc2(*args, **kwargs):
|
||||
pass
|
||||
|
||||
async def cofunc3(*args, **kwargs):
|
||||
pass
|
||||
|
||||
def func(*args, **kwargs):
|
||||
pass
|
||||
|
||||
return Cog()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def existing_func(rpc, cog):
|
||||
rpc.add_method(cog.cofunc)
|
||||
|
||||
return cog.cofunc
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def existing_multi_func(rpc, cog):
|
||||
funcs = [cog.cofunc, cog.cofunc2, cog.cofunc3]
|
||||
rpc.add_multi_method(*funcs)
|
||||
|
||||
return funcs
|
||||
from redbot.pytest.rpc import *
|
||||
from redbot.core.rpc import get_name
|
||||
|
||||
|
||||
def test_get_name(cog):
|
||||
|
||||
Reference in New Issue
Block a user