@@ -149,20 +149,20 @@ def tmp_git_repo_dir(tmpdir, cd, git_init, git_commit, git_config):
149149
150150
151151@mock .patch ("subprocess.check_output" )
152- def test_get_base_branch (subprocess_check_output ):
152+ def test_get_base_branch (subprocess_check_output , config ):
153153 # The format of cherry-pick branches we create are::
154154 # backport-{SHA}-{base_branch}
155155 subprocess_check_output .return_value = b"22a594a0047d7706537ff2ac676cdc0f1dcb329c"
156156 cherry_pick_branch = "backport-22a594a-2.7"
157- result = get_base_branch (cherry_pick_branch )
157+ result = get_base_branch (cherry_pick_branch , config = config )
158158 assert result == "2.7"
159159
160160
161161@mock .patch ("subprocess.check_output" )
162- def test_get_base_branch_which_has_dashes (subprocess_check_output ):
162+ def test_get_base_branch_which_has_dashes (subprocess_check_output , config ):
163163 subprocess_check_output .return_value = b"22a594a0047d7706537ff2ac676cdc0f1dcb329c"
164164 cherry_pick_branch = "backport-22a594a-baseprefix-2.7-basesuffix"
165- result = get_base_branch (cherry_pick_branch )
165+ result = get_base_branch (cherry_pick_branch , config = config )
166166 assert result == "baseprefix-2.7-basesuffix"
167167
168168
@@ -171,14 +171,14 @@ def test_get_base_branch_which_has_dashes(subprocess_check_output):
171171 [
172172 "backport-22a594a" , # Not enough fields
173173 "prefix-22a594a-2.7" , # Not the prefix we were expecting
174- "backport-22a594a-base " , # No version info in the base branch
174+ "backport-22a594a-" , # No base branch
175175 ],
176176)
177177@mock .patch ("subprocess.check_output" )
178- def test_get_base_branch_invalid (subprocess_check_output , cherry_pick_branch ):
178+ def test_get_base_branch_invalid (subprocess_check_output , cherry_pick_branch , config ):
179179 subprocess_check_output .return_value = b"22a594a0047d7706537ff2ac676cdc0f1dcb329c"
180180 with pytest .raises (ValueError ):
181- get_base_branch (cherry_pick_branch )
181+ get_base_branch (cherry_pick_branch , config = config )
182182
183183
184184@mock .patch ("subprocess.check_output" )
@@ -206,18 +206,33 @@ def test_get_author_info_from_short_sha(subprocess_check_output):
206206
207207
208208@pytest .mark .parametrize (
209- "input_branches,sorted_branches" ,
209+ "input_branches,sorted_branches,require_version " ,
210210 [
211- (["3.1" , "2.7" , "3.10" , "3.6" ], ["3.10" , "3.6" , "3.1" , "2.7" ]),
211+ (["3.1" , "2.7" , "3.10" , "3.6" ], ["3.10" , "3.6" , "3.1" , "2.7" ], True ),
212212 (
213213 ["stable-3.1" , "lts-2.7" , "3.10-other" , "smth3.6else" ],
214214 ["3.10-other" , "smth3.6else" , "stable-3.1" , "lts-2.7" ],
215+ True ,
216+ ),
217+ (["3.1" , "2.7" , "3.10" , "3.6" ], ["3.10" , "3.6" , "3.1" , "2.7" ], False ),
218+ (
219+ ["stable-3.1" , "lts-2.7" , "3.10-other" , "smth3.6else" ],
220+ ["3.10-other" , "smth3.6else" , "stable-3.1" , "lts-2.7" ],
221+ False ,
222+ ),
223+ (
224+ ["3.7" , "3.10" , "2.7" , "foo" , "stable" , "branch" ],
225+ ["3.10" , "3.7" , "2.7" , "branch" , "foo" , "stable" ],
226+ False ,
215227 ),
216228 ],
217229)
218230@mock .patch ("os.path.exists" )
219- def test_sorted_branch (os_path_exists , config , input_branches , sorted_branches ):
231+ def test_sorted_branch (
232+ os_path_exists , config , input_branches , sorted_branches , require_version
233+ ):
220234 os_path_exists .return_value = True
235+ config ["require_version_in_branch_name" ] = require_version
221236 cp = CherryPicker (
222237 "origin" ,
223238 "22a594a0047d7706537ff2ac676cdc0f1dcb329c" ,
@@ -227,6 +242,21 @@ def test_sorted_branch(os_path_exists, config, input_branches, sorted_branches):
227242 assert cp .sorted_branches == sorted_branches
228243
229244
245+ @mock .patch ("os.path.exists" )
246+ def test_invalid_branch_empty_string (os_path_exists , config ):
247+ os_path_exists .return_value = True
248+ # already tested for require_version_in_branch_name=True below
249+ config ["require_version_in_branch_name" ] = False
250+ cp = CherryPicker (
251+ "origin" ,
252+ "22a594a0047d7706537ff2ac676cdc0f1dcb329c" ,
253+ ["3.1" , "2.7" , "3.10" , "3.6" , "" ],
254+ config = config ,
255+ )
256+ with pytest .raises (ValueError , match = r"^Branch name is an empty string\.$" ):
257+ cp .sorted_branches
258+
259+
230260@pytest .mark .parametrize (
231261 "input_branches" ,
232262 [
@@ -460,6 +490,7 @@ def test_load_full_config(tmp_git_repo_dir, git_add, git_commit):
460490 "team" : "python" ,
461491 "fix_commit_msg" : True ,
462492 "default_branch" : "devel" ,
493+ "require_version_in_branch_name" : True ,
463494 },
464495 )
465496
@@ -483,6 +514,7 @@ def test_load_partial_config(tmp_git_repo_dir, git_add, git_commit):
483514 "team" : "python" ,
484515 "fix_commit_msg" : True ,
485516 "default_branch" : "main" ,
517+ "require_version_in_branch_name" : True ,
486518 },
487519 )
488520
@@ -511,6 +543,7 @@ def test_load_config_no_head_sha(tmp_git_repo_dir, git_add, git_commit):
511543 "team" : "python" ,
512544 "fix_commit_msg" : True ,
513545 "default_branch" : "devel" ,
546+ "require_version_in_branch_name" : True ,
514547 },
515548 )
516549
0 commit comments