/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
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
165
    def delete_any(self, name, base=branch_dir):
166
        self.tree.remove([name], keep_files=False)
167
        self.tree.commit('delete %s' % name)
168
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
169
    def add_dir(self, name, base=branch_dir):
170
        os.mkdir(base + name)
171
        self.tree.add(name)
172
        self.tree.commit('add directory %s' % name)
173
174
    def rename_any(self, old_name, new_name):
175
        self.tree.rename_one(old_name, new_name)
176
        self.tree.commit('rename %s into %s' % (old_name, new_name))
0.152.13 by Vincent Ladeuil
Test uploading an empty tree.
177
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
178
    def do_full_upload(self, *args, **kwargs):
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
179
        upload = cmd_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
180
        up_url = self.get_transport(self.upload_dir).external_url()
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
181
        if kwargs.get('directory', None) is None:
182
            kwargs['directory'] = 'branch'
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
183
        kwargs['full'] = True
184
        upload.run(up_url, *args, **kwargs)
185
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
186
    def do_incremental_upload(self, *args, **kwargs):
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
187
        upload = cmd_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
188
        up_url = self.get_transport(self.upload_dir).external_url()
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
189
        if kwargs.get('directory', None) is None:
190
            kwargs['directory'] = 'branch'
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
191
        upload.run(up_url, *args, **kwargs)
