tests: prune empty lines and add docstrings
[git-cola.git] / test / gitcmds_test.py
blobfe8b625988207bb20403b030b33276902633ba9e
1 """Test the cola.gitcmds module"""
2 import os
4 from cola import core
5 from cola import gitcmds
6 from cola.widgets.remote import get_default_remote
8 from . import helper
9 from .helper import app_context
12 # Prevent unused imports lint errors.
13 assert app_context is not None
16 def test_currentbranch(app_context):
17 """Test current_branch()."""
18 assert gitcmds.current_branch(app_context) == 'main'
21 def test_branch_list_local(app_context):
22 """Test branch_list(remote=False)."""
23 helper.commit_files()
24 expect = ['main']
25 actual = gitcmds.branch_list(app_context, remote=False)
26 assert expect == actual
29 def test_branch_list_remote(app_context):
30 """Test branch_list(remote=False)."""
31 expect = []
32 actual = gitcmds.branch_list(app_context, remote=True)
33 assert expect == actual
35 helper.commit_files()
36 helper.run_git('remote', 'add', 'origin', '.')
37 helper.run_git('fetch', 'origin')
39 expect = ['origin/main']
40 actual = gitcmds.branch_list(app_context, remote=True)
41 assert expect == actual
43 helper.run_git('remote', 'rm', 'origin')
44 expect = []
45 actual = gitcmds.branch_list(app_context, remote=True)
46 assert expect == actual
49 def test_upstream_remote(app_context):
50 """Test getting the configured upstream remote"""
51 assert gitcmds.upstream_remote(app_context) is None
52 helper.run_git('config', 'branch.main.remote', 'test')
53 app_context.cfg.reset()
54 assert gitcmds.upstream_remote(app_context) == 'test'
57 def test_default_push(app_context):
58 """Test getting what default branch to push to"""
59 # no default push, no remote branch configured
60 assert get_default_remote(app_context) == 'origin'
62 # default push set, no remote branch configured
63 helper.run_git('config', 'remote.pushDefault', 'test')
64 app_context.cfg.reset()
65 assert get_default_remote(app_context) == 'test'
67 # default push set, default remote branch configured
68 helper.run_git('config', 'branch.main.remote', 'test2')
69 app_context.cfg.reset()
70 assert get_default_remote(app_context) == 'test2'
72 # default push set, default remote branch configured, on different branch
73 helper.run_git('checkout', '-b', 'other-branch')
74 assert get_default_remote(app_context) == 'test'
77 def test_tracked_branch(app_context):
78 """Test tracked_branch()."""
79 assert gitcmds.tracked_branch(app_context) is None
80 helper.run_git('config', 'branch.main.remote', 'test')
81 helper.run_git('config', 'branch.main.merge', 'refs/heads/main')
82 app_context.cfg.reset()
83 assert gitcmds.tracked_branch(app_context) == 'test/main'
86 def test_tracked_branch_other(app_context):
87 """Test tracked_branch('other')"""
88 assert gitcmds.tracked_branch(app_context, 'other') is None
89 helper.run_git('config', 'branch.other.remote', 'test')
90 helper.run_git('config', 'branch.other.merge', 'refs/heads/other/branch')
91 app_context.cfg.reset()
92 assert gitcmds.tracked_branch(app_context, 'other') == 'test/other/branch'
95 def test_untracked_files(app_context):
96 """Test untracked_files()."""
97 helper.touch('C', 'D', 'E')
98 assert gitcmds.untracked_files(app_context) == ['C', 'D', 'E']
101 def test_all_files(app_context):
102 helper.touch('other-file')
103 all_files = gitcmds.all_files(app_context)
105 assert 'A' in all_files
106 assert 'B' in all_files
107 assert 'other-file' in all_files
110 def test_tag_list(app_context):
111 """Test tag_list()"""
112 helper.commit_files()
113 helper.run_git('tag', 'a')
114 helper.run_git('tag', 'b')
115 helper.run_git('tag', 'c')
116 assert gitcmds.tag_list(app_context) == ['c', 'b', 'a']
119 def test_merge_message_path(app_context):
120 """Test merge_message_path()."""
121 helper.touch('.git/SQUASH_MSG')
122 assert gitcmds.merge_message_path(app_context) == os.path.abspath('.git/SQUASH_MSG')
123 helper.touch('.git/MERGE_MSG')
124 assert gitcmds.merge_message_path(app_context) == os.path.abspath('.git/MERGE_MSG')
125 os.unlink(gitcmds.merge_message_path(app_context))
126 assert gitcmds.merge_message_path(app_context) == os.path.abspath('.git/SQUASH_MSG')
127 os.unlink(gitcmds.merge_message_path(app_context))
128 assert gitcmds.merge_message_path(app_context) is None
131 def test_all_refs(app_context):
132 helper.commit_files()
133 helper.run_git('branch', 'a')
134 helper.run_git('branch', 'b')
135 helper.run_git('branch', 'c')
136 helper.run_git('tag', 'd')
137 helper.run_git('tag', 'e')
138 helper.run_git('tag', 'f')
139 helper.run_git('remote', 'add', 'origin', '.')
140 helper.run_git('fetch', 'origin')
142 refs = gitcmds.all_refs(app_context)
144 assert refs == [
145 'a',
146 'b',
147 'c',
148 'main',
149 'origin/a',
150 'origin/b',
151 'origin/c',
152 'origin/main',
153 'f',
154 'e',
155 'd',
159 def test_all_refs_split(app_context):
160 helper.commit_files()
161 helper.run_git('branch', 'a')
162 helper.run_git('branch', 'b')
163 helper.run_git('branch', 'c')
164 helper.run_git('tag', 'd')
165 helper.run_git('tag', 'e')
166 helper.run_git('tag', 'f')
167 helper.run_git('remote', 'add', 'origin', '.')
168 helper.run_git('fetch', 'origin')
170 local, remote, tags = gitcmds.all_refs(app_context, split=True)
172 assert local == ['a', 'b', 'c', 'main']
173 assert remote == ['origin/a', 'origin/b', 'origin/c', 'origin/main']
174 assert tags == ['f', 'e', 'd']
177 def test_binary_files(app_context):
178 # Create a binary file and ensure that it's detected as binary.
179 with core.xopen('binary-file.txt', 'wb') as f:
180 f.write(b'hello\0world\n')
181 assert gitcmds.is_binary(app_context, 'binary-file.txt')
183 # Create a text file and ensure that it's not detected as binary.
184 with core.open_write('text-file.txt') as f:
185 f.write('hello world\n')
186 assert not gitcmds.is_binary(app_context, 'text-file.txt')
188 # Create a .gitattributes file and mark text-file.txt as binary.
189 app_context.cfg.reset()
190 with core.open_write('.gitattributes') as f:
191 f.write('text-file.txt binary\n')
192 assert gitcmds.is_binary(app_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 core.open_write('.gitattributes') as f:
197 f.write('binary-file.txt -binary\n')
198 assert not gitcmds.is_binary(app_context, 'binary-file.txt')
201 def test_is_valid_ref(app_context):
202 """Verify the behavior of is_valid_ref()"""
203 # We are initially in a "git init" state. HEAD must be invalid.
204 assert not gitcmds.is_valid_ref(app_context, 'HEAD')
205 # Create the first commit onto the "test" branch.
206 app_context.git.symbolic_ref('HEAD', 'refs/heads/test')
207 app_context.git.commit(m='initial commit')
208 assert gitcmds.is_valid_ref(app_context, 'HEAD')
209 assert gitcmds.is_valid_ref(app_context, 'test')
210 assert gitcmds.is_valid_ref(app_context, 'refs/heads/test')
213 def test_diff_helper(app_context):
214 helper.commit_files()
215 with core.open_write('A') as f:
216 f.write('A change\n')
217 helper.run_git('add', 'A')
219 expect_n = '+A change\n'
220 expect_rn = '+A change\r\n'
221 actual = gitcmds.diff_helper(app_context, ref='HEAD', cached=True)
222 assert expect_n in actual or expect_rn in actual