Fix permission problems:
[gitosis/httpauth.git] / gitosis / test / test_repository.py
blobcfb2dd7381e1ee19e9cd23a6d0b52548dbeaf45f
1 from nose.tools import eq_ as eq
3 import os
4 import subprocess
5 import random
7 from gitosis import repository
9 from gitosis.test.util import (
10 mkdir,
11 maketemp,
12 readFile,
13 writeFile,
14 check_mode,
15 assert_raises,
18 def check_bare(path):
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():
23 tmp = maketemp()
24 path = os.path.join(tmp, 'repo.git')
25 repository.init(path)
26 check_mode(path, 0750, is_dir=True)
27 check_bare(path)
29 def test_init_exist_dir():
30 tmp = maketemp()
31 path = os.path.join(tmp, 'repo.git')
32 mkdir(path, 0710)
33 check_mode(path, 0710, is_dir=True)
34 repository.init(path)
35 # my weird access mode is preserved
36 check_mode(path, 0710, is_dir=True)
37 check_bare(path)
39 def test_init_exist_git():
40 tmp = maketemp()
41 path = os.path.join(tmp, 'repo.git')
42 repository.init(path)
43 repository.init(path)
44 check_mode(path, 0750, is_dir=True)
45 check_bare(path)
47 def test_init_templates():
48 tmp = maketemp()
49 path = os.path.join(tmp, 'repo.git')
50 templatedir = os.path.join(
51 os.path.dirname(__file__),
52 'mocktemplates',
54 os.umask(0022)
55 repository.init(path, template=templatedir)
56 repository.init(path)
57 got = readFile(os.path.join(path, 'no-confusion'))
58 eq(got, 'i should show up\n')
59 check_mode(
60 os.path.join(path, 'hooks', 'post-update'),
61 0755,
62 is_file=True,
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():
70 tmp = maketemp()
71 path = os.path.join(tmp, 'repo.git')
72 mockbindir = os.path.join(tmp, 'mockbin')
73 os.mkdir(mockbindir)
74 mockgit = os.path.join(mockbindir, 'git')
75 writeFile(mockgit, '''\
76 #!/bin/sh
77 set -e
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
82 PATH="${PATH#*:}"
84 exec git "$@"
85 ''')
86 os.chmod(mockgit, 0755)
87 magic_cookie = '%d' % random.randint(1, 100000)
88 good_path = os.environ['PATH']
89 try:
90 os.environ['PATH'] = '%s:%s' % (mockbindir, good_path)
91 os.environ['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
92 repository.init(path)
93 finally:
94 os.environ['PATH'] = good_path
95 os.environ.pop('GITOSIS_UNITTEST_COOKIE', None)
96 eq(
97 sorted(os.listdir(tmp)),
98 sorted([
99 'mockbin',
100 'cookie',
101 'repo.git',
104 got = readFile(os.path.join(tmp, 'cookie'))
105 eq(got, magic_cookie)
107 def test_fast_import_environment():
108 tmp = maketemp()
109 path = os.path.join(tmp, 'repo.git')
110 repository.init(path=path)
111 mockbindir = os.path.join(tmp, 'mockbin')
112 os.mkdir(mockbindir)
113 mockgit = os.path.join(mockbindir, 'git')
114 writeFile(mockgit, '''\
115 #!/bin/sh
116 set -e
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
121 PATH="${PATH#*:}"
123 exec git "$@"
124 ''')
125 os.chmod(mockgit, 0755)
126 magic_cookie = '%d' % random.randint(1, 100000)
127 good_path = os.environ['PATH']
128 try:
129 os.environ['PATH'] = '%s:%s' % (mockbindir, good_path)
130 os.environ['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
131 repository.fast_import(
132 git_dir=path,
133 commit_msg='foo initial bar',
134 committer='Mr. Unit Test <unit.test@example.com>',
135 files=[
136 ('foo', 'bar\n'),
139 finally:
140 os.environ['PATH'] = good_path
141 os.environ.pop('GITOSIS_UNITTEST_COOKIE', None)
143 sorted(os.listdir(tmp)),
144 sorted([
145 'mockbin',
146 'cookie',
147 'repo.git',
150 got = readFile(os.path.join(tmp, 'cookie'))
151 eq(got, magic_cookie)
153 def test_export_simple():
154 tmp = maketemp()
155 git_dir = os.path.join(tmp, 'repo.git')
156 repository.init(path=git_dir)
157 repository.fast_import(
158 git_dir=git_dir,
159 committer='John Doe <jdoe@example.com>',
160 commit_msg="""\
161 Reverse the polarity of the neutron flow.
163 Frobitz the quux and eschew obfuscation.
164 """,
165 files=[
166 ('foo', 'content'),
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(
178 args=[
179 'git',
180 '--git-dir=%s' % git_dir,
181 'cat-file',
182 'commit',
183 'HEAD',
185 cwd=git_dir,
186 stdout=subprocess.PIPE,
187 close_fds=True,
189 got = child.stdout.read().splitlines()
190 returncode = child.wait()
191 if returncode != 0:
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>')
198 eq(got[3], '')
199 eq(got[4], 'Reverse the polarity of the neutron flow.')
200 eq(got[5], '')
201 eq(got[6], 'Frobitz the quux and eschew obfuscation.')
202 eq(got[7:], [])
204 def test_export_environment():
205 tmp = maketemp()
206 git_dir = os.path.join(tmp, 'repo.git')
207 mockbindir = os.path.join(tmp, 'mockbin')
208 os.mkdir(mockbindir)
209 mockgit = os.path.join(mockbindir, 'git')
210 writeFile(mockgit, '''\
211 #!/bin/sh
212 set -e
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
217 PATH="${PATH#*:}"
219 exec git "$@"
220 ''')
221 os.chmod(mockgit, 0755)
222 repository.init(path=git_dir)
223 repository.fast_import(
224 git_dir=git_dir,
225 committer='John Doe <jdoe@example.com>',
226 commit_msg="""\
227 Reverse the polarity of the neutron flow.
229 Frobitz the quux and eschew obfuscation.
230 """,
231 files=[
232 ('foo', 'content'),
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']
239 try:
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)
243 finally:
244 os.environ['PATH'] = good_path
245 os.environ.pop('GITOSIS_UNITTEST_COOKIE', None)
246 got = readFile(os.path.join(tmp, 'cookie'))
248 got,
249 # export runs git twice
250 '%s\n%s\n' % (magic_cookie, magic_cookie),
253 def test_has_initial_commit_fail_notAGitDir():
254 tmp = maketemp()
255 e = assert_raises(
256 repository.GitRevParseError,
257 repository.has_initial_commit,
258 git_dir=tmp)
259 eq(str(e), 'rev-parse failed: exit status 128')
261 def test_has_initial_commit_no():
262 tmp = maketemp()
263 repository.init(path=tmp)
264 got = repository.has_initial_commit(git_dir=tmp)
265 eq(got, False)
267 def test_has_initial_commit_yes():
268 tmp = maketemp()
269 repository.init(path=tmp)
270 repository.fast_import(
271 git_dir=tmp,
272 commit_msg='fakecommit',
273 committer='John Doe <jdoe@example.com>',
274 files=[],
276 got = repository.has_initial_commit(git_dir=tmp)
277 eq(got, True)
279 def test_has_initial_commit_environment():
280 tmp = maketemp()
281 git_dir = os.path.join(tmp, 'repo.git')
282 mockbindir = os.path.join(tmp, 'mockbin')
283 os.mkdir(mockbindir)
284 mockgit = os.path.join(mockbindir, 'git')
285 writeFile(mockgit, '''\
286 #!/bin/sh
287 set -e
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
292 PATH="${PATH#*:}"
294 exec git "$@"
295 ''')
296 os.chmod(mockgit, 0755)
297 repository.init(path=tmp)
298 repository.fast_import(
299 git_dir=tmp,
300 commit_msg='fakecommit',
301 committer='John Doe <jdoe@example.com>',
302 files=[],
304 magic_cookie = '%d' % random.randint(1, 100000)
305 good_path = os.environ['PATH']
306 try:
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)
310 finally:
311 os.environ['PATH'] = good_path
312 os.environ.pop('GITOSIS_UNITTEST_COOKIE', None)
313 eq(got, True)
314 got = readFile(os.path.join(tmp, 'cookie'))
315 eq(got, magic_cookie)
317 def test_fast_import_parent():
318 tmp = maketemp()
319 path = os.path.join(tmp, 'repo.git')
320 repository.init(path=path)
321 repository.fast_import(
322 git_dir=path,
323 commit_msg='foo initial bar',
324 committer='Mr. Unit Test <unit.test@example.com>',
325 files=[
326 ('foo', 'bar\n'),
329 repository.fast_import(
330 git_dir=path,
331 commit_msg='another',
332 committer='Sam One Else <sam@example.com>',
333 parent='refs/heads/master^0',
334 files=[
335 ('quux', 'thud\n'),
338 export = os.path.join(tmp, 'export')
339 repository.export(
340 git_dir=path,
341 path=export,
343 eq(sorted(os.listdir(export)),
344 sorted(['foo', 'quux']))