/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
1
# Copyright (C) 2008 Canonical Ltd
0.152.1 by Vincent Ladeuil
Empty shell
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
0.152.2 by v.ladeuil+lp at free
Add failing tests for basic usage.
17
import os
18
19
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
20
from bzrlib import (
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
21
    branch,
0.152.8 by Vincent Ladeuil
Handle uploading directories.
22
    bzrdir,
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
23
    errors,
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
24
    remote,
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
25
    revisionspec,
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
26
    tests,
0.152.8 by Vincent Ladeuil
Handle uploading directories.
27
    transport,
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
28
    )
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
29
from bzrlib.smart import server as smart_server
30
31
from bzrlib.tests import (
32
    test_transport_implementations,
33
    branch_implementations,
34
    )
35
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
36
37
from bzrlib.plugins.upload import cmd_upload
0.152.1 by Vincent Ladeuil
Empty shell
38
39
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
40
class TransportAdapter(
41
    test_transport_implementations.TransportTestProviderAdapter):
42
    """A tool to generate a suite testing all transports for a single test.
43
44
    We restrict the transports to the ones we want to support.
45
    """
46
47
    def _test_permutations(self):
48
        """Return a list of the klass, server_factory pairs to test."""
49
        result = []
50
        transport_modules =['bzrlib.transport.ftp',
51
                            'bzrlib.transport.sftp']
52
        for module in transport_modules:
53
            try:
54
                permutations = self.get_transport_test_permutations(
55
                    reduce(getattr, (module).split('.')[1:],
56
                           __import__(module)))
57
                for (klass, server_factory) in permutations:
58
                    scenario = (server_factory.__name__,
59
                        {"transport_class":klass,
60
                         "transport_server":server_factory})
61
                    result.append(scenario)
62
            except errors.DependencyNotPresent, e:
63
                # Continue even if a dependency prevents us 
64
                # from adding this test
65
                pass
66
        return result
67
68
69
def load_tests(standard_tests, module, loader):
70
    """Multiply tests for tranport implementations."""
71
    result = loader.suiteClass()
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
72
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
73
    is_testing_for_transports = tests.condition_isinstance(
74
        (TestFullUpload,
75
         TestIncrementalUpload,))
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
76
    transport_adapter = TransportAdapter()
77
78
    is_testing_for_branches = tests.condition_isinstance(
79
        (TestBranchUploadLocations,))
80
    # Generate a list of branch formats and their associated bzrdir formats to
81
    # use.
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
82
    # XXX: This was copied from bzrlib.tests.branch_implementations.tests_suite
83
    # and need to be shared in a better way.
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
84
    combinations = [(format, format._matchingbzrdir) for format in
85
         branch.BranchFormat._formats.values() + branch._legacy_formats]
86
    BTPA = branch_implementations.BranchTestProviderAdapter
87
    branch_adapter = BTPA(
88
        # None here will cause the default vfs transport server to be used.
89
        None,
90
        # None here will cause a readonly decorator to be created
91
        # by the TestCaseWithTransport.get_readonly_transport method.
92
        None,
93
        combinations)
94
    branch_adapter_for_ss = BTPA(
95
        smart_server.SmartTCPServer_for_testing,
96
        smart_server.ReadonlySmartTCPServer_for_testing,
97
        [(remote.RemoteBranchFormat(), remote.RemoteBzrDirFormat())],
98
        # XXX: Report to bzr list, this parameter is not used in the
99
        # constructor
100
101
        # MemoryServer
102
        )
103
104
    for test_class in tests.iter_suite_tests(standard_tests):
105
        # Each test class is either standalone or testing for some combination
106
        # of transport or branch. Use the right adpater (or none) depending on
107
        # the class.
108
        if is_testing_for_transports(test_class):
109
            result.addTests(transport_adapter.adapt(test_class))
110
        elif is_testing_for_branches(test_class):
111
            result.addTests(branch_adapter.adapt(test_class))
