/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

Read long description from README.

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
from cStringIO import StringIO
 
20
 
 
21
import time
 
22
 
 
23
from bzrlib import (
 
24
    errors as bzr_errors,
 
25
    tests,
 
26
    )
 
27
try:
 
28
    from bzrlib.tests.features import Feature
 
29
except ImportError: # bzr < 2.5
 
30
    from bzrlib.tests import Feature
 
31
from bzrlib.plugins.git import (
 
32
    import_dulwich,
 
33
    )
 
34
from fastimport import (
 
35
    commands,
 
36
    )
 
37
 
 
38
TestCase = tests.TestCase
 
39
TestCaseInTempDir = tests.TestCaseInTempDir
 
40
TestCaseWithTransport = tests.TestCaseWithTransport
 
41
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
 
42
 
 
43
class _DulwichFeature(Feature):
 
44
 
 
45
    def _probe(self):
 
46
        try:
 
47
            import_dulwich()
 
48
        except bzr_errors.DependencyNotPresent:
 
49
            return False
 
50
        return True
 
51
 
 
52
    def feature_name(self):
 
53
        return 'dulwich'
 
54
 
 
55
 
 
56
DulwichFeature = _DulwichFeature()
 
57
 
 
58
 
 
59
class GitBranchBuilder(object):
 
60
 
 
61
    def __init__(self, stream=None):
 
62
        self.commit_info = []
 
63
        self.orig_stream = stream
 
64
        if stream is None:
 
65
            self.stream = StringIO()
 
66
        else:
 
67
            self.stream = stream
 
68
        self._counter = 0
 
69
        self._branch = 'refs/heads/master'
 
70
 
 
71
    def set_branch(self, branch):
 
72
        """Set the branch we are committing."""
 
73
        self._branch = branch
 
74
 
 
75
    def _write(self, text):
 
76
        self.stream.write(text)
 
77
 
 
78
    def _writelines(self, lines):
 
79
        self.stream.writelines(lines)
 
80
 
 
81
    def _create_blob(self, content):
 
82
        self._counter += 1
 
83
        blob = commands.BlobCommand(str(self._counter), content)
 
84
        self._write(str(blob)+"\n")
 
85
        return self._counter
 
86
 
 
87
    def set_symlink(self, path, content):
 
88
        """Create or update symlink at a given path."""
 
89
        mark = self._create_blob(content)
 
90
        mode = '120000'
 
91
        self.commit_info.append('M %s :%d %s\n'
 
92
                % (mode, mark, self._encode_path(path)))
 
93
 
 
94
    def set_file(self, path, content, executable):
 
95
        """Create or update content at a given path."""
 
96
        mark = self._create_blob(content)
 
97
        if executable:
 
98
            mode = '100755'
 
99
        else:
 
100
            mode = '100644'
 
101
        self.commit_info.append('M %s :%d %s\n'
 
102
                                % (mode, mark, self._encode_path(path)))
 
103
 
 
104
    def set_link(self, path, link_target):
 
105
        """Create or update a link at a given path."""
 
106
        mark = self._create_blob(link_target)
 
107
        self.commit_info.append('M 120000 :%d %s\n'
 
108
                                % (mark, self._encode_path(path)))
 
109
 
 
110
    def delete_entry(self, path):
 
111
        """This will delete files or symlinks at the given location."""
 
112
        self.commit_info.append('D %s\n' % (self._encode_path(path),))
 
113
 
 
114
    @staticmethod
 
115
    def _encode_path(path):
 
116
        if '\n' in path or path[0] == '"':
 
117
            path = path.replace('\\', '\\\\')
 
118
            path = path.replace('\n', '\\n')
 
119
            path = path.replace('"', '\\"')
 
120
            path = '"' + path + '"'
 
121
        return path.encode('utf-8')
 
122
 
 
123
    # TODO: Author
 
124
    # TODO: Author timestamp+timezone
 
125
    def commit(self, committer, message, timestamp=None,
 
126
               timezone='+0000', author=None,
 
127
               merge=None, base=None):
 
128
        """Commit the new content.
 
129
 
 
130
        :param committer: The name and address for the committer
 
131
        :param message: The commit message
 
132
        :param timestamp: The timestamp for the commit
 
133
        :param timezone: The timezone of the commit, such as '+0000' or '-1000'
 
134
        :param author: The name and address of the author (if different from
 
135
            committer)
 
136
        :param merge: A list of marks if this should merge in another commit
 
137
        :param base: An id for the base revision (primary parent) if that
 
138
            is not the last commit.
 
139
        :return: A mark which can be used in the future to reference this
 
140
            commit.
 
141
        """
 
142
        self._counter += 1
 
143
        mark = str(self._counter)
 
144
        if timestamp is None:
 
145
            timestamp = int(time.time())
 
146
        self._write('commit %s\n' % (self._branch,))
 
147
        self._write('mark :%s\n' % (mark,))
 
148
        self._write('committer %s %s %s\n'
 
149
                    % (committer, timestamp, timezone))
 
150
        message = message.encode('UTF-8')
 
151
        self._write('data %d\n' % (len(message),))
 
152
        self._write(message)
 
153
        self._write('\n')
 
154
        if base is not None:
 
155
            self._write('from :%s\n' % (base,))
 
156
        if merge is not None:
 
157
            for m in merge:
 
158
                self._write('merge :%s\n' % (m,))
 
159
        self._writelines(self.commit_info)
 
160
        self._write('\n')
 
161
        self.commit_info = []
 
162
        return mark
 
163
 
 
164
    def reset(self, ref=None, mark=None):
 
165
        """Create or recreate the named branch.
 
166
 
 
167
        :param ref: branch name, defaults to the current branch.
 
168
        :param mark: commit the branch will point to.
 
169
        """
 
170
        if ref is None:
 
171
            ref = self._branch
 
172
        self._write('reset %s\n' % (ref,))
 
173
        if mark is not None:
 
174
            self._write('from :%s\n' % mark)
 
175
        self._write('\n')
 
176
 
 
177
    def finish(self):
 
178
        """We are finished building, close the stream, get the id mapping"""
 
179
        self.stream.seek(0)
 
180
        if self.orig_stream is None:
 
181
            from dulwich.repo import Repo
 
182
            r = Repo(".")
 
183
            from dulwich.fastexport import GitImportProcessor
 
184
            importer = GitImportProcessor(r)
 
185
            return importer.import_stream(self.stream)
 
186
 
 
187
 
 
188
def test_suite():
 
189
    loader = tests.TestUtil.TestLoader()
 
190
 
 
191
    suite = tests.TestUtil.TestSuite()
 
192
 
 
193
    testmod_names = [
 
194
        'test_blackbox',
 
195
        'test_builder',
 
196
        'test_branch',
 
197
        'test_cache',
 
198
        'test_dir',
 
199
        'test_fetch',
 
200
        'test_mapping',
 
201
        'test_object_store',
 
202
        'test_push',
 
203
        'test_remote',
 
204
        'test_repository',
 
205
        'test_refs',
 
206
        'test_revspec',
 
207
        'test_roundtrip',
 
208
        'test_transportgit',
 
209
        'test_unpeel_map',
 
210
        'test_workingtree',
 
211
        ]
 
212
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
 
213
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))
 
214
 
 
215
    return suite