/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

add setup.py

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,
26
 
    )
27
 
from bzrlib.plugins.git import (
28
 
    import_dulwich,
29
 
    )
 
25
    trace,
 
26
    )
 
27
from  bzrlib.plugins.git import errors
30
28
 
31
29
TestCase = tests.TestCase
32
30
TestCaseInTempDir = tests.TestCaseInTempDir
33
31
TestCaseWithTransport = tests.TestCaseWithTransport
34
32
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
35
33
 
36
 
class _DulwichFeature(tests.Feature):
 
34
class _GitCommandFeature(tests.Feature):
37
35
 
38
36
    def _probe(self):
39
37
        try:
40
 
            import_dulwich()
41
 
        except bzr_errors.DependencyNotPresent:
 
38
            p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
 
39
        except IOError:
42
40
            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 'dulwich'
47
 
 
48
 
 
49
 
DulwichFeature = _DulwichFeature()
 
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
50
61
 
51
62
 
52
63
class GitBranchBuilder(object):
53
64
 
54
65
    def __init__(self, stream=None):
55
66
        self.commit_info = []
56
 
        self.orig_stream = stream
57
 
        if stream is None:
58
 
            self.stream = StringIO()
59
 
        else:
60
 
            self.stream = stream
 
67
        self.stream = stream
 
68
        self._process = None
61
69
        self._counter = 0
62
70
        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
63
88
 
64
89
    def set_branch(self, branch):
65
90
        """Set the branch we are committing."""
66
91
        self._branch = branch
67
92
 
68
93
    def _write(self, text):
69
 
        self.stream.write(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())
70
102
 
71
103
    def _writelines(self, lines):
72
 
        self.stream.writelines(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())
73
112
 
74
113
    def _create_blob(self, content):
75
114
        self._counter += 1
80
119
        self._write('\n')
81
120
        return self._counter
82
121
 
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
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)
172
204
 
173
205
    def finish(self):
174
206
        """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)
 
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
182
222
 
183
223
 
184
224
def test_suite():
185
 
    loader = tests.TestUtil.TestLoader()
 
225
    loader = tests.TestLoader()
186
226
 
187
 
    suite = tests.TestUtil.TestSuite()
 
227
    suite = tests.TestSuite()
188
228
 
189
229
    testmod_names = [
 
230
        'test_builder',
 
231
        'test_git_branch',
 
232
        'test_git_dir',
 
233
        'test_git_repository',
 
234
        'test_model',
 
235
        'test_ids',
190
236
        'test_blackbox',
191
 
        'test_builder',
192
 
        'test_branch',
193
 
        'test_cache',
194
 
        'test_dir',
195
 
        'test_fetch',
196
 
        'test_mapping',
197
 
        'test_object_store',
198
 
        'test_push',
199
 
        'test_remote',
200
 
        'test_repository',
201
 
        'test_refs',
202
 
        'test_revspec',
203
 
        'test_roundtrip',
204
 
        'test_transportgit',
205
237
        ]
206
238
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
207
239
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))