-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
218 additions
and
5 deletions.
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
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
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 |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import io | ||
import sys | ||
import unittest | ||
from unittest.mock import patch | ||
from unittest.mock import ANY, MagicMock, PropertyMock, patch | ||
|
||
import git | ||
|
||
from skaraMirror import ( | ||
add_skara_upstream, | ||
check_args, | ||
clone_github_repo, | ||
fetch_and_sort_tags, | ||
perform_merge_from_skara_into_git, | ||
perform_merge_into_release_from_master, | ||
sort_jdk8_tags, | ||
sort_jdk11plus_tags, | ||
) | ||
|
||
|
||
class SuppressPrint: | ||
def __enter__(self): | ||
self._original_stdout = sys.stdout | ||
sys.stdout = io.StringIO() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
sys.stdout = self._original_stdout | ||
|
||
|
||
class TestCheckArgs(unittest.TestCase): | ||
@patch("sys.argv", ["script_name", "jdk8u", "https://example.com/repo", "dev"]) | ||
def test_with_full_arguments(self): | ||
|
@@ -25,10 +43,200 @@ def test_with_minimum_arguments(self): | |
self.assertEqual(args.branch, "master") | ||
|
||
|
||
class TestCloneGitHubRepo(unittest.TestCase): | ||
@patch("skaraMirror.os.path.isdir") | ||
@patch("skaraMirror.Repo.clone_from") | ||
@patch("skaraMirror.tqdm") | ||
def test_clone_repo_not_exists(self, mock_tqdm, mock_clone_from, mock_isdir): | ||
""" | ||
Test cloning a repository that does not already exist locally. | ||
""" | ||
# Setup | ||
mock_isdir.return_value = False | ||
jdk_version = "jdk11u" | ||
repo_url = "[email protected]:adoptium/jdk11u.git" | ||
workspace = "/tmp/workspace" | ||
|
||
# Execute | ||
with SuppressPrint(): | ||
clone_github_repo(jdk_version, repo_url, workspace) | ||
|
||
# Assert | ||
mock_isdir.assert_called_once_with("/tmp/workspace/jdk11u") | ||
mock_clone_from.assert_called_once_with( | ||
"[email protected]:adoptium/jdk11u.git", "/tmp/workspace/jdk11u", progress=ANY | ||
) | ||
|
||
@patch("skaraMirror.os.path.isdir") | ||
@patch("skaraMirror.Repo.clone_from") | ||
@patch("skaraMirror.tqdm") | ||
def test_clone_repo_exists(self, mock_tqdm, mock_clone_from, mock_isdir): | ||
""" | ||
Test attempting to clone a repository that already exists locally. | ||
""" | ||
# Setup | ||
mock_isdir.return_value = True | ||
jdk_version = "jdk11u" | ||
repo_url = "[email protected]:adoptium/jdk11u.git" | ||
workspace = "/tmp/workspace" | ||
|
||
# Execute | ||
with SuppressPrint(): | ||
clone_github_repo(jdk_version, repo_url, workspace) | ||
|
||
# Assert | ||
mock_isdir.assert_called_once_with("/tmp/workspace/jdk11u") | ||
mock_clone_from.assert_not_called() # Clone should not be called since repo exists | ||
|
||
|
||
class TestAddSkaraUpstream(unittest.TestCase): | ||
@patch("skaraMirror.Repo") | ||
def test_add_skara_remote_not_exist(self, mock_repo): | ||
# Setup: Configure the mock repo object | ||
mock_repo.return_value.remotes = MagicMock() | ||
mock_repo.return_value.remotes.__iter__.return_value = [] | ||
|
||
# Define your function parameters | ||
workspace = "/tmp/workspace" | ||
jdk_version = "jdk11u" | ||
skara_repo = "https://github.com/openjdk/skara" | ||
branch = "master" | ||
|
||
# Execute the function | ||
with SuppressPrint(): | ||
add_skara_upstream(workspace, jdk_version, skara_repo, branch) | ||
|
||
# Assertions: Check if the remote was added | ||
mock_repo.return_value.create_remote.assert_called_once_with( | ||
"skara", skara_repo | ||
) | ||
|
||
@patch("skaraMirror.Repo") | ||
def test_skara_remote_already_exists(self, mock_repo): | ||
# Setup: Simulate existing 'skara' remote | ||
mock_remote = MagicMock() | ||
mock_remote.name = "skara" | ||
mock_repo.return_value.remotes = MagicMock() | ||
mock_repo.return_value.remotes.__iter__.return_value = [mock_remote] | ||
|
||
# Execute the function with the same parameters as before | ||
with SuppressPrint(): | ||
add_skara_upstream( | ||
"/tmp/workspace", "jdk11u", "https://github.com/openjdk/skara", "master" | ||
) | ||
|
||
# Assertions: Ensure create_remote was not called since 'skara' exists | ||
mock_repo.return_value.create_remote.assert_not_called() | ||
|
||
|
||
class TestPerformMergeFromSkaraIntoGit(unittest.TestCase): | ||
@patch("skaraMirror.Repo") | ||
@patch("skaraMirror.tqdm") | ||
def test_successful_merge_from_skara(self, mock_tqdm, mock_repo): | ||
""" | ||
Test successful fetching, rebasing, and pushing from the Skara remote. | ||
""" | ||
# Setup | ||
workspace = "/tmp/workspace" | ||
github_repo = "jdk11u" | ||
branch = "master" | ||
|
||
# Mock remotes and methods | ||
mock_skara_remote = MagicMock() | ||
mock_repo.return_value.remotes.skara = mock_skara_remote | ||
mock_repo.return_value.git.rebase = MagicMock() | ||
mock_repo.return_value.remotes.origin.push = MagicMock() | ||
|
||
# Execute | ||
with SuppressPrint(): | ||
perform_merge_from_skara_into_git(workspace, github_repo, branch) | ||
|
||
# Assert | ||
mock_skara_remote.fetch.assert_called_once() | ||
mock_repo.return_value.git.rebase.assert_called_once_with(f"skara/{branch}") | ||
mock_repo.return_value.remotes.origin.push.assert_called_once_with( | ||
branch, follow_tags=True, progress=ANY | ||
) | ||
|
||
@patch("skaraMirror.Repo") | ||
def test_git_command_error_during_fetch(self, mock_repo): | ||
""" | ||
Test handling of GitCommandError during fetch operation from Skara remote. | ||
""" | ||
# Setup to raise GitCommandError on fetch | ||
mock_repo.return_value.remotes.skara.fetch.side_effect = ( | ||
git.exc.GitCommandError("fetch", "error") | ||
) | ||
|
||
workspace = "/tmp/workspace" | ||
github_repo = "jdk11u" | ||
branch = "master" | ||
|
||
# Expect the function to handle the exception and not crash | ||
with SuppressPrint(): | ||
with self.assertRaises(SystemExit) as cm: | ||
perform_merge_from_skara_into_git(workspace, github_repo, branch) | ||
|
||
self.assertEqual(cm.exception.code, 1) | ||
|
||
|
||
class TestPerformMergeIntoReleaseFromMaster(unittest.TestCase): | ||
def setUp(self): | ||
self.mock_repo = MagicMock(spec=git.Repo) | ||
|
||
# Mock the master branch as initially the only branch | ||
self.mock_master_branch = MagicMock(spec=git.Head, name="master") | ||
|
||
# Prepare a mock for the repo's heads that supports item getting and iteration | ||
self.mock_heads = {"master": self.mock_master_branch} | ||
|
||
# Use PropertyMock to simulate the repo.heads dynamic nature | ||
type(self.mock_repo).heads = PropertyMock(side_effect=lambda: self.mock_heads) | ||
|
||
# Mock remotes setup | ||
self.mock_origin_remote = MagicMock(spec=git.Remote, name="origin") | ||
self.mock_repo.remotes = MagicMock() | ||
self.mock_repo.remotes.__getitem__.side_effect = ( | ||
lambda x: self.mock_origin_remote if x == "origin" else None | ||
) | ||
|
||
# Mock fetching, pushing, and tag listing | ||
self.mock_origin_remote.fetch = MagicMock() | ||
self.mock_origin_remote.push = MagicMock() | ||
self.mock_tags = ["jdk-11.0.1+10", "jdk-11.0.2+9"] | ||
self.mock_repo.git.tag.return_value = "\n".join(self.mock_tags) | ||
|
||
@patch("skaraMirror.subprocess.run") | ||
@patch("skaraMirror.Repo") | ||
@patch("skaraMirror.tqdm") | ||
def test_release_branch_does_not_exist( | ||
self, mock_tqdm, mock_repo_class, mock_subprocess_run | ||
): | ||
mock_repo_class.return_value = self.mock_repo | ||
|
||
# Assert setup: Verify initially no 'release' branch | ||
self.assertNotIn("release", self.mock_repo.heads) | ||
|
||
# Simulate the function's execution | ||
with SuppressPrint(): | ||
perform_merge_into_release_from_master("/tmp/workspace", "jdk11u", "master") | ||
|
||
# verify that the patch was applied to the branch | ||
mock_subprocess_run.assert_called() | ||
|
||
# Dynamically add 'release' branch to simulate its creation during function execution | ||
self.mock_heads["release"] = MagicMock(spec=git.Head, name="release") | ||
|
||
# Verify 'release' branch creation logic was triggered | ||
self.assertIn("release", self.mock_repo.heads) | ||
self.mock_repo.git.checkout.assert_called_once_with( | ||
"-b", "release", "origin/release" | ||
) | ||
self.mock_repo.git.tag.assert_called() | ||
|
||
|
||
class TestFetchAndSortTags(unittest.TestCase): | ||
@patch( | ||
"skaraMirror.Repo" | ||
) # Adjust the patch path according to your script's structure | ||
@patch("skaraMirror.Repo") | ||
def test_fetch_and_sort_tags(self, mock_repo): | ||
# Mock git.tag() to return a list of tags | ||
mock_repo.return_value.git.tag.return_value = ( | ||
|