/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 breezy/git/tests/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-06 01:18:08 UTC
  • mfrom: (7143 work)
  • mto: This revision was merged to the branch mainline in revision 7151.
  • Revision ID: jelmer@jelmer.uk-20181106011808-y870f4vq0ork3ahu
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2018 Jelmer Vernoij <jelmer@jelmer.uk>
 
2
# Copyright (C) 2006, 2007 Canonical Ltd
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""The basic test suite for bzr-git."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from io import BytesIO
 
23
 
 
24
import time
 
25
 
 
26
from ... import (
 
27
    errors as bzr_errors,
 
28
    tests,
 
29
    )
 
30
from ...tests.features import (
 
31
    Feature,
 
32
    ModuleAvailableFeature,
 
33
    )
 
34
from .. import (
 
35
    import_dulwich,
 
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
FastimportFeature = ModuleAvailableFeature('fastimport')
 
58
 
 
59
 
 
60
class GitBranchBuilder(object):
 
61
 
 
62
    def __init__(self, stream=None):
 
63
        if not FastimportFeature.available():
 
64
            raise tests.UnavailableFeature(FastimportFeature)
 
65
        self.commit_info = []
 
66
        self.orig_stream = stream
 
67
        if stream is None:
 
68
            self.stream = BytesIO()
 
69
        else:
 
70
            self.stream = stream
 
71
        self._counter = 0
 
72
        self._branch = b'refs/heads/master'
 
73
 
 
74
    def set_branch(self, branch):
 
75
        """Set the branch we are committing."""
 
76
        self._branch = branch
 
77
 
 
78
    def _write(self, text):
 
79
        self.stream.write(text)
 
80
 
 
81
    def _writelines(self, lines):
 
82
        self.stream.writelines(lines)
 
83
 
 
84
    def _create_blob(self, content):
 
85
        self._counter += 1
 
86
        from fastimport.commands import BlobCommand
 
87
        blob = BlobCommand(b'%d' % self._counter, content)
 
88
        self._write(bytes(blob)+b"\n")
 
89
        return self._counter
 
90
 
 
91
    def set_symlink(self, path, content):
 
92
        """Create or update symlink at a given path."""
 
93
        mark = self._create_blob(self._encode_path(content))
 
94
        mode = b'120000'
 
95
        self.commit_info.append(b'M %s :%d %s\n'
 
96
                % (mode, mark, self._encode_path(path)))
 
97
 
 
98
    def set_file(self, path, content, executable):
 
99
        """Create or update content at a given path."""
 
100
        mark = self._create_blob(content)
 
101
        if executable:
 
102
            mode = b'100755'
 
103
        else:
 
104
            mode = b'100644'
 
105
        self.commit_info.append(b'M %s :%d %s\n'
 
106
                                % (mode, mark, self._encode_path(path)))
 
107
 
 
108
    def delete_entry(self, path):
 
109
        """This will delete files or symlinks at the given location."""
 
110
        self.commit_info.append(b'D %s\n' % (self._encode_path(path),))
 
111
 
 
112
    @staticmethod
 
113
    def _encode_path(path):
 
114
        if isinstance(path, bytes):
 
115
            return 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=b'+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 = b'%d' % (self._counter,)
 
144
        if timestamp is None:
 
145
            timestamp = int(time.time())
 
146
        self._write(b'commit %s\n' % (self._branch,))
 
147
        self._write(b'mark :%s\n' % (mark,))
 
148
        self._write(b'committer %s %ld %s\n'
 
149
                    % (committer, timestamp, timezone))
 
150
        if not isinstance(message, bytes):
 
151
            message = message.encode('UTF-8')
 
152
        self._write(b'data %d\n' % (len(message),))
 
153
        self._write(message)
 
154
        self._write(b'\n')
 
155
        if base is not None:
 
156
            self._write(b'from :%s\n' % (base,))
 
157
        if merge is not None:
 
158
            for m in merge:
 
159
                self._write(b'merge :%s\n' % (m,))
 
160
        self._writelines(self.commit_info)
 
161
        self._write(b'\n')
 
162
        self.commit_info = []
 
163
        return mark
 
164
 
 
165
    def reset(self, ref=None, mark=None):
 
166
        """Create or recreate the named branch.
 
167
 
 
168
        :param ref: branch name, defaults to the current branch.
 
169
        :param mark: commit the branch will point to.
 
170
        """
 
171
        if ref is None:
 
172
            ref = self._branch
 
173
        self._write(b'reset %s\n' % (ref,))
 
174
        if mark is not None:
 
175
            self._write(b'from :%s\n' % mark)
 
176
        self._write(b'\n')
 
177
 
 
178
    def finish(self):
 
179
        """We are finished building, close the stream, get the id mapping"""
 
180
        self.stream.seek(0)
 
181
        if self.orig_stream is None:
 
182
            from dulwich.repo import Repo
 
183
            r = Repo(".")
 
184
            from dulwich.fastexport import GitImportProcessor
 
185
            importer = GitImportProcessor(r)
 
186
            return importer.import_stream(self.stream)
 
187
 
 
188
 
 
189
class MissingFeature(tests.TestCase):
 
190
 
 
191
    def test_dulwich(self):
 
192
        self.requireFeature(DulwichFeature)
 
193
 
 
194
 
 
195
def test_suite():
 
196
    loader = tests.TestUtil.TestLoader()
 
197
 
 
198
    suite = tests.TestUtil.TestSuite()
 
199
 
 
200
    if not DulwichFeature.available():
 
201
        suite.addTests(loader.loadTestsFromTestCase(MissingFeature))
 
202
        return suite
 
203
 
 
204
    testmod_names = [
 
205
        'test_blackbox',
 
206
        'test_builder',
 
207
        'test_branch',
 
208
        'test_cache',
 
209
        'test_dir',
 
210
        'test_fetch',
 
211
        'test_git_remote_helper',
 
212
        'test_mapping',
 
213
        'test_memorytree',
 
214
        'test_object_store',
 
215
        'test_pristine_tar',
 
216
        'test_push',
 
217
        'test_remote',
 
218
        'test_repository',
 
219
        'test_refs',
 
220
        'test_revspec',
 
221
        'test_roundtrip',
 
222
        'test_server',
 
223
        'test_transportgit',
 
224
        'test_unpeel_map',
 
225
        'test_urls',
 
226
        'test_workingtree',
 
227
        ]
 
228
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
 
229
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))
 
230
 
 
231
    return suite