7 from cola
.models
import main
8 from cola
.models
.main
import FETCH
, PULL
, PUSH
11 from .helper
import app_context
12 from .helper
import Mock
15 # prevent unused imports lint errors.
16 assert app_context
is not None
19 LOCAL_BRANCH
= 'local'
20 REMOTE_BRANCH
= 'remote'
25 """Return a Mock context for testing"""
27 context
.git
= git
.create()
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."""
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
.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."""
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(
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
):
122 (args
, kwargs
) = main
.remote_args(
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_fetch_into_tracking_branch(mock_context
):
137 (args
, kwargs
) = main
.remote_args(
141 remote_branch
=REMOTE_BRANCH
,
143 assert args
== [REMOTE
, 'remote:refs/remotes/server/remote']
146 def test_remote_args_pull(mock_context
):
148 (args
, kwargs
) = main
.remote_args(
153 remote_branch
=REMOTE_BRANCH
,
155 assert args
== [REMOTE
, 'remote']
156 assert kwargs
['verbose']
157 assert 'rebase' not in kwargs
158 assert 'tags' not in kwargs
161 def test_remote_args_pull_rebase(mock_context
):
163 (args
, kwargs
) = main
.remote_args(
169 remote_branch
=REMOTE_BRANCH
,
171 assert args
== [REMOTE
, 'remote']
172 assert kwargs
['verbose']
173 assert kwargs
['rebase']
174 assert 'tags' not in kwargs
177 def test_remote_args_push(mock_context
):
178 """PUSH swaps local and remote branches"""
179 (args
, kwargs
) = main
.remote_args(
183 local_branch
=LOCAL_BRANCH
,
184 remote_branch
=REMOTE_BRANCH
,
186 assert args
== [REMOTE
, 'local:remote']
187 assert kwargs
['verbose']
188 assert 'tags' not in kwargs
189 assert 'rebase' not in kwargs
192 def test_remote_args_push_tags(mock_context
):
193 """Pushing tags uses --tags"""
194 (args
, kwargs
) = main
.remote_args(
199 local_branch
=LOCAL_BRANCH
,
200 remote_branch
=REMOTE_BRANCH
,
202 assert args
== [REMOTE
, 'local:remote']
203 assert kwargs
['verbose']
204 assert kwargs
['tags']
205 assert 'rebase' not in kwargs
208 def test_remote_args_push_same_remote_and_local(mock_context
):
209 (args
, kwargs
) = main
.remote_args(
214 local_branch
=LOCAL_BRANCH
,
215 remote_branch
=LOCAL_BRANCH
,
217 assert args
== [REMOTE
, 'local']
218 assert kwargs
['verbose']
219 assert kwargs
['tags']
220 assert 'rebase' not in kwargs
223 def test_remote_args_push_set_upstream(mock_context
):
224 (args
, kwargs
) = main
.remote_args(
229 local_branch
=LOCAL_BRANCH
,
230 remote_branch
=LOCAL_BRANCH
,
233 assert args
== [REMOTE
, 'local']
234 assert kwargs
['verbose']
235 assert kwargs
['tags']
236 assert kwargs
['set_upstream']
237 assert 'rebase' not in kwargs
240 def test_remote_args_rebase_only(mock_context
):
241 (_
, kwargs
) = main
.remote_args(
242 mock_context
, REMOTE
, PULL
, rebase
=True, ff_only
=True
244 assert kwargs
['rebase']
245 assert 'ff_only' not in kwargs
248 def test_run_remote_action(mock_context
):
249 """Test running a remote action"""
250 (args
, kwargs
) = main
.run_remote_action(
252 lambda *args
, **kwargs
: (args
, kwargs
),
255 local_branch
=LOCAL_BRANCH
,
256 remote_branch
=REMOTE_BRANCH
,
258 assert args
== (REMOTE
, 'remote:local')
259 assert kwargs
['verbose']
260 assert 'tags' not in kwargs
261 assert 'rebase' not in kwargs