Skip to content

Commit

Permalink
test: adds unit tests to GitHub and GitLab providers
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 28, 2023
1 parent e3b86e8 commit b044d32
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
2 changes: 2 additions & 0 deletions tests/trestlebot/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def test_with_target_branch(monkeypatch, valid_args_dict, capsys):
args_dict["target-branch"] = "main"
monkeypatch.setattr(sys, "argv", ["trestlebot", *args_dict_to_list(args_dict)])

# Patch is_github_actions since these tests will be running in
# GitHub Actions
with patch("trestlebot.cli.is_github_actions") as mock_check:
mock_check.return_value = False

Expand Down
17 changes: 17 additions & 0 deletions tests/trestlebot/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Test for GitHub provider logic"""

from typing import Tuple
from unittest.mock import patch

import pytest
from git.repo import Repo
Expand Down Expand Up @@ -70,3 +71,19 @@ def test_parse_repository_with_incorrect_name() -> None:
match="https://notgithub.com/owner/repo.git is an invalid GitHub repo URL",
):
gh.parse_repository("https://notgithub.com/owner/repo.git")


def test_create_pull_request_invalid_repo() -> None:
"""Test triggering an error during pull request creation"""
gh = GitHub("fake")
with patch("github3.GitHub.repository") as mock_pull:
mock_pull.return_value = None

with pytest.raises(
GitProviderException,
match="Repository for owner/repo cannot be None",
):
gh.create_pull_request(
"owner", "repo", "main", "test", "My PR", "Has Changes"
)
mock_pull.assert_called_once()
41 changes: 40 additions & 1 deletion tests/trestlebot/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

"""Test for GitLab provider logic"""

from typing import Tuple
from typing import Callable, Tuple
from unittest.mock import patch

import pytest
from git.repo import Repo
from gitlab.exceptions import GitlabAuthenticationError, GitlabCreateError

from tests.testutils import clean
from trestlebot.gitlab import GitLab
Expand Down Expand Up @@ -88,3 +90,40 @@ def test_parse_repository_with_incorrect_name() -> None:
match="https://notgitlab.com/owner/repo.git is an invalid repo URL",
):
gl.parse_repository("https://notgitlab.com/owner/repo.git")


def create_side_effect(name: str) -> None:
raise GitlabCreateError("example")


def auth_side_effect(name: str) -> None:
raise GitlabAuthenticationError("example")


@pytest.mark.parametrize(
"side_effect, msg",
[
(create_side_effect, "Failed to create merge request in .*: example"),
(
auth_side_effect,
"Authentication error during merge request creation in .*: example",
),
],
)
def test_create_pull_request_with_exceptions(
side_effect: Callable[[str], None], msg: str
) -> None:
"""Test triggering an error during pull request creation"""
gl = GitLab("fake")

with patch("gitlab.v4.objects.ProjectManager.get") as mock_get:
mock_get.side_effect = side_effect

with pytest.raises(
GitProviderException,
match=msg,
):
gl.create_pull_request(
"owner", "repo", "main", "test", "My PR", "Has Changes"
)
mock_get.assert_called_once()
32 changes: 19 additions & 13 deletions trestlebot/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Optional, Tuple

import github3
from github3.exceptions import AuthenticationFailed
from github3.repos.repo import Repository

from trestlebot.provider import GitProvider, GitProviderException
Expand Down Expand Up @@ -85,23 +86,28 @@ def create_pull_request(
Returns:
Pull request number
"""
repository: Optional[Repository] = self._session.repository(
owner=ns, repository=repo_name
)
if repository is None:
raise GitProviderException(
f"Repository for {ns}/{repo_name} cannot be None"
try:
repository: Optional[Repository] = self._session.repository(
owner=ns, repository=repo_name
)
if repository is None:
raise GitProviderException(
f"Repository for {ns}/{repo_name} cannot be None"
)

pull_request = repository.create_pull(
title=title, body=body, base=base_branch, head=head_branch
)
pull_request = repository.create_pull(
title=title, body=body, base=base_branch, head=head_branch
)

if pull_request:
return pull_request.number
else:
if pull_request:
return pull_request.number
else:
raise GitProviderException(
"Failed to create pull request in {ns}/{repo_name} for {head_branch} to {base_branch}"
)
except AuthenticationFailed as e:
raise GitProviderException(
"Failed to create pull request in {ns}/{repo_name} for {head_branch} to {base_branch}"
f"Authentication error during pull request creation in {ns}/{repo_name}: {e}"
)


Expand Down

0 comments on commit b044d32

Please sign in to comment.