1+ import io
2+ import sys
13import unittest
2- from unittest .mock import patch
4+ from unittest .mock import ANY , MagicMock , PropertyMock , patch
5+
6+ import git
37
48from skaraMirror import (
9+ add_skara_upstream ,
510 check_args ,
11+ clone_github_repo ,
612 fetch_and_sort_tags ,
13+ perform_merge_from_skara_into_git ,
14+ perform_merge_into_release_from_master ,
715 sort_jdk8_tags ,
816 sort_jdk11plus_tags ,
917)
1018
1119
20+ class SuppressPrint :
21+ def __enter__ (self ):
22+ self ._original_stdout = sys .stdout
23+ sys .stdout = io .StringIO ()
24+ return self
25+
26+ def __exit__ (self , exc_type , exc_val , exc_tb ):
27+ sys .stdout = self ._original_stdout
28+
29+
1230class TestCheckArgs (unittest .TestCase ):
1331 @patch ("sys.argv" , ["script_name" , "jdk8u" , "https://example.com/repo" , "dev" ])
1432 def test_with_full_arguments (self ):
@@ -25,10 +43,200 @@ def test_with_minimum_arguments(self):
2543 self .assertEqual (args .branch , "master" )
2644
2745
46+ class TestCloneGitHubRepo (unittest .TestCase ):
47+ @patch ("skaraMirror.os.path.isdir" )
48+ @patch ("skaraMirror.Repo.clone_from" )
49+ @patch ("skaraMirror.tqdm" )
50+ def test_clone_repo_not_exists (self , mock_tqdm , mock_clone_from , mock_isdir ):
51+ """
52+ Test cloning a repository that does not already exist locally.
53+ """
54+ # Setup
55+ mock_isdir .return_value = False
56+ jdk_version = "jdk11u"
57+ repo_url = "[email protected] :adoptium/jdk11u.git" 58+ workspace = "/tmp/workspace"
59+
60+ # Execute
61+ with SuppressPrint ():
62+ clone_github_repo (jdk_version , repo_url , workspace )
63+
64+ # Assert
65+ mock_isdir .assert_called_once_with ("/tmp/workspace/jdk11u" )
66+ mock_clone_from .assert_called_once_with (
67+ "[email protected] :adoptium/jdk11u.git" ,
"/tmp/workspace/jdk11u" ,
progress = ANY 68+ )
69+
70+ @patch ("skaraMirror.os.path.isdir" )
71+ @patch ("skaraMirror.Repo.clone_from" )
72+ @patch ("skaraMirror.tqdm" )
73+ def test_clone_repo_exists (self , mock_tqdm , mock_clone_from , mock_isdir ):
74+ """
75+ Test attempting to clone a repository that already exists locally.
76+ """
77+ # Setup
78+ mock_isdir .return_value = True
79+ jdk_version = "jdk11u"
80+ repo_url = "[email protected] :adoptium/jdk11u.git" 81+ workspace = "/tmp/workspace"
82+
83+ # Execute
84+ with SuppressPrint ():
85+ clone_github_repo (jdk_version , repo_url , workspace )
86+
87+ # Assert
88+ mock_isdir .assert_called_once_with ("/tmp/workspace/jdk11u" )
89+ mock_clone_from .assert_not_called () # Clone should not be called since repo exists
90+
91+
92+ class TestAddSkaraUpstream (unittest .TestCase ):
93+ @patch ("skaraMirror.Repo" )
94+ def test_add_skara_remote_not_exist (self , mock_repo ):
95+ # Setup: Configure the mock repo object
96+ mock_repo .return_value .remotes = MagicMock ()
97+ mock_repo .return_value .remotes .__iter__ .return_value = []
98+
99+ # Define your function parameters
100+ workspace = "/tmp/workspace"
101+ jdk_version = "jdk11u"
102+ skara_repo = "https://github.com/openjdk/skara"
103+ branch = "master"
104+
105+ # Execute the function
106+ with SuppressPrint ():
107+ add_skara_upstream (workspace , jdk_version , skara_repo , branch )
108+
109+ # Assertions: Check if the remote was added
110+ mock_repo .return_value .create_remote .assert_called_once_with (
111+ "skara" , skara_repo
112+ )
113+
114+ @patch ("skaraMirror.Repo" )
115+ def test_skara_remote_already_exists (self , mock_repo ):
116+ # Setup: Simulate existing 'skara' remote
117+ mock_remote = MagicMock ()
118+ mock_remote .name = "skara"
119+ mock_repo .return_value .remotes = MagicMock ()
120+ mock_repo .return_value .remotes .__iter__ .return_value = [mock_remote ]
121+
122+ # Execute the function with the same parameters as before
123+ with SuppressPrint ():
124+ add_skara_upstream (
125+ "/tmp/workspace" , "jdk11u" , "https://github.com/openjdk/skara" , "master"
126+ )
127+
128+ # Assertions: Ensure create_remote was not called since 'skara' exists
129+ mock_repo .return_value .create_remote .assert_not_called ()
130+
131+
132+ class TestPerformMergeFromSkaraIntoGit (unittest .TestCase ):
133+ @patch ("skaraMirror.Repo" )
134+ @patch ("skaraMirror.tqdm" )
135+ def test_successful_merge_from_skara (self , mock_tqdm , mock_repo ):
136+ """
137+ Test successful fetching, rebasing, and pushing from the Skara remote.
138+ """
139+ # Setup
140+ workspace = "/tmp/workspace"
141+ github_repo = "jdk11u"
142+ branch = "master"
143+
144+ # Mock remotes and methods
145+ mock_skara_remote = MagicMock ()
146+ mock_repo .return_value .remotes .skara = mock_skara_remote
147+ mock_repo .return_value .git .rebase = MagicMock ()
148+ mock_repo .return_value .remotes .origin .push = MagicMock ()
149+
150+ # Execute
151+ with SuppressPrint ():
152+ perform_merge_from_skara_into_git (workspace , github_repo , branch )
153+
154+ # Assert
155+ mock_skara_remote .fetch .assert_called_once ()
156+ mock_repo .return_value .git .rebase .assert_called_once_with (f"skara/{ branch } " )
157+ mock_repo .return_value .remotes .origin .push .assert_called_once_with (
158+ branch , follow_tags = True , progress = ANY
159+ )
160+
161+ @patch ("skaraMirror.Repo" )
162+ def test_git_command_error_during_fetch (self , mock_repo ):
163+ """
164+ Test handling of GitCommandError during fetch operation from Skara remote.
165+ """
166+ # Setup to raise GitCommandError on fetch
167+ mock_repo .return_value .remotes .skara .fetch .side_effect = (
168+ git .exc .GitCommandError ("fetch" , "error" )
169+ )
170+
171+ workspace = "/tmp/workspace"
172+ github_repo = "jdk11u"
173+ branch = "master"
174+
175+ # Expect the function to handle the exception and not crash
176+ with SuppressPrint ():
177+ with self .assertRaises (SystemExit ) as cm :
178+ perform_merge_from_skara_into_git (workspace , github_repo , branch )
179+
180+ self .assertEqual (cm .exception .code , 1 )
181+
182+
183+ class TestPerformMergeIntoReleaseFromMaster (unittest .TestCase ):
184+ def setUp (self ):
185+ self .mock_repo = MagicMock (spec = git .Repo )
186+
187+ # Mock the master branch as initially the only branch
188+ self .mock_master_branch = MagicMock (spec = git .Head , name = "master" )
189+
190+ # Prepare a mock for the repo's heads that supports item getting and iteration
191+ self .mock_heads = {"master" : self .mock_master_branch }
192+
193+ # Use PropertyMock to simulate the repo.heads dynamic nature
194+ type(self .mock_repo ).heads = PropertyMock (side_effect = lambda : self .mock_heads )
195+
196+ # Mock remotes setup
197+ self .mock_origin_remote = MagicMock (spec = git .Remote , name = "origin" )
198+ self .mock_repo .remotes = MagicMock ()
199+ self .mock_repo .remotes .__getitem__ .side_effect = (
200+ lambda x : self .mock_origin_remote if x == "origin" else None
201+ )
202+
203+ # Mock fetching, pushing, and tag listing
204+ self .mock_origin_remote .fetch = MagicMock ()
205+ self .mock_origin_remote .push = MagicMock ()
206+ self .mock_tags = ["jdk-11.0.1+10" , "jdk-11.0.2+9" ]
207+ self .mock_repo .git .tag .return_value = "\n " .join (self .mock_tags )
208+
209+ @patch ("skaraMirror.subprocess.run" )
210+ @patch ("skaraMirror.Repo" )
211+ @patch ("skaraMirror.tqdm" )
212+ def test_release_branch_does_not_exist (
213+ self , mock_tqdm , mock_repo_class , mock_subprocess_run
214+ ):
215+ mock_repo_class .return_value = self .mock_repo
216+
217+ # Assert setup: Verify initially no 'release' branch
218+ self .assertNotIn ("release" , self .mock_repo .heads )
219+
220+ # Simulate the function's execution
221+ with SuppressPrint ():
222+ perform_merge_into_release_from_master ("/tmp/workspace" , "jdk11u" , "master" )
223+
224+ # verify that the patch was applied to the branch
225+ mock_subprocess_run .assert_called ()
226+
227+ # Dynamically add 'release' branch to simulate its creation during function execution
228+ self .mock_heads ["release" ] = MagicMock (spec = git .Head , name = "release" )
229+
230+ # Verify 'release' branch creation logic was triggered
231+ self .assertIn ("release" , self .mock_repo .heads )
232+ self .mock_repo .git .checkout .assert_called_once_with (
233+ "-b" , "release" , "origin/release"
234+ )
235+ self .mock_repo .git .tag .assert_called ()
236+
237+
28238class TestFetchAndSortTags (unittest .TestCase ):
29- @patch (
30- "skaraMirror.Repo"
31- ) # Adjust the patch path according to your script's structure
239+ @patch ("skaraMirror.Repo" )
32240 def test_fetch_and_sort_tags (self , mock_repo ):
33241 # Mock git.tag() to return a list of tags
34242 mock_repo .return_value .git .tag .return_value = (
0 commit comments