1 from nose
.tools
import eq_
as eq
7 from gitosis
import repository
9 from gitosis
.test
.util
import (
19 # we want it to be a bare repository
20 assert not os
.path
.exists(os
.path
.join(path
, '.git'))
22 def test_init_simple():
24 path
= os
.path
.join(tmp
, 'repo.git')
26 check_mode(path
, 0750, is_dir
=True)
29 def test_init_exist_dir():
31 path
= os
.path
.join(tmp
, 'repo.git')
33 check_mode(path
, 0710, is_dir
=True)
35 # my weird access mode is preserved
36 check_mode(path
, 0710, is_dir
=True)
39 def test_init_exist_git():
41 path
= os
.path
.join(tmp
, 'repo.git')
44 check_mode(path
, 0750, is_dir
=True)
47 def test_init_templates():
49 path
= os
.path
.join(tmp
, 'repo.git')
50 templatedir
= os
.path
.join(
51 os
.path
.dirname(__file__
),
55 repository
.init(path
, template
=templatedir
)
57 got
= readFile(os
.path
.join(path
, 'no-confusion'))
58 eq(got
, 'i should show up\n')
60 os
.path
.join(path
, 'hooks', 'post-update'),
64 got
= readFile(os
.path
.join(path
, 'hooks', 'post-update'))
65 eq(got
, '#!/bin/sh\n# i can override standard templates\n')
66 # standard templates are there, too
67 assert os
.path
.isfile(os
.path
.join(path
, 'hooks', 'pre-rebase'))
69 def test_init_environment():
71 path
= os
.path
.join(tmp
, 'repo.git')
72 mockbindir
= os
.path
.join(tmp
, 'mockbin')
74 mockgit
= os
.path
.join(mockbindir
, 'git')
75 writeFile(mockgit
, '''\
78 # git wrapper for gitosis unit tests
79 printf '%s' "$GITOSIS_UNITTEST_COOKIE" >"$(dirname "$0")/../cookie"
81 # strip away my special PATH insert so system git will be found
86 os
.chmod(mockgit
, 0755)
87 magic_cookie
= '%d' % random
.randint(1, 100000)
88 good_path
= os
.environ
['PATH']
90 os
.environ
['PATH'] = '%s:%s' % (mockbindir
, good_path
)
91 os
.environ
['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
94 os
.environ
['PATH'] = good_path
95 os
.environ
.pop('GITOSIS_UNITTEST_COOKIE', None)
97 sorted(os
.listdir(tmp
)),
104 got
= readFile(os
.path
.join(tmp
, 'cookie'))
105 eq(got
, magic_cookie
)
107 def test_fast_import_environment():
109 path
= os
.path
.join(tmp
, 'repo.git')
110 repository
.init(path
=path
)
111 mockbindir
= os
.path
.join(tmp
, 'mockbin')
113 mockgit
= os
.path
.join(mockbindir
, 'git')
114 writeFile(mockgit
, '''\
117 # git wrapper for gitosis unit tests
118 printf '%s' "$GITOSIS_UNITTEST_COOKIE" >"$(dirname "$0")/../cookie"
120 # strip away my special PATH insert so system git will be found
125 os
.chmod(mockgit
, 0755)
126 magic_cookie
= '%d' % random
.randint(1, 100000)
127 good_path
= os
.environ
['PATH']
129 os
.environ
['PATH'] = '%s:%s' % (mockbindir
, good_path
)
130 os
.environ
['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
131 repository
.fast_import(
133 commit_msg
='foo initial bar',
134 committer
='Mr. Unit Test <unit.test@example.com>',
140 os
.environ
['PATH'] = good_path
141 os
.environ
.pop('GITOSIS_UNITTEST_COOKIE', None)
143 sorted(os
.listdir(tmp
)),
150 got
= readFile(os
.path
.join(tmp
, 'cookie'))
151 eq(got
, magic_cookie
)
153 def test_export_simple():
155 git_dir
= os
.path
.join(tmp
, 'repo.git')
156 repository
.init(path
=git_dir
)
157 repository
.fast_import(
159 committer
='John Doe <jdoe@example.com>',
161 Reverse the polarity of the neutron flow.
163 Frobitz the quux and eschew obfuscation.
167 ('bar/quux', 'another'),
170 export
= os
.path
.join(tmp
, 'export')
171 repository
.export(git_dir
=git_dir
, path
=export
)
172 eq(sorted(os
.listdir(export
)),
173 sorted(['foo', 'bar']))
174 eq(readFile(os
.path
.join(export
, 'foo')), 'content')
175 eq(os
.listdir(os
.path
.join(export
, 'bar')), ['quux'])
176 eq(readFile(os
.path
.join(export
, 'bar', 'quux')), 'another')
177 child
= subprocess
.Popen(
180 '--git-dir=%s' % git_dir
,
186 stdout
=subprocess
.PIPE
,
189 got
= child
.stdout
.read().splitlines()
190 returncode
= child
.wait()
192 raise RuntimeError('git exit status %d' % returncode
)
193 eq(got
[0].split(None, 1)[0], 'tree')
194 eq(got
[1].rsplit(None, 2)[0],
195 'author John Doe <jdoe@example.com>')
196 eq(got
[2].rsplit(None, 2)[0],
197 'committer John Doe <jdoe@example.com>')
199 eq(got
[4], 'Reverse the polarity of the neutron flow.')
201 eq(got
[6], 'Frobitz the quux and eschew obfuscation.')
204 def test_export_environment():
206 git_dir
= os
.path
.join(tmp
, 'repo.git')
207 mockbindir
= os
.path
.join(tmp
, 'mockbin')
209 mockgit
= os
.path
.join(mockbindir
, 'git')
210 writeFile(mockgit
, '''\
213 # git wrapper for gitosis unit tests
214 printf '%s\n' "$GITOSIS_UNITTEST_COOKIE" >>"$(dirname "$0")/../cookie"
216 # strip away my special PATH insert so system git will be found
221 os
.chmod(mockgit
, 0755)
222 repository
.init(path
=git_dir
)
223 repository
.fast_import(
225 committer
='John Doe <jdoe@example.com>',
227 Reverse the polarity of the neutron flow.
229 Frobitz the quux and eschew obfuscation.
233 ('bar/quux', 'another'),
236 export
= os
.path
.join(tmp
, 'export')
237 magic_cookie
= '%d' % random
.randint(1, 100000)
238 good_path
= os
.environ
['PATH']
240 os
.environ
['PATH'] = '%s:%s' % (mockbindir
, good_path
)
241 os
.environ
['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
242 repository
.export(git_dir
=git_dir
, path
=export
)
244 os
.environ
['PATH'] = good_path
245 os
.environ
.pop('GITOSIS_UNITTEST_COOKIE', None)
246 got
= readFile(os
.path
.join(tmp
, 'cookie'))
249 # export runs git twice
250 '%s\n%s\n' % (magic_cookie
, magic_cookie
),
253 def test_has_initial_commit_fail_notAGitDir():
256 repository
.GitRevParseError
,
257 repository
.has_initial_commit
,
259 eq(str(e
), 'rev-parse failed: exit status 128')
261 def test_has_initial_commit_no():
263 repository
.init(path
=tmp
)
264 got
= repository
.has_initial_commit(git_dir
=tmp
)
267 def test_has_initial_commit_yes():
269 repository
.init(path
=tmp
)
270 repository
.fast_import(
272 commit_msg
='fakecommit',
273 committer
='John Doe <jdoe@example.com>',
276 got
= repository
.has_initial_commit(git_dir
=tmp
)
279 def test_has_initial_commit_environment():
281 git_dir
= os
.path
.join(tmp
, 'repo.git')
282 mockbindir
= os
.path
.join(tmp
, 'mockbin')
284 mockgit
= os
.path
.join(mockbindir
, 'git')
285 writeFile(mockgit
, '''\
288 # git wrapper for gitosis unit tests
289 printf '%s' "$GITOSIS_UNITTEST_COOKIE" >"$(dirname "$0")/../cookie"
291 # strip away my special PATH insert so system git will be found
296 os
.chmod(mockgit
, 0755)
297 repository
.init(path
=tmp
)
298 repository
.fast_import(
300 commit_msg
='fakecommit',
301 committer
='John Doe <jdoe@example.com>',
304 magic_cookie
= '%d' % random
.randint(1, 100000)
305 good_path
= os
.environ
['PATH']
307 os
.environ
['PATH'] = '%s:%s' % (mockbindir
, good_path
)
308 os
.environ
['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
309 got
= repository
.has_initial_commit(git_dir
=tmp
)
311 os
.environ
['PATH'] = good_path
312 os
.environ
.pop('GITOSIS_UNITTEST_COOKIE', None)
314 got
= readFile(os
.path
.join(tmp
, 'cookie'))
315 eq(got
, magic_cookie
)
317 def test_fast_import_parent():
319 path
= os
.path
.join(tmp
, 'repo.git')
320 repository
.init(path
=path
)
321 repository
.fast_import(
323 commit_msg
='foo initial bar',
324 committer
='Mr. Unit Test <unit.test@example.com>',
329 repository
.fast_import(
331 commit_msg
='another',
332 committer
='Sam One Else <sam@example.com>',
333 parent
='refs/heads/master^0',
338 export
= os
.path
.join(tmp
, 'export')
343 eq(sorted(os
.listdir(export
)),
344 sorted(['foo', 'quux']))