/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

More work on roundtrip push support.

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
 
import subprocess
 
19
from cStringIO import StringIO
 
20
 
20
21
import time
21
22
 
22
23
from bzrlib import (
23
 
    osutils,
 
24
    errors as bzr_errors,
24
25
    tests,
25
 
    trace,
26
 
    )
27
 
from  bzrlib.plugins.git import errors
 
26
    )
 
27
from bzrlib.plugins.git import (
 
28
    import_dulwich,
 
29
    )
28
30
 
29
31
TestCase = tests.TestCase
30
32
TestCaseInTempDir = tests.TestCaseInTempDir
31
33
TestCaseWithTransport = tests.TestCaseWithTransport
32
34
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
33
35
 
34
 
class _GitCommandFeature(tests.Feature):
 
36
class _DulwichFeature(tests.Feature):
35
37
 
36
38
    def _probe(self):
37
39
        try:
38
 
            p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
39
 
        except IOError:
 
40
            import_dulwich()
 
41
        except bzr_errors.DependencyNotPresent:
40
42
            return False
41
 
        out, err = p.communicate()
42
 
        trace.mutter('Using: %s', out.rstrip('\n'))
43
43
        return True
44
44
 
45
45
    def feature_name(self):
46
 
        return 'git'
47
 
 
48
 
GitCommandFeature = _GitCommandFeature()
49
 
 
50
 
 
51
 
def run_git(*args):
52
 
    cmd = ['git'] + list(args)
53
 
    p = subprocess.Popen(cmd,
54
 
                         stdout=subprocess.PIPE,
55
 
                         stderr=subprocess.PIPE)
56
 
    out, err = p.communicate()
57
 
    if p.returncode != 0:
58
 
        raise AssertionError('Bad return code: %d for %s:\n%s'
59
 
                             % (p.returncode, ' '.join(cmd), err))
60
 
    return out
 
46
        return 'dulwich'
 
47
 
 
48
 
 
49
DulwichFeature = _DulwichFeature()
61
50
 
62
51
 
63
52
class GitBranchBuilder(object):
64
53
 
65
54
    def __init__(self, stream=None):
66
55
        self.commit_info = []
67
 
        self.stream = stream
68
 
        self._process = None
 
56
        self.orig_stream = stream
 
57
        if stream is None:
 
58
            self.stream = StringIO()
 
59
        else:
 
60
            self.stream = stream
69
61
        self._counter = 0
70
62
        self._branch = 'refs/heads/master'
71
 
        if stream is None:
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,
80
 
                ],
81
 
                stdout=subprocess.PIPE,
82
 
                stderr=subprocess.PIPE,
83
 
                stdin=subprocess.PIPE,
84
 
                )
85
 
            self.stream = self._process.stdin
86
 
        else:
87
 
            self._process = None
88
63
 
89
64
    def set_branch(self, branch):
90
65
        """Set the branch we are committing."""
91
66
        self._branch = branch
92
67
 
93
68
    def _write(self, text):
94
 
        try:
95
 
            self.stream.write(text)
96
 
        except IOError, e:
97
 
            if self._process is None:
98
 
                raise
99
 
            raise errors.GitCommandError(self._process.returncode,
100
 
                                         'git fast-import',
101
 
                                         self._process.stderr.read())
 
69
        self.stream.write(text)
102
70
 
103
71
    def _writelines(self, lines):
104
 
        try:
105
 
            self.stream.writelines(lines)
106
 
        except IOError, e:
107
 
            if self._process is None:
108
 
                raise
109
 
            raise errors.GitCommandError(self._process.returncode,
110
 
                                         'git fast-import',
111
 
                                         self._process.stderr.read())
 
72
        self.stream.writelines(lines)
112
73
 
113
74
    def _create_blob(self, content):
114
75
        self._counter += 1
119
80
        self._write('\n')
120
81
        return self._counter
121
82
 
 
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
 
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)
204
172
 
205
173
    def finish(self):
206
174
        """We are finished building, close the stream, get the id mapping"""
207
 
        self.stream.close()
208
 
        if self._process is None:
209
 
            return {}
210
 
        if self._process.wait() != 0:
211
 
            raise errors.GitCommandError(self._process.returncode,
212
 
                                         'git fast-import',
213
 
                                         self._process.stderr.read())
214
 
        marks_file = open(self._marks_file_name)
215
 
        mapping = {}
216
 
        for line in marks_file:
217
 
            mark, shasum = line.split()
218
 
            assert mark.startswith(':')
219
 
            mapping[int(mark[1:])] = shasum
220
 
        marks_file.close()
221
 
        return 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)
222
182
 
223
183
 
224
184
def test_suite():
225
 
    loader = tests.TestLoader()
 
185
    loader = tests.TestUtil.TestLoader()
226
186
 
227
 
    suite = tests.TestSuite()
 
187
    suite = tests.TestUtil.TestSuite()
228
188
 
229
189
    testmod_names = [
 
190
        'test_blackbox',
230
191
        'test_builder',
231
192
        'test_branch',
 
193
        'test_cache',
232
194
        'test_dir',
 
195
        'test_fetch',
 
196
        'test_mapping',
 
197
        'test_object_store',
 
198
        'test_push',
 
199
        'test_remote',
233
200
        'test_repository',
234
 
        'test_ids',
235
 
        'test_blackbox',
 
201
        'test_refs',
 
202
        'test_revspec',
 
203
        'test_roundtrip',
 
204
        'test_transportgit',
236
205
        ]
237
206
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
238
207
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))