192
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
193
    def test_create_file(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
194
        self.make_local_branch()
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
195
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
196
        self.add_file('hello', 'foo')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
197
        self.do_upload()
198
199
        self.assertUpFileEqual('foo', 'hello')
200
201
    def test_create_file_in_subdir(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
202
        self.make_local_branch()
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
203
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
204
        self.add_dir('dir')
205
        self.add_file('dir/goodbye', 'baz')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
206
207
        self.failIfUpFileExists('dir/goodbye')
208
        self.do_upload()
209
        self.assertUpFileEqual('baz', 'dir/goodbye')
210
211
    def test_modify_file(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
212
        self.make_local_branch()
213
        self.add_file('hello', 'foo')
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.modify_file('hello', 'bar')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
216
217
        self.assertUpFileEqual('foo', 'hello')
218
        self.do_upload()
219
        self.assertUpFileEqual('bar', 'hello')
220
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
221
    def test_rename_one_file(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
222
        self.make_local_branch()
223
        self.add_file('hello', 'foo')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
224
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
225
        self.rename_any('hello', 'goodbye')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
226
227
        self.assertUpFileEqual('foo', 'hello')
228
        self.do_upload()
229
        self.assertUpFileEqual('foo', 'goodbye')
230
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
231
    def test_rename_two_files(self):
232
        self.make_local_branch()
233
        self.add_file('a', 'foo')
234
        self.add_file('b', 'qux')
235
        self.do_full_upload()
236
        # We rely on the assumption that bzr will topologically sort the
237
        # renames which will cause a -> b to appear *before* b -> c
238
        self.rename_any('b', 'c')
239
        self.rename_any('a', 'b')
240
241
        self.assertUpFileEqual('foo', 'a')
242
        self.assertUpFileEqual('qux', 'b')
243
        self.do_upload()
244
        self.assertUpFileEqual('foo', 'b')
245
        self.assertUpFileEqual('qux', 'c')
246
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
247
    def test_upload_revision(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
248
        self.make_local_branch() # rev1
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
249
        self.do_full_upload()
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
250
        self.add_file('hello', 'foo') # rev2
251
        self.modify_file('hello', 'bar') # rev3
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
252
253
        self.failIfUpFileExists('hello')
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
254
        revspec = revisionspec.RevisionSpec.from_string('2')
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
255
        self.do_upload(revision=[revspec])
256
        self.assertUpFileEqual('foo', 'hello')
257
258
259
class TestFullUpload(tests.TestCaseWithTransport, TestUploadMixin):
260
261
    do_upload = TestUploadMixin.do_full_upload
262
0.152.13 by Vincent Ladeuil
Test uploading an empty tree.
263
    def test_full_upload_empty_tree(self):
264
        self.make_local_branch()
265
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
266
        self.do_full_upload()
0.152.13 by Vincent Ladeuil
Test uploading an empty tree.
267
268
        upload = cmd_upload()
269
        self.failUnlessUpFileExists(upload.bzr_upload_revid_file_name)
270
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
271
    def test_invalid_revspec(self):
0.152.15 by Vincent Ladeuil
Simplify test writing (for good ?).
272
        self.make_local_branch()
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
273
        rev1 = revisionspec.RevisionSpec.from_string('1')
274
        rev2 = revisionspec.RevisionSpec.from_string('2')
0.152.8 by Vincent Ladeuil
Handle uploading directories.
275
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
276
        self.assertRaises(errors.BzrCommandError,
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
277
                          self.do_incremental_upload, revision=[rev1, rev2])
278
279
280
class TestIncrementalUpload(tests.TestCaseWithTransport, TestUploadMixin):
281
282
    do_upload = TestUploadMixin.do_incremental_upload
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
283
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
284
    # Note that full upload doesn't handle deletions....
285
286
    def test_delete_one_file(self):
287
        self.make_local_branch()
288
        self.add_file('hello', 'foo')
289
        self.do_full_upload()
290
        self.delete_any('hello')
291
292
        self.assertUpFileEqual('foo', 'hello')
293
        self.do_upload()
294
        self.failIfUpFileExists('hello')
295
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
296
    def test_delete_dir_and_subdir(self):
297
        self.make_local_branch()
298
        self.add_dir('dir')
299
        self.add_dir('dir/subdir')
300
        self.add_file('dir/subdir/a', 'foo')
301
        self.do_full_upload()
302
        self.rename_any('dir/subdir/a', 'a')
303
        self.delete_any('dir/subdir')
304
        self.delete_any('dir')
305
306
        self.assertUpFileEqual('foo', 'dir/subdir/a')
307
        self.do_upload()
308
        self.failIfUpFileExists('dir/subdir/a')
309
        self.failIfUpFileExists('dir/subdir')
310
        self.failIfUpFileExists('dir')
311
        self.assertUpFileEqual('foo', 'a')
312
313
    def test_delete_one_file_rename_to_deleted(self):
314
        self.make_local_branch()
315
        self.add_file('a', 'foo')
316
        self.add_file('b', 'bar')
317
        self.do_full_upload()
318
        self.delete_any('a')
319
        self.rename_any('b', 'a')
320
321
        self.assertUpFileEqual('foo', 'a')
322
        self.do_upload()
323
        self.failIfUpFileExists('b')
324
        self.assertUpFileEqual('bar', 'a')
325
326
    def test_rename_outside_dir_delete_dir(self):
327
        self.make_local_branch()
328
        self.add_dir('dir')
329
        self.add_file('dir/a', 'foo')
330
        self.do_full_upload()
331
        self.rename_any('dir/a', 'a')
332
        self.delete_any('dir')
333
334
        self.assertUpFileEqual('foo', 'dir/a')
335
        self.do_upload()
336
        self.failIfUpFileExists('dir/a')
337
        self.failIfUpFileExists('dir')
338
        self.assertUpFileEqual('foo', 'a')
339
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
340
341
class TestBranchUploadLocations(branch_implementations.TestCaseWithBranch):
342
343
    def test_get_upload_location_unset(self):
344
        config = self.get_branch().get_config()
345
        self.assertEqual(None, config.get_user_option('upload_location'))
346
347
    def test_get_push_location_exact(self):
348
        from bzrlib.config import (locations_config_filename,
349
                                   ensure_config_dir_exists)
350
        ensure_config_dir_exists()
351
        fn = locations_config_filename()
352
        b = self.get_branch()
353
        open(fn, 'wt').write(("[%s]\n"
354
                                  "upload_location=foo\n" %
355
                                  b.base[:-1]))
356
        config = b.get_config()
357
        self.assertEqual("foo", config.get_user_option('upload_location'))
358
359
    def test_set_push_location(self):
360
        config = self.get_branch().get_config()
361
        config.set_user_option('upload_location', 'foo')
362
        self.assertEqual('foo', config.get_user_option('upload_location'))
363