17
17
"""The basic test suite for bzr-git."""
19
from cStringIO import StringIO
23
22
from bzrlib import (
27
from bzrlib.plugins.git import (
27
from bzrlib.plugins.git import errors
31
29
TestCase = tests.TestCase
32
30
TestCaseInTempDir = tests.TestCaseInTempDir
33
31
TestCaseWithTransport = tests.TestCaseWithTransport
34
32
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
36
class _DulwichFeature(tests.Feature):
34
class _GitCommandFeature(tests.Feature):
41
except bzr_errors.DependencyNotPresent:
38
p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
41
out, err = p.communicate()
42
trace.mutter('Using: %s', out.rstrip('\n'))
45
45
def feature_name(self):
49
DulwichFeature = _DulwichFeature()
48
GitCommandFeature = _GitCommandFeature()
52
cmd = ['git'] + list(args)
53
p = subprocess.Popen(cmd,
54
stdout=subprocess.PIPE,
55
stderr=subprocess.PIPE)
56
out, err = p.communicate()
58
raise AssertionError('Bad return code: %d for %s:\n%s'
59
% (p.returncode, ' '.join(cmd), err))
52
63
class GitBranchBuilder(object):
54
65
def __init__(self, stream=None):
55
66
self.commit_info = []
56
self.orig_stream = stream
58
self.stream = StringIO()
62
70
self._branch = 'refs/heads/master'
72
# Write the marks file into the git sandbox.
73
self._marks_file_name = osutils.abspath('marks')
74
self._process = subprocess.Popen(
75
['git', 'fast-import', '--quiet',
76
# GIT doesn't support '--export-marks foo'
77
# it only supports '--export-marks=foo'
78
# And gives a 'unknown option' otherwise.
79
'--export-marks=' + self._marks_file_name,
81
stdout=subprocess.PIPE,
82
stderr=subprocess.PIPE,
83
stdin=subprocess.PIPE,
85
self.stream = self._process.stdin
64
89
def set_branch(self, branch):
65
90
"""Set the branch we are committing."""
66
91
self._branch = branch
68
93
def _write(self, text):
69
self.stream.write(text)
95
self.stream.write(text)
97
if self._process is None:
99
raise errors.GitCommandError(self._process.returncode,
101
self._process.stderr.read())
71
103
def _writelines(self, lines):
72
self.stream.writelines(lines)
105
self.stream.writelines(lines)
107
if self._process is None:
109
raise errors.GitCommandError(self._process.returncode,
111
self._process.stderr.read())
74
113
def _create_blob(self, content):
75
114
self._counter += 1
81
120
return self._counter
83
def set_symlink(self, path, content):
84
"""Create or update symlink at a given path."""
85
mark = self._create_blob(content)
87
self.commit_info.append('M %s :%d %s\n'
88
% (mode, mark, self._encode_path(path)))
90
122
def set_file(self, path, content, executable):
91
123
"""Create or update content at a given path."""
92
124
mark = self._create_blob(content)
173
205
def finish(self):
174
206
"""We are finished building, close the stream, get the id mapping"""
176
if self.orig_stream is None:
177
from dulwich.repo import Repo
179
from dulwich.fastexport import FastImporter
180
importer = FastImporter(r)
181
return importer.import_stream(self.stream)
208
if self._process is None:
210
if self._process.wait() != 0:
211
raise errors.GitCommandError(self._process.returncode,
213
self._process.stderr.read())
214
marks_file = open(self._marks_file_name)
216
for line in marks_file:
217
mark, shasum = line.split()
218
assert mark.startswith(':')
219
mapping[int(mark[1:])] = shasum
184
224
def test_suite():
185
loader = tests.TestUtil.TestLoader()
225
loader = tests.TestLoader()
187
suite = tests.TestUtil.TestSuite()
227
suite = tests.TestSuite()
189
229
testmod_names = [
233
'test_git_repository',
206
238
testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
207
239
suite.addTests(loader.loadTestsFromModuleNames(testmod_names))