From 7e6c825a7835589509e1daaec736354e3470c1e3 Mon Sep 17 00:00:00 2001 From: Philip Claesson Date: Fri, 17 Nov 2023 15:34:39 +0100 Subject: [PATCH] write tests for tag_tracker --- .../opal_common/git/tests/conftest.py | 12 +++ .../opal_common/git/tests/tag_tracker_test.py | 78 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 packages/opal-common/opal_common/git/tests/tag_tracker_test.py diff --git a/packages/opal-common/opal_common/git/tests/conftest.py b/packages/opal-common/opal_common/git/tests/conftest.py index c60099a5..cbe156cb 100644 --- a/packages/opal-common/opal_common/git/tests/conftest.py +++ b/packages/opal-common/opal_common/git/tests/conftest.py @@ -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: @@ -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 diff --git a/packages/opal-common/opal_common/git/tests/tag_tracker_test.py b/packages/opal-common/opal_common/git/tests/tag_tracker_test.py new file mode 100644 index 00000000..fe7d007f --- /dev/null +++ b/packages/opal-common/opal_common/git/tests/tag_tracker_test.py @@ -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")