Added Downloader cog (#786)

This commit is contained in:
Will
2017-06-17 19:31:32 -04:00
committed by Twentysix
parent b12a41cd77
commit 53810b2262
16 changed files with 1461 additions and 1 deletions

0
tests/cogs/__init__.py Normal file
View File

View File

View File

@@ -0,0 +1,136 @@
from collections import namedtuple
from raven.versioning import fetch_git_sha
import pytest
from cogs.downloader.repo_manager import RepoManager, Repo
from pathlib import Path
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(scope="module")
def repo_manager(tmpdir_factory, config):
config.register_global(repos={})
rm = RepoManager(config)
rm.repos_folder = Path(str(tmpdir_factory.getbasetemp())) / 'repos'
return rm
@pytest.fixture
def repo(tmpdir):
from cogs.downloader.repo_manager import Repo
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):
from cogs.downloader.repo_manager import Repo
repo_folder = Path(str(tmpdir)) / 'repos' / 'squid' / '.git'
repo_folder.mkdir(parents=True, exist_ok=True)
r = Repo(
url="https://github.com/tekulvw/Squid-Plugins",
name="squid",
branch="rewrite_cogs",
folder_path=repo_folder.parent
)
exists, _ = r._existing_git_repo()
assert exists is True
@pytest.mark.asyncio
async def test_clone_repo(repo_norun, capsys):
await repo_norun.clone()
clone_cmd, _ = capsys.readouterr()
clone_cmd = clone_cmd.strip('[\']').split('\', \'')
assert clone_cmd[0] == 'git'
assert clone_cmd[1] == 'clone'
assert clone_cmd[2] == '-b'
assert clone_cmd[3] == 'rewrite_cogs'
assert clone_cmd[4] == repo_norun.url
assert 'repos/squid' in clone_cmd[5]
@pytest.mark.asyncio
async def test_add_repo(monkeypatch, repo_manager):
monkeypatch.setattr("cogs.downloader.repo_manager.Repo._run",
fake_run_noprint)
squid = await repo_manager.add_repo(
url="https://github.com/tekulvw/Squid-Plugins",
name="squid",
branch="rewrite_cogs"
)
assert squid.available_modules == []
@pytest.mark.asyncio
async def test_current_branch(bot_repo):
branch = await bot_repo.current_branch()
# So this does work, just not sure how to fully automate the test
assert branch not in ("WRONG", "")
@pytest.mark.asyncio
async def test_current_hash(bot_repo):
branch = await bot_repo.current_branch()
bot_repo.branch = branch
commit = await bot_repo.current_commit()
sentry_sha = fetch_git_sha(str(bot_repo.folder_path))
assert sentry_sha == commit

View File

@@ -0,0 +1,70 @@
import pytest
import json
from cogs.downloader.installable import Installable, InstallableType
from pathlib import Path
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():
if k == "type":
assert installable.type == InstallableType.COG
else:
assert getattr(installable, k) == v
# noinspection PyProtectedMember
def test_location_is_dir(installable):
assert installable._location.exists()
assert installable._location.is_dir()
# noinspection PyProtectedMember
def test_info_file_is_file(installable):
assert installable._info_file.exists()
assert installable._info_file.is_file()
def test_name(installable):
assert installable.name == "test_cog"
def test_repo_name(installable):
assert installable.repo_name == "test_repo"
def test_serialization(installable):
data = installable.to_json()
location = data["location"]
assert location[-1] == "test_cog"

View File

@@ -5,10 +5,18 @@ import pytest
import random
from core.bot import Red
from _pytest.monkeypatch import MonkeyPatch
from core.drivers import red_json
from core import Config
@pytest.fixture(scope="session")
def monkeysession(request):
mpatch = MonkeyPatch()
yield mpatch
mpatch.undo()
@pytest.fixture(scope="module")
def json_driver(tmpdir_factory):
driver = red_json.JSON(