Skip to content

Commit

Permalink
write tests for tag_tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
philipclaesson committed Nov 17, 2023
1 parent 3d6b3a9 commit 7e6c825
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/opal-common/opal_common/git/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def create_rename_file_commit(
repo.index.move([filename, new_filename])
repo.index.commit(commit_msg, author=author)

@staticmethod
def create_new_tag(repo: Repo, tag_name: str):
repo.create_tag(tag_name)

@staticmethod
def update_tag_to_head(repo: Repo, tag_name: str):
repo.delete_tag(tag_name)
repo.create_tag(tag_name)


@pytest.fixture
def helpers() -> Helpers:
Expand Down Expand Up @@ -140,6 +149,9 @@ def local_repo(tmp_path, helpers: Helpers) -> Repo:

# create a "delete" commit
helpers.create_delete_file_commit(repo, root / "deleted.rego")

# create a test tag
helpers.create_new_tag(repo, "test_tag")
return repo


Expand Down
78 changes: 78 additions & 0 deletions packages/opal-common/opal_common/git/tests/tag_tracker_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import sys

import pytest

# Add root opal dir to use local src as package for tests (i.e, no need for python -m pytest)
root_dir = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
os.path.pardir,
os.path.pardir,
os.path.pardir,
)
)
sys.path.append(root_dir)

from pathlib import Path

from git import Repo
from git.objects.commit import Commit
from opal_common.git.tag_tracker import TagTracker
from opal_common.git.exceptions import GitFailed


def test_pull_with_no_changes(local_repo_clone: Repo):
"""Test pulling when there are no changes on the remote repo."""
repo: Repo = local_repo_clone # local repo, cloned from another local repo
tracker = TagTracker(repo=repo, tag_name="test_tag")
latest_commit: Commit = repo.head.commit
assert latest_commit == tracker.latest_commit == tracker.prev_commit
has_changes, prev, latest = tracker.pull() # pulls from origin
assert has_changes == False
assert latest_commit == prev == latest


def test_pull_with_new_commits(
local_repo: Repo,
local_repo_clone: Repo,
helpers,
):
"""Test pulling when there are changes (new commits) on the remote repo."""
remote_repo: Repo = (
local_repo # local repo, the 'origin' remote of 'local_repo_clone'
)
repo: Repo = local_repo_clone # local repo, cloned from 'local_repo'

tracker = TagTracker(repo=repo, tag_name="test_tag")
most_recent_commit_before_pull: Commit = repo.head.commit

assert (
most_recent_commit_before_pull == tracker.latest_commit == tracker.prev_commit
)

# create new file commit on the remote repo
helpers.create_new_file_commit(
remote_repo, Path(remote_repo.working_tree_dir) / "2.txt"
)

helpers.update_tag_to_head(remote_repo, "test_tag")

# now the remote repo tag is pointing at a different commit
assert remote_repo.tags.__getattr__("test_tag").commit != repo.head.commit
# and our tag tracker does not know it yet
assert remote_repo.tags.__getattr__("test_tag").commit != tracker.latest_commit

has_changes, prev, latest = tracker.pull() # pulls from origin
assert has_changes == True
assert prev != latest
assert most_recent_commit_before_pull == prev
assert (
remote_repo.tags.__getattr__("test_tag").commit == repo.tags.__getattr__("test_tag").commit == latest == tracker.latest_commit
)


def test_tracked_branch_does_not_exist(local_repo: Repo):
"""Test that tag tracker throws when tag does not exist."""
with pytest.raises(GitFailed):
tracker = TagTracker(local_repo, tag_name="no_such_tag")

0 comments on commit 7e6c825

Please sign in to comment.