CHANGES: mention the improvements from #1205 and #1206
[git-cola.git] / test / cmds_test.py
blob4679c0c322d44a9d4b8b7fced50fe85e59a1fcd1
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 model.set_filename.assert_called_once_with('test_filename')
62 assert model.set_filename.call_count == 1
65 @patch('cola.interaction.Interaction.confirm')
66 def test_submodule_add(confirm):
67 # "git submodule" should not be called if the answer is "no"
68 context = Mock()
69 url = 'url'
70 path = ''
71 reference = ''
72 branch = ''
73 depth = 0
74 cmd = cmds.SubmoduleAdd(context, url, path, branch, depth, reference)
76 confirm.return_value = False
77 cmd.do()
78 assert not context.git.submodule.called
80 expect = ['--', 'url']
81 actual = cmd.get_args()
82 assert expect == actual
84 cmd.path = 'path'
85 expect = ['--', 'url', 'path']
86 actual = cmd.get_args()
87 assert expect == actual
89 cmd.reference = 'ref'
90 expect = ['--reference', 'ref', '--', 'url', 'path']
91 actual = cmd.get_args()
92 assert expect == actual
94 cmd.branch = 'branch'
95 expect = ['--branch', 'branch', '--reference', 'ref', '--', 'url', 'path']
96 actual = cmd.get_args()
97 assert expect == actual
99 cmd.reference = ''
100 cmd.branch = ''
101 cmd.depth = 1
102 expect = ['--depth', '1', '--', 'url', 'path']
103 actual = cmd.get_args()
104 assert expect == actual
106 # Run the command and assert that "git submodule" was called.
107 confirm.return_value = True
108 context.git.submodule.return_value = (0, '', '')
109 cmd.do()
110 context.git.submodule.assert_called_once_with('add', *expect)
111 assert context.model.update_file_status.called
112 assert context.model.update_submodules_list.called
115 @patch('cola.version.check_git')
116 @patch('cola.interaction.Interaction.confirm')
117 def test_submodule_update(confirm, check_git):
118 context = Mock()
119 path = 'sub/path'
120 update_path_cmd = cmds.SubmoduleUpdate(context, path)
121 update_all_cmd = cmds.SubmodulesUpdate(context)
123 # Nothing is called when confirm() returns False.
124 confirm.return_value = False
126 update_path_cmd.do()
127 assert not context.git.submodule.called
129 update_all_cmd.do()
130 assert not context.git.submodule.called
132 # Confirm command execution.
133 confirm.return_value = True
135 # Test the old command-line arguments first
136 check_git.return_value = False
138 expect = ['update', '--', 'sub/path']
139 actual = update_path_cmd.get_args()
140 assert expect == actual
142 context.model.update_file_status = Mock()
143 context.git.submodule = Mock(return_value=(0, '', ''))
144 update_path_cmd.do()
145 context.git.submodule.assert_called_once_with(*expect)
146 assert context.model.update_file_status.called
148 expect = ['update']
149 actual = update_all_cmd.get_args()
150 assert expect == actual
152 context.model.update_file_status = Mock()
153 context.git.submodule = Mock(return_value=(0, '', ''))
154 update_all_cmd.do()
155 context.git.submodule.assert_called_once_with(*expect)
156 assert context.model.update_file_status.called
158 # Test the new command-line arguments (git v1.6.5+)
159 check_git.return_value = True
161 expect = ['update', '--recursive', '--', 'sub/path']
162 actual = update_path_cmd.get_args()
163 assert expect == actual
165 context.model.update_file_status = Mock()
166 context.git.submodule = Mock(return_value=(0, '', ''))
167 update_path_cmd.do()
168 context.git.submodule.assert_called_once_with(*expect)
169 assert context.model.update_file_status.called
171 expect = ['update', '--recursive']
172 actual = update_all_cmd.get_args()
173 assert expect == actual
175 context.model.update_file_status = Mock()
176 context.git.submodule = Mock(return_value=(0, '', ''))
177 update_all_cmd.do()
178 context.git.submodule.assert_called_once_with(*expect)
179 assert context.model.update_file_status.called
182 @patch('cola.cmds.Interaction')
183 @patch('cola.cmds.prefs')
184 def test_undo_last_commit_confirms_action(prefs, interaction):
185 """Test the behavior around confirmation of UndoLastCommit actions"""
186 context = Mock()
187 context.model = Mock()
188 # First, test what happens when the commit is published and we say "yes".
189 prefs.check_published_commits = Mock(return_value=True)
190 context.model.is_commit_published = Mock(return_value=True)
191 interaction.confirm = Mock(return_value=True)
193 cmd = cmds.UndoLastCommit(context)
194 assert cmd.confirm()
195 context.model.is_commit_published.assert_called_once()
196 interaction.confirm.assert_called_once()
198 # Now, test what happens when we say "no".
199 interaction.confirm = Mock(return_value=False)
200 assert not cmd.confirm()
201 interaction.confirm.assert_called_once()
203 # Now check what happens when the commit is published but our preferences
204 # say to not check for published commits.
205 prefs.check_published_commits = Mock(return_value=False)
206 context.model.is_commit_published = Mock(return_value=True)
207 interaction.confirm = Mock(return_value=True)
209 assert cmd.confirm()
210 context.model.is_commit_published.assert_not_called()
211 interaction.confirm.assert_called_once()
213 # Lastly, check what when the commit is not published and we do check
214 # for published commits.
215 prefs.check_published_commits = Mock(return_value=True)
216 context.model.is_commit_published = Mock(return_value=False)
217 interaction.confirm = Mock(return_value=True)
219 assert cmd.confirm()
220 context.model.is_commit_published.assert_called_once()
221 interaction.confirm.assert_called_once()