/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

update copyright years

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
172
213
 
173
214
    def finish(self):
174
215
        """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)
 
216
        self.stream.close()
 
217
        if self._process is None:
 
218
            return {}
 
219
        if self._process.wait() != 0:
 
220
            raise errors.GitCommandError(self._process.returncode,
 
221
                                         'git fast-import',
 
222
                                         self._process.stderr.read())
 
223
        marks_file = open(self._marks_file_name)
 
224
        mapping = {}
 
225
        for line in marks_file:
 
226
            mark, shasum = line.split()
 
227
            assert mark.startswith(':')
 
228
            mapping[int(mark[1:])] = shasum
 
229
        marks_file.close()
 
230
        return mapping
182
231
 
183
232
 
184
233
def test_suite():
185
 
    loader = tests.TestUtil.TestLoader()
 
234
    loader = tests.TestLoader()
186
235
 
187
 
    suite = tests.TestUtil.TestSuite()
 
236
    suite = tests.TestSuite()
188
237
 
189
238
    testmod_names = [
190
239
        'test_blackbox',
191
240
        'test_builder',
192
241
        'test_branch',
193
 
        'test_cache',
194
242
        'test_dir',
195
243
        'test_fetch',
196
244
        'test_mapping',
197
245
        'test_object_store',
198
 
        'test_push',
199
246
        'test_remote',
200
247
        'test_repository',
 
248
        'test_shamap',
201
249
        'test_refs',
202
250
        'test_revspec',
203
 
        'test_roundtrip',
204
 
        'test_transportgit',
205
251
        ]
206
252
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
207
253
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))