2024-11-02 18:46:53 +00:00
|
|
|
import unittest.mock
|
2024-11-02 22:30:39 +00:00
|
|
|
|
|
|
|
import pytest
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
from frg.context import GitContext, get_git_context, parse_remote_url
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="mock_context")
|
|
|
|
def fixture_mock_context():
|
|
|
|
return GitContext(
|
|
|
|
repo_name="test-repo",
|
|
|
|
repo_author="test-user",
|
|
|
|
current_branch="main",
|
|
|
|
host="https://git.my.server",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_subprocess(mock_context):
|
|
|
|
def mock_run_impl(*args, **kwargs):
|
|
|
|
call_args = args[0][1:]
|
|
|
|
return_val = unittest.mock.Mock()
|
|
|
|
|
|
|
|
if call_args == ["branch", "--show-current"]:
|
|
|
|
return_val.stdout = mock_context.current_branch.encode("utf8")
|
|
|
|
if call_args == ["config", "--get", "remote.origin.url"]:
|
|
|
|
return_val.stdout = f"{mock_context.host}/{mock_context.repo_author}/{mock_context.repo_name}.git".encode(
|
|
|
|
"utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
return return_val
|
|
|
|
|
|
|
|
with unittest.mock.patch("subprocess.run") as mock_run:
|
|
|
|
mock_run.side_effect = mock_run_impl
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
def test_git_context_pulls_current_branch(mock_context):
|
|
|
|
assert get_git_context().current_branch == mock_context.current_branch
|
|
|
|
|
|
|
|
|
|
|
|
def test_git_context_pulls_host(mock_context):
|
|
|
|
assert get_git_context().host == mock_context.host
|
|
|
|
|
|
|
|
|
|
|
|
def test_git_context_pulls_repo_owner(mock_context):
|
|
|
|
assert get_git_context().repo_author == mock_context.repo_author
|
|
|
|
|
|
|
|
|
|
|
|
def test_git_context_pulls_repo_name(mock_context):
|
|
|
|
assert get_git_context().repo_name == mock_context.repo_name
|
|
|
|
|
|
|
|
|
|
|
|
def test_git_context_remaps_host_if_aliase_provided(mock_context):
|
|
|
|
aliases = {mock_context.host: "other.host"}
|
|
|
|
|
|
|
|
assert get_git_context(domain_aliases=aliases).host == "other.host"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"url, parts",
|
|
|
|
[
|
|
|
|
["git@myhost.com:user/repo.git", ("https://myhost.com", "user", "repo")],
|
|
|
|
["https://myhost.com/user/repo.git", ("https://myhost.com", "user", "repo")],
|
|
|
|
],
|
|
|
|
ids=["ssh", "http"],
|
|
|
|
)
|
|
|
|
def test_parse_remote_url_parses_url(url, parts):
|
|
|
|
actual_parts = parse_remote_url(url)
|
|
|
|
|
|
|
|
assert parts == actual_parts
|