/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/__init__.py

Cope with API changes in Dulwich.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""The basic test suite for bzr-git."""
18
18
 
19
 
from cStringIO import StringIO
20
 
 
 
19
import subprocess
21
20
import time
22
21
 
23
22
from bzrlib import (
24
 
    errors as bzr_errors,
 
23
    osutils,
25
24
    tests,
 
25
    trace,
26
26
    )
27
27
from bzrlib.plugins.git import (
28
 
    import_dulwich,
 
28
    errors,
29
29
    )
30
30
 
31
31
TestCase = tests.TestCase
33
33
TestCaseWithTransport = tests.TestCaseWithTransport
34
34
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
35
35
 
36
 
class _DulwichFeature(tests.Feature):
 
36
class _GitCommandFeature(tests.Feature):
37
37
 
38
38
    def _probe(self):
39
39
        try:
40
 
            import_dulwich()
41
 
        except bzr_errors.DependencyNotPresent:
 
40
            p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
 
41
        except IOError:
42
42
            return False
 
43
        out, err = p.communicate()
 
44
        trace.mutter('Using: %s', out.rstrip('\n'))
43
45
        return True
44
46
 
45
47
    def feature_name(self):
46
 
        return 'dulwich'
47
 
 
48
 
 
49
 
DulwichFeature = _DulwichFeature()
 
48
        return 'git'
 
49
 
 
50
GitCommandFeature = _GitCommandFeature()
 
51
 
 
52
 
 
53
def run_git(*args):
 
54
    cmd = ['git'] + list(args)
 
55
    p = subprocess.Popen(cmd,
 
56
                         stdout=subprocess.PIPE,
 
57
                         stderr=subprocess.PIPE)
 
58
    out, err = p.communicate()
 
59
    if p.returncode != 0:
 
60
        raise AssertionError('Bad return code: %d for %s:\n%s'
 
61
                             % (p.returncode, ' '.join(cmd), err))
 
62
    return out
50
63
 
51
64
 
52
65
class GitBranchBuilder(object):
53
66
 
54
67
    def __init__(self, stream=None):
55
68
        self.commit_info = []
56
 
        self.orig_stream = stream
57
 
        if stream is None:
58
 
            self.stream = StringIO()
59
 
        else:
60
 
            self.stream = stream
 
69
        self.stream = stream
 
70
        self._process = None
61
71
        self._counter = 0
62
72
        self._branch = 'refs/heads/master'
 
73
        if stream is None:
 
74
            # Write the marks file into the git sandbox.
 
75
            self._marks_file_name = osutils.abspath('marks')
 
76
            self._process = subprocess.Popen(
 
77
                ['git', 'fast-import', '--quiet',
 
78
                 # GIT doesn't support '--export-marks foo'
 
79
                 # it only supports '--export-marks=foo'
 
80
                 # And gives a 'unknown option' otherwise.
 
81
                 '--export-marks=' + self._marks_file_name,
 
82
                ],
 
83
                stdout=subprocess.PIPE,
 
84
                stderr=subprocess.PIPE,
 
85
                stdin=subprocess.PIPE,
 
86
                )
 
87
            self.stream = self._process.stdin
 
88
        else:
 
89
            self._process = None
63
90
 
64
91
    def set_branch(self, branch):
65
92
        """Set the branch we are committing."""
66
93
        self._branch = branch
67
94
 
68
95
    def _write(self, text):
69
 
        self.stream.write(text)
 
96
        try:
 
97
            self.stream.write(text)
 
98
        except IOError, e:
 
99
            if self._process is None:
 
100
                raise
 
101
            raise errors.GitCommandError(self._process.returncode,
 
102
                                         'git fast-import',
 
103
                                         self._process.stderr.read())
70
104
 
71
105
    def _writelines(self, lines):
72
 
        self.stream.writelines(lines)
 
106
        try:
 
107
            self.stream.writelines(lines)
 
108
        except IOError, e:
 
109
            if self._process is None:
 
110
                raise
 
111
            raise errors.GitCommandError(self._process.returncode,
 
112
                                         'git fast-import',
 
113
                                         self._process.stderr.read())
73
114
 
74
115
    def _create_blob(self, content):
75
116
        self._counter += 1
80
121
        self._write('\n')
81
122
        return self._counter
82
123
 
83
 
    def set_symlink(self, path, content):
84
 
        """Create or update symlink at a given path."""
85
 
        mark = self._create_blob(content)
86
 
        mode = '120000'
87
 
        self.commit_info.append('M %s :%d %s\n'
88
 
                % (mode, mark, self._encode_path(path)))
89
 
 
90
124
    def set_file(self, path, content, executable):
91
125
        """Create or update content at a given path."""
92
126
        mark = self._create_blob(content)
172
206
 
173
207
    def finish(self):
174
208
        """We are finished building, close the stream, get the id mapping"""
175
 
        self.stream.seek(0)
176
 
        if self.orig_stream is None:
177
 
            from dulwich.repo import Repo
178
 
            r = Repo(".")
179
 
            from dulwich.fastexport import FastImporter
180
 
            importer = FastImporter(r)
181
 
            return importer.import_stream(self.stream)
 
209
        self.stream.close()
 
210
        if self._process is None:
 
211
            return {}
 
212
        if self._process.wait() != 0:
 
213
            raise errors.GitCommandError(self._process.returncode,
 
214
                                         'git fast-import',
 
215
                                         self._process.stderr.read())
 
216
        marks_file = open(self._marks_file_name)
 
217
        mapping = {}
 
218
        for line in marks_file:
 
219
            mark, shasum = line.split()
 
220
            assert mark.startswith(':')
 
221
            mapping[int(mark[1:])] = shasum
 
222
        marks_file.close()
 
223
        return mapping
182
224
 
183
225
 
184
226
def test_suite():
185
 
    loader = tests.TestUtil.TestLoader()
 
227
    loader = tests.TestLoader()
186
228
 
187
 
    suite = tests.TestUtil.TestSuite()
 
229
    suite = tests.TestSuite()
188
230
 
189
231
    testmod_names = [
190
 
        'test_blackbox',
191
232
        'test_builder',
192
233
        'test_branch',
193
 
        'test_cache',
194
234
        'test_dir',
195
235
        'test_fetch',
196
236
        'test_mapping',
197
 
        'test_object_store',
198
 
        'test_push',
199
 
        'test_remote',
200
237
        'test_repository',
201
 
        'test_refs',
202
 
        'test_revspec',
203
 
        'test_roundtrip',
204
 
        'test_transportgit',
 
238
        'test_shamap',
 
239
        'test_blackbox',
205
240
        ]
206
241
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
207
242
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))