forked from git-cola/git-cola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_test.py
283 lines (229 loc) · 9.25 KB
/
git_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
"""Tests various operations using the cola.git module
"""
from __future__ import absolute_import, division, unicode_literals
import os
import time
import unittest
try:
from unittest.mock import patch
except ImportError:
from mock import patch
from cola import git
from cola.compat import WIN32
from cola.git import STDOUT
class GitModuleTestCase(unittest.TestCase):
@patch('cola.git.is_git_dir')
def test_find_git_dir_None(self, is_git_dir):
paths = git.find_git_directory(None)
self.assertFalse(is_git_dir.called)
self.assertEqual(None, paths.git_dir)
self.assertEqual(None, paths.git_file)
self.assertEqual(None, paths.worktree)
@patch('cola.git.is_git_dir')
def test_find_git_dir_empty_string(self, is_git_dir):
paths = git.find_git_directory('')
self.assertFalse(is_git_dir.called)
self.assertEqual(None, paths.git_dir)
self.assertEqual(None, paths.git_file)
self.assertEqual(None, paths.worktree)
@patch('cola.git.is_git_dir')
def test_find_git_dir_never_found(self, is_git_dir):
is_git_dir.return_value = False
paths = git.find_git_directory('/does/not/exist')
self.assertTrue(is_git_dir.called)
self.assertEqual(None, paths.git_dir)
self.assertEqual(None, paths.git_file)
self.assertEqual(None, paths.worktree)
self.assertEqual(8, is_git_dir.call_count)
kwargs = {}
is_git_dir.assert_has_calls([
(('/does/not/exist',), kwargs),
(('/does/not/exist/.git',), kwargs),
(('/does/not',), kwargs),
(('/does/not/.git',), kwargs),
(('/does',), kwargs),
(('/does/.git',), kwargs),
(('/',), kwargs),
(('/.git',), kwargs),
])
@patch('cola.git.is_git_dir')
def test_find_git_dir_found_right_away(self, is_git_dir):
git_dir = '/seems/to/exist/.git'
worktree = '/seems/to/exist'
is_git_dir.return_value = True
paths = git.find_git_directory(git_dir)
self.assertTrue(is_git_dir.called)
self.assertEqual(git_dir, paths.git_dir)
self.assertEqual(None, paths.git_file)
self.assertEqual(worktree, paths.worktree)
@patch('cola.git.is_git_dir')
def test_find_git_does_discovery(self, is_git_dir):
git_dir = '/the/root/.git'
worktree = '/the/root'
is_git_dir.side_effect = lambda x: x == git_dir
paths = git.find_git_directory('/the/root/sub/dir')
self.assertEqual(git_dir, paths.git_dir)
self.assertEqual(None, paths.git_file)
self.assertEqual(worktree, paths.worktree)
@patch('cola.git.read_git_file')
@patch('cola.git.is_git_file')
@patch('cola.git.is_git_dir')
def test_find_git_honors_git_files(self,
is_git_dir,
is_git_file,
read_git_file):
git_file = '/the/root/.git'
worktree = '/the/root'
git_dir = '/super/module/.git/modules/root'
is_git_dir.side_effect = lambda x: x == git_file
is_git_file.side_effect = lambda x: x == git_file
read_git_file.return_value = git_dir
paths = git.find_git_directory('/the/root/sub/dir')
self.assertEqual(git_dir, paths.git_dir)
self.assertEqual(git_file, paths.git_file)
self.assertEqual(worktree, paths.worktree)
kwargs = {}
self.assertEqual(6, is_git_dir.call_count)
is_git_dir.assert_has_calls([
(('/the/root/sub/dir',), kwargs),
(('/the/root/sub/dir/.git',), kwargs),
(('/the/root/sub',), kwargs),
(('/the/root/sub/.git',), kwargs),
(('/the/root',), kwargs),
(('/the/root/.git',), kwargs),
])
read_git_file.assert_called_once_with('/the/root/.git')
@patch('cola.core.getenv')
@patch('cola.git.is_git_dir')
def test_find_git_honors_ceiling_dirs(self, is_git_dir, getenv):
git_dir = '/ceiling/.git'
ceiling = '/tmp:/ceiling:/other/ceiling'
is_git_dir.side_effect = lambda x: x == git_dir
def mock_getenv(k, v=None):
if k == 'GIT_CEILING_DIRECTORIES':
return ceiling
return v
getenv.side_effect = mock_getenv
paths = git.find_git_directory('/ceiling/sub/dir')
self.assertEqual(None, paths.git_dir)
self.assertEqual(None, paths.git_file)
self.assertEqual(None, paths.worktree)
self.assertEqual(4, is_git_dir.call_count)
kwargs = {}
is_git_dir.assert_has_calls([
(('/ceiling/sub/dir',), kwargs),
(('/ceiling/sub/dir/.git',), kwargs),
(('/ceiling/sub',), kwargs),
(('/ceiling/sub/.git',), kwargs),
])
@patch('cola.core.islink')
@patch('cola.core.isdir')
@patch('cola.core.isfile')
def test_is_git_dir_finds_linked_repository(self, isfile, isdir, islink):
dirs = set([
'/foo',
'/foo/.git',
'/foo/.git/refs',
'/foo/.git/objects',
'/foo/.git/worktrees',
'/foo/.git/worktrees/foo',
])
files = set([
'/foo/.git/HEAD',
'/foo/.git/worktrees/foo/HEAD',
'/foo/.git/worktrees/foo/index',
'/foo/.git/worktrees/foo/commondir',
'/foo/.git/worktrees/foo/gitdir',
])
islink.return_value = False
isfile.side_effect = lambda x: x in files
isdir.side_effect = lambda x: x in dirs
self.assertTrue(git.is_git_dir('/foo/.git/worktrees/foo'))
self.assertTrue(git.is_git_dir('/foo/.git'))
class GitCommandTest(unittest.TestCase):
"""Runs tests using a git.Git instance"""
def setUp(self):
"""Creates a git.Git instance for later use"""
self.git = git.Git()
def test_transform_kwargs(self):
expect = []
actual = self.git.transform_kwargs(foo=None, bar=False)
self.assertEqual(expect, actual)
expect = ['-a']
actual = self.git.transform_kwargs(a=True)
self.assertEqual(expect, actual)
expect = ['--abc']
actual = self.git.transform_kwargs(abc=True)
self.assertEqual(expect, actual)
expect = ['-a1']
actual = self.git.transform_kwargs(a=1)
self.assertEqual(expect, actual)
expect = ['--abc=1']
actual = self.git.transform_kwargs(abc=1)
self.assertEqual(expect, actual)
expect = ['-abc']
actual = self.git.transform_kwargs(a='bc')
self.assertEqual(expect, actual)
expect = ['--abc=def']
actual = self.git.transform_kwargs(abc='def')
self.assertEqual(expect, actual)
def test_version(self):
"""Test running 'git version'"""
version = self.git.version()[STDOUT]
self.failUnless(version.startswith('git version'))
def test_tag(self):
"""Test running 'git tag'"""
tags = self.git.tag()[STDOUT].splitlines()
if os.getenv('GIT_COLA_NO_HISTORY', False):
return
self.failUnless('v1.0.0' in tags)
def test_show(self):
"""Test running 'git show'"""
oid = 'HEAD'
content = self.git.show(oid)[STDOUT]
self.failUnless(content.startswith('commit '))
def test_stdout(self):
"""Test overflowing the stdout buffer"""
# Write to stdout only
code = ('import sys;'
's = "\\0" * (1024 * 16 + 1);'
'sys.stdout.write(s);')
status, out, err = git.Git.execute(['python', '-c', code], _raw=True)
self.assertEqual(status, 0)
self.assertEqual(len(out), 1024 * 16 + 1)
self.assertEqual(len(err), 0)
def test_stderr(self):
"""Test that stderr is seen"""
# Write to stderr and capture it
code = ('import sys;'
's = "\\0" * (1024 * 16 + 1);'
'sys.stderr.write(s);')
status, out, err = git.Git.execute(['python', '-c', code], _raw=True)
self.assertEqual(status, 0)
self.assertEqual(len(out), 0)
self.assertEqual(len(err), 1024 * 16 + 1)
def test_stdout_and_stderr(self):
"""Test ignoring stderr when stdout+stderr are provided (v2)"""
# Write to stdout and stderr but only capture stdout
code = ('import sys;'
's = "\\0" * (1024 * 16 + 1);'
'sys.stdout.write(s);'
'sys.stderr.write(s);')
status, out, err = git.Git.execute(['python', '-c', code], _raw=True)
self.assertEqual(status, 0)
self.assertEqual(len(out), 1024 * 16 + 1)
self.assertEqual(len(err), 1024 * 16 + 1)
def test_it_doesnt_deadlock(self):
"""Test that we don't deadlock with both stderr and stdout"""
# 16k+1 bytes to exhaust any output buffers
code = ('import sys;'
's = "\\0" * (1024 * 16 + 1);'
'sys.stderr.write(s);'
'sys.stdout.write(s);')
status, out, err = git.Git.execute(['python', '-c', code], _raw=True)
self.assertEqual(status, 0)
self.assertEqual(out, '\0' * (1024 * 16 + 1))
self.assertEqual(err, '\0' * (1024 * 16 + 1))
if __name__ == '__main__':
unittest.main()