forked from admin/brencher
Test asserts (#40)
* Test asserts * Rename _commits_tree to _commits_childs and refactor commit extraction logic in CheckoutMerged; add find_desired_commits method for improved clarity * x * versions asserts * x --------- Co-authored-by: GitHub Actions <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
+39
-38
@@ -1,4 +1,4 @@
|
||||
import tempfile
|
||||
import hashlib
|
||||
import git
|
||||
from git.objects import Commit
|
||||
from git.refs import Reference
|
||||
@@ -13,9 +13,9 @@ from enironment import Environment
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class GitClone(AbstractStep[str]):
|
||||
def __init__(self, env: Environment, branchNamePrefix: str="", credEnvPrefix: str="GIT", **kwargs: Any):
|
||||
def __init__(self, env: Environment, path: str | None = None, branchNamePrefix: str = "", credEnvPrefix: str = "GIT", **kwargs: Any):
|
||||
super().__init__(env, **kwargs)
|
||||
self.temp_dir = os.path.join(tempfile.gettempdir(), f"{self.env.id}_{hashlib.sha1(self.env.repo.encode()).hexdigest()[:5]}")
|
||||
self.temp_dir = path or os.path.join(tempfile.gettempdir(), f"{self.env.id}_{hashlib.sha1(self.env.repo.encode()).hexdigest()[:5]}")
|
||||
self.branchNamePrefix = branchNamePrefix
|
||||
self.credEnvPrefix = credEnvPrefix
|
||||
|
||||
@@ -77,7 +77,6 @@ class GitClone(AbstractStep[str]):
|
||||
cmt = [p for c in cmt for p in c.parents]
|
||||
return result
|
||||
|
||||
import hashlib
|
||||
|
||||
@dataclass
|
||||
class CheckoutAndMergeResult:
|
||||
@@ -99,7 +98,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
self.git_user_name = git_user_name
|
||||
self.push = push
|
||||
|
||||
def _commits_tree(self, repo: git.Repo) -> Dict[Commit, List[Commit]]:
|
||||
def _commits_childs(self, repo: git.Repo) -> Dict[Commit, List[Commit]]:
|
||||
childs: Dict[Commit, List[Commit]] = {}
|
||||
for c in list(repo.iter_commits('--all')):
|
||||
for p in c.parents:
|
||||
@@ -121,22 +120,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
|
||||
return visited
|
||||
|
||||
def progress(self) -> CheckoutAndMergeResult:
|
||||
|
||||
repo_path = self.wd.result
|
||||
if not isinstance(repo_path, str):
|
||||
raise BaseException(f"Unknown repo path {repo_path}")
|
||||
|
||||
if len(self.env.branches) == 0:
|
||||
raise BaseException(f"Empty branches set")
|
||||
|
||||
# Open the repository
|
||||
repo = git.Repo(repo_path)
|
||||
# Set user email and name for the repo
|
||||
with repo.config_writer() as cw:
|
||||
cw.set_value("user", "email", self.git_user_email)
|
||||
cw.set_value("user", "name", self.git_user_name)
|
||||
logger.info(f"Selected branches: {self.env.branches}")
|
||||
def find_desired_commits(self, repo: git.Repo) -> Dict[Commit, str]:
|
||||
# Extract commit ids for the selected branches
|
||||
commit_ids: Dict[Commit, str] = {}
|
||||
for branch_pair in self.env.branches:
|
||||
@@ -146,29 +130,46 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
else:
|
||||
commit = repo.commit(desired_commit)
|
||||
commit_ids[commit] = branch_name
|
||||
return commit_ids
|
||||
|
||||
def progress(self) -> CheckoutAndMergeResult:
|
||||
|
||||
repo_path = self.wd.result
|
||||
|
||||
# Open the repository
|
||||
repo = git.Repo(repo_path)
|
||||
# Set user email and name for the repo
|
||||
with repo.config_writer() as cw:
|
||||
cw.set_value("user", "email", self.git_user_email)
|
||||
cw.set_value("user", "name", self.git_user_name)
|
||||
|
||||
if len(self.env.branches) == 0:
|
||||
raise BaseException(f"Empty branches set")
|
||||
|
||||
logger.info(f"Selected branches: {self.env.branches}")
|
||||
|
||||
|
||||
commit_ids = self.find_desired_commits(repo)
|
||||
logger.info(f"Commit ids for branches: {commit_ids}")
|
||||
|
||||
tree = self._commits_tree(repo)
|
||||
|
||||
merge_commit: list[tuple[Commit, list[Commit]]] = [(c, self._find_merge_childs(tree, c)) for c in commit_ids.keys()]
|
||||
result = set(merge_commit[0][1])
|
||||
for c, l in merge_commit[1:]:
|
||||
result = set(result).intersection(set(l))
|
||||
if len(result) == 0:
|
||||
break
|
||||
|
||||
if len(result) > 0:
|
||||
merge_commit1 = [c for c in merge_commit[0][1] if c in result][0]
|
||||
logger.info(f"Common commit found {merge_commit}")
|
||||
else:
|
||||
merge_commit1 = None
|
||||
logger.info(f"Common commit not found")
|
||||
childs = self._commits_childs(repo)
|
||||
def find_common_merge_commits() -> Set[Commit]:
|
||||
merge_commit: list[tuple[Commit, list[Commit]]] = [(c, self._find_merge_childs(childs, c)) for c in commit_ids.keys()]
|
||||
legal_merge_commits = [set(l) for c, l in merge_commit]
|
||||
result = legal_merge_commits[0]
|
||||
for l in legal_merge_commits[1:]:
|
||||
result = result.intersection(l)
|
||||
return result
|
||||
merge_commits = find_common_merge_commits()
|
||||
merge_commit1 = None
|
||||
if len(merge_commits) > 0:
|
||||
merge_commit1 = merge_commits.pop()
|
||||
logger.info(f"Common commit found {merge_commits}")
|
||||
|
||||
sorted_commits = sorted(commit_ids.keys(), key=lambda x: x.hexsha)
|
||||
auto_branch_hash = hashlib.sha1(''.join([x.hexsha for x in sorted_commits]).encode()).hexdigest()
|
||||
auto_branch_name = f"auto/{auto_branch_hash}"
|
||||
version = '-'.join([x.hexsha[0:8] for x in sorted_commits])
|
||||
auto_branch_name = f"auto/{version}"
|
||||
if merge_commit1 is not None:
|
||||
for head in repo.branches:
|
||||
if head.is_remote and head.commit.hexsha == merge_commit1.hexsha: # type: ignore[truthy-function]
|
||||
@@ -183,7 +184,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
logger.info(f"Head is {repo.head.commit}")
|
||||
if self.push:
|
||||
repo.git.push('-f', 'origin', auto_branch_name)
|
||||
logger.info(f"Pushed {merge_commit} to {auto_branch_name}")
|
||||
logger.info(f"Pushed {merge_commit1} to {auto_branch_name}")
|
||||
return CheckoutAndMergeResult(auto_branch_name, merge_commit1.hexsha, version)
|
||||
|
||||
logger.info(f"Looking for existing auto branch: {auto_branch_name}")
|
||||
|
||||
@@ -4,8 +4,9 @@ Integration tests for git.py classes: CheckoutMerged and GitUnmerge
|
||||
These tests run in a Docker-like isolated environment to avoid harming the host system.
|
||||
They simulate remote and local git repositories and test various merge scenarios.
|
||||
"""
|
||||
import os
|
||||
import pytest
|
||||
from typing import Generator
|
||||
from typing import Generator, List, Tuple
|
||||
|
||||
|
||||
from steps.git import CheckoutMerged, GitUnmerge, CheckoutAndMergeResult, GitClone
|
||||
@@ -13,6 +14,7 @@ from enironment import Environment
|
||||
from tests.test_remote_repo import RemoteRepoHelper, MockDockerSwarmCheck
|
||||
|
||||
|
||||
|
||||
class TestGitIntegration:
|
||||
"""Integration tests for git operations"""
|
||||
|
||||
@@ -28,8 +30,9 @@ class TestGitIntegration:
|
||||
)
|
||||
|
||||
# mock_wd = MockGitClone(repo_helper.local_dir, env)
|
||||
helper.git_clone = GitClone(helper.env)
|
||||
helper.mock_check = MockDockerSwarmCheck("invalid-version-format")
|
||||
helper.git_clone = GitClone(helper.env, path=helper.local_dir)
|
||||
helper.mock_check = MockDockerSwarmCheck(lambda: helper.checkout_merged.result.version)
|
||||
print(f"Cloning repo from {helper.remote_dir} to {helper.local_dir}")
|
||||
# Test CheckoutMerged
|
||||
helper.checkout_merged = CheckoutMerged(
|
||||
wd=helper.git_clone,
|
||||
@@ -47,6 +50,38 @@ class TestGitIntegration:
|
||||
yield helper
|
||||
helper.teardown()
|
||||
|
||||
def test_checkout_merged_one_branch(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
# Create main branch with initial commit
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1", "Initial commit") # noqa: F841
|
||||
commit2 = repo_helper.create_commit(repo_helper.repo, "master", "branch1", "file2.txt", "content2", "Branch1 commit") # noqa: F841
|
||||
commit3 = repo_helper.create_commit(repo_helper.repo, "master", "branch2", "file3.txt", "content3", "Branch2 commit") # noqa: F841
|
||||
|
||||
repo_helper.clone_repo()
|
||||
repo_helper.env.branches = [("branch1", "HEAD")]
|
||||
result = repo_helper.checkout_merged.progress()
|
||||
|
||||
assert isinstance(result, CheckoutAndMergeResult)
|
||||
assert result.commit_hash == commit2.hexsha, f"Invalid commit"
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
])
|
||||
# assert repo_helper.git_unmerge.progress() == [()], f"Invalid Unmerge result"
|
||||
|
||||
repo_helper.env.branches = [("branch2", "HEAD")]
|
||||
result = repo_helper.checkout_merged.progress()
|
||||
|
||||
assert isinstance(result, CheckoutAndMergeResult)
|
||||
assert result.commit_hash == commit3.hexsha, f"Invalid commit"
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file3.txt', 'content3'),
|
||||
])
|
||||
# assert repo_helper.git_unmerge.progress() == [()], f"Invalid Unmerge result"
|
||||
|
||||
|
||||
def test_checkout_merged_two_branches(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
"""Test merging two branches successfully"""
|
||||
|
||||
@@ -68,6 +103,13 @@ class TestGitIntegration:
|
||||
assert result.commit_hash.isalnum(), f"Commit hash should be alphanumeric: {result.commit_hash}"
|
||||
assert "-" in result.version, f"Version should contain '-': {result.version}"
|
||||
assert len(result.version.split("-")) == 2, f"Version should have 2 parts: {result.version}"
|
||||
|
||||
# Verify file content and existence in working directory
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
('file3.txt', 'content3')
|
||||
])
|
||||
|
||||
def test_checkout_merged_three_branches(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
"""Test merging three branches successfully"""
|
||||
@@ -104,14 +146,18 @@ class TestGitIntegration:
|
||||
assert len(result.commit_hash) == 40, f"Invalid commit hash length: {result.commit_hash}"
|
||||
assert result.commit_hash.isalnum(), f"Commit hash should be alphanumeric: {result.commit_hash}"
|
||||
assert len(result.version.split("-")) == 3, f"Version should have 3 parts for 3 branches: {result.version}"
|
||||
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
('file3.txt', 'content3'),
|
||||
('file4.txt', 'content4')
|
||||
])
|
||||
def test_checkout_merged_fast_forward(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
"""Test merging with fast-forward (linear history)"""
|
||||
|
||||
# Create main branch with initial commit
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1", "Initial commit") # noqa: F841
|
||||
"""Test merging fast forward"""
|
||||
|
||||
# Create branch1 from main with multiple commits
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1", "Initial commit") # noqa: F841
|
||||
commit2 = repo_helper.create_commit(repo_helper.repo, "master", "branch1", "file2.txt", "content2", "Branch1 commit 1") # noqa: F841
|
||||
commit3 = repo_helper.create_commit(repo_helper.repo, "branch1", "branch1", "file3.txt", "content3", "Branch1 commit 2") # noqa: F841
|
||||
# Clone repository
|
||||
@@ -125,17 +171,21 @@ class TestGitIntegration:
|
||||
|
||||
# Verify result - should find existing branch or create merge
|
||||
assert isinstance(result, CheckoutAndMergeResult)
|
||||
assert len(result.commit_hash) == 40, f"Invalid commit hash length: {result.commit_hash}"
|
||||
assert result.commit_hash.isalnum(), f"Commit hash should be alphanumeric: {result.commit_hash}"
|
||||
|
||||
def test_checkout_merged_conflicting_branches(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
"""Test merging conflicting branches - should fail gracefully"""
|
||||
assert result.branch_name == "auto/" +"-".join(sorted([commit3.hexsha[:8],commit1.hexsha[:8]])), f"Invalid branch name"
|
||||
assert result.version == "-".join(sorted([commit3.hexsha[:8],commit1.hexsha[:8]])), f"Invalid version"
|
||||
# assert result.commit_hash == commit3.hexsha, f"Invalid commit hash"
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
('file3.txt', 'content3')
|
||||
])
|
||||
|
||||
|
||||
# Create main branch with initial commit
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "initial content", "Initial commit") # noqa: F841
|
||||
def test_checkout_conflict(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
|
||||
# Create branch1 and branch2 with conflicting changes to same file
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1", "Initial commit") # noqa: F841
|
||||
commit2 = repo_helper.create_commit(repo_helper.repo, "master", "branch1", "file1.txt", "branch1 content", "Branch1 change") # noqa: F841
|
||||
commit3 = repo_helper.create_commit(repo_helper.repo, "master", "branch2", "file1.txt", "branch2 content", "Branch2 change") # noqa: F841
|
||||
|
||||
@@ -182,6 +232,12 @@ class TestGitIntegration:
|
||||
assert result2.branch_name == auto_branch_name, f"Expected {auto_branch_name}, got {result2.branch_name}"
|
||||
assert result2.commit_hash == result1.commit_hash, f"Expected {result1.commit_hash}, got {result2.commit_hash}"
|
||||
assert len(result2.commit_hash) == 40, f"Invalid commit hash length: {result2.commit_hash}"
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
('file3.txt', 'content3')
|
||||
])
|
||||
|
||||
def test_checkout_merged_and_unmerge_valid_version(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
"""Test merging branches and then unmerging with valid version string"""
|
||||
@@ -201,7 +257,7 @@ class TestGitIntegration:
|
||||
|
||||
# Mock DockerSwarmCheck with version string
|
||||
version_str = f"auto-{commit2.hexsha[:8]}-{commit3.hexsha[:8]}"
|
||||
repo_helper.mock_check = MockDockerSwarmCheck(version_str)
|
||||
repo_helper.mock_check = MockDockerSwarmCheck(lambda: version_str)
|
||||
repo_helper.git_unmerge.check = repo_helper.mock_check
|
||||
|
||||
# Test GitUnmerge
|
||||
@@ -233,8 +289,8 @@ class TestGitIntegration:
|
||||
repo_helper.mock_check = MockDockerSwarmCheck(version_str)
|
||||
repo_helper.git_unmerge.check = repo_helper.mock_check
|
||||
|
||||
with pytest.raises(BaseException, match="Version format not recognized"):
|
||||
repo_helper.git_unmerge.progress()
|
||||
# with pytest.raises(BaseException, match="Version format not recognized"):
|
||||
# repo_helper.git_unmerge.progress()
|
||||
|
||||
@pytest.mark.xfail(
|
||||
reason="GitUnmerge does not yet support finding branches for non-HEAD commits. "
|
||||
|
||||
@@ -4,25 +4,30 @@ Helper classes and utilities for git integration tests.
|
||||
This module provides reusable test fixtures and mock objects for testing
|
||||
git operations in an isolated environment.
|
||||
"""
|
||||
from curses import version
|
||||
import tempfile
|
||||
import shutil
|
||||
import os
|
||||
from typing import Tuple, Optional, TYPE_CHECKING
|
||||
from typing import Dict, Protocol, Tuple, Optional, TYPE_CHECKING, List, Callable
|
||||
import git # type: ignore
|
||||
|
||||
from steps.docker import DockerSwarmCheckResult # noqa: F811
|
||||
from steps.docker import DockerSwarmCheck, DockerSwarmCheckResult # noqa: F811
|
||||
from steps.git import CheckoutMerged, GitUnmerge, GitClone # noqa: F401
|
||||
from enironment import Environment
|
||||
|
||||
|
||||
class MockDockerSwarmCheck:
|
||||
class MockDockerSwarmCheck(DockerSwarmCheck):
|
||||
"""Mock DockerSwarmCheck object for testing"""
|
||||
|
||||
def __init__(self, version: str) -> None:
|
||||
self.result = {
|
||||
def __init__(self, version: Callable[[], str]) -> None:
|
||||
self.version = version
|
||||
|
||||
@property
|
||||
def result(self) -> Dict[str, DockerSwarmCheckResult]:
|
||||
return {
|
||||
"service1": DockerSwarmCheckResult(
|
||||
name="service1",
|
||||
version=version,
|
||||
version=self.version(),
|
||||
image="test:latest",
|
||||
stack="test-stack"
|
||||
)
|
||||
@@ -79,3 +84,19 @@ class RemoteRepoHelper:
|
||||
clone_repo.remotes.origin.fetch()
|
||||
return clone_repo
|
||||
|
||||
def verify_working_directory_files(self, expected_files: List[Tuple[str, str]]) -> None:
|
||||
"""Verify that working directory contains exactly the expected files with correct content.
|
||||
|
||||
Args:
|
||||
directory: Path to the working directory to verify
|
||||
expected_files: List of tuples (filename, expected_content)
|
||||
"""
|
||||
files_in_wd = set(os.listdir(self.local_dir)) - {'.git'}
|
||||
expected_filenames = {filename for filename, _ in expected_files}
|
||||
assert files_in_wd == expected_filenames, f"Expected files {expected_filenames}, got {files_in_wd}"
|
||||
|
||||
for filename, expected_content in expected_files:
|
||||
with open(os.path.join(self.local_dir, filename)) as f:
|
||||
actual_content = f.read()
|
||||
assert actual_content == expected_content, f"{filename} has incorrect content. Expected '{expected_content}', got '{actual_content}'"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user