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