/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

Avoid invoking git directly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""The basic test suite for bzr-git."""
 
18
 
 
19
import subprocess
 
20
import time
 
21
 
 
22
from bzrlib import (
 
23
    errors as bzr_errors,
 
24
    osutils,
 
25
    tests,
 
26
    trace,
 
27
    )
 
28
from bzrlib.plugins.git import (
 
29
    errors,
 
30
    import_dulwich,
 
31
    )
 
32
 
 
33
TestCase = tests.TestCase
 
34
TestCaseInTempDir = tests.TestCaseInTempDir
 
35
TestCaseWithTransport = tests.TestCaseWithTransport
 
36
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
 
37
 
 
38
class _DulwichFeature(tests.Feature):
 
39
 
 
40
    def _probe(self):
 
41
        try:
 
42
            import_dulwich()
 
43
        except bzr_errors.DependencyNotPresent:
 
44
            return False
 
45
        return True
 
46
 
 
47
    def feature_name(self):
 
48
        return 'dulwich'
 
49
 
 
50
 
 
51
DulwichFeature = _DulwichFeature()
 
52
 
 
53
 
 
54
class GitBranchBuilder(object):
 
55
 
 
56
    def __init__(self, stream=None):
 
57
        self.commit_info = []
 
58
        self.stream = stream
 
59
        self._process = None
 
60
        self._counter = 0
 
61
        self._branch = 'refs/heads/master'
 
62
        if stream is None:
 
63
            # Write the marks file into the git sandbox.
 
64
            self._marks_file_name = osutils.abspath('marks')
 
65
            self._process = subprocess.Popen(
 
66
                ['git', 'fast-import', '--quiet',
 
67
                 # GIT doesn't support '--export-marks foo'
 
68
                 # it only supports '--export-marks=foo'
 
69
                 # And gives a 'unknown option' otherwise.
 
70
                 '--export-marks=' + self._marks_file_name,
 
71
                ],
 
72
                stdout=subprocess.PIPE,
 
73
                stderr=subprocess.PIPE,
 
74
                stdin=subprocess.PIPE,
 
75
                )
 
76
            self.stream = self._process.stdin
 
77
        else:
 
78
            self._process = None
 
79
 
 
80
    def set_branch(self, branch):
 
81
        """Set the branch we are committing."""
 
82
        self._branch = branch
 
83
 
 
84
    def _write(self, text):
 
85
        try:
 
86
            self.stream.write(text)
 
87
        except IOError, e:
 
88
            if self._process is None:
 
89
                raise
 
90
            raise errors.GitCommandError(self._process.returncode,
 
91
                                         'git fast-import',
 
92
                                         self._process.stderr.read())
 
93
 
 
94
    def _writelines(self, lines):
 
95
        try:
 
96
            self.stream.writelines(lines)
 
97
        except IOError, e:
 
98
            if self._process is None:
 
99
                raise
 
100
            raise errors.GitCommandError(self._process.returncode,
 
101
                                         'git fast-import',
 
102
                                         self._process.stderr.read())
 
103
 
 
104
    def _create_blob(self, content):
 
105
        self._counter += 1
 
106
        self._write('blob\n')
 
107
        self._write('mark :%d\n' % (self._counter,))
 
108
        self._write('data %d\n' % (len(content),))
 
109
        self._write(content)
 
110
        self._write('\n')
 
111
        return self._counter
 
112
 
 
113
    def set_symlink(self, path, content):
 
114
        """Create or update symlink at a given path."""
 
115
        mark = self._create_blob(content)
 
116
        mode = '120000'
 
117
        self.commit_info.append('M %s :%d %s\n'
 
118
                % (mode, mark, self._encode_path(path)))
 
119
 
 
120
    def set_file(self, path, content, executable):
 
121
        """Create or update content at a given path."""
 
122
        mark = self._create_blob(content)
 
123
        if executable:
 
124
            mode = '100755'
 
125
        else:
 
126
            mode = '100644'
 
127
        self.commit_info.append('M %s :%d %s\n'
 
128
                                % (mode, mark, self._encode_path(path)))
 
129
 
 
130
    def set_link(self, path, link_target):
 
131
        """Create or update a link at a given path."""
 
132
        mark = self._create_blob(link_target)
 
133
        self.commit_info.append('M 120000 :%d %s\n'
 
134
                                % (mark, self._encode_path(path)))
 
135
 
 
136
    def delete_entry(self, path):
 
137
        """This will delete files or symlinks at the given location."""
 
138
        self.commit_info.append('D %s\n' % (self._encode_path(path),))
 
139
 
 
140
    @staticmethod
 
141
    def _encode_path(path):
 
142
        if '\n' in path or path[0] == '"':
 
143
            path = path.replace('\\', '\\\\')
 
144
            path = path.replace('\n', '\\n')
 
145
            path = path.replace('"', '\\"')
 
146
            path = '"' + path + '"'
 
147
        return path.encode('utf-8')
 
148
 
 
149
    # TODO: Author
 
150
    # TODO: Author timestamp+timezone
 
151
    def commit(self, committer, message, timestamp=None,
 
152
               timezone='+0000', author=None,
 
153
               merge=None, base=None):
 
154
        """Commit the new content.
 
