aboutsummaryrefslogtreecommitdiff
path: root/gitosis/test/test_repository.py
blob: 0920c4a5417257ef21a3babd0b2dc6c6c30dc5a5 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from nose.tools import eq_ as eq
 
import os
import subprocess
import random
 
from gitosis import repository
 
from gitosis.test.util import (
    mkdir,
    maketemp,
    readFile,
    writeFile,
    check_mode,
    assert_raises,
    )
 
def check_bare(path):
    # we want it to be a bare repository 
    assert not os.path.exists(os.path.join(path'.git'))
 
def test_init_simple():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    repository.init(path)
    check_mode(path0750is_dir=True)
    check_bare(path)
 
def test_init_exist_dir():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    mkdir(path0710)
    check_mode(path0710is_dir=True)
    repository.init(path)
    # my weird access mode is preserved 
    check_mode(path0710is_dir=True)
    check_bare(path)
 
def test_init_custom_perm():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    repository.init(pathmode=0711)
    check_mode(path0711is_dir=True)
    check_bare(path)
 
def test_init_exist_git():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    repository.init(path)
    repository.init(path)
    check_mode(path0750is_dir=True)
    check_bare(path)
 
def test_init_templates():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    templatedir = os.path.join(
        os.path.dirname(__file__),
        'mocktemplates',
        )
    repository.init(pathtemplate=templatedir)
    repository.init(path)
    got = readFile(os.path.join(path'no-confusion'))
    eq(got'i should show up\n')
    check_mode(
        os.path.join(path'hooks''post-update'),
        0755,
        is_file=True,
        )
    got = readFile(os.path.join(path'hooks''post-update'))
    eq(got'#!/bin/sh\n# i can override standard templates\n')
    # standard templates are there, too 
    assert (os.path.isfile(os.path.join(path'hooks''pre-rebase.sample')) or os.path.isfile(os.path.join(path'hooks''pre-rebase')))
 
