Debian Codegraph @

git_delete_merged_branches/tests/test_engine.py

raw symbols SCIP
1 # Copyright (C) 2020 Sebastian Pipping <[email protected]>
2 # Licensed under GPL v3 or later
3
4 import os
5 from tempfile import TemporaryDirectory
6 from textwrap import dedent
7 from unittest import TestCase
8 from unittest.mock import Mock
9
10 from parameterized import parameterized
11
12 from .._engine import DeleteMergedBranches
13 from .helpers import create_dmb, create_git, run_script
14
15
16 class MergeDetectionTest(TestCase):
17 def test_effort_1_truly_merged(self):
18 setup_script = dedent("""
19 git init -b trunk
20
21 # Create a commit to base future branches upon
22 echo line1 > file.txt
23 git add file.txt
24 git commit -m 'Add file.txt with one line'
25
26 # Create a merged branch: With HEAD sitting in trunk's past
27 git branch merged1
28
29 # Create a merged branch: Topic branch with original commit
30 git checkout -b merged2
31 echo line2 >> file.txt
32 git commit -a -m 'Add line 2'
33 git checkout trunk
34 git merge --no-ff --no-edit merged2
35
36 # Create a not-merged branch
37 git checkout -b not-merged1
38 echo line3 >> file.txt
39 git commit -a -m 'Add line 3'
40 """)
41 with TemporaryDirectory() as d:
42 run_script(setup_script, cwd=d)
43 git = create_git(d)
44 dmb = create_dmb(git, effort_level=1)
45 self.assertEqual(
46 git.find_local_branches(), ["merged1", "merged2", "not-merged1", "trunk"]
47 )
48
49 truly_merged, defacto_merged = (
50 dmb._find_branches_merged_to_all_targets_for_single_remote(
51 {"trunk"}, set(), remote_name=None
52 )
53 )
54
55 self.assertEqual(truly_merged, {"merged1", "merged2"})
56 self.assertEqual(defacto_merged, set())
57
58 def test_effort_2_unsquashed_cherries(self):
59 setup_script = dedent("""
60 git init -b trunk
61
62 # Create a commit to base future branches upon
63 echo line1 > file1.txt
64 git add file1.txt
65 git commit -m 'Add file1.txt'
66
67 # Create a de-facto merged branch: forward order
68 git checkout -b defacto-merged1
69 cp file1.txt file2.txt
70 git add file2.txt
71 git commit -m 'Add file2.txt'
72 cp file1.txt file3.txt
73 git add file3.txt
74 git commit -m 'Add file3.txt'
75
76 # Create a de-facto merged branch: backward order
77 git checkout -b defacto-merged2 trunk
78 cp file1.txt file3.txt
79 git add file3.txt
80 git commit -m 'Add file3.txt'
81 cp file1.txt file2.txt
82 git add file2.txt
83 git commit -m 'Add file2.txt'
84
85 # Add an extra commit on trunk so that we don't get
86 # identical SHA1s when cherry-picking, after
87 git checkout trunk
88 cp file1.txt file4.txt
89 git add file4.txt
90 git commit -m 'Add file4.txt'
91
92 # Get the commits on trunk that will make
93 # branches defacto-merged{1,2} be detected as de-facto merged
94 git cherry-pick defacto-merged1{^,}
95
96 # Create a not-defacto-merged branch
97 git checkout -b not-defacto-merged1 defacto-merged1
98 cp file1.txt file5.txt
99 git add file5.txt
100 git commit -m 'Add file5.txt'
101 """)
102 with TemporaryDirectory() as d:
103 run_script(setup_script, cwd=d)
104 git = create_git(d)
105 dmb = create_dmb(git, effort_level=2)
106 self.assertEqual(
107 git.find_local_branches(),
108 ["defacto-merged1", "defacto-merged2", "not-defacto-merged1", "trunk"],
109 )
110
111 truly_merged, defacto_merged = (
112 dmb._find_branches_merged_to_all_targets_for_single_remote(
113 {"trunk"}, set(), remote_name=None
114 )
115 )
116
117 self.assertEqual(truly_merged, set())
118 self.assertEqual(defacto_merged, {"defacto-merged1", "defacto-merged2"})
119
120 def test_effort_3_squashed_cherries(self):
121 setup_script = dedent("""
122 git init -b trunk
123
124 # Create a commit to base future branches upon
125 echo line1 > file1.txt
126 git add file1.txt
127 git commit -m 'Add file1.txt'
128
129 # Create a de-facto squash-merged branch: Adding up to full diff
130 git checkout -b defacto-squash-merged1
131 cp file1.txt file2.txt
132 git add file2.txt
133 git commit -m 'Add file2.txt'
134 cp file1.txt file3.txt
135 git add file3.txt
136 git commit -m 'Add file3.txt'
137
138 # Create de-facto squash-merged branch: With reverts, adding up
139 git checkout -b defacto-squash-merged2
140 git revert --no-edit HEAD
141 git revert --no-edit HEAD # i.e. revert the revert
142
143 # Get the a squashed commit on trunk that will make
144 # branches defacto-squash-merged{1,2} be detected as de-facto merged
145 git checkout trunk
146 git merge --squash defacto-squash-merged1
147 git commit -m "Add squashed copy of 'defacto-squash-merged1'"
148
149 # Create not-defacto-squash-merged branch: Squashed copy commit
150 # .. will not have a counterpart
151 git checkout -b not-defacto-squash-merged1
152 git revert --no-edit HEAD
153 """)
154 with TemporaryDirectory() as d:
155 run_script(setup_script, cwd=d)
156 git = create_git(d)
157 dmb = create_dmb(git, effort_level=3)
158 self.assertEqual(
159 git.find_local_branches(),
160 [
161 "defacto-squash-merged1",
162 "defacto-squash-merged2",
163 "not-defacto-squash-merged1",
164 "trunk",
165 ],
166 )
167
168 truly_merged, defacto_merged = (
169 dmb._find_branches_merged_to_all_targets_for_single_remote(
170 {"trunk"}, set(), remote_name=None
171 )
172 )
173
174 self.assertEqual(truly_merged, set())
175 self.assertEqual(defacto_merged, {"defacto-squash-merged1", "defacto-squash-merged2"})
176
177
178 class RefreshTargetBranchesTest(TestCase):
179 def test_refresh_gets_branches_back_in_sync(self):
180 setup_script = dedent("""
181 mkdir upstream
182 cd upstream
183 git init -b trunk
184 git commit --allow-empty -m 'Dummy commit #1'
185 git branch pull-works
186 git branch pull-trouble
187 git checkout -b checkout-trouble
188 echo line1 > collision.txt
189 git add collision.txt
190 git commit -m 'Add collision.txt'
191 git checkout trunk
192 cd ..
193 git clone -o upstream upstream downstream
194 cd downstream
195 git branch --track checkout-trouble upstream/checkout-trouble
196 git branch --track pull-trouble upstream/pull-trouble
197 git branch --track pull-works upstream/pull-works
198 cd ..
199 cd upstream
200 git checkout pull-trouble
201 git merge --ff checkout-trouble
202 git checkout pull-works
203 git commit --allow-empty -m 'Dummy commit #2'
204 cd ..
205 cd downstream
206 git checkout -b topic1
207 echo line1 > collision.txt # uncommitted, just present
208 """)
209
210 with TemporaryDirectory() as d:
211 run_script(setup_script, cwd=d)
212
213 downstream_git = create_git(os.path.join(d, "downstream"))
214 downstream_dmb = create_dmb(downstream_git, effort_level=3)
215 self.assertEqual(downstream_git.find_current_branch(), "topic1")
216 self.assertEqual(
217 downstream_git.find_local_branches(),
218 ["checkout-trouble", "pull-trouble", "pull-works", "topic1", "trunk"],
219 )
220 downstream_dmb.refresh_remotes(["upstream"])
221 self.assertEqual(len(downstream_git.cherry("pull-works", "upstream/pull-works")), 1)
222
223 downstream_dmb.refresh_target_branches(
224 ["checkout-trouble", "pull-trouble", "pull-works"]
225 )
226
227 self.assertEqual(len(downstream_git.cherry("pull-works", "upstream/pull-works")), 0)
228 self.assertEqual(downstream_git.find_current_branch(), "topic1")
229
230
231 class GitConfigKeysContainDotsTest(TestCase):
232 @parameterized.expand(
233 [
234 (
235 DeleteMergedBranches.find_required_branches,
236 "branch.release-1.0.x.dmb-required",
237 "release-1.0.x",
238 ),
239 (
240 DeleteMergedBranches.find_excluded_branches,
241 "branch.release-1.0.x.dmb-excluded",
242 "release-1.0.x",
243 ),
244 (
245 DeleteMergedBranches.find_enabled_remotes,
246 "remote.linux-6.x.dmb-enabled",
247 "linux-6.x",
248 ),
249 ]
250 )
251 def test_supports_branch_names_containing_dots(
252 self, extractor_function, git_config_dict_key, expected_value
253 ):
254 assert "." in expected_value
255 git_config_dict = {
256 git_config_dict_key: "true",
257 }
258 self.assertEqual(extractor_function(git_config_dict), [expected_value])
259
260
261 class DetermineExcludedBranchesTest(TestCase):
262 @parameterized.expand(
263 [
264 ("git config exclude", ["b1", "b2", "b3"], ["b1", "b3"], [], [], {"b1", "b3"}),
265 ("--exclude", ["b1", "b2", "b3"], [], ["b1", "b3"], [], {"b1", "b3"}),
266 ("--exclude + config exclude", ["b1", "b2", "b3"], ["b1"], ["b3"], [], {"b1", "b3"}),
267 (
268 "--include-regex match full",
269 ["b1", "b2", "b3"],
270 [],
271 [],
272 ["^..$", "^b2$"],
273 {"b1", "b3"},
274 ),
275 ("--include-regex match partial", ["b1", "b2", "b3"], [], [], [r"\d", "b"], set()),
276 (
277 "--include-regex mismatch",
278 ["b1", "b2", "b3"],
279 [],
280 [],
281 ["^b1$", "^b2$"],
282 {"b1", "b2", "b3"},
283 ),
284 (
285 "--include-regex + --exclude + config exclude",
286 ["b1", "b2", "b3"],
287 ["b1"],
288 ["b3"],
289 [r"^b\d$"],
290 {"b1", "b3"},
291 ),
292 ]
293 )
294 def test(
295 self,
296 _label,
297 existing_branches,
298 excluded_branches_from_config,
299 excluded_branches_extra,
300 included_branches_patterns,
301 expected_exclusion_set,
302 ):
303 git_mock = Mock(find_all_branch_names=Mock(return_value=existing_branches))
304 dmb = DeleteMergedBranches(
305 git=git_mock, messenger=Mock(), confirmation=Mock(), selector=Mock(), effort_level=999
306 )
307 git_config_key_format = DeleteMergedBranches._FORMAT_BRANCH_EXCLUDED
308 git_config = {
309 git_config_key_format.format(name=branch_name): DeleteMergedBranches._CONFIG_VALUE_TRUE
310 for branch_name in excluded_branches_from_config
311 }
312
313 actual_exclusion_set = dmb.determine_excluded_branches(
314 git_config, excluded_branches_extra, included_branches_patterns
315 )
316
317 self.assertEqual(actual_exclusion_set, expected_exclusion_set)