git-cola v4.3.1
[git-cola.git] / test / cmds_test.py
blob6c07f13228fa41d066874a92952b2a7697e62088
1 """Test the cmds module"""
2 from __future__ import absolute_import, division, print_function, unicode_literals
4 from cola import cmds
5 from cola.compat import uchr
7 from .helper import Mock, patch
10 def test_Commit_strip_comments():
11 """Ensure that commit messages are stripped of comments"""
12 msg = 'subject\n\n#comment\nbody'
13 expect = 'subject\n\nbody\n'
14 actual = cmds.Commit.strip_comments(msg)
15 assert expect == actual
18 def test_commit_strip_comments_unicode():
19 """Ensure that unicode is preserved in stripped commit messages"""
20 msg = uchr(0x1234) + '\n\n#comment\nbody'
21 expect = uchr(0x1234) + '\n\nbody\n'
22 actual = cmds.Commit.strip_comments(msg)
23 assert expect == actual
26 def test_unix_path_win32():
27 path = r'Z:\Program Files\git-cola\bin\git-dag'
28 expect = '/Z/Program Files/git-cola/bin/git-dag'
29 actual = cmds.unix_path(path, is_win32=lambda: True)
30 assert expect == actual
33 def test_unix_path_network_win32():
34 path = r'\\Z\Program Files\git-cola\bin\git-dag'
35 expect = '//Z/Program Files/git-cola/bin/git-dag'
36 actual = cmds.unix_path(path, is_win32=lambda: True)
37 assert expect == actual
40 def test_unix_path_is_a_noop_on_sane_platforms():
41 path = r'/:we/don\t/need/no/stinking/badgers!'
42 expect = path
43 actual = cmds.unix_path(path, is_win32=lambda: False)
44 assert expect == actual
47 def test_context_edit_command():
48 context = Mock()
49 model = context.model
51 cmd = cmds.EditModel(context)
52 cmd.new_diff_text = 'test_diff_text'
53 cmd.new_diff_type = 'test_diff_type'
54 cmd.new_mode = 'test_mode'
55 cmd.new_filename = 'test_filename'
56 cmd.do()
58 model.set_diff_text.assert_called_once_with('test_diff_text')
59 model.set_diff_type.assert_called_once_with('test_diff_type')
60 model.set_mode.assert_called_once_with('test_mode')
61 assert model.filename == 'test_filename'
64 @patch('cola.interaction.Interaction.confirm')
65 def test_submodule_add(confirm):
66 # "git submodule" should not be called if the answer is "no"
67 context = Mock()
68 url = 'url'
69 path = ''
70 reference = ''
71 branch = ''
72 depth = 0
73 cmd = cmds.SubmoduleAdd(context, url, path, branch, depth, reference)
75 confirm.return_value = False
76 cmd.do()
77 assert not context.git.submodule.called
79 expect = ['--', 'url']
80 actual = cmd.get_args()
81 assert expect == actual
83 cmd.path = 'path'
84 expect = ['--', 'url', 'path']
85 actual = cmd.get_args()
86 assert expect == actual
88 cmd.reference = 'ref'
89 expect = ['--reference', 'ref', '--', 'url', 'path']
90 actual = cmd.get_args()
91 assert expect == actual
93 cmd.branch = 'branch'
94 expect = ['--branch', 'branch', '--reference', 'ref', '--', 'url', 'path']
95 actual = cmd.get_args()
96 assert expect == actual
98 cmd.reference = ''
99 cmd.branch = ''
100 cmd.depth = 1
101 expect = ['--depth', '1', '--', 'url', 'path']
102 actual = cmd.get_args()
103 assert expect == actual
105 # Run the command and assert that "git submodule" was called.
106 confirm.return_value = True
107 context.git.submodule.return_value = (0, '', '')
108 cmd.do()
109 context.git.submodule.assert_called_once_with('add', *expect)
110 assert context.model.update_file_status.called
111 assert context.model.update_submodules_list.called
114 @patch('cola.version.check_git')
115 @patch('cola.interaction.Interaction.confirm')
116 def test_submodule_update(confirm, check_git):
117 context = Mock()
118 path = 'sub/path'
119 update_path_cmd = cmds.SubmoduleUpdate(context, path)
120 update_all_cmd = cmds.SubmodulesUpdate(context)
122 # Nothing is called when confirm() returns False.
123 confirm.return_value = False
125 update_path_cmd.do()
126 assert not context.git.submodule.called
128 update_all_cmd.do()
129 assert not context.git.submodule.called
131 # Confirm command execution.
132 confirm.return_value = True
134 # Test the old command-line arguments first
135 check_git.return_value = False
137 expect = ['update', '--', 'sub/path']
138 actual = update_path_cmd.get_args()
139 assert expect == actual
141 context.model.update_file_status = Mock()
142 context.git.submodule = Mock(return_value=(0, '', ''))
143 update_path_cmd.do()
144 context.git.submodule.assert_called_once_with(*expect)
145 assert context.model.update_file_status.called
147 expect = ['update']
148 actual = update_all_cmd.get_args()
149 assert expect == actual
151 context.model.update_file_status = Mock()
152 context.git.submodule = Mock(return_value=(0, '', ''))
153 update_all_cmd.do()
154 context.git.submodule.assert_called_once_with(*expect)
155 assert context.model.update_file_status.called
157 # Test the new command-line arguments (git v1.6.5+)
158 check_git.return_value = True
160 expect = ['update', '--recursive', '--', 'sub/path']
161 actual = update_path_cmd.get_args()
162 assert expect == actual
164 context.model.update_file_status = Mock()
165 context.git.submodule = Mock(return_value=(0, '', ''))
166 update_path_cmd.do()
167 context.git.submodule.assert_called_once_with(*expect)
168 assert context.model.update_file_status.called
170 expect = ['update', '--recursive']
171 actual = update_all_cmd.get_args()
172 assert expect == actual
174 context.model.update_file_status = Mock()
175 context.git.submodule = Mock(return_value=(0, '', ''))
176 update_all_cmd.do()
177 context.git.submodule.assert_called_once_with(*expect)
178 assert context.model.update_file_status.called
181 @patch('cola.cmds.Interaction')
182 @patch('cola.cmds.prefs')
183 def test_undo_last_commit_confirms_action(prefs, interaction):
184 """Test the behavior around confirmation of UndoLastCommit actions"""
185 context = Mock()
186 context.model = Mock()
187 # First, test what happens when the commit is published and we say "yes".
188 prefs.check_published_commits = Mock(return_value=True)
189 context.model.is_commit_published = Mock(return_value=True)
190 interaction.confirm = Mock(return_value=True)
192 cmd = cmds.UndoLastCommit(context)
193 assert cmd.confirm()
194 context.model.is_commit_published.assert_called_once()
195 interaction.confirm.assert_called_once()
197 # Now, test what happens when we say "no".
198 interaction.confirm = Mock(return_value=False)
199 assert not cmd.confirm()
200 interaction.confirm.assert_called_once()
202 # Now check what happens when the commit is published but our preferences
203 # say to not check for published commits.
204 prefs.check_published_commits = Mock(return_value=False)
205 context.model.is_commit_published = Mock(return_value=True)
206 interaction.confirm = Mock(return_value=True)
208 assert cmd.confirm()
209 context.model.is_commit_published.assert_not_called()
210 interaction.confirm.assert_called_once()
212 # Lastly, check what when the commit is not published and we do check
213 # for published commits.
214 prefs.check_published_commits = Mock(return_value=True)
215 context.model.is_commit_published = Mock(return_value=False)
216 interaction.confirm = Mock(return_value=True)
218 assert cmd.confirm()
219 context.model.is_commit_published.assert_called_once()
220 interaction.confirm.assert_called_once()