155
 
 
156
        :param committer: The name and address for the committer
 
157
        :param message: The commit message
 
158
        :param timestamp: The timestamp for the commit
 
159
        :param timezone: The timezone of the commit, such as '+0000' or '-1000'
 
160
        :param author: The name and address of the author (if different from
 
161
            committer)
 
162
        :param merge: A list of marks if this should merge in another commit
 
163
        :param base: An id for the base revision (primary parent) if that
 
164
            is not the last commit.
 
165
        :return: A mark which can be used in the future to reference this
 
166
            commit.
 
167
        """
 
168
        self._counter += 1
 
169
        mark = self._counter
 
170
        if timestamp is None:
 
171
            timestamp = int(time.time())
 
172
        self._write('commit %s\n' % (self._branch,))
 
173
        self._write('mark :%d\n' % (mark,))
 
174
        self._write('committer %s %s %s\n'
 
175
                    % (committer, timestamp, timezone))
 
176
        message = message.encode('UTF-8')
 
177
        self._write('data %d\n' % (len(message),))
 
178
        self._write(message)
 
179
        self._write('\n')
 
180
        if base is not None:
 
181
            self._write('from :%d\n' % (base,))
 
182
        if merge is not None:
 
183
            for m in merge:
 
184
                self._write('merge :%d\n' % (m,))
 
185
        self._writelines(self.commit_info)
 
186
        self._write('\n')
 
187
        self.commit_info = []
 
188
        return mark
 
189
 
 
190
    def reset(self, ref=None, mark=None):
 
191
        """Create or recreate the named branch.
 
192
 
 
193
        :param ref: branch name, defaults to the current branch.
 
194
        :param mark: commit the branch will point to.
 
195
        """
 
196
        if ref is None:
 
197
            ref = self._branch
 
198
        self._write('reset %s\n' % (ref,))
 
199
        if mark is not None:
 
200
            self._write('from :%d\n' % mark)
 
201
        self._write('\n')
 
202
 
 
203
    def finish(self):
 
204
        """We are finished building, close the stream, get the id mapping"""
 
205
        self.stream.close()
 
206
        if self._process is None:
 
207
            return {}
 
208
        if self._process.wait() != 0:
 
209
            raise errors.GitCommandError(self._process.returncode,
 
210
                                         'git fast-import',
 
211
                                         self._process.stderr.read())
 
212
        marks_file = open(self._marks_file_name)
 
213
        mapping = {}
 
214
        for line in marks_file:
 
215
            mark, shasum = line.split()
 
216
            assert mark.startswith(':')
 
217
            mapping[int(mark[1:])] = shasum
 
218
        marks_file.close()
 
219
        return mapping
 
220
 
 
221
 
 
222
def test_suite():
 
223
    loader = tests.TestLoader()
 
224
 
 
225
    suite = tests.TestSuite()
 
226
 
 
227
    testmod_names = [
 
228
        'test_blackbox',
 
229
        'test_builder',
 
230
        'test_branch',
 
231
        'test_cache',
 
232
        'test_dir',
 
233
        'test_fetch',
 
234
        'test_mapping',
 
235
        'test_object_store',
 
236
        'test_push',
 
237
        'test_remote',
 
238
        'test_repository',
 
239
        'test_refs',
 
240
        'test_revspec',
 
241
        'test_roundtrip',
 
242
        'test_transportgit',
 
243
        ]
 
244
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
 
245
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))
 
246
 
 
247
    return suite