def test_init_environment():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    mockbindir = os.path.join(tmp'mockbin')
    os.mkdir(mockbindir)
    mockgit = os.path.join(mockbindir'git')
    writeFile(mockgit'''\
#!/bin/sh
set -e
# git wrapper for gitosis unit tests
printf '%s' "$GITOSIS_UNITTEST_COOKIE" >"$(dirname "$0")/../cookie"
 
# strip away my special PATH insert so system git will be found
PATH="${PATH#*:}"
 
exec git "$@"
''')
    os.chmod(mockgit0755)
    magic_cookie = '%d' % random.randint(1100000)
    good_path = os.environ['PATH']
    try:
        os.environ['PATH'] = '%s:%s' % (mockbindirgood_path)
        os.environ['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
        repository.init(path)
    finally:
        os.environ['PATH'] = good_path
        os.environ.pop('GITOSIS_UNITTEST_COOKIE'None)
    eq(
        sorted(os.listdir(tmp)),
        sorted([ 
                'mockbin', 
                'cookie', 
                'repo.git', 
                ]),
        )
    got = readFile(os.path.join(tmp'cookie'))
    eq(gotmagic_cookie)
 
def test_fast_import_environment():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    repository.init(path=path)
    mockbindir = os.path.join(tmp'mockbin')
    os.mkdir(mockbindir)
    mockgit = os.path.join(mockbindir'git')
    writeFile(mockgit'''\
#!/bin/sh
set -e
# git wrapper for gitosis unit tests
printf '%s' "$GITOSIS_UNITTEST_COOKIE" >"$(dirname "$0")/../cookie"
 
# strip away my special PATH insert so system git will be found
PATH="${PATH#*:}"
 
exec git "$@"
''')
    os.chmod(mockgit0755)
    magic_cookie = '%d' % random.randint(1100000)
    good_path = os.environ['PATH']
    try:
        os.environ['PATH'] = '%s:%s' % (mockbindirgood_path)
        os.environ['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
        repository.fast_import(
            git_dir=path,
            commit_msg='foo initial bar',
            committer='Mr. Unit Test <unit.test@example.com>',
            files=[ 
                ('foo''bar\n'), 
                ],
            )
    finally:
        os.environ['PATH'] = good_path
        os.environ.pop('GITOSIS_UNITTEST_COOKIE'None)
    eq(
        sorted(os.listdir(tmp)),
        sorted([ 
                'mockbin', 
                'cookie', 
                'repo.git', 
                ]),
        )
    got = readFile(os.path.join(tmp'cookie'))
    eq(gotmagic_cookie)
 
def test_export_simple():
    tmp = maketemp()
    git_dir = os.path.join(tmp'repo.git')
    repository.init(path=git_dir)
    repository.fast_import(
        git_dir=git_dir,
        committer='John Doe <jdoe@example.com>',
        commit_msg="""\
Reverse the polarity of the neutron flow.
 
Frobitz the quux and eschew obfuscation.
""",
        files=[ 
            ('foo''content'), 
            ('bar/quux''another'), 
            ],
        )
    export = os.path.join(tmp'export')
    repository.export(git_dir=git_dir, path=export)
    eq(sorted(os.listdir(export)),
       sorted(['foo', 'bar']))
    eq(readFile(os.path.join(export'foo'))'content')
    eq(os.listdir(os.path.join(export'bar'))['quux'])
    eq(readFile(os.path.join(export'bar''quux'))'another')
    child = subprocess.Popen(
        args=[ 
            'git', 
            '--git-dir=%s' % git_dir, 
            'cat-file', 
            'commit', 
            'HEAD', 
            ],
        cwd=git_dir,
        stdout=subprocess.PIPE,
        close_fds=True,
        )
    got = child.stdout.read().splitlines()
    returncode = child.wait()
    if returncode != 0:
        raise RuntimeError('git exit status %d' % returncode)
    eq(got[0].split(None1)[0]'tree')
    eq(got[1].rsplit(None2)[0],
       'author John Doe <jdoe@example.com>')
    eq(got[2].rsplit(None2)[0],
       'committer John Doe <jdoe@example.com>')
    eq(got[3]'')
    eq(got[4]'Reverse the polarity of the neutron flow.')
    eq(got[5]'')
    eq(got[6]'Frobitz the quux and eschew obfuscation.')
    eq(got[7:][])
 
def test_export_environment():
    tmp = maketemp()
    git_dir = os.path.join(tmp'repo.git')
    mockbindir = os.path.join(tmp'mockbin')
    os.mkdir(mockbindir)
    mockgit = os.path.join(mockbindir'git')
    writeFile(mockgit'''\
#!/bin/sh
set -e
# git wrapper for gitosis unit tests
printf '%s\n' "$GITOSIS_UNITTEST_COOKIE" >>"$(dirname "$0")/../cookie"
 
# strip away my special PATH insert so system git will be found
PATH="${PATH#*:}"
 
exec git "$@"
''')
    os.chmod(mockgit0755)
    repository.init(path=git_dir)
    repository.fast_import(
        git_dir=git_dir,
        committer='John Doe <jdoe@example.com>',
        commit_msg="""\
Reverse the polarity of the neutron flow.
 
Frobitz the quux and eschew obfuscation.
""",
        files=[ 
            ('foo''content'), 
            ('bar/quux''another'), 
            ],
        )
    export = os.path.join(tmp'export')
    magic_cookie = '%d' % random.randint(1100000)
    good_path = os.environ['PATH']
    try:
        os.environ['PATH'] = '%s:%s' % (mockbindirgood_path)
        os.environ['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
        repository.export(git_dir=git_dir, path=export)
    finally:
        os.environ['PATH'] = good_path
        os.environ.pop('GITOSIS_UNITTEST_COOKIE'None)
    got = readFile(os.path.join(tmp'cookie'))
    eq(
        got,
        # export runs git twice 
        '%s\n%s\n' % (magic_cookiemagic_cookie),
        )
 
def test_has_initial_commit_fail_notAGitDir():
    tmp = maketemp()
    e = assert_raises(
        repository.GitRevParseError,
        repository.has_initial_commit,
        git_dir=tmp)
    eq(str(e)'rev-parse failed: exit status 128')
 
def test_has_initial_commit_no():
    tmp = maketemp()
    repository.init(path=tmp)
    got = repository.has_initial_commit(git_dir=tmp)
    eq(gotFalse)
 
def test_has_initial_commit_yes():
    tmp = maketemp()
    repository.init(path=tmp)
    repository.fast_import(
        git_dir=tmp,
        commit_msg='fakecommit',
        committer='John Doe <jdoe@example.com>',
        files=[],
        )
    got = repository.has_initial_commit(git_dir=tmp)
    eq(gotTrue)
 
def test_has_initial_commit_environment():
    tmp = maketemp()
    git_dir = os.path.join(tmp'repo.git')
    mockbindir = os.path.join(tmp'mockbin')
    os.mkdir(mockbindir)
    mockgit = os.path.join(mockbindir'git')
    writeFile(mockgit'''\
#!/bin/sh
set -e
# git wrapper for gitosis unit tests
printf '%s' "$GITOSIS_UNITTEST_COOKIE" >"$(dirname "$0")/../cookie"
 
# strip away my special PATH insert so system git will be found
PATH="${PATH#*:}"
 
exec git "$@"
''')
    os.chmod(mockgit0755)
    repository.init(path=tmp)
    repository.fast_import(
        git_dir=tmp,
        commit_msg='fakecommit',
        committer='John Doe <jdoe@example.com>',
        files=[],
        )
    magic_cookie = '%d' % random.randint(1100000)
    good_path = os.environ['PATH']
    try:
        os.environ['PATH'] = '%s:%s' % (mockbindirgood_path)
        os.environ['GITOSIS_UNITTEST_COOKIE'] = magic_cookie
        got = repository.has_initial_commit(git_dir=tmp)
    finally:
        os.environ['PATH'] = good_path
        os.environ.pop('GITOSIS_UNITTEST_COOKIE'None)
    eq(gotTrue)
    got = readFile(os.path.join(tmp'cookie'))
    eq(gotmagic_cookie)
 
def test_fast_import_parent():
    tmp = maketemp()
    path = os.path.join(tmp'repo.git')
    repository.init(path=path)
    repository.fast_import(
        git_dir=path,
        commit_msg='foo initial bar',
        committer='Mr. Unit Test <unit.test@example.com>',
        files=[ 
            ('foo''bar\n'), 
            ],
        )
    repository.fast_import(
        git_dir=path,
        commit_msg='another',
        committer='Sam One Else <sam@example.com>',
        parent='refs/heads/master^0',
        files=[ 
            ('quux''thud\n'), 
            ],
        )
    export = os.path.join(tmp'export')
    repository.export(
        git_dir=path,
        path=export,
        )
    eq(sorted(os.listdir(export)),
       sorted(['foo', 'quux']))