tree-wide: update copyrights notices for 2024
[git-cola.git] / test / gitcmds_test.py
blob393e44e1a79d93efcfc390081b99fc3a54bbc343
1 """Test the cola.gitcmds module"""
2 # pylint: disable=redefined-outer-name
3 import os
5 from cola import core
6 from cola import gitcmds
7 from cola.widgets.remote import get_default_remote
9 from . import helper
10 from .helper import app_context
13 # These assertions make pylint happy. It considers them unused imports otherwise.
14 assert app_context is not None
17 def test_currentbranch(app_context):
18 """Test current_branch()."""
19 assert gitcmds.current_branch(app_context) == 'main'
22 def test_branch_list_local(app_context):
23 """Test branch_list(remote=False)."""
24 helper.commit_files()
25 expect = ['main']
26 actual = gitcmds.branch_list(app_context, remote=False)
27 assert expect == actual
30 def test_branch_list_remote(app_context):
31 """Test branch_list(remote=False)."""
32 expect = []
33 actual = gitcmds.branch_list(app_context, remote=True)
34 assert expect == actual
36 helper.commit_files()
37 helper.run_git('remote', 'add', 'origin', '.')
38 helper.run_git('fetch', 'origin')
40 expect = ['origin/main']
41 actual = gitcmds.branch_list(app_context, remote=True)
42 assert expect == actual
44 helper.run_git('remote', 'rm', 'origin')
45 expect = []
46 actual = gitcmds.branch_list(app_context, remote=True)
47 assert expect == actual
50 def test_upstream_remote(app_context):
51 """Test getting the configured upstream remote"""
52 assert gitcmds.upstream_remote(app_context) is None
53 helper.run_git('config', 'branch.main.remote', 'test')
54 app_context.cfg.reset()
55 assert gitcmds.upstream_remote(app_context) == 'test'
58 def test_default_push(app_context):
59 """Test getting what default branch to push to"""
60 # no default push, no remote branch configured
61 assert get_default_remote(app_context) == 'origin'
63 # default push set, no remote branch configured
64 helper.run_git('config', 'remote.pushDefault', 'test')
65 app_context.cfg.reset()
66 assert get_default_remote(app_context) == 'test'
68 # default push set, default remote branch configured
69 helper.run_git('config', 'branch.main.remote', 'test2')
70 app_context.cfg.reset()
71 assert get_default_remote(app_context) == 'test2'
73 # default push set, default remote branch configured, on different branch
74 helper.run_git('checkout', '-b', 'other-branch')
75 assert get_default_remote(app_context) == 'test'
78 def test_tracked_branch(app_context):
79 """Test tracked_branch()."""
80 assert gitcmds.tracked_branch(app_context) is None
81 helper.run_git('config', 'branch.main.remote', 'test')
82 helper.run_git('config', 'branch.main.merge', 'refs/heads/main')
83 app_context.cfg.reset()
84 assert gitcmds.tracked_branch(app_context) == 'test/main'
87 def test_tracked_branch_other(app_context):
88 """Test tracked_branch('other')"""
89 assert gitcmds.tracked_branch(app_context, 'other') is None
90 helper.run_git('config', 'branch.other.remote', 'test')
91 helper.run_git('config', 'branch.other.merge', 'refs/heads/other/branch')
92 app_context.cfg.reset()
93 assert gitcmds.tracked_branch(app_context, 'other') == 'test/other/branch'
96 def test_untracked_files(app_context):
97 """Test untracked_files()."""
98 helper.touch('C', 'D', 'E')
99 assert gitcmds.untracked_files(app_context) == ['C', 'D', 'E']
102 def test_all_files(app_context):
103 helper.touch('other-file')
104 all_files = gitcmds.all_files(app_context)
106 assert 'A' in all_files
107 assert 'B' in all_files
108 assert 'other-file' in all_files
111 def test_tag_list(app_context):
112 """Test tag_list()"""
113 helper.commit_files()
114 helper.run_git('tag', 'a')
115 helper.run_git('tag', 'b')
116 helper.run_git('tag', 'c')
117 assert gitcmds.tag_list(app_context) == ['c', 'b', 'a']
120 def test_merge_message_path(app_context):
121 """Test merge_message_path()."""
122 helper.touch('.git/SQUASH_MSG')
123 assert gitcmds.merge_message_path(app_context) == os.path.abspath('.git/SQUASH_MSG')
124 helper.touch('.git/MERGE_MSG')
125 assert gitcmds.merge_message_path(app_context) == os.path.abspath('.git/MERGE_MSG')
126 os.unlink(gitcmds.merge_message_path(app_context))
127 assert gitcmds.merge_message_path(app_context) == os.path.abspath('.git/SQUASH_MSG')
128 os.unlink(gitcmds.merge_message_path(app_context))
129 assert gitcmds.merge_message_path(app_context) is None
132 def test_all_refs(app_context):
133 helper.commit_files()
134 helper.run_git('branch', 'a')
135 helper.run_git('branch', 'b')
136 helper.run_git('branch', 'c')
137 helper.run_git('tag', 'd')
138 helper.run_git('tag', 'e')
139 helper.run_git('tag', 'f')
140 helper.run_git('remote', 'add', 'origin', '.')
141 helper.run_git('fetch', 'origin')
143 refs = gitcmds.all_refs(app_context)
145 assert refs == [
146 'a',
147 'b',
148 'c',
149 'main',
150 'origin/a',
151 'origin/b',
152 'origin/c',
153 'origin/main',
154 'f',
155 'e',
156 'd',
160 def test_all_refs_split(app_context):
161 helper.commit_files()
162 helper.run_git('branch', 'a')
163 helper.run_git('branch', 'b')
164 helper.run_git('branch', 'c')
165 helper.run_git('tag', 'd')
166 helper.run_git('tag', 'e')
167 helper.run_git('tag', 'f')
168 helper.run_git('remote', 'add', 'origin', '.')
169 helper.run_git('fetch', 'origin')
171 local, remote, tags = gitcmds.all_refs(app_context, split=True)
173 assert local == ['a', 'b', 'c', 'main']
174 assert remote == ['origin/a', 'origin/b', 'origin/c', 'origin/main']
175 assert tags == ['f', 'e', 'd']
178 def test_binary_files(app_context):
179 # Create a binary file and ensure that it's detected as binary.
180 with core.xopen('binary-file.txt', 'wb') as f:
181 f.write(b'hello\0world\n')
182 assert gitcmds.is_binary(app_context, 'binary-file.txt')
184 # Create a text file and ensure that it's not detected as binary.
185 with core.open_write('text-file.txt') as f:
186 f.write('hello world\n')
187 assert not gitcmds.is_binary(app_context, 'text-file.txt')
189 # Create a .gitattributes file and mark text-file.txt as binary.
190 app_context.cfg.reset()
191 with core.open_write('.gitattributes') as f:
192 f.write('text-file.txt binary\n')
193 assert gitcmds.is_binary(app_context, 'text-file.txt')
195 # Remove the "binary" attribute using "-binary" from binary-file.txt.
196 # Ensure that we do not flag this file as binary.
197 with core.open_write('.gitattributes') as f:
198 f.write('binary-file.txt -binary\n')
199 assert not gitcmds.is_binary(app_context, 'binary-file.txt')
202 def test_is_valid_ref(app_context):
203 """Verify the behavior of is_valid_ref()"""
204 # We are initially in a "git init" state. HEAD must be invalid.
205 assert not gitcmds.is_valid_ref(app_context, 'HEAD')
206 # Create the first commit onto the "test" branch.
207 app_context.git.symbolic_ref('HEAD', 'refs/heads/test')
208 app_context.git.commit(m='initial commit')
209 assert gitcmds.is_valid_ref(app_context, 'HEAD')
210 assert gitcmds.is_valid_ref(app_context, 'test')
211 assert gitcmds.is_valid_ref(app_context, 'refs/heads/test')
214 def test_diff_helper(app_context):
215 helper.commit_files()
216 with core.open_write('A') as f:
217 f.write('A change\n')
218 helper.run_git('add', 'A')
220 expect_n = '+A change\n'
221 expect_rn = '+A change\r\n'
222 actual = gitcmds.diff_helper(app_context, ref='HEAD', cached=True)
223 assert expect_n in actual or expect_rn in actual