112
            result.addTests(branch_adapter_for_ss.adapt(test_class))
113
        else:
114
            result.addTest(test_class)
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
115
    return result
116
117
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
118
class TestUploadMixin(object):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
119
    """Helper class to share tests between full and incremental uploads.
120
121
    This class also provides helpers to simplify test writing. The emphasis is
122
    on easy test writing, so each tree modification is committed. This doesn't
123
    preclude writing tests spawning several revisions to upload more complex
124
    changes.
125
    """
126
127
    upload_dir = 'upload/'
128
    branch_dir = 'branch/'
0.152.2 by v.ladeuil+lp at free
Add failing tests for basic usage.
129
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
130
    def make_local_branch(self):
0.152.8 by Vincent Ladeuil
Handle uploading directories.
131
        t = transport.get_transport('branch')
132
        t.ensure_base()
133
        branch = bzrdir.BzrDir.create_branch_convenience(
134
            t.base,
135
            format=bzrdir.format_registry.make_bzrdir('default'),
136
            force_new_tree=False)
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
137
        self.tree = branch.bzrdir.create_workingtree()
138
        self.tree.commit('initial empty tree')
139
140
    def assertUpFileEqual(self, content, path, base=upload_dir):
141
        self.assertFileEqual(content, base + path)
142
143
    def failIfUpFileExists(self, path, base=upload_dir):
144
        self.failIfExists(base + path)
145
146
    def failUnlessUpFileExists(self, path, base=upload_dir):
147
        self.failUnlessExists(base + path)
148
149
    def set_file_content(self, name, content, base=branch_dir):
150
        f = file(base + name, 'wb')
151
        try:
152
            f.write(content)
153
        finally:
154
            f.close()
155
156
    def add_file(self, name, content, base=branch_dir):
157
        self.set_file_content(name, content, base)
158
        self.tree.add(name)
159
        self.tree.commit('add file %s' % name)
160
161
    def modify_file(self, name, content, base=branch_dir):
162
        self.set_file_content(name, content, base)
163
        self.tree.commit('modify file %s' % name)
164
165
    def add_dir(self, name, base=branch_dir):
166
        os.mkdir(base + name)
167
        self.tree.add(name)
168
        self.tree.commit('add directory %s' % name)
169
170
    def rename_any(self, old_name, new_name):
171
        self.tree.rename_one(old_name, new_name)
172
        self.tree.commit('rename %s into %s' % (old_name, new_name))
0.152.13 by Vincent Ladeuil
Test uploading an empty tree.
173
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
174
    def do_full_upload(self, *args, **kwargs):
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
175
        upload = cmd_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
176
        up_url = self.get_transport(self.upload_dir).external_url()
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
177
        if kwargs.get('directory', None) is None:
178
            kwargs['directory'] = 'branch'
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
179
        kwargs['full'] = True
180
        upload.run(up_url, *args, **kwargs)
181
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
182
    def do_incremental_upload(self, *args, **kwargs):
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
183
        upload = cmd_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
184
        up_url = self.get_transport(self.upload_dir).external_url()
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
185
        if kwargs.get('directory', None) is None:
186
            kwargs['directory'] = 'branch'
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
187
        upload.run(up_url, *args, **kwargs)
