docs: advertise Alt + M for amend mode
[git-cola.git] / test / cmds_test.py
blob513a2cd19fb421b63c5af360f74982692a911a27
1 """Test the cmds module"""
3 from cola import cmds
4 from cola.compat import uchr
6 from .helper import Mock, patch
9 def test_Commit_strip_comments():
10 """Ensure that commit messages are stripped of comments"""
11 msg = 'subject\n\n#comment\nbody'
12 expect = 'subject\n\nbody\n'
13 actual = cmds.Commit.strip_comments(msg)
14 assert expect == actual
17 def test_commit_strip_comments_unicode():
18 """Ensure that unicode is preserved in stripped commit messages"""
19 msg = uchr(0x1234) + '\n\n#comment\nbody'
20 expect = uchr(0x1234) + '\n\nbody\n'
21 actual = cmds.Commit.strip_comments(msg)
22 assert expect == actual
25 def test_unix_path_win32():
26 path = r'Z:\Program Files\git-cola\bin\git-dag'
27 expect = '/Z/Program Files/git-cola/bin/git-dag'
28 actual = cmds.unix_path(path, is_win32=lambda: True)
29 assert expect == actual
32 def test_unix_path_network_win32():
33 path = r'\\Z\Program Files\git-cola\bin\git-dag'
34 expect = '//Z/Program Files/git-cola/bin/git-dag'
35 actual = cmds.unix_path(path, is_win32=lambda: True)
36 assert expect == actual
39 def test_unix_path_is_a_noop_on_sane_platforms():
40 path = r'/:we/don\t/need/no/stinking/badgers!'
41 expect = path
42 actual = cmds.unix_path(path, is_win32=lambda: False)
43 assert expect == actual
46 def test_context_edit_command():
47 context = Mock()
48 model = context.model
50 cmd = cmds.EditModel(context)
51 cmd.new_diff_text = 'test_diff_text'
52 cmd.new_diff_type = 'test_diff_type'
53 cmd.new_mode = 'test_mode'
54 cmd.new_filename = 'test_filename'
55 cmd.do()
57 model.set_diff_text.assert_called_once_with('test_diff_text')
58 model.set_diff_type.assert_called_once_with('test_diff_type')
59 model.set_mode.assert_called_once_with('test_mode')
60 assert model.filename == 'test_filename'
63 @patch('cola.interaction.Interaction.confirm')
64 def test_submodule_add(confirm):
65 # "git submodule" should not be called if the answer is "no"
66 context = Mock()
67 url = 'url'
68 path = ''
69 reference = ''
70 branch = ''
71 depth = 0
72 cmd = cmds.SubmoduleAdd(context, url, path, branch, depth, reference)
74 confirm.return_value = False
75 cmd.do()
76 assert not context.git.submodule.called
78 expect = ['--', 'url']
79 actual = cmd.get_args()
80 assert expect == actual
82 cmd.path = 'path'
83 expect = ['--', 'url', 'path']
84 actual = cmd.get_args()
85 assert expect == actual
87 cmd.reference = 'ref'
88 expect = ['--reference', 'ref', '--', 'url', 'path']
89 actual = cmd.get_args()
90 assert expect == actual
92 cmd.branch = 'branch'
93 expect = ['--branch', 'branch', '--reference', 'ref', '--', 'url', 'path']
94 actual = cmd.get_args()
95 assert expect == actual
97 cmd.reference = ''
98 cmd.branch = ''
99 cmd.depth = 1
100 expect = ['--depth', '1', '--', 'url', 'path']
101 actual = cmd.get_args()
102 assert expect == actual
104 # Run the command and assert that "git submodule" was called.
105 confirm.return_value = True
106 context.git.submodule.return_value = (0, '', '')
107 cmd.do()
108 context.git.submodule.assert_called_once_with('add', *expect)
109 assert context.model.update_file_status.called
110 assert context.model.update_submodules_list.called
113 @patch('cola.version.check_git')
114 @patch('cola.interaction.Interaction.confirm')
115 def test_submodule_update(confirm, check_git):
116 context = Mock()
117 path = 'sub/path'
118 update_path_cmd = cmds.SubmoduleUpdate(context, path)
119 update_all_cmd = cmds.SubmodulesUpdate(context)
121 # Nothing is called when confirm() returns False.
122 confirm.return_value = False
124 update_path_cmd.do()
125 assert not context.git.submodule.called
127 update_all_cmd.do()
128 assert not context.git.submodule.called
130 # Confirm command execution.
131 confirm.return_value = True
133 # Test the old command-line arguments first
134 check_git.return_value = False
136 expect = ['update', '--', 'sub/path']
137 actual = update_path_cmd.get_args()
138 assert expect == actual
140 context.model.update_file_status = Mock()
141 context.git.submodule = Mock(return_value=(0, '', ''))
142 update_path_cmd.do()
143 context.git.submodule.assert_called_once_with(*expect)
144 assert context.model.update_file_status.called
146 expect = ['update']
147 actual = update_all_cmd.get_args()
148 assert expect == actual
150 context.model.update_file_status = Mock()
151 context.git.submodule = Mock(return_value=(0, '', ''))
152 update_all_cmd.do()
153 context.git.submodule.assert_called_once_with(*expect)
154 assert context.model.update_file_status.called
156 # Test the new command-line arguments (git v1.6.5+)
157 check_git.return_value = True
159 expect = ['update', '--recursive', '--', 'sub/path']
160 actual = update_path_cmd.get_args()
161 assert expect == actual
163 context.model.update_file_status = Mock()
164 context.git.submodule = Mock(return_value=(0, '', ''))
165 update_path_cmd.do()
166 context.git.submodule.assert_called_once_with(*expect)
167 assert context.model.update_file_status.called
169 expect = ['update', '--recursive']
170 actual = update_all_cmd.get_args()
171 assert expect == actual
173 context.model.update_file_status = Mock()
174 context.git.submodule = Mock(return_value=(0, '', ''))
175 update_all_cmd.do()
176 context.git.submodule.assert_called_once_with(*expect)
177 assert context.model.update_file_status.called
180 @patch('cola.cmds.Interaction')
181 @patch('cola.cmds.prefs')
182 def test_undo_last_commit_confirms_action(prefs, interaction):
183 """Test the behavior around confirmation of UndoLastCommit actions"""
184 context = Mock()
185 context.model = Mock()
186 # First, test what happens when the commit is published and we say "yes".
187 prefs.check_published_commits = Mock(return_value=True)
188 context.model.is_commit_published = Mock(return_value=True)
189 interaction.confirm = Mock(return_value=True)
191 cmd = cmds.UndoLastCommit(context)
192 assert cmd.confirm()
193 context.model.is_commit_published.assert_called_once()
194 interaction.confirm.assert_called_once()
196 # Now, test what happens when we say "no".
197 interaction.confirm = Mock(return_value=False)
198 assert not cmd.confirm()
199 interaction.confirm.assert_called_once()
201 # Now check what happens when the commit is published but our preferences
202 # say to not check for published commits.
203 prefs.check_published_commits = Mock(return_value=False)
204 context.model.is_commit_published = Mock(return_value=True)
205 interaction.confirm = Mock(return_value=True)
207 assert cmd.confirm()
208 context.model.is_commit_published.assert_not_called()
209 interaction.confirm.assert_called_once()
211 # Lastly, check what when the commit is not published and we do check
212 # for published commits.
213 prefs.check_published_commits = Mock(return_value=True)
214 context.model.is_commit_published = Mock(return_value=False)
215 interaction.confirm = Mock(return_value=True)
217 assert cmd.confirm()
218 context.model.is_commit_published.assert_called_once()
219 interaction.confirm.assert_called_once()