1 from __future__
import absolute_import
, division
, unicode_literals
5 from cola
import gitcmds
6 from cola
.widgets
.remote
import get_default_remote
11 class GitCmdsTestCase(helper
.GitRepositoryTestCase
):
12 """Tests the cola.gitcmds module."""
14 def test_currentbranch(self
):
15 """Test current_branch()."""
16 self
.assertEqual(gitcmds
.current_branch(self
.context
), 'main')
18 def test_branch_list_local(self
):
19 """Test branch_list(remote=False)."""
20 context
= self
.context
23 actual
= gitcmds
.branch_list(context
, remote
=False)
24 self
.assertEqual(expect
, actual
)
26 def test_branch_list_remote(self
):
27 """Test branch_list(remote=False)."""
28 context
= self
.context
30 actual
= gitcmds
.branch_list(context
, remote
=True)
31 self
.assertEqual(expect
, actual
)
34 self
.run_git('remote', 'add', 'origin', '.')
35 self
.run_git('fetch', 'origin')
36 expect
= ['origin/main']
37 actual
= gitcmds
.branch_list(context
, remote
=True)
38 self
.assertEqual(expect
, actual
)
40 self
.run_git('remote', 'rm', 'origin')
42 actual
= gitcmds
.branch_list(context
, remote
=True)
43 self
.assertEqual(expect
, actual
)
45 def test_upstream_remote(self
):
46 """Test getting the configured upstream remote"""
47 context
= self
.context
48 self
.assertEqual(gitcmds
.upstream_remote(context
), None)
49 self
.run_git('config', 'branch.main.remote', 'test')
51 self
.assertEqual(gitcmds
.upstream_remote(context
), 'test')
53 def test_default_push(self
):
54 """Test getting what default branch to push to"""
55 context
= self
.context
57 # no default push, no remote branch configured
58 self
.assertEqual(get_default_remote(context
), 'origin')
60 # default push set, no remote branch configured
61 self
.run_git('config', 'remote.pushDefault', 'test')
63 self
.assertEqual(get_default_remote(context
), 'test')
65 # default push set, default remote branch configured
66 self
.run_git('config', 'branch.main.remote', 'test2')
68 self
.assertEqual(get_default_remote(context
), 'test2')
70 # default push set, default remote branch configured, on different branch
71 self
.run_git('checkout', '-b', 'other-branch')
72 self
.assertEqual(get_default_remote(context
), 'test')
74 def test_tracked_branch(self
):
75 """Test tracked_branch()."""
76 context
= self
.context
77 self
.assertEqual(gitcmds
.tracked_branch(context
), None)
78 self
.run_git('config', 'branch.main.remote', 'test')
79 self
.run_git('config', 'branch.main.merge', 'refs/heads/main')
81 self
.assertEqual(gitcmds
.tracked_branch(context
), 'test/main')
83 def test_tracked_branch_other(self
):
84 """Test tracked_branch('other')."""
85 context
= self
.context
86 self
.assertEqual(gitcmds
.tracked_branch(context
, 'other'), None)
87 self
.run_git('config', 'branch.other.remote', 'test')
88 self
.run_git('config', 'branch.other.merge', 'refs/heads/other/branch')
90 self
.assertEqual(gitcmds
.tracked_branch(context
, 'other'), 'test/other/branch')
92 def test_untracked_files(self
):
93 """Test untracked_files()."""
94 context
= self
.context
95 self
.touch('C', 'D', 'E')
96 self
.assertEqual(gitcmds
.untracked_files(context
), ['C', 'D', 'E'])
98 def test_all_files(self
):
99 context
= self
.context
100 self
.touch('other-file')
101 all_files
= gitcmds
.all_files(context
)
103 self
.assertTrue('A' in all_files
)
104 self
.assertTrue('B' in all_files
)
105 self
.assertTrue('other-file' in all_files
)
107 def test_tag_list(self
):
108 """Test tag_list()."""
109 context
= self
.context
111 self
.run_git('tag', 'a')
112 self
.run_git('tag', 'b')
113 self
.run_git('tag', 'c')
114 self
.assertEqual(gitcmds
.tag_list(context
), ['c', 'b', 'a'])
116 def test_merge_message_path(self
):
117 """Test merge_message_path()."""
118 context
= self
.context
119 self
.touch('.git/SQUASH_MSG')
121 gitcmds
.merge_message_path(context
), os
.path
.abspath('.git/SQUASH_MSG')
123 self
.touch('.git/MERGE_MSG')
125 gitcmds
.merge_message_path(context
), os
.path
.abspath('.git/MERGE_MSG')
127 os
.unlink(gitcmds
.merge_message_path(context
))
129 gitcmds
.merge_message_path(context
), os
.path
.abspath('.git/SQUASH_MSG')
131 os
.unlink(gitcmds
.merge_message_path(context
))
132 self
.assertEqual(gitcmds
.merge_message_path(context
), None)
134 def test_all_refs(self
):
136 self
.run_git('branch', 'a')
137 self
.run_git('branch', 'b')
138 self
.run_git('branch', 'c')
139 self
.run_git('tag', 'd')
140 self
.run_git('tag', 'e')
141 self
.run_git('tag', 'f')
142 self
.run_git('remote', 'add', 'origin', '.')
143 self
.run_git('fetch', 'origin')
144 refs
= gitcmds
.all_refs(self
.context
)
162 def test_all_refs_split(self
):
164 self
.run_git('branch', 'a')
165 self
.run_git('branch', 'b')
166 self
.run_git('branch', 'c')
167 self
.run_git('tag', 'd')
168 self
.run_git('tag', 'e')
169 self
.run_git('tag', 'f')
170 self
.run_git('remote', 'add', 'origin', '.')
171 self
.run_git('fetch', 'origin')
172 local
, remote
, tags
= gitcmds
.all_refs(self
.context
, split
=True)
173 self
.assertEqual(local
, ['a', 'b', 'c', 'main'])
174 self
.assertEqual(remote
, ['origin/a', 'origin/b', 'origin/c', 'origin/main'])
175 self
.assertEqual(tags
, ['f', 'e', 'd'])
177 def test_binary_files(self
):
178 # Create a binary file and ensure that it's detected as binary.
179 with
open('binary-file.txt', 'wb') as f
:
180 f
.write(b
'hello\0world\n')
181 assert gitcmds
.is_binary(self
.context
, 'binary-file.txt')
183 # Create a text file and ensure that it's not detected as binary.
184 with
open('text-file.txt', 'w') as f
:
185 f
.write('hello world\n')
186 assert not gitcmds
.is_binary(self
.context
, 'text-file.txt')
188 # Create a .gitattributes file and mark text-file.txt as binary.
190 with
open('.gitattributes', 'w') as f
:
191 f
.write('text-file.txt binary\n')
192 assert gitcmds
.is_binary(self
.context
, 'text-file.txt')
194 # Remove the "binary" attribute using "-binary" from binary-file.txt.
195 # Ensure that we do not flag this file as binary.
196 with
open('.gitattributes', 'w') as f
:
197 f
.write('binary-file.txt -binary\n')
198 assert not gitcmds
.is_binary(self
.context
, 'binary-file.txt')
201 if __name__
== '__main__':