188
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
189
    def test_create_file(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
190
        self.make_local_branch()
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
191
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
192
        self.add_file('hello', 'foo')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
193
        self.do_upload()
194
195
        self.assertUpFileEqual('foo', 'hello')
196
197
    def test_create_file_in_subdir(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
198
        self.make_local_branch()
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
199
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
200
        self.add_dir('dir')
201
        self.add_file('dir/goodbye', 'baz')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
202
203
        self.failIfUpFileExists('dir/goodbye')
204
        self.do_upload()
205
        self.assertUpFileEqual('baz', 'dir/goodbye')
206
207
    def test_modify_file(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
208
        self.make_local_branch()
209
        self.add_file('hello', 'foo')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
210
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
211
        self.modify_file('hello', 'bar')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
212
213
        self.assertUpFileEqual('foo', 'hello')
214
        self.do_upload()
215
        self.assertUpFileEqual('bar', 'hello')
216
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
217
    def test_rename_one_file(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
218
        self.make_local_branch()
219
        self.add_file('hello', 'foo')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
220
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
221
        self.rename_any('hello', 'goodbye')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
222
223
        self.assertUpFileEqual('foo', 'hello')
224
        self.do_upload()
225
        self.assertUpFileEqual('foo', 'goodbye')
226
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
227
    def test_rename_two_files(self):
228
        self.make_local_branch()
229
        self.add_file('a', 'foo')
230
        self.add_file('b', 'qux')
231
        self.do_full_upload()
232
        # We rely on the assumption that bzr will topologically sort the
233
        # renames which will cause a -> b to appear *before* b -> c
234
        self.rename_any('b', 'c')
235
        self.rename_any('a', 'b')
236
237
        self.assertUpFileEqual('foo', 'a')
238
        self.assertUpFileEqual('qux', 'b')
239
        self.do_upload()
240
        self.assertUpFileEqual('foo', 'b')
241
        self.assertUpFileEqual('qux', 'c')
242
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
243
    def test_upload_revision(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
244
        self.make_local_branch() # rev1
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
245
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
246
        self.add_file('hello', 'foo') # rev2
247
        self.modify_file('hello', 'bar') # rev3
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
248
249
        self.failIfUpFileExists('hello')
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
250
        revspec = revisionspec.RevisionSpec.from_string('2')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
251
        self.do_upload(revision=[revspec])
252
        self.assertUpFileEqual('foo', 'hello')
253
254
255
class TestFullUpload(tests.TestCaseWithTransport, TestUploadMixin):
256
257
    do_upload = TestUploadMixin.do_full_upload
258
0.152.13 by Vincent Ladeuil
Test uploading an empty tree.
259
    def test_full_upload_empty_tree(self):
260
        self.make_local_branch()
261
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
262
        self.do_full_upload()
0.152.13 by Vincent Ladeuil
Test uploading an empty tree.
263
264
        upload = cmd_upload()
265
        self.failUnlessUpFileExists(upload.bzr_upload_revid_file_name)
266
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
267
    def test_invalid_revspec(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
268
        self.make_local_branch()
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
269
        rev1 = revisionspec.RevisionSpec.from_string('1')
270
        rev2 = revisionspec.RevisionSpec.from_string('2')
0.152.8 by Vincent Ladeuil
Handle uploading directories.
271
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
272
        self.assertRaises(errors.BzrCommandError,
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
273
                          self.do_incremental_upload, revision=[rev1, rev2])
274
275
276
class TestIncrementalUpload(tests.TestCaseWithTransport, TestUploadMixin):
277
278
    do_upload = TestUploadMixin.do_incremental_upload
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
279
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
280
281
class TestBranchUploadLocations(branch_implementations.TestCaseWithBranch):
282
283
    def test_get_upload_location_unset(self):
284
        config = self.get_branch().get_config()
285
        self.assertEqual(None, config.get_user_option('upload_location'))
286
287
    def test_get_push_location_exact(self):
288
        from bzrlib.config import (locations_config_filename,
289
                                   ensure_config_dir_exists)
290
        ensure_config_dir_exists()
291
        fn = locations_config_filename()
292
        b = self.get_branch()
293
        open(fn, 'wt').write(("[%s]\n"
294
                                  "upload_location=foo\n" %
295
                                  b.base[:-1]))
296
        config = b.get_config()
297
        self.assertEqual("foo", config.get_user_option('upload_location'))
298
299
    def test_set_push_location(self):
300
        config = self.get_branch().get_config()
301
        config.set_user_option('upload_location', 'foo')
302
        self.assertEqual('foo', config.get_user_option('upload_location'))
303