1010 get_full_sha_from_short , get_author_info_from_short_sha , \
1111 CherryPicker , InvalidRepoException , \
1212 normalize_commit_message , DEFAULT_CONFIG , \
13- find_config , load_config
13+ get_sha1_from , find_config , load_config , validate_sha
1414
1515
1616@pytest .fixture
@@ -116,16 +116,22 @@ def test_get_cherry_pick_branch(os_path_exists, config):
116116 assert cp .get_cherry_pick_branch ("3.6" ) == "backport-22a594a-3.6"
117117
118118
119- @mock .patch ('os.path.exists' )
120- @mock .patch ('subprocess.check_output' )
121- def test_get_pr_url (subprocess_check_output , os_path_exists , config ):
122- os_path_exists .return_value = True
123- subprocess_check_output .return_value = b'https://github.com/mock_user/cpython.git'
119+ def test_get_pr_url (config ):
124120 branches = ["3.6" ]
125121 cp = CherryPicker ('origin' , '22a594a0047d7706537ff2ac676cdc0f1dcb329c' ,
126122 branches , config = config )
127- assert cp .get_pr_url ("3.6" , cp .get_cherry_pick_branch ("3.6" )) \
128- == "https://github.com/python/cpython/compare/3.6...mock_user:backport-22a594a-3.6?expand=1"
123+ backport_target_branch = cp .get_cherry_pick_branch ("3.6" )
124+ expected_pr_url = (
125+ 'https://github.com/python/cpython/compare/'
126+ '3.6...mock_user:backport-22a594a-3.6?expand=1'
127+ )
128+ with mock .patch (
129+ 'subprocess.check_output' ,
130+ return_value = b'https://github.com/mock_user/cpython.git' ,
131+ ):
132+ actual_pr_url = cp .get_pr_url ("3.6" , backport_target_branch )
133+
134+ assert actual_pr_url == expected_pr_url
129135
130136
131137@pytest .mark .parametrize ('url' , [
@@ -137,42 +143,44 @@ def test_get_pr_url(subprocess_check_output, os_path_exists, config):
137143 b'https://github.com/mock_user/cpython' ,
138144 ])
139145def test_username (url , config ):
146+ branches = ["3.6" ]
147+ cp = CherryPicker ('origin' , '22a594a0047d7706537ff2ac676cdc0f1dcb329c' ,
148+ branches , config = config )
140149 with mock .patch ('subprocess.check_output' , return_value = url ):
141- branches = ["3.6" ]
142- cp = CherryPicker ('origin' , '22a594a0047d7706537ff2ac676cdc0f1dcb329c' ,
143- branches , config = config )
144150 assert cp .username == 'mock_user'
145151
146152
147- @mock .patch ('os.path.exists' )
148- @mock .patch ('subprocess.check_output' )
149- def test_get_updated_commit_message (subprocess_check_output , os_path_exists ,
150- config ):
151- os_path_exists .return_value = True
152- subprocess_check_output .return_value = b'bpo-123: Fix Spam Module (#113)'
153+ def test_get_updated_commit_message (config ):
153154 branches = ["3.6" ]
154155 cp = CherryPicker ('origin' , '22a594a0047d7706537ff2ac676cdc0f1dcb329c' ,
155156 branches , config = config )
156- assert cp .get_commit_message ('22a594a0047d7706537ff2ac676cdc0f1dcb329c' ) \
157- == 'bpo-123: Fix Spam Module (GH-113)'
157+ with mock .patch (
158+ 'subprocess.check_output' ,
159+ return_value = b'bpo-123: Fix Spam Module (#113)' ,
160+ ):
161+ actual_commit_message = (
162+ cp .get_commit_message ('22a594a0047d7706537ff2ac676cdc0f1dcb329c' )
163+ )
164+ assert actual_commit_message == 'bpo-123: Fix Spam Module (GH-113)'
158165
159166
160- @mock .patch ('os.path.exists' )
161- @mock .patch ('subprocess.check_output' )
162- def test_get_updated_commit_message_without_links_replacement (
163- subprocess_check_output , os_path_exists , config ):
164- os_path_exists .return_value = True
165- subprocess_check_output .return_value = b'bpo-123: Fix Spam Module (#113)'
167+ def test_get_updated_commit_message_without_links_replacement (config ):
166168 config ['fix_commit_msg' ] = False
167169 branches = ["3.6" ]
168170 cp = CherryPicker ('origin' , '22a594a0047d7706537ff2ac676cdc0f1dcb329c' ,
169171 branches , config = config )
170- assert cp .get_commit_message ('22a594a0047d7706537ff2ac676cdc0f1dcb329c' ) \
171- == 'bpo-123: Fix Spam Module (#113)'
172+ with mock .patch (
173+ 'subprocess.check_output' ,
174+ return_value = b'bpo-123: Fix Spam Module (#113)' ,
175+ ):
176+ actual_commit_message = (
177+ cp .get_commit_message ('22a594a0047d7706537ff2ac676cdc0f1dcb329c' )
178+ )
179+ assert actual_commit_message == 'bpo-123: Fix Spam Module (#113)'
172180
173181
174182@mock .patch ('subprocess.check_output' )
175- def test_is_cpython_repo (subprocess_check_output , config ):
183+ def test_is_cpython_repo (subprocess_check_output ):
176184 subprocess_check_output .return_value = """commit 7f777ed95a19224294949e1b4ce56bbffcb1fe9f
177185Author: Guido van Rossum <[email protected] > 178186Date: Thu Aug 9 14:25:15 1990 +0000
@@ -181,8 +189,7 @@ def test_is_cpython_repo(subprocess_check_output, config):
181189
182190"""
183191 # should not raise an exception
184- CherryPicker ('origin' , '22a594a0047d7706537ff2ac676cdc0f1dcb329c' ,
185- ["3.6" ], config = config )
192+ validate_sha ('22a594a0047d7706537ff2ac676cdc0f1dcb329c' )
186193
187194
188195def test_is_not_cpython_repo ():
@@ -195,48 +202,72 @@ def test_is_not_cpython_repo():
195202def test_find_config (tmpdir , cd ):
196203 cd (tmpdir )
197204 subprocess .run ('git init .' .split (), check = True )
198- cfg = tmpdir .join ('.cherry_picker.toml' )
205+ relative_config_path = '.cherry_picker.toml'
206+ cfg = tmpdir .join (relative_config_path )
199207 cfg .write ('param = 1' )
200- assert str (find_config ()) == str (cfg )
208+ subprocess .run ('git add .' .split (), check = True )
209+ subprocess .run (('git' , 'commit' , '-m' , 'Initial commit' ), check = True )
210+ scm_revision = get_sha1_from ('HEAD' )
211+ assert find_config (scm_revision ) == scm_revision + ':' + relative_config_path
201212
202213
203214def test_find_config_not_found (tmpdir , cd ):
204215 cd (tmpdir )
205216 subprocess .run ('git init .' .split (), check = True )
206- assert find_config () is None
217+ subprocess .run (('git' , 'commit' , '-m' , 'Initial commit' , '--allow-empty' ), check = True )
218+ scm_revision = get_sha1_from ('HEAD' )
219+ assert find_config (scm_revision ) is None
207220
208221
209222def test_load_full_config (tmpdir , cd ):
210223 cd (tmpdir )
211224 subprocess .run ('git init .' .split (), check = True )
212- cfg = tmpdir .join ('.cherry_picker.toml' )
225+ relative_config_path = '.cherry_picker.toml'
226+ cfg = tmpdir .join (relative_config_path )
213227 cfg .write ('''\
214228 team = "python"
215229 repo = "core-workfolow"
216230 check_sha = "5f007046b5d4766f971272a0cc99f8461215c1ec"
217231 default_branch = "devel"
218232 ''' )
233+ subprocess .run ('git add .' .split (), check = True )
234+ subprocess .run (('git' , 'commit' , '-m' , 'Initial commit' ), check = True )
235+ scm_revision = get_sha1_from ('HEAD' )
219236 cfg = load_config (None )
220- assert cfg == {'check_sha' : '5f007046b5d4766f971272a0cc99f8461215c1ec' ,
221- 'repo' : 'core-workfolow' ,
222- 'team' : 'python' ,
223- 'fix_commit_msg' : True ,
224- 'default_branch' : 'devel' ,
225- }
237+ assert cfg == (
238+ scm_revision + ':' + relative_config_path ,
239+ {
240+ 'check_sha' : '5f007046b5d4766f971272a0cc99f8461215c1ec' ,
241+ 'repo' : 'core-workfolow' ,
242+ 'team' : 'python' ,
243+ 'fix_commit_msg' : True ,
244+ 'default_branch' : 'devel' ,
245+ },
246+ )
226247
227248
228249def test_load_partial_config (tmpdir , cd ):
229- cfg = tmpdir .join ('.cherry_picker.toml' )
250+ cd (tmpdir )
251+ subprocess .run ('git init .' .split (), check = True )
252+ relative_config_path = '.cherry_picker.toml'
253+ cfg = tmpdir .join (relative_config_path )
230254 cfg .write ('''\
231255 repo = "core-workfolow"
232256 ''' )
233- cfg = load_config (pathlib .Path (str (cfg )))
234- assert cfg == {'check_sha' : '7f777ed95a19224294949e1b4ce56bbffcb1fe9f' ,
235- 'repo' : 'core-workfolow' ,
236- 'team' : 'python' ,
237- 'fix_commit_msg' : True ,
238- 'default_branch' : 'master' ,
239- }
257+ subprocess .run ('git add .' .split (), check = True )
258+ subprocess .run (('git' , 'commit' , '-m' , 'Initial commit' ), check = True )
259+ scm_revision = get_sha1_from ('HEAD' )
260+ cfg = load_config (relative_config_path )
261+ assert cfg == (
262+ scm_revision + ':' + relative_config_path ,
263+ {
264+ 'check_sha' : '7f777ed95a19224294949e1b4ce56bbffcb1fe9f' ,
265+ 'repo' : 'core-workfolow' ,
266+ 'team' : 'python' ,
267+ 'fix_commit_msg' : True ,
268+ 'default_branch' : 'master' ,
269+ },
270+ )
240271
241272
242273def test_normalize_long_commit_message ():
0 commit comments