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