tests: use "ruff" to validate code instead of "pylint"
[git-cola.git] / test / main_model_test.py
blobeca01600e627dac56bdfab5115438c46f7bae982
1 # pylint: disable=redefined-outer-name
2 import os
4 import pytest
6 from cola import core
7 from cola import git
8 from cola.models import main
10 from . import helper
11 from .helper import app_context
12 from .helper import Mock
15 # These assertions make pylint happy. It considers them unused imports otherwise.
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 == []
50 helper.commit_files()
51 helper.run_git('remote', 'add', 'origin', '.')
52 helper.run_git('fetch', 'origin')
53 app_context.model.update_status()
54 assert app_context.model.remote_branches == ['origin/main']
57 def test_modified(app_context):
58 """Test the 'modified' attribute."""
59 helper.write_file('A', 'change')
60 app_context.model.update_status()
61 assert app_context.model.modified == ['A']
64 def test_unstaged(app_context):
65 """Test the 'unstaged' attribute."""
66 helper.write_file('A', 'change')
67 helper.write_file('C', 'C')
68 app_context.model.update_status()
69 assert app_context.model.unstaged == ['A', 'C']
72 def test_untracked(app_context):
73 """Test the 'untracked' attribute."""
74 helper.write_file('C', 'C')
75 app_context.model.update_status()
76 assert app_context.model.untracked == ['C']
79 def test_stageable(app_context):
80 """Test the 'stageable' attribute."""
81 assert not app_context.model.is_stageable()
84 def test_remotes(app_context):
85 """Test the 'remote' attribute."""
86 helper.run_git('remote', 'add', 'origin', '.')
87 app_context.model.update_status()
88 assert app_context.model.remotes == ['origin']
91 def test_currentbranch(app_context):
92 """Test the 'currentbranch' attribute."""
93 helper.run_git('checkout', '-b', 'test')
94 app_context.model.update_status()
95 assert app_context.model.currentbranch == 'test'
98 def test_tags(app_context):
99 """Test the 'tags' attribute."""
100 helper.commit_files()
101 helper.run_git('tag', 'test')
102 app_context.model.update_status()
103 assert app_context.model.tags == ['test']
106 def test_remote_args_fetch(mock_context):
107 # Fetch
108 (args, kwargs) = main.remote_args(
109 mock_context,
110 REMOTE,
111 local_branch=LOCAL_BRANCH,
112 remote_branch=REMOTE_BRANCH,
115 assert args == [REMOTE, 'remote:local']
116 assert kwargs['verbose']
117 assert 'tags' not in kwargs
118 assert 'rebase' not in kwargs
121 def test_remote_args_fetch_tags(mock_context):
122 # Fetch tags
123 (args, kwargs) = main.remote_args(
124 mock_context,
125 REMOTE,
126 tags=True,
127 local_branch=LOCAL_BRANCH,
128 remote_branch=REMOTE_BRANCH,
131 assert args == [REMOTE, 'remote:local']
132 assert kwargs['verbose']
133 assert kwargs['tags']
134 assert 'rebase' not in kwargs
137 def test_remote_args_pull(mock_context):
138 # Pull
139 (args, kwargs) = main.remote_args(
140 mock_context,
141 REMOTE,
142 pull=True,
143 local_branch='',
144 remote_branch=REMOTE_BRANCH,
147 assert args == [REMOTE, 'remote']
148 assert kwargs['verbose']
149 assert 'rebase' not in kwargs
150 assert 'tags' not in kwargs
153 def test_remote_args_pull_rebase(mock_context):
154 # Rebasing pull
155 (args, kwargs) = main.remote_args(
156 mock_context,
157 REMOTE,
158 pull=True,
159 rebase=True,
160 local_branch='',
161 remote_branch=REMOTE_BRANCH,
164 assert args == [REMOTE, 'remote']
165 assert kwargs['verbose']
166 assert kwargs['rebase']
167 assert 'tags' not in kwargs
170 def test_remote_args_push(mock_context):
171 # Push, swap local and remote
172 (args, kwargs) = main.remote_args(
173 mock_context,
174 REMOTE,
175 local_branch=REMOTE_BRANCH,
176 remote_branch=LOCAL_BRANCH,
179 assert args == [REMOTE, 'local:remote']
180 assert kwargs['verbose']
181 assert 'tags' not in kwargs
182 assert 'rebase' not in kwargs
185 def test_remote_args_push_tags(mock_context):
186 # Push, swap local and remote
187 (args, kwargs) = main.remote_args(
188 mock_context,
189 REMOTE,
190 tags=True,
191 local_branch=REMOTE_BRANCH,
192 remote_branch=LOCAL_BRANCH,
195 assert args == [REMOTE, 'local:remote']
196 assert kwargs['verbose']
197 assert kwargs['tags']
198 assert 'rebase' not in kwargs
201 def test_remote_args_push_same_remote_and_local(mock_context):
202 (args, kwargs) = main.remote_args(
203 mock_context,
204 REMOTE,
205 tags=True,
206 local_branch=LOCAL_BRANCH,
207 remote_branch=LOCAL_BRANCH,
208 push=True,
211 assert args == [REMOTE, 'local']
212 assert kwargs['verbose']
213 assert kwargs['tags']
214 assert 'rebase' not in kwargs
217 def test_remote_args_push_set_upstream(mock_context):
218 (args, kwargs) = main.remote_args(
219 mock_context,
220 REMOTE,
221 tags=True,
222 local_branch=LOCAL_BRANCH,
223 remote_branch=LOCAL_BRANCH,
224 push=True,
225 set_upstream=True,
228 assert args == [REMOTE, 'local']
229 assert kwargs['verbose']
230 assert kwargs['tags']
231 assert kwargs['set_upstream']
232 assert 'rebase' not in kwargs
235 def test_remote_args_rebase_only(mock_context):
236 (_, kwargs) = main.remote_args(
237 mock_context, REMOTE, pull=True, rebase=True, ff_only=True
239 assert kwargs['rebase']
240 assert 'ff_only' not in kwargs
243 def test_run_remote_action(mock_context):
244 def passthrough(*args, **kwargs):
245 return (args, kwargs)
247 (args, kwargs) = main.run_remote_action(
248 mock_context,
249 passthrough,
250 REMOTE,
251 local_branch=LOCAL_BRANCH,
252 remote_branch=REMOTE_BRANCH,
255 assert args == (REMOTE, 'remote:local')
256 assert kwargs['verbose']
257 assert 'tags' not in kwargs
258 assert 'rebase' not in kwargs