github/workflows: use a virtualenv for setuptools
[git-cola.git] / test / main_model_test.py
blob7fb0b7a7735d69ac64c25165e7e9ae881913cebe
1 # pylint: disable=redefined-outer-name
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 import os
5 import pytest
7 from cola import core
8 from cola import git
9 from cola.models import main
11 from . import helper
12 from .helper import app_context
13 from .helper import Mock
16 # These assertions make flake8 happy. It considers them unused imports otherwise.
17 assert app_context is not None
19 REMOTE = 'server'
20 LOCAL_BRANCH = 'local'
21 REMOTE_BRANCH = 'remote'
24 @pytest.fixture
25 def mock_context():
26 """Return a Mock context for testing"""
27 context = Mock()
28 context.git = git.create()
29 return context
32 def test_project(app_context):
33 """Test the 'project' attribute."""
34 project = os.path.basename(core.getcwd())
35 app_context.model.set_worktree(core.getcwd())
36 assert app_context.model.project == project
39 def test_local_branches(app_context):
40 """Test the 'local_branches' attribute."""
41 helper.commit_files()
42 app_context.model.update_status()
43 assert app_context.model.local_branches == ['main']
46 def test_remote_branches(app_context):
47 """Test the 'remote_branches' attribute."""
48 app_context.model.update_status()
49 assert app_context.model.remote_branches == []
51 helper.commit_files()
52 helper.run_git('remote', 'add', 'origin', '.')
53 helper.run_git('fetch', 'origin')
54 app_context.model.update_status()
55 assert app_context.model.remote_branches == ['origin/main']
58 def test_modified(app_context):
59 """Test the 'modified' attribute."""
60 helper.write_file('A', 'change')
61 app_context.model.update_status()
62 assert app_context.model.modified == ['A']
65 def test_unstaged(app_context):
66 """Test the 'unstaged' attribute."""
67 helper.write_file('A', 'change')
68 helper.write_file('C', 'C')
69 app_context.model.update_status()
70 assert app_context.model.unstaged == ['A', 'C']
73 def test_untracked(app_context):
74 """Test the 'untracked' attribute."""
75 helper.write_file('C', 'C')
76 app_context.model.update_status()
77 assert app_context.model.untracked == ['C']
80 def test_remotes(app_context):
81 """Test the 'remote' attribute."""
82 helper.run_git('remote', 'add', 'origin', '.')
83 app_context.model.update_status()
84 assert app_context.model.remotes == ['origin']
87 def test_currentbranch(app_context):
88 """Test the 'currentbranch' attribute."""
89 helper.run_git('checkout', '-b', 'test')
90 app_context.model.update_status()
91 assert app_context.model.currentbranch == 'test'
94 def test_tags(app_context):
95 """Test the 'tags' attribute."""
96 helper.commit_files()
97 helper.run_git('tag', 'test')
98 app_context.model.update_status()
99 assert app_context.model.tags == ['test']
102 def test_remote_args_fetch(mock_context):
103 # Fetch
104 (args, kwargs) = main.remote_args(
105 mock_context,
106 REMOTE,
107 local_branch=LOCAL_BRANCH,
108 remote_branch=REMOTE_BRANCH,
111 assert args == [REMOTE, 'remote:local']
112 assert kwargs['verbose']
113 assert 'tags' not in kwargs
114 assert 'rebase' not in kwargs
117 def test_remote_args_fetch_tags(mock_context):
118 # Fetch tags
119 (args, kwargs) = main.remote_args(
120 mock_context,
121 REMOTE,
122 tags=True,
123 local_branch=LOCAL_BRANCH,
124 remote_branch=REMOTE_BRANCH,
127 assert args == [REMOTE, 'remote:local']
128 assert kwargs['verbose']
129 assert kwargs['tags']
130 assert 'rebase' not in kwargs
133 def test_remote_args_pull(mock_context):
134 # Pull
135 (args, kwargs) = main.remote_args(
136 mock_context,
137 REMOTE,
138 pull=True,
139 local_branch='',
140 remote_branch=REMOTE_BRANCH,
143 assert args == [REMOTE, 'remote']
144 assert kwargs['verbose']
145 assert 'rebase' not in kwargs
146 assert 'tags' not in kwargs
149 def test_remote_args_pull_rebase(mock_context):
150 # Rebasing pull
151 (args, kwargs) = main.remote_args(
152 mock_context,
153 REMOTE,
154 pull=True,
155 rebase=True,
156 local_branch='',
157 remote_branch=REMOTE_BRANCH,
160 assert args == [REMOTE, 'remote']
161 assert kwargs['verbose']
162 assert kwargs['rebase']
163 assert 'tags' not in kwargs
166 def test_remote_args_push(mock_context):
167 # Push, swap local and remote
168 (args, kwargs) = main.remote_args(
169 mock_context,
170 REMOTE,
171 local_branch=REMOTE_BRANCH,
172 remote_branch=LOCAL_BRANCH,
175 assert args == [REMOTE, 'local:remote']
176 assert kwargs['verbose']
177 assert 'tags' not in kwargs
178 assert 'rebase' not in kwargs
181 def test_remote_args_push_tags(mock_context):
182 # Push, swap local and remote
183 (args, kwargs) = main.remote_args(
184 mock_context,
185 REMOTE,
186 tags=True,
187 local_branch=REMOTE_BRANCH,
188 remote_branch=LOCAL_BRANCH,
191 assert args == [REMOTE, 'local:remote']
192 assert kwargs['verbose']
193 assert kwargs['tags']
194 assert 'rebase' not in kwargs
197 def test_remote_args_push_same_remote_and_local(mock_context):
198 (args, kwargs) = main.remote_args(
199 mock_context,
200 REMOTE,
201 tags=True,
202 local_branch=LOCAL_BRANCH,
203 remote_branch=LOCAL_BRANCH,
204 push=True,
207 assert args == [REMOTE, 'local']
208 assert kwargs['verbose']
209 assert kwargs['tags']
210 assert 'rebase' not in kwargs
213 def test_remote_args_push_set_upstream(mock_context):
214 (args, kwargs) = main.remote_args(
215 mock_context,
216 REMOTE,
217 tags=True,
218 local_branch=LOCAL_BRANCH,
219 remote_branch=LOCAL_BRANCH,
220 push=True,
221 set_upstream=True,
224 assert args == [REMOTE, 'local']
225 assert kwargs['verbose']
226 assert kwargs['tags']
227 assert kwargs['set_upstream']
228 assert 'rebase' not in kwargs
231 def test_remote_args_rebase_only(mock_context):
232 (_, kwargs) = main.remote_args(
233 mock_context, REMOTE, pull=True, rebase=True, ff_only=True
235 assert kwargs['rebase']
236 assert 'ff_only' not in kwargs
239 def test_run_remote_action(mock_context):
241 def passthrough(*args, **kwargs):
242 return (args, kwargs)
244 (args, kwargs) = main.run_remote_action(
245 mock_context,
246 passthrough,
247 REMOTE,
248 local_branch=LOCAL_BRANCH,
249 remote_branch=REMOTE_BRANCH,
252 assert args == (REMOTE, 'remote:local')
253 assert kwargs['verbose']
254 assert 'tags' not in kwargs
255 assert 'rebase' not in kwargs