1 """Test the cola.gitcmds module"""
2 # pylint: disable=redefined-outer-name
3 from __future__
import absolute_import
, division
, print_function
, unicode_literals
7 from cola
import gitcmds
8 from cola
.widgets
.remote
import get_default_remote
11 from .helper
import app_context
14 # These assertions make pylint happy. It considers them unused imports otherwise.
15 assert app_context
is not None
18 def test_currentbranch(app_context
):
19 """Test current_branch()."""
20 assert gitcmds
.current_branch(app_context
) == 'main'
23 def test_branch_list_local(app_context
):
24 """Test branch_list(remote=False)."""
27 actual
= gitcmds
.branch_list(app_context
, remote
=False)
28 assert expect
== actual
31 def test_branch_list_remote(app_context
):
32 """Test branch_list(remote=False)."""
34 actual
= gitcmds
.branch_list(app_context
, remote
=True)
35 assert expect
== actual
38 helper
.run_git('remote', 'add', 'origin', '.')
39 helper
.run_git('fetch', 'origin')
41 expect
= ['origin/main']
42 actual
= gitcmds
.branch_list(app_context
, remote
=True)
43 assert expect
== actual
45 helper
.run_git('remote', 'rm', 'origin')
47 actual
= gitcmds
.branch_list(app_context
, remote
=True)
48 assert expect
== actual
51 def test_upstream_remote(app_context
):
52 """Test getting the configured upstream remote"""
53 assert gitcmds
.upstream_remote(app_context
) is None
54 helper
.run_git('config', 'branch.main.remote', 'test')
55 app_context
.cfg
.reset()
56 assert gitcmds
.upstream_remote(app_context
) == 'test'
59 def test_default_push(app_context
):
60 """Test getting what default branch to push to"""
61 # no default push, no remote branch configured
62 assert get_default_remote(app_context
) == 'origin'
64 # default push set, no remote branch configured
65 helper
.run_git('config', 'remote.pushDefault', 'test')
66 app_context
.cfg
.reset()
67 assert get_default_remote(app_context
) == 'test'
69 # default push set, default remote branch configured
70 helper
.run_git('config', 'branch.main.remote', 'test2')
71 app_context
.cfg
.reset()
72 assert get_default_remote(app_context
) == 'test2'
74 # default push set, default remote branch configured, on different branch
75 helper
.run_git('checkout', '-b', 'other-branch')
76 assert get_default_remote(app_context
) == 'test'
79 def test_tracked_branch(app_context
):
80 """Test tracked_branch()."""
81 assert gitcmds
.tracked_branch(app_context
) is None
82 helper
.run_git('config', 'branch.main.remote', 'test')
83 helper
.run_git('config', 'branch.main.merge', 'refs/heads/main')
84 app_context
.cfg
.reset()
85 assert gitcmds
.tracked_branch(app_context
) == 'test/main'
88 def test_tracked_branch_other(app_context
):
89 """Test tracked_branch('other')"""
90 assert gitcmds
.tracked_branch(app_context
, 'other') is None
91 helper
.run_git('config', 'branch.other.remote', 'test')
92 helper
.run_git('config', 'branch.other.merge', 'refs/heads/other/branch')
93 app_context
.cfg
.reset()
94 assert gitcmds
.tracked_branch(app_context
, 'other') == 'test/other/branch'
97 def test_untracked_files(app_context
):
98 """Test untracked_files()."""
99 helper
.touch('C', 'D', 'E')
100 assert gitcmds
.untracked_files(app_context
) == ['C', 'D', 'E']
103 def test_all_files(app_context
):
104 helper
.touch('other-file')
105 all_files
= gitcmds
.all_files(app_context
)
107 assert 'A' in all_files
108 assert 'B' in all_files
109 assert 'other-file' in all_files
112 def test_tag_list(app_context
):
113 """Test tag_list()"""
114 helper
.commit_files()
115 helper
.run_git('tag', 'a')
116 helper
.run_git('tag', 'b')
117 helper
.run_git('tag', 'c')
118 assert gitcmds
.tag_list(app_context
) == ['c', 'b', 'a']
121 def test_merge_message_path(app_context
):
122 """Test merge_message_path()."""
123 helper
.touch('.git/SQUASH_MSG')
124 assert gitcmds
.merge_message_path(app_context
) == os
.path
.abspath('.git/SQUASH_MSG')
125 helper
.touch('.git/MERGE_MSG')
126 assert gitcmds
.merge_message_path(app_context
) == os
.path
.abspath('.git/MERGE_MSG')
127 os
.unlink(gitcmds
.merge_message_path(app_context
))
128 assert gitcmds
.merge_message_path(app_context
) == os
.path
.abspath('.git/SQUASH_MSG')
129 os
.unlink(gitcmds
.merge_message_path(app_context
))
130 assert gitcmds
.merge_message_path(app_context
) is None
133 def test_all_refs(app_context
):
134 helper
.commit_files()
135 helper
.run_git('branch', 'a')
136 helper
.run_git('branch', 'b')
137 helper
.run_git('branch', 'c')
138 helper
.run_git('tag', 'd')
139 helper
.run_git('tag', 'e')
140 helper
.run_git('tag', 'f')
141 helper
.run_git('remote', 'add', 'origin', '.')
142 helper
.run_git('fetch', 'origin')
144 refs
= gitcmds
.all_refs(app_context
)
161 def test_all_refs_split(app_context
):
162 helper
.commit_files()
163 helper
.run_git('branch', 'a')
164 helper
.run_git('branch', 'b')
165 helper
.run_git('branch', 'c')
166 helper
.run_git('tag', 'd')
167 helper
.run_git('tag', 'e')
168 helper
.run_git('tag', 'f')
169 helper
.run_git('remote', 'add', 'origin', '.')
170 helper
.run_git('fetch', 'origin')
172 local
, remote
, tags
= gitcmds
.all_refs(app_context
, split
=True)
174 assert local
== ['a', 'b', 'c', 'main']
175 assert remote
== ['origin/a', 'origin/b', 'origin/c', 'origin/main']
176 assert tags
== ['f', 'e', 'd']
179 def test_binary_files(app_context
):
180 # Create a binary file and ensure that it's detected as binary.
181 with core
.xopen('binary-file.txt', 'wb') as f
:
182 f
.write(b
'hello\0world\n')
183 assert gitcmds
.is_binary(app_context
, 'binary-file.txt')
185 # Create a text file and ensure that it's not detected as binary.
186 with core
.open_write('text-file.txt') as f
:
187 f
.write('hello world\n')
188 assert not gitcmds
.is_binary(app_context
, 'text-file.txt')
190 # Create a .gitattributes file and mark text-file.txt as binary.
191 app_context
.cfg
.reset()
192 with core
.open_write('.gitattributes') as f
:
193 f
.write('text-file.txt binary\n')
194 assert gitcmds
.is_binary(app_context
, 'text-file.txt')
196 # Remove the "binary" attribute using "-binary" from binary-file.txt.
197 # Ensure that we do not flag this file as binary.
198 with core
.open_write('.gitattributes') as f
:
199 f
.write('binary-file.txt -binary\n')
200 assert not gitcmds
.is_binary(app_context
, 'binary-file.txt')
203 def test_is_valid_ref(app_context
):
204 """Verify the behavior of is_valid_ref()"""
205 # We are initially in a "git init" state. HEAD must be invalid.
206 assert not gitcmds
.is_valid_ref(app_context
, 'HEAD')
207 # Create the first commit onto the "test" branch.
208 app_context
.git
.symbolic_ref('HEAD', 'refs/heads/test')
209 app_context
.git
.commit(m
='initial commit')
210 assert gitcmds
.is_valid_ref(app_context
, 'HEAD')
211 assert gitcmds
.is_valid_ref(app_context
, 'test')
212 assert gitcmds
.is_valid_ref(app_context
, 'refs/heads/test')
215 def test_diff_helper(app_context
):
216 helper
.commit_files()
217 with core
.open_write('A') as f
:
218 f
.write('A change\n')
219 helper
.run_git('add', 'A')
221 expect
= '+A change\n'
222 actual
= gitcmds
.diff_helper(app_context
, ref
='HEAD', cached
=True)
223 assert expect
in actual