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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
import os
import pytest
from cola import core
from cola import git
from cola.models import main
from cola.models.main import FETCH, FETCH_HEAD, PULL, PUSH
from . import helper
from .helper import app_context
from .helper import Mock
# prevent unused imports lint errors.
assert app_context is not None
REMOTE = 'server'
LOCAL_BRANCH = 'local'
REMOTE_BRANCH = 'remote'
@pytest.fixture
def mock_context():
"""Return a Mock context for testing"""
context = Mock()
context.git = git.create()
return context
def test_project(app_context):
"""Test the 'project' attribute."""
project = os.path.basename(core.getcwd())
app_context.model.set_worktree(core.getcwd())
assert app_context.model.project == project
def test_local_branches(app_context):
"""Test the 'local_branches' attribute."""
helper.commit_files()
app_context.model.update_status()
assert app_context.model.local_branches == ['main']
def test_remote_branches(app_context):
"""Test the 'remote_branches' attribute."""
app_context.model.update_status()
assert app_context.model.remote_branches == []
helper.commit_files()
helper.run_git('remote', 'add', 'origin', '.')
helper.run_git('fetch', 'origin')
app_context.model.update_status()
assert app_context.model.remote_branches == ['origin/main']
def test_modified(app_context):
"""Test the 'modified' attribute."""
helper.write_file('A', 'change')
app_context.model.update_status()
assert app_context.model.modified == ['A']
def test_unstaged(app_context):
"""Test the 'unstaged' attribute."""
helper.write_file('A', 'change')
helper.write_file('C', 'C')
app_context.model.update_status()
assert app_context.model.unstaged == ['A', 'C']
def test_untracked(app_context):
"""Test the 'untracked' attribute."""
helper.write_file('C', 'C')
app_context.model.update_status()
assert app_context.model.untracked == ['C']
def test_stageable(app_context):
"""Test the 'stageable' attribute."""
assert not app_context.model.is_stageable()
def test_remotes(app_context):
"""Test the 'remote' attribute."""
helper.run_git('remote', 'add', 'origin', '.')
app_context.model.update_status()
assert app_context.model.remotes == ['origin']
def test_currentbranch(app_context):
"""Test the 'currentbranch' attribute."""
helper.run_git('checkout', '-b', 'test')
app_context.model.update_status()
assert app_context.model.currentbranch == 'test'
def test_tags(app_context):
"""Test the 'tags' attribute."""
helper.commit_files()
helper.run_git('tag', 'test')
app_context.model.update_status()
assert app_context.model.tags == ['test']
def test_remote_args_fetch(mock_context):
"""FETCH swaps arguments vs. PUSH and PULL"""
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
FETCH,
local_branch=LOCAL_BRANCH,
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'remote:local']
assert kwargs['verbose']
assert 'tags' not in kwargs
assert 'rebase' not in kwargs
def test_remote_args_fetch_head(mock_context):
"""Fetch handles the implicit FETCH_HEAD ref"""
# When FETCH_HEAD is used then we should not specify a tracking branch target.
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
FETCH,
local_branch=FETCH_HEAD,
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'remote']
def test_remote_args_fetch_tags(mock_context):
# Fetch tags
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
FETCH,
tags=True,
local_branch=LOCAL_BRANCH,
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'remote:local']
assert kwargs['verbose']
assert kwargs['tags']
assert 'rebase' not in kwargs
def test_remote_args_fetch_into_tracking_branch(mock_context):
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
FETCH,
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'remote:refs/remotes/server/remote']
def test_remote_args_pull(mock_context):
# Pull
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
PULL,
local_branch='',
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'remote']
assert kwargs['verbose']
assert 'rebase' not in kwargs
assert 'tags' not in kwargs
def test_remote_args_pull_rebase(mock_context):
# Rebasing pull
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
PULL,
rebase=True,
local_branch='',
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'remote']
assert kwargs['verbose']
assert kwargs['rebase']
assert 'tags' not in kwargs
def test_remote_args_push(mock_context):
"""PUSH swaps local and remote branches"""
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
PUSH,
local_branch=LOCAL_BRANCH,
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'local:remote']
assert kwargs['verbose']
assert 'tags' not in kwargs
assert 'rebase' not in kwargs
def test_remote_args_push_tags(mock_context):
"""Pushing tags uses --tags"""
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
PUSH,
tags=True,
local_branch=LOCAL_BRANCH,
remote_branch=REMOTE_BRANCH,
)
assert args == [REMOTE, 'local:remote']
assert kwargs['verbose']
assert kwargs['tags']
assert 'rebase' not in kwargs
def test_remote_args_push_same_remote_and_local(mock_context):
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
PUSH,
tags=True,
local_branch=LOCAL_BRANCH,
remote_branch=LOCAL_BRANCH,
)
assert args == [REMOTE, 'local']
assert kwargs['verbose']
assert kwargs['tags']
assert 'rebase' not in kwargs
def test_remote_args_push_set_upstream(mock_context):
(args, kwargs) = main.remote_args(
mock_context,
REMOTE,
PUSH,
tags=True,
local_branch=LOCAL_BRANCH,
remote_branch=LOCAL_BRANCH,
set_upstream=True,
)
assert args == [REMOTE, 'local']
assert kwargs['verbose']
assert kwargs['tags']
assert kwargs['set_upstream']
assert 'rebase' not in kwargs
def test_remote_args_rebase_only(mock_context):
(_, kwargs) = main.remote_args(
mock_context, REMOTE, PULL, rebase=True, ff_only=True
)
assert kwargs['rebase']
assert 'ff_only' not in kwargs
def test_run_remote_action(mock_context):
"""Test running a remote action"""
(args, kwargs) = main.run_remote_action(
mock_context,
lambda *args, **kwargs: (args, kwargs),
REMOTE,
FETCH,
local_branch=LOCAL_BRANCH,
remote_branch=REMOTE_BRANCH,
)
assert args == (REMOTE, 'remote:local')
assert kwargs['verbose']
assert 'tags' not in kwargs
assert 'rebase' not in kwargs
|