Red-DiscordBot/tests/cogs/downloader/test_downloader.py
jack1142 e2c8b11008 [V3 Downloader] Revision tracking (#2571)
* feat(downloader): Install cog from specific commit in repo (initial commit)

- Repo and Installable have commit property now
- New class inheriting from Installable -
InstalledCog (old one from converters.py removed)
- New Repo.checkout() method, which is also async ctx manager
ref #2527

* fix(downloader): Keep information about repo's branch in config

- This is needed to make sure that repo can go back from detached state in some rare unexpected
cases
- current branch is determined by `git symbolic-ref` now as this command errors for detached
HEAD

* feat(downloader): Update repo without cogs, update single cog

The most important part of issue #2527 has been added here
- `[p]repo update` command added
- new conf format - nested dictionary repo_name->cog_name->cog_json
  installed libraries are now kept in conf too
  - `InstalledCog` renamed to `InstalledModule` - installed libraries use this class
  - `Downloader.installed_libraries()` and `Downloader.installed_modules()` added
  - `Downloader._add_to_installed()` and `Downloader._remove_from_installed()`
    now accept list of modules, of both cogs and libraries
- `[p]cog install` tells about fails of copying cog and installing shared libraries
- `[p]cog update` will truly update only chosen cogs (if provided) or cogs that need update
  - pinned cogs aren't checked
  - before update, repos are updated
  - to determine if update is needed `Repo.get_modified_modules()` is used
- `[p]cog pin` and `[p]cog unpin` commands for pinning/unpinning cogs added
- `Repo.checkout()` allows to choose ctx manager exit's checkout revision
- `Repo.install_cog()` returns `InstalledModule` now and raises CopyingError (maybe breaking?)
- `Repo.install_libraries()` returns 2-tuple of installed and failed libraries (maybe breaking?)
- `RepoManager.get_all_cogs()` added, which returns cogs from all repos
- `RepoManager.repos` property added, which contains tuple of `Repo`

* test(downloader): Repo.current_branch() throws an exception, when branch can't be determined

* style(downloader): rename _add_to_installed to _save_to_installed

This method is used for both adding and updating existing modules in Config

* refactor(downloader): add ctx.typing() for few commands

`[p]cog install` is nested hell, can't wait for moving install logic to separate method

* fix(downloader): refactor and fix `set` usage

* perf(downloader): update commits for ALL checked modules to omit diffs next time

This will also disable running git diff for cogs that have the same commit as the latest one

* style(downloader): few style improvements

- use of mutable object in method definition
- make Repo._get_full_sha1() public method
- too long
line
- don't use len to check if sequence is empty

* feat(downloader): add `[p]cog updateallfromrepos` and `[p]cog updatetoversion` commands

- moved cog update logic into `Downloader._cog_update_logic()` (lack of better name)
  - splitted
whole cog update process into smaller methods
  - might still need some improvements
- added new
methods to `Repo` class:
  - `is_on_branch()` to check if repo is currently checked out to branch

- `is_ancestor()` to check if one commit is ancestor of the other
- fix for
`Downloader._available_updates()` behaviour
broken by commit
5755ab08ba67556b3863e907c6f44d80f4f13d88

* feat(downloader): try to find last commit where module is still present

Enhancements:
- `Installable` now has `repo` attribute containing repo object or `None` if repo is
missing
- `Downloader._install_cogs()` and `Downloader._reinstall_libraries()` are able to install
modules from different commits of repo
- `Repo.checkout()` as ctx manager will now exit to commit
which was active before checking out
- unification of `rev` and `hash` terms:
All function
parameters are explicitly called `hash`, if it can only be commit's full sha1 hash or `rev` if it
can be anything that names a commit object, see
[link](https://git-scm.com/docs/git-rev-parse#_specifying_revisions)
- new
`Repo.get_last_module_occurence()` method, which gets module's Installable from last commit in which
it still occurs

* docs(downloader): Add basic description for `InstalledModule`

* fix(downloader): cog ignored during updates if its commit was missing

After config format update, commit string is empty until update and when such cog was checked and it
wasn't available in repo anymore, it was ignored

* refactor(downloader): Installing cogs from specific rev will pin them

* perf(downloader): Don't checkout when current commit equals target hash

- changes to `Repo.checkout()`:
  - `exit_to_rev` is now keyword only argument
  - added
`force_checkout` to force checkout even if `Repo.commit` value is the same as target hash

* refactor(downloader): Repo._run() stderr is redirected to debug log now

- added two keyword arguments:
  - `valid_exit_codes` which specifies valid exit codes, used to
determine if stderr should be sent as debug or error level in logging
  - `debug_only` which
specifies if stderr can be sent only as debug level in logging

* style(downloader): stop using `set` as arg name in `_load_repos()`

* feat(downloader): pass multiple cogs to `[p]cog (un)pin`

* refactor(downloader): accept module name instead of instance, fix spelling

* style(downloader): few small style changes

* fix(downloader): add type annotations + fixes based on them

- fix wrong type annotations and add a lot of new ones
- add checks for `Installable.repo` being `None`
- fix wrong return type in `Downloader._install_requirements`
- show repo names correctly when updating all repos
- fix error when some requirement fails to install

BREAKING CHANGE:
- type of `Repo.available_modules` is now consistent (always `tuple`)

* tests: use same event loop policy as in Red's code

* enhance(downloader): fully handle ambiguous revisions

* build(deps): add pytest-mock dependency to tests extra

* fix(downloader): minor fixes

* feat(downloader): add tool for editing Downloader's test repo

This script aims to help update the human-readable version of repo
used for git integration tests in ``redbot/tests/downloader_testrepo.export``
by exporting/importing it in/from provided directory.

Note
----
Editing `downloader_git_test_repo.export` file manually is strongly discouraged,
especially editing any part of commit directives as that causes a change in the commit's hash.
Another problem devs could encounter when trying to manually edit that file
are editors that will use CRLF instead of LF for new line character(s) and therefore break it.

I also used `.gitattributes` to prevent autocrlf from breaking testrepo.

Also, if Git ever changes currently used SHA-1 to SHA-256 we will have to
update old hashes with new ones. But it's a small drawback,
when we can have human-readable version of repo.

Known limitations
-----------------
``git fast-export`` exports commits without GPG signs so this script disables it in repo's config.
This also means devs shouldn't use ``--gpg-sign`` flag in ``git commit`` within the test repo.

* tests(downloader): add git tests and test repo for them

Also added Markdown file that is even more clear than export file
on what the test repo contains.
This is manually created but can be automated on later date.

* test(downloader): add more tests related to RepoManager

These tests use expected output that is already guaranteed by git tests.

* chore(CODEOWNERS): add jack1142 to Downloader's folders

I know this doesn't actually give any benefit to people that don't have
write permission to the repo but I saw other big fella devs doing this,
so I think this might be advisable.

* enhance(downloader): allow easy schema updates in future

* enhance(downloader): more typing fixes, add comments for clarity

* feat(downloader): add python and bot version check to update process

follow-up on #2605, this commit fully fixes #1866

* chore(changelog): add towncrier entries

* fix(downloader): use `*args` instead of `commands.Greedy`

* fix(downloader): hot-reload issue - `InstallableType` now inherits from `IntEnum`

There's desync of `InstallableType` class types due to hot-reload
and `IntEnum` allows for equality check between different types

* enhance(downloader): ensure there's no cog with same name installed

should fix #2927

* fix(downloader): last few changes before marking as ready for review
2019-11-07 20:36:16 -05:00

421 lines
13 KiB
Python

import asyncio
import pathlib
from collections import namedtuple
from typing import Any, NamedTuple
from pathlib import Path
import pytest
from pytest_mock import MockFixture
from redbot.pytest.downloader import *
from redbot.cogs.downloader.repo_manager import Installable
from redbot.cogs.downloader.repo_manager import Candidate, ProcessFormatter, RepoManager, Repo
from redbot.cogs.downloader.errors import (
AmbiguousRevision,
ExistingGitRepo,
GitException,
UnknownRevision,
)
class FakeCompletedProcess(NamedTuple):
returncode: int
stdout: bytes = b""
stderr: bytes = b""
async def async_return(ret: Any):
return ret
def _mock_run(
mocker: MockFixture, repo: Repo, returncode: int, stdout: bytes = b"", stderr: bytes = b""
):
return mocker.patch.object(
repo,
"_run",
autospec=True,
return_value=async_return(FakeCompletedProcess(returncode, stdout, stderr)),
)
def _mock_setup_repo(mocker: MockFixture, repo: Repo, commit: str):
def update_commit(*args, **kwargs):
repo.commit = commit
return mocker.DEFAULT
return mocker.patch.object(
repo,
"_setup_repo",
autospec=True,
side_effect=update_commit,
return_value=async_return(None),
)
def test_existing_git_repo(tmp_path):
repo_folder = tmp_path / "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",
commit="6acb5decbb717932e5dc0cda7fca0eff452c47dd",
folder_path=repo_folder.parent,
)
exists, git_path = r._existing_git_repo()
assert exists is True
assert git_path == repo_folder
ancestor_rev = "c950fc05a540dd76b944719c2a3302da2e2f3090"
descendant_rev = "fb99eb7d2d5bed514efc98fe6686b368f8425745"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"maybe_ancestor_rev,descendant_rev,returncode,expected",
[(ancestor_rev, descendant_rev, 0, True), (descendant_rev, ancestor_rev, 1, False)],
)
async def test_is_ancestor(mocker, repo, maybe_ancestor_rev, descendant_rev, returncode, expected):
m = _mock_run(mocker, repo, returncode)
ret = await repo.is_ancestor(maybe_ancestor_rev, descendant_rev)
m.assert_called_once_with(
ProcessFormatter().format(
repo.GIT_IS_ANCESTOR,
path=repo.folder_path,
maybe_ancestor_rev=maybe_ancestor_rev,
descendant_rev=descendant_rev,
),
valid_exit_codes=(0, 1),
)
assert ret is expected
@pytest.mark.asyncio
async def test_is_ancestor_raise(mocker, repo):
m = _mock_run(mocker, repo, 128)
with pytest.raises(GitException):
await repo.is_ancestor("invalid1", "invalid2")
m.assert_called_once_with(
ProcessFormatter().format(
repo.GIT_IS_ANCESTOR,
path=repo.folder_path,
maybe_ancestor_rev="invalid1",
descendant_rev="invalid2",
),
valid_exit_codes=(0, 1),
)
@pytest.mark.asyncio
async def test_get_file_update_statuses(mocker, repo):
old_rev = "c950fc05a540dd76b944719c2a3302da2e2f3090"
new_rev = "fb99eb7d2d5bed514efc98fe6686b368f8425745"
m = _mock_run(
mocker,
repo,
0,
b"A\x00added_file.txt\x00\t"
b"M\x00mycog/__init__.py\x00\t"
b"D\x00sample_file1.txt\x00\t"
b"D\x00sample_file2.txt\x00\t"
b"A\x00sample_file3.txt",
)
ret = await repo._get_file_update_statuses(old_rev, new_rev)
m.assert_called_once_with(
ProcessFormatter().format(
repo.GIT_DIFF_FILE_STATUS, path=repo.folder_path, old_rev=old_rev, new_rev=new_rev
)
)
assert ret == {
"added_file.txt": "A",
"mycog/__init__.py": "M",
"sample_file1.txt": "D",
"sample_file2.txt": "D",
"sample_file3.txt": "A",
}
@pytest.mark.asyncio
async def test_is_module_modified(mocker, repo):
old_rev = "c950fc05a540dd76b944719c2a3302da2e2f3090"
new_rev = "fb99eb7d2d5bed514efc98fe6686b368f8425745"
FakeInstallable = namedtuple("Installable", "name commit")
module = FakeInstallable("mycog", new_rev)
m = mocker.patch.object(
repo,
"_get_file_update_statuses",
autospec=True,
return_value=async_return(
{
"added_file.txt": "A",
"mycog/__init__.py": "M",
"sample_file1.txt": "D",
"sample_file2.txt": "D",
"sample_file3.txt": "A",
}
),
)
ret = await repo._is_module_modified(module, old_rev)
m.assert_called_once_with(old_rev, new_rev)
assert ret is True
@pytest.mark.asyncio
async def test_get_full_sha1_success(mocker, repo):
commit = "c950fc05a540dd76b944719c2a3302da2e2f3090"
m = _mock_run(mocker, repo, 0, commit.encode())
ret = await repo.get_full_sha1(commit)
m.assert_called_once_with(
ProcessFormatter().format(repo.GIT_GET_FULL_SHA1, path=repo.folder_path, rev=commit)
)
assert ret == commit
@pytest.mark.asyncio
async def test_get_full_sha1_notfound(mocker, repo):
m = _mock_run(mocker, repo, 128, b"", b"fatal: Needed a single revision")
with pytest.raises(UnknownRevision):
await repo.get_full_sha1("invalid")
m.assert_called_once_with(
ProcessFormatter().format(repo.GIT_GET_FULL_SHA1, path=repo.folder_path, rev="invalid")
)
@pytest.mark.asyncio
async def test_get_full_sha1_ambiguous(mocker, repo):
m = _mock_run(
mocker,
repo,
128,
b"",
b"error: short SHA1 c6f0 is ambiguous\n"
b"hint: The candidates are:\n"
b"hint: c6f028f tag ambiguous_tag_66387\n"
b"hint: c6f0e5e commit 2019-10-24 - Commit ambiguous with tag.\n"
b"fatal: Needed a single revision",
)
with pytest.raises(AmbiguousRevision) as exc_info:
await repo.get_full_sha1("c6f0")
m.assert_called_once_with(
ProcessFormatter().format(repo.GIT_GET_FULL_SHA1, path=repo.folder_path, rev="c6f0")
)
assert exc_info.value.candidates == [
Candidate("c6f028f", "tag", "ambiguous_tag_66387"),
Candidate("c6f0e5e", "commit", "2019-10-24 - Commit ambiguous with tag."),
]
def test_update_available_modules(repo):
module = repo.folder_path / "mycog" / "__init__.py"
submodule = module.parent / "submodule" / "__init__.py"
module.parent.mkdir(parents=True)
module.touch()
submodule.parent.mkdir()
submodule.touch()
ret = repo._update_available_modules()
assert (
ret
== repo.available_modules
== (Installable(location=module.parent, repo=repo, commit=repo.commit),)
)
@pytest.mark.asyncio
async def test_checkout(mocker, repo):
commit = "c950fc05a540dd76b944719c2a3302da2e2f3090"
m = _mock_run(mocker, repo, 0)
_mock_setup_repo(mocker, repo, commit)
git_path = repo.folder_path / ".git"
git_path.mkdir()
await repo._checkout(commit)
assert repo.commit == commit
m.assert_called_once_with(
ProcessFormatter().format(repo.GIT_CHECKOUT, path=repo.folder_path, rev=commit)
)
@pytest.mark.asyncio
async def test_checkout_ctx_manager(mocker, repo):
commit = "c950fc05a540dd76b944719c2a3302da2e2f3090"
m = mocker.patch.object(repo, "_checkout", autospec=True, return_value=async_return(None))
old_commit = repo.commit
async with repo.checkout(commit):
m.assert_called_with(commit, force_checkout=False)
m.return_value = async_return(None)
m.assert_called_with(old_commit, force_checkout=False)
@pytest.mark.asyncio
async def test_checkout_await(mocker, repo):
commit = "c950fc05a540dd76b944719c2a3302da2e2f3090"
m = mocker.patch.object(repo, "_checkout", autospec=True, return_value=async_return(None))
await repo.checkout(commit)
m.assert_called_once_with(commit, force_checkout=False)
@pytest.mark.asyncio
async def test_clone_with_branch(mocker, repo):
branch = repo.branch = "dont_add_commits"
commit = "a0ccc2390883c85a361f5a90c72e1b07958939fa"
repo.commit = ""
m = _mock_run(mocker, repo, 0)
_mock_setup_repo(mocker, repo, commit)
await repo.clone()
assert repo.commit == commit
m.assert_called_once_with(
ProcessFormatter().format(
repo.GIT_CLONE, branch=branch, url=repo.url, folder=repo.folder_path
)
)
@pytest.mark.asyncio
async def test_clone_without_branch(mocker, repo):
branch = "dont_add_commits"
commit = "a0ccc2390883c85a361f5a90c72e1b07958939fa"
repo.branch = None
repo.commit = ""
m = _mock_run(mocker, repo, 0)
_mock_setup_repo(mocker, repo, commit)
mocker.patch.object(repo, "current_branch", autospec=True, return_value=async_return(branch))
await repo.clone()
assert repo.commit == commit
m.assert_called_once_with(
ProcessFormatter().format(repo.GIT_CLONE_NO_BRANCH, url=repo.url, folder=repo.folder_path)
)
@pytest.mark.asyncio
async def test_update(mocker, repo):
old_commit = repo.commit
new_commit = "a0ccc2390883c85a361f5a90c72e1b07958939fa"
m = _mock_run(mocker, repo, 0)
_mock_setup_repo(mocker, repo, new_commit)
mocker.patch.object(
repo, "latest_commit", autospec=True, return_value=async_return(old_commit)
)
mocker.patch.object(repo, "hard_reset", autospec=True, return_value=async_return(None))
ret = await repo.update()
assert ret == (old_commit, new_commit)
m.assert_called_once_with(ProcessFormatter().format(repo.GIT_PULL, path=repo.folder_path))
# old tests
@pytest.mark.asyncio
async def test_add_repo(monkeypatch, repo_manager):
monkeypatch.setattr("redbot.cogs.downloader.repo_manager.Repo._run", fake_run_noprint)
monkeypatch.setattr(
"redbot.cogs.downloader.repo_manager.Repo.current_commit", fake_current_commit
)
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_lib_install_requirements(monkeypatch, library_installable, repo, tmpdir):
monkeypatch.setattr("redbot.cogs.downloader.repo_manager.Repo._run", fake_run_noprint)
monkeypatch.setattr(
"redbot.cogs.downloader.repo_manager.Repo.available_libraries", (library_installable,)
)
lib_path = Path(str(tmpdir)) / "cog_data_path" / "lib"
sharedlib_path = lib_path / "cog_shared"
sharedlib_path.mkdir(parents=True, exist_ok=True)
installed, failed = await repo.install_libraries(
target_dir=sharedlib_path, req_target_dir=lib_path
)
assert len(installed) == 1
assert len(failed) == 0
@pytest.mark.asyncio
async def test_remove_repo(monkeypatch, repo_manager):
monkeypatch.setattr("redbot.cogs.downloader.repo_manager.Repo._run", fake_run_noprint)
monkeypatch.setattr(
"redbot.cogs.downloader.repo_manager.Repo.current_commit", fake_current_commit
)
await repo_manager.add_repo(
url="https://github.com/tekulvw/Squid-Plugins", name="squid", branch="rewrite_cogs"
)
assert repo_manager.get_repo("squid") is not None
await repo_manager.delete_repo("squid")
assert repo_manager.get_repo("squid") is None
@pytest.mark.asyncio
async def test_existing_repo(mocker, repo_manager):
repo_manager.does_repo_exist = mocker.MagicMock(return_value=True)
with pytest.raises(ExistingGitRepo):
await repo_manager.add_repo("http://test.com", "test")
repo_manager.does_repo_exist.assert_called_once_with("test")
def test_tree_url_parse(repo_manager):
cases = [
{
"input": ("https://github.com/Tobotimus/Tobo-Cogs", None),
"expected": ("https://github.com/Tobotimus/Tobo-Cogs", None),
},
{
"input": ("https://github.com/Tobotimus/Tobo-Cogs", "V3"),
"expected": ("https://github.com/Tobotimus/Tobo-Cogs", "V3"),
},
{
"input": ("https://github.com/Tobotimus/Tobo-Cogs/tree/V3", None),
"expected": ("https://github.com/Tobotimus/Tobo-Cogs", "V3"),
},
{
"input": ("https://github.com/Tobotimus/Tobo-Cogs/tree/V3", "V4"),
"expected": ("https://github.com/Tobotimus/Tobo-Cogs", "V4"),
},
]
for test_case in cases:
assert test_case["expected"] == repo_manager._parse_url(*test_case["input"])
def test_tree_url_non_github(repo_manager):
cases = [
{
"input": ("https://gitlab.com/Tobotimus/Tobo-Cogs", None),
"expected": ("https://gitlab.com/Tobotimus/Tobo-Cogs", None),
},
{
"input": ("https://my.usgs.gov/bitbucket/scm/Tobotimus/Tobo-Cogs", "V3"),
"expected": ("https://my.usgs.gov/bitbucket/scm/Tobotimus/Tobo-Cogs", "V3"),
},
]
for test_case in cases:
assert test_case["expected"] == repo_manager._parse_url(*test_case["input"])