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