Debian Codegraph @

git_delete_merged_branches/tests/test_git.py

raw symbols SCIP
1 # Copyright (C) 2020 Sebastian Pipping <[email protected]>
2 # Licensed under GPL v3 or later
3 import os
4 import shlex
5 import subprocess
6 from tempfile import TemporaryDirectory
7 from textwrap import dedent
8 from unittest import TestCase
9 from unittest.mock import Mock, call, patch
10
11 from parameterized import parameterized
12
13 from .._git import Git
14 from .._messenger import Messenger
15 from .helpers import create_git, run_script
16
17
18 class FindBranchesTest(TestCase):
19 def test_find_branches_drops_head(self):
20 existing_branches = ["remote1/HEAD", "remote2/trunk"]
21 expected_branches = ["remote2/trunk"]
22 git = Git(Messenger(colorize=False), pretend=True, verbose=False)
23 command_output_to_inject = ("\n".join(existing_branches) + "\n").encode("utf-8")
24 assert isinstance(command_output_to_inject, bytes)
25
26 with patch.object(subprocess, "check_output", return_value=command_output_to_inject):
27 actual_branches = git._find_branches()
28
29 self.assertEqual(actual_branches, expected_branches)
30
31 @parameterized.expand(
32 [
33 # NOTE: No point in testing values "never" or "auto"
34 ("column.ui", "always"),
35 # NOTE: No point in testing value "plain"
36 ("column.branch", "column"),
37 ("column.branch", "row"),
38 ]
39 )
40 def test_find_branches_robust_towards_column_mode(self, git_config_key, git_config_value):
41 expected_branches = ["main_1", "main_2"]
42 with TemporaryDirectory() as tempdir:
43 run_script(
44 dedent(
45 f"""
46 git -c init.defaultBranch=main_1 init
47 git commit --allow-empty -m 'First commit'
48 git config {shlex.quote(git_config_key)} {shlex.quote(git_config_value)}
49 git branch main_2 main_1
50 """
51 ),
52 tempdir,
53 )
54 git = Git(Messenger(colorize=False), pretend=True, verbose=False, work_dir=tempdir)
55 actual_branches = git._find_branches()
56 self.assertEqual(actual_branches, expected_branches)
57
58
59 class FindBranchNamesTest(TestCase):
60 def test(self):
61 find_branches_lines = [
62 "heads/b1",
63 "heads/b2",
64 "remotes/remote1/b3",
65 "remotes/remote2/b1",
66 ]
67 expected = {"b1", "b2", "b3"}
68 git = Git(messenger=Mock(), pretend=True, verbose=False)
69
70 with patch.object(
71 git, "_find_branches", return_value=find_branches_lines
72 ) as find_branches_mock:
73 actual = git.find_all_branch_names()
74
75 self.assertEqual(actual, expected)
76 self.assertEqual(find_branches_mock.call_args_list, [call(["--all"], strip_left=1)])
77
78
79 class FindWorkingTreeBranchesTest(TestCase):
80 def test_find_branches_drops_head(self):
81 expected_branches = [None, "branch-arrow-shift", "refactor-layout-window"]
82 git = Git(Messenger(colorize=False), pretend=True, verbose=False)
83 command_output_to_inject = dedent("""
84 worktree /tmp/tmp.mgTEbE434g/pymux
85 HEAD 493723318912cb44b1a3e47ba3fbc0e50b2a8f5c
86 detached
87
88 worktree /tmp/tmp.mgTEbE434g/branch-arrow-shift
89 HEAD 3f66e62b9de4b2251c7f9afad6c516dc5a30ec67
90 branch refs/heads/branch-arrow-shift
91
92 worktree /tmp/tmp.mgTEbE434g/refactor-layout-window
93 HEAD 3f66e62b9de4b2251c7f9afad6c516dc5a30ec67
94 branch refs/heads/refactor-layout-window
95
96 """).encode("utf-8") # with two trialing newlines like Git would do
97 assert isinstance(command_output_to_inject, bytes)
98
99 with patch.object(subprocess, "check_output", return_value=command_output_to_inject):
100 actual_branches = git.find_working_tree_branches()
101
102 self.assertEqual(actual_branches, expected_branches)
103
104
105 class OutputBytesToLinesTest(TestCase):
106 @parameterized.expand(
107 [
108 (b"one\ntwo", ["one", "two"]),
109 (b"one\ntwo\n", ["one", "two"]),
110 (b"one\ntwo\n\n", ["one", "two"]),
111 (b"", []),
112 (b"\n", []),
113 (b"\n\n", []),
114 ]
115 )
116 def test_trailing_newlines(self, output_bytes, extected_interpretation):
117 self.assertEqual(Git._output_bytes_to_lines(output_bytes), extected_interpretation)
118
119
120 class ExtractGitConfigTest(TestCase):
121 def test_escapes(self):
122 expected_config = {
123 "z.singlequote": "'",
124 "z.doublequote": '"',
125 "z.doublequote-doublequote": '""',
126 "z.doublequote-doublequote-doublequote": '"""',
127 "z.backlslash": "\\",
128 "z.backlslash-n": "\\n",
129 "z.backlslash-t": "\\t",
130 "z.backlslash-b": "\\b",
131 "z.backlslash-doublequote": '\\"',
132 "z.backlslash-backlslash": "\\\\",
133 "z.linefeed": "\n",
134 "z.tab": "\t",
135 "z.backspace": chr(8),
136 "z.empty": "",
137 }
138
139 mock_messenger = Mock()
140
141 with TemporaryDirectory() as d:
142 subprocess.call(["git", "init"], cwd=d)
143 git = Git(messenger=mock_messenger, pretend=False, verbose=True, work_dir=d)
144 for k, v in expected_config.items():
145 git.set_config(k, v)
146
147 with open(os.path.join(d, ".git/config"), "a") as f:
148 f.write("\tname-without-assignment") # GitHub issue #96
149
150 actual_config = {
151 k: v for k, v in git.extract_git_config().items() if k in expected_config
152 }
153
154 self.assertEqual(actual_config, expected_config)
155
156 self.assertEqual(
157 mock_messenger.tell_info.call_args_list,
158 [
159 call("Git config option 'z.name-without-assignment' lacks assignment of a value."),
160 ],
161 )
162
163
164 class RemoteBranchCollidesWithATagTest(TestCase):
165 def test_remote_branch_deletable_despite_existing_tag_with_the_same_name(self):
166 setup_script = dedent("""\
167 mkdir upstream
168
169 pushd upstream
170 git init
171 git commit --allow-empty -m 'First commit'
172 git tag -m '' 1.0.0
173 git branch 1.0.0
174 popd
175
176 git clone upstream downstream
177 """)
178
179 with TemporaryDirectory() as d:
180 run_script(setup_script, cwd=d)
181 downstream_git = create_git(work_dir=os.path.join(d, "downstream"))
182 self.assertIn("origin/1.0.0", downstream_git.find_remote_branches_at("origin"))
183
184 downstream_git.delete_remote_branches(["origin/1.0.0"], "origin")
185
186 self.assertNotIn("origin/1.0.0", downstream_git.find_remote_branches_at("origin"))