17
17
"""The basic test suite for bzr-git."""
19
from cStringIO import StringIO
22
23
from bzrlib import (
27
from bzrlib.plugins.git import errors
27
from bzrlib.plugins.git import (
29
31
TestCase = tests.TestCase
30
32
TestCaseInTempDir = tests.TestCaseInTempDir
31
33
TestCaseWithTransport = tests.TestCaseWithTransport
32
34
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
34
class _GitCommandFeature(tests.Feature):
36
class _DulwichFeature(tests.Feature):
38
p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
41
except bzr_errors.DependencyNotPresent:
41
out, err = p.communicate()
42
trace.mutter('Using: %s', out.rstrip('\n'))
45
45
def feature_name(self):
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))
49
DulwichFeature = _DulwichFeature()
63
52
class GitBranchBuilder(object):
65
54
def __init__(self, stream=None):
66
55
self.commit_info = []
56
self.orig_stream = stream
58
self.stream = StringIO()
70
62
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
89
64
def set_branch(self, branch):
90
65
"""Set the branch we are committing."""
91
66
self._branch = branch
93
68
def _write(self, text):
95
self.stream.write(text)
97
if self._process is None:
99
raise errors.GitCommandError(self._process.returncode,
101
self._process.stderr.read())
69
self.stream.write(text)
103
71
def _writelines(self, lines):
105
self.stream.writelines(lines)
107
if self._process is None:
109
raise errors.GitCommandError(self._process.returncode,
111
self._process.stderr.read())
72
self.stream.writelines(lines)
113
74
def _create_blob(self, content):
114
75
self._counter += 1
120
81
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)))
122
90
def set_file(self, path, content, executable):
123
91
"""Create or update content at a given path."""
124
92
mark = self._create_blob(content)
205
173
def finish(self):
206
174
"""We are finished building, close the stream, get the id mapping"""
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
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)
224
184
def test_suite():
225
loader = tests.TestLoader()
185
loader = tests.TestUtil.TestLoader()
227
suite = tests.TestSuite()
187
suite = tests.TestUtil.TestSuite()
229
189
testmod_names = [
233
200
'test_repository',
237
206
testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
238
207
suite.addTests(loader.loadTestsFromModuleNames(testmod_names))