Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: vcs.git.backend tests #9507

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
106 changes: 101 additions & 5 deletions tests/vcs/git/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import pytest

from dulwich.client import FetchPackResult
from dulwich.repo import Repo
from tests.helpers import MOCK_DEFAULT_GIT_REVISION

from poetry.packages.direct_origin import _get_package_from_git
from poetry.vcs.git.backend import Git
from poetry.vcs.git.backend import GitRefSpec
from poetry.vcs.git.backend import annotated_tag
from poetry.vcs.git.backend import is_revision_sha
from poetry.vcs.git.backend import urlpathjoin
Expand All @@ -15,6 +19,36 @@
VALID_SHA = "c5c7624ef64f34d9f50c3b7e8118f7f652fddbbd"


@pytest.fixture(autouse=True)
def git_mock() -> Repo:
repo = MagicMock(spec=Repo)

repo.get_config.return_value.get.return_value = (
b"https://github.com/python-poetry/poetry.git"
)

repo.head.return_value = MOCK_DEFAULT_GIT_REVISION.encode("utf-8")

# Clear any cache in the Git module
_get_package_from_git.cache_clear()

return repo


@pytest.fixture()
def fetch_pack_result() -> MagicMock:
mock_fetch_pack_result = MagicMock(spec=FetchPackResult)
mock_fetch_pack_result.refs = {
b"refs/heads/main": b"abc123",
b"refs/tags/v1.0.0": b"def456",
annotated_tag(b"refs/tags/v1.0.0"): b"def456",
b"HEAD": b"abc123",
}
mock_fetch_pack_result.symrefs = {b"HEAD": b"refs/heads/main"}

return mock_fetch_pack_result


def test_invalid_revision_sha() -> None:
result = is_revision_sha("invalid_input")
assert result is False
Expand Down Expand Up @@ -55,15 +89,26 @@ def test_annotated_tag(tag: str | bytes) -> None:
assert tag == b"my-tag^{}"


def test_get_remote_url() -> None:
repo = MagicMock(spec=Repo)
repo.get_config.return_value.get.return_value = (
b"https://github.com/python-poetry/poetry.git"
)
def test_get_remote_url(git_mock: Repo) -> None:
repo = git_mock

assert Git.get_remote_url(repo) == "https://github.com/python-poetry/poetry.git"


def test_get_revision(git_mock: Repo) -> None:
assert Git.get_revision(git_mock) == MOCK_DEFAULT_GIT_REVISION


def test_info(git_mock: Repo) -> None:
repo = git_mock
info = Git.info(repo)

assert info.origin == "https://github.com/python-poetry/poetry.git"
assert (
info.revision == MOCK_DEFAULT_GIT_REVISION
) # revision already mocked in helper


@pytest.mark.parametrize(
"url, expected_result",
[
Expand All @@ -75,3 +120,54 @@ def test_urlpathjoin(url: str, expected_result: str) -> None:
path = "../other-repo"
result = urlpathjoin(url, path)
assert result == expected_result


def test_git_refspec() -> None:
git_ref = GitRefSpec("main", "1234", "v2")

assert git_ref.branch == "main"
assert git_ref.revision == "1234"
assert git_ref.tag == "v2"
assert git_ref.ref == b"HEAD"


@pytest.mark.parametrize(
"refspec_params, expected_ref, expected_branch, expected_revision, expected_tag",
[
({"branch": "main"}, b"refs/heads/main", "main", None, None),
(
{"revision": "v1.0.0"},
annotated_tag(b"refs/tags/v1.0.0"),
None,
None,
"v1.0.0",
),
({"revision": "abc"}, b"refs/heads/main", None, "abc", None),
],
)
Comment on lines +134 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicks, but can you update this to pass in GitRefSpec instances as parameters instead? And make sure we cover, branch, revision, tag, ref parameters.

Additionally, if we can move to using real(ish) short and full shas instead would help with reasoning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And resolution should verify that shart shas when it exists in the fetch pack gets resolved to full shas. So, abc must resolve to abc123 from the fixture.

def test_git_ref_spec_resolve(
fetch_pack_result: FetchPackResult,
refspec_params: dict[str, str | bytes | None],
expected_ref: bytes,
expected_branch: str,
expected_revision: str,
expected_tag: str,
) -> None:
"""
Parameterized test for GitRefSpec resolve with different parameters.

Args:
fetch_pack_result (FetchPackResult): The mocked FetchPackResult object.
refspec_params (dict): Parameters for creating GitRefSpec.
expected_ref (bytes): The expected resolved ref.
expected_branch (str): The expected resolved branch.
expected_revision (str): The expected resolved revision.
expected_tag (str): The expected resolved tag.
"""
Comment on lines +157 to +166
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Parameterized test for GitRefSpec resolve with different parameters.
Args:
fetch_pack_result (FetchPackResult): The mocked FetchPackResult object.
refspec_params (dict): Parameters for creating GitRefSpec.
expected_ref (bytes): The expected resolved ref.
expected_branch (str): The expected resolved branch.
expected_revision (str): The expected resolved revision.
expected_tag (str): The expected resolved tag.
"""

refspec = GitRefSpec(**refspec_params)
refspec.resolve(fetch_pack_result)

assert refspec.ref == expected_ref
assert refspec.branch == expected_branch
assert refspec.revision == expected_revision
assert refspec.tag == expected_tag
Loading