-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
indrajithi
wants to merge
9
commits into
python-poetry:main
Choose a base branch
from
indrajithi:feature/vcs-backend-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
53561fc
test: add test for Git.get_revision and info
indrajithi 89b76a6
test: vsc.backend GitRefSpec.test_resolve
indrajithi cc64c83
fix missing types in test_backend
indrajithi 3405fc1
Merge branch 'main' into feature/vcs-backend-tests
indrajithi 698c79e
Parameterized test for GitRefSpec
indrajithi d396887
Merge branch 'python-poetry:main' into feature/vcs-backend-tests
indrajithi 7048dbb
Merge branch 'feature/vcs-backend-tests' of github.com:indrajithi/poe…
indrajithi 2deaefb
Merge branch 'main' into feature/vcs-backend-tests
Secrus d849c4f
Fix typing issue in `test_backend.py`
Secrus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||||||||
|
@@ -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 | ||||||||||||||||||||||
|
@@ -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", | ||||||||||||||||||||||
[ | ||||||||||||||||||||||
|
@@ -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), | ||||||||||||||||||||||
], | ||||||||||||||||||||||
) | ||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 toabc123
from the fixture.