tests: prune empty lines and add docstrings
[git-cola.git] / test / main_model_test.py
blob57c4fcbfad859f8f8d1ef9e5565d6931b5d028e3
1 import os
3 import pytest
5 from cola import core
6 from cola import git
7 from cola.models import main
8 from cola.models.main import FETCH, PULL, PUSH
10 from . import helper
11 from .helper import app_context
12 from .helper import Mock
15 # prevent unused imports lint errors.
16 assert app_context is not None
18 REMOTE = 'server'
19 LOCAL_BRANCH = 'local'
20 REMOTE_BRANCH = 'remote'
23 @pytest.fixture
24 def mock_context():
25 """Return a Mock context for testing"""
26 context = Mock()
27 context.git = git.create()
28 return context
31 def test_project(app_context):
32 """Test the 'project' attribute."""
33 project = os.path.basename(core.getcwd())
34 app_context.model.set_worktree(core.getcwd())
35 assert app_context.model.project == project
38 def test_local_branches(app_context):
39 """Test the 'local_branches' attribute."""
40 helper.commit_files()
41 app_context.model.update_status()
42 assert app_context.model.local_branches == ['main']
45 def test_remote_branches(app_context):
46 """Test the 'remote_branches' attribute."""
47 app_context.model.update_status()
48 assert app_context.model.remote_branches == []
49 helper.commit_files()
50 helper.run_git('remote', 'add', 'origin', '.')
51 helper.run_git('fetch', 'origin')
52 app_context.model.update_status()
53 assert app_context.model.remote_branches == ['origin/main']
56 def test_modified(app_context):
57 """Test the 'modified' attribute."""
58 helper.write_file('A', 'change')
59 app_context.model.update_status()
60 assert app_context.model.modified == ['A']
63 def test_unstaged(app_context):
64 """Test the 'unstaged' attribute."""
65 helper.write_file('A', 'change')
66 helper.write_file('C', 'C')
67 app_context.model.update_status()
68 assert app_context.model.unstaged == ['A', 'C']
71 def test_untracked(app_context):
72 """Test the 'untracked' attribute."""
73 helper.write_file('C', 'C')
74 app_context.model.update_status()
75 assert app_context.model.untracked == ['C']
78 def test_stageable(app_context):
79 """Test the 'stageable' attribute."""
80 assert not app_context.model.is_stageable()
83 def test_remotes(app_context):
84 """Test the 'remote' attribute."""
85 helper.run_git('remote', 'add', 'origin', '.')
86 app_context.model.update_status()
87 assert app_context.model.remotes == ['origin']
90 def test_currentbranch(app_context):
91 """Test the 'currentbranch' attribute."""
92 helper.run_git('checkout', '-b', 'test')
93 app_context.model.update_status()
94 assert app_context.model.currentbranch == 'test'
97 def test_tags(app_context):
98 """Test the 'tags' attribute."""
99 helper.commit_files()
100 helper.run_git('tag', 'test')
101 app_context.model.update_status()
102 assert app_context.model.tags == ['test']
105 def test_remote_args_fetch(mock_context):
106 """FETCH swaps arguments vs. PUSH and PULL"""
107 (args, kwargs) = main.remote_args(
108 mock_context,
109 REMOTE,
110 FETCH,
111 local_branch=LOCAL_BRANCH,
112 remote_branch=REMOTE_BRANCH,
114 assert args == [REMOTE, 'remote:local']
115 assert kwargs['verbose']
116 assert 'tags' not in kwargs
117 assert 'rebase' not in kwargs
120 def test_remote_args_fetch_tags(mock_context):
121 # Fetch tags
122 (args, kwargs) = main.remote_args(
123 mock_context,
124 REMOTE,
125 FETCH,
126 tags=True,
127 local_branch=LOCAL_BRANCH,
128 remote_branch=REMOTE_BRANCH,
130 assert args == [REMOTE, 'remote:local']
131 assert kwargs['verbose']
132 assert kwargs['tags']
133 assert 'rebase' not in kwargs
136 def test_remote_args_pull(mock_context):
137 # Pull
138 (args, kwargs) = main.remote_args(
139 mock_context,
140 REMOTE,
141 PULL,
142 local_branch='',
143 remote_branch=REMOTE_BRANCH,
145 assert args == [REMOTE, 'remote']
146 assert kwargs['verbose']
147 assert 'rebase' not in kwargs
148 assert 'tags' not in kwargs
151 def test_remote_args_pull_rebase(mock_context):
152 # Rebasing pull
153 (args, kwargs) = main.remote_args(
154 mock_context,
155 REMOTE,
156 PULL,
157 rebase=True,
158 local_branch='',
159 remote_branch=REMOTE_BRANCH,
161 assert args == [REMOTE, 'remote']
162 assert kwargs['verbose']
163 assert kwargs['rebase']
164 assert 'tags' not in kwargs
167 def test_remote_args_push(mock_context):
168 """PUSH swaps local and remote branches"""
169 (args, kwargs) = main.remote_args(
170 mock_context,
171 REMOTE,
172 PUSH,
173 local_branch=LOCAL_BRANCH,
174 remote_branch=REMOTE_BRANCH,
176 assert args == [REMOTE, 'local:remote']
177 assert kwargs['verbose']
178 assert 'tags' not in kwargs
179 assert 'rebase' not in kwargs
182 def test_remote_args_push_tags(mock_context):
183 """Pushing tags uses --tags"""
184 (args, kwargs) = main.remote_args(
185 mock_context,
186 REMOTE,
187 PUSH,
188 tags=True,
189 local_branch=LOCAL_BRANCH,
190 remote_branch=REMOTE_BRANCH,
192 assert args == [REMOTE, 'local:remote']
193 assert kwargs['verbose']
194 assert kwargs['tags']
195 assert 'rebase' not in kwargs
198 def test_remote_args_push_same_remote_and_local(mock_context):
199 (args, kwargs) = main.remote_args(
200 mock_context,
201 REMOTE,
202 PUSH,
203 tags=True,
204 local_branch=LOCAL_BRANCH,
205 remote_branch=LOCAL_BRANCH,
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 PUSH,
218 tags=True,
219 local_branch=LOCAL_BRANCH,
220 remote_branch=LOCAL_BRANCH,
221 set_upstream=True,
223 assert args == [REMOTE, 'local']
224 assert kwargs['verbose']
225 assert kwargs['tags']
226 assert kwargs['set_upstream']
227 assert 'rebase' not in kwargs
230 def test_remote_args_rebase_only(mock_context):
231 (_, kwargs) = main.remote_args(
232 mock_context, REMOTE, PULL, rebase=True, ff_only=True
234 assert kwargs['rebase']
235 assert 'ff_only' not in kwargs
238 def test_run_remote_action(mock_context):
239 """Test running a remote action"""
240 (args, kwargs) = main.run_remote_action(
241 mock_context,
242 lambda *args, **kwargs: (args, kwargs),
243 REMOTE,
244 FETCH,
245 local_branch=LOCAL_BRANCH,
246 remote_branch=REMOTE_BRANCH,
248 assert args == (REMOTE, 'remote:local')
249 assert kwargs['verbose']
250 assert 'tags' not in kwargs
251 assert 'rebase' not in kwargs