/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 tests/test_upload.py

(garyvdm, vila) Allows uploading from remote branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    )
39
39
 
40
40
 
41
 
from bzrlib.plugins.upload import cmd_upload, BzrUploader, get_upload_auto
 
41
from bzrlib.plugins.upload import (
 
42
    cmd_upload,
 
43
    BzrUploader,
 
44
    get_upload_auto,
 
45
    CannotUploadToWorkingTreeError,
 
46
    )
42
47
 
43
48
 
44
49
class TransportAdapter(
95
100
 
96
101
    is_testing_for_transports = tests.condition_isinstance(
97
102
        (TestFullUpload,
98
 
         TestIncrementalUpload,))
 
103
         TestIncrementalUpload,
 
104
         TestUploadFromRemoteBranch))
99
105
    transport_adapter = TransportAdapter()
100
106
 
101
107
    is_testing_for_branches = tests.condition_isinstance(
138
144
    return result
139
145
 
140
146
 
141
 
class TestUploadMixin(object):
142
 
    """Helper class to share tests between full and incremental uploads.
 
147
class UploadUtilsMixin(object):
 
148
    """Helper class to write upload tests.
143
149
 
144
 
    This class also provides helpers to simplify test writing. The emphasis is
145
 
    on easy test writing, so each tree modification is committed. This doesn't
 
150
    This class provides helpers to simplify test writing. The emphasis is on
 
151
    easy test writing, so each tree modification is committed. This doesn't
146
152
    preclude writing tests spawning several revisions to upload more complex
147
153
    changes.
148
154
    """
149
155
 
150
 
    upload_dir = 'upload/'
151
 
    branch_dir = 'branch/'
 
156
    upload_dir = 'upload'
 
157
    branch_dir = 'branch'
152
158
 
153
 
    def make_local_branch(self):
154
 
        t = transport.get_transport('branch')
 
159
    def make_branch_and_working_tree(self):
 
160
        t = transport.get_transport(self.branch_dir)
155
161
        t.ensure_base()
156
162
        branch = bzrdir.BzrDir.create_branch_convenience(
157
163
            t.base,
161
167
        self.tree.commit('initial empty tree')
162
168
 
163
169
    def assertUpFileEqual(self, content, path, base=upload_dir):
164
 
        self.assertFileEqual(content, base + path)
 
170
        self.assertFileEqual(content, osutils.pathjoin(base, path))
165
171
 
166
172
    def assertUpPathModeEqual(self, path, expected_mode, base=upload_dir):
167
173
        # FIXME: the tests needing that assertion should depend on the server
169
175
        # against servers that can't. Note that some bzrlib transports define
170
176
        # _can_roundtrip_unix_modebits in a incomplete way, this property
171
177
        # should depend on both the client and the server, not the client only.
172
 
        st = os.stat(base + path)
 
178
        full_path = osutils.pathjoin(base, path)
 
179
        st = os.stat(full_path)
173
180
        mode = st.st_mode & 0777
174
181
        if expected_mode == mode:
175
182
            return
176
183
        raise AssertionError(
177
184
            'For path %s, mode is %s not %s' %
178
 
            (base + path, oct(mode), oct(expected_mode)))
 
185
            (full_path, oct(mode), oct(expected_mode)))
179
186
 
180
187
    def failIfUpFileExists(self, path, base=upload_dir):
181
 
        self.failIfExists(base + path)
 
188
        self.failIfExists(osutils.pathjoin(base, path))
182
189
 
183
190
    def failUnlessUpFileExists(self, path, base=upload_dir):
184
 
        self.failUnlessExists(base + path)
 
191
        self.failUnlessExists(osutils.pathjoin(base, path))
185
192
 
186
 
    def set_file_content(self, name, content, base=branch_dir):
187
 
        f = file(base + name, 'wb')
 
193
    def set_file_content(self, path, content, base=branch_dir):
 
194
        f = file(osutils.pathjoin(base, path), 'wb')
188
195
        try:
189
196
            f.write(content)
190
197
        finally:
191
198
            f.close()
192
199
 
193
 
    def add_file(self, name, content, base=branch_dir):
194
 
        self.set_file_content(name, content, base)
195
 
        self.tree.add(name)
196
 
        self.tree.commit('add file %s' % name)
197
 
 
198
 
    def modify_file(self, name, content, base=branch_dir):
199
 
        self.set_file_content(name, content, base)
200
 
        self.tree.commit('modify file %s' % name)
201
 
 
202
 
    def chmod_file(self, name, mode, base=branch_dir):
203
 
        path = base + name
204
 
        os.chmod(path, mode)
205
 
        self.tree.commit('change file %s mode to %s' % (name, oct(mode)))
206
 
 
207
 
    def delete_any(self, name, base=branch_dir):
208
 
        self.tree.remove([name], keep_files=False)
209
 
        self.tree.commit('delete %s' % name)
210
 
 
211
 
    def add_dir(self, name, base=branch_dir):
212
 
        os.mkdir(base + name)
213
 
        self.tree.add(name)
214
 
        self.tree.commit('add directory %s' % name)
215
 
 
216
 
    def rename_any(self, old_name, new_name):
217
 
        self.tree.rename_one(old_name, new_name)
218
 
        self.tree.commit('rename %s into %s' % (old_name, new_name))
219
 
 
220
 
    def transform_dir_into_file(self, name, content, base=branch_dir):
221
 
        osutils.delete_any(base + name)
222
 
        self.set_file_content(name, content, base)
223
 
        self.tree.commit('change %s from dir to file' % name)
224
 
 
225
 
    def transform_file_into_dir(self, name, base=branch_dir):
 
200
    def add_file(self, path, content, base=branch_dir):
 
201
        self.set_file_content(path, content, base)
 
202
        self.tree.add(path)
 
203
        self.tree.commit('add file %s' % path)
 
204
 
 
205
    def modify_file(self, path, content, base=branch_dir):
 
206
        self.set_file_content(path, content, base)
 
207
        self.tree.commit('modify file %s' % path)
 
208
 
 
209
    def chmod_file(self, path, mode, base=branch_dir):
 
210
        full_path = osutils.pathjoin(base, path)
 
211
        os.chmod(full_path, mode)
 
212
        self.tree.commit('change file %s mode to %s' % (path, oct(mode)))
 
213
 
 
214
    def delete_any(self, path, base=branch_dir):
 
215
        self.tree.remove([path], keep_files=False)
 
216
        self.tree.commit('delete %s' % path)
 
217
 
 
218
    def add_dir(self, path, base=branch_dir):
 
219
        os.mkdir(osutils.pathjoin(base, path))
 
220
        self.tree.add(path)
 
221
        self.tree.commit('add directory %s' % path)
 
222
 
 
223
    def rename_any(self, old_path, new_path):
 
224
        self.tree.rename_one(old_path, new_path)
 
225
        self.tree.commit('rename %s into %s' % (old_path, new_path))
 
226
 
 
227
    def transform_dir_into_file(self, path, content, base=branch_dir):
 
228
        osutils.delete_any(osutils.pathjoin(base, path))
 
229
        self.set_file_content(path, content, base)
 
230
        self.tree.commit('change %s from dir to file' % path)
 
231
 
 
232
    def transform_file_into_dir(self, path, base=branch_dir):
226
233
        # bzr can't handle that kind change in a single commit without an
227
234
        # intervening bzr status (see bug #205636).
228
 
        self.tree.remove([name], keep_files=False)
229
 
        os.mkdir(base + name)
230
 
        self.tree.add(name)
231
 
        self.tree.commit('change %s from file to dir' % name)
 
235
        self.tree.remove([path], keep_files=False)
 
236
        os.mkdir(osutils.pathjoin(base, path))
 
237
        self.tree.add(path)
 
238
        self.tree.commit('change %s from file to dir' % path)
232
239
 
233
240
    def _get_cmd_upload(self):
234
241
        upload = cmd_upload()
240
247
 
241
248
    def do_full_upload(self, *args, **kwargs):
242
249
        upload = self._get_cmd_upload()
243
 
        up_url = self.get_transport(self.upload_dir).external_url()
 
250
        up_url = self.get_url(self.upload_dir)
244
251
        if kwargs.get('directory', None) is None:
245
 
            kwargs['directory'] = 'branch'
 
252
            kwargs['directory'] = self.branch_dir
246
253
        kwargs['full'] = True
247
254
        kwargs['quiet'] = True
248
255
        upload.run(up_url, *args, **kwargs)
249
256
 
250
257
    def do_incremental_upload(self, *args, **kwargs):
251
258
        upload = self._get_cmd_upload()
252
 
        up_url = self.get_transport(self.upload_dir).external_url()
 
259
        up_url = self.get_url(self.upload_dir)
253
260
        if kwargs.get('directory', None) is None:
254
 
            kwargs['directory'] = 'branch'
 
261
            kwargs['directory'] = self.branch_dir
255
262
        kwargs['quiet'] = True
256
263
        upload.run(up_url, *args, **kwargs)
257
264
 
 
265
 
 
266
class TestUploadMixin(UploadUtilsMixin):
 
267
    """Helper class to share tests between full and incremental uploads."""
 
268
 
258
269
    def test_create_file(self):
259
 
        self.make_local_branch()
 
270
        self.make_branch_and_working_tree()
260
271
        self.do_full_upload()
261
272
        self.add_file('hello', 'foo')
262
273
 
265
276
        self.assertUpFileEqual('foo', 'hello')
266
277
 
267
278
    def test_create_file_in_subdir(self):
268
 
        self.make_local_branch()
 
279
        self.make_branch_and_working_tree()
269
280
        self.do_full_upload()
270
281
        self.add_dir('dir')
271
282
        self.add_file('dir/goodbye', 'baz')
278
289
        self.assertUpPathModeEqual('dir', 0775)
279
290
 
280
291
    def test_modify_file(self):
281
 
        self.make_local_branch()
 
292
        self.make_branch_and_working_tree()
282
293
        self.add_file('hello', 'foo')
283
294
        self.do_full_upload()
284
295
        self.modify_file('hello', 'bar')
290
301
        self.assertUpFileEqual('bar', 'hello')
291
302
 
292
303
    def test_rename_one_file(self):
293
 
        self.make_local_branch()
 
304
        self.make_branch_and_working_tree()
294
305
        self.add_file('hello', 'foo')
295
306
        self.do_full_upload()
296
307
        self.rename_any('hello', 'goodbye')
302
313
        self.assertUpFileEqual('foo', 'goodbye')
303
314
 
304
315
    def test_rename_and_change_file(self):
305
 
        self.make_local_branch()
 
316
        self.make_branch_and_working_tree()
306
317
        self.add_file('hello', 'foo')
307
318
        self.do_full_upload()
308
319
        self.rename_any('hello', 'goodbye')
315
326
        self.assertUpFileEqual('bar', 'goodbye')
316
327
 
317
328
    def test_rename_two_files(self):
318
 
        self.make_local_branch()
 
329
        self.make_branch_and_working_tree()
319
330
        self.add_file('a', 'foo')
320
331
        self.add_file('b', 'qux')
321
332
        self.do_full_upload()
333
344
        self.assertUpFileEqual('qux', 'c')
334
345
 
335
346
    def test_upload_revision(self):
336
 
        self.make_local_branch() # rev1
 
347
        self.make_branch_and_working_tree() # rev1
337
348
        self.do_full_upload()
338
349
        self.add_file('hello', 'foo') # rev2
339
350
        self.modify_file('hello', 'bar') # rev3
346
357
        self.assertUpFileEqual('foo', 'hello')
347
358
 
348
359
    def test_no_upload_when_changes(self):
349
 
        self.make_local_branch()
 
360
        self.make_branch_and_working_tree()
350
361
        self.add_file('a', 'foo')
351
362
        self.set_file_content('a', 'bar')
352
363
 
353
364
        self.assertRaises(errors.UncommittedChanges, self.do_upload)
354
365
 
355
366
    def test_no_upload_when_conflicts(self):
356
 
        self.make_local_branch()
 
367
        self.make_branch_and_working_tree()
357
368
        self.add_file('a', 'foo')
358
369
        self.run_bzr('branch branch other')
359
370
        self.modify_file('a', 'bar')
366
377
        self.assertRaises(errors.UncommittedChanges, self.do_upload)
367
378
 
368
379
    def test_change_file_into_dir(self):
369
 
        self.make_local_branch()
 
380
        self.make_branch_and_working_tree()
370
381
        self.add_file('hello', 'foo')
371
382
        self.do_full_upload()
372
383
        self.transform_file_into_dir('hello')
379
390
        self.assertUpFileEqual('bar', 'hello/file')
380
391
 
381
392
    def test_change_dir_into_file(self):
382
 
        self.make_local_branch()
 
393
        self.make_branch_and_working_tree()
383
394
        self.add_dir('hello')
384
395
        self.add_file('hello/file', 'foo')
385
396
        self.do_full_upload()
393
404
        self.assertUpFileEqual('bar', 'hello')
394
405
 
395
406
    def test_make_file_executable(self):
396
 
        self.make_local_branch()
 
407
        self.make_branch_and_working_tree()
397
408
        self.add_file('hello', 'foo')
398
409
        self.chmod_file('hello', 0664)
399
410
        self.do_full_upload()
405
416
 
406
417
        self.assertUpPathModeEqual('hello', 0775)
407
418
 
 
419
    def get_upload_auto(self):
 
420
        return get_upload_auto(self.tree.branch)
 
421
 
408
422
    def test_upload_auto(self):
409
423
        """Test that upload --auto sets the upload_auto option"""
410
 
        self.make_local_branch()
 
424
        self.make_branch_and_working_tree()
 
425
 
411
426
        self.add_file('hello', 'foo')
412
 
        self.assertFalse(get_upload_auto(self.tree.branch))
 
427
        self.assertFalse(self.get_upload_auto())
413
428
        self.do_full_upload(auto=True)
414
429
        self.assertUpFileEqual('foo', 'hello')
415
 
        self.assertTrue(get_upload_auto(self.tree.branch))
 
430
        self.assertTrue(self.get_upload_auto())
 
431
 
416
432
        # and check that it stays set until it is unset
417
433
        self.add_file('bye', 'bar')
418
434
        self.do_full_upload()
419
435
        self.assertUpFileEqual('bar', 'bye')
420
 
        self.assertTrue(get_upload_auto(self.tree.branch))
 
436
        self.assertTrue(self.get_upload_auto())
421
437
 
422
438
    def test_upload_noauto(self):
423
439
        """Test that upload --no-auto unsets the upload_auto option"""
424
 
        self.make_local_branch()
 
440
        self.make_branch_and_working_tree()
 
441
 
425
442
        self.add_file('hello', 'foo')
426
 
        self.assertFalse(get_upload_auto(self.tree.branch))
427
443
        self.do_full_upload(auto=True)
428
444
        self.assertUpFileEqual('foo', 'hello')
429
 
        self.assertTrue(get_upload_auto(self.tree.branch))
 
445
        self.assertTrue(self.get_upload_auto())
 
446
 
430
447
        self.add_file('bye', 'bar')
431
448
        self.do_full_upload(auto=False)
432
449
        self.assertUpFileEqual('bar', 'bye')
433
 
        self.assertFalse(get_upload_auto(self.tree.branch))
 
450
        self.assertFalse(self.get_upload_auto())
 
451
 
434
452
        # and check that it stays unset until it is set
435
453
        self.add_file('again', 'baz')
436
454
        self.do_full_upload()
437
455
        self.assertUpFileEqual('baz', 'again')
438
 
        self.assertFalse(get_upload_auto(self.tree.branch))
 
456
        self.assertFalse(self.get_upload_auto())
439
457
 
440
458
    def test_upload_from_subdir(self):
441
 
        self.make_local_branch()
 
459
        self.make_branch_and_working_tree()
442
460
        self.build_tree(['branch/foo/', 'branch/foo/bar'])
443
461
        self.tree.add(['foo/', 'foo/bar'])
444
462
        self.tree.commit("Add directory")
450
468
    do_upload = TestUploadMixin.do_full_upload
451
469
 
452
470
    def test_full_upload_empty_tree(self):
453
 
        self.make_local_branch()
 
471
        self.make_branch_and_working_tree()
454
472
 
455
473
        self.do_full_upload()
456
474
 
457
475
        self.failUnlessUpFileExists(BzrUploader.bzr_upload_revid_file_name)
458
476
 
459
477
    def test_invalid_revspec(self):
460
 
        self.make_local_branch()
 
478
        self.make_branch_and_working_tree()
461
479
        rev1 = revisionspec.RevisionSpec.from_string('1')
462
480
        rev2 = revisionspec.RevisionSpec.from_string('2')
463
481
 
465
483
                          self.do_incremental_upload, revision=[rev1, rev2])
466
484
 
467
485
    def test_create_remote_dir_twice(self):
468
 
        self.make_local_branch()
 
486
        self.make_branch_and_working_tree()
469
487
        self.add_dir('dir')
470
488
        self.do_full_upload()
471
489
        self.add_file('dir/goodbye', 'baz')
485
503
    # XXX: full upload doesn't handle deletions....
486
504
 
487
505
    def test_delete_one_file(self):
488
 
        self.make_local_branch()
 
506
        self.make_branch_and_working_tree()
489
507
        self.add_file('hello', 'foo')
490
508
        self.do_full_upload()
491
509
        self.delete_any('hello')
497
515
        self.failIfUpFileExists('hello')
498
516
 
499
517
    def test_delete_dir_and_subdir(self):
500
 
        self.make_local_branch()
 
518
        self.make_branch_and_working_tree()
501
519
        self.add_dir('dir')
502
520
        self.add_dir('dir/subdir')
503
521
        self.add_file('dir/subdir/a', 'foo')
516
534
        self.assertUpFileEqual('foo', 'a')
517
535
 
518
536
    def test_delete_one_file_rename_to_deleted(self):
519
 
        self.make_local_branch()
 
537
        self.make_branch_and_working_tree()
520
538
        self.add_file('a', 'foo')
521
539
        self.add_file('b', 'bar')
522
540
        self.do_full_upload()
531
549
        self.assertUpFileEqual('bar', 'a')
532
550
 
533
551
    def test_rename_outside_dir_delete_dir(self):
534
 
        self.make_local_branch()
 
552
        self.make_branch_and_working_tree()
535
553
        self.add_dir('dir')
536
554
        self.add_file('dir/a', 'foo')
537
555
        self.do_full_upload()
547
565
        self.assertUpFileEqual('foo', 'a')
548
566
 
549
567
    def test_upload_for_the_first_time_do_a_full_upload(self):
550
 
        self.make_local_branch()
 
568
        self.make_branch_and_working_tree()
551
569
        self.add_file('hello', 'bar')
552
570
 
553
571
        self.failIfUpFileExists(BzrUploader.bzr_upload_revid_file_name)
580
598
        config.set_user_option('upload_location', 'foo')
581
599
        self.assertEqual('foo', config.get_user_option('upload_location'))
582
600
 
 
601
 
 
602
class TestUploadFromRemoteBranch(tests.TestCaseWithTransport,
 
603
                                 UploadUtilsMixin):
 
604
 
 
605
    remote_branch_dir = 'remote_branch'
 
606
 
 
607
    def make_remote_branch_without_working_tree(self):
 
608
        """Creates a branch without working tree to upload from.
 
609
 
 
610
        It's created from the existing self.branch_dir one which still has its
 
611
        working tree.
 
612
        """
 
613
        self.make_branch_and_working_tree()
 
614
        self.add_file('hello', 'foo')
 
615
 
 
616
        remote_branch_url = self.get_url(self.remote_branch_dir)
 
617
        self.run_bzr(['push', remote_branch_url,
 
618
                      '--directory', self.branch_dir])
 
619
        return remote_branch_url
 
620
 
 
621
    def test_no_upload_to_remote_working_tree(self):
 
622
        remote_branch_url = self.make_remote_branch_without_working_tree()
 
623
        upload = self._get_cmd_upload()
 
624
        up_url = self.get_url(self.branch_dir)
 
625
        # Let's try to upload from the just created remote branch into the
 
626
        # branch (with has a working tree).
 
627
        self.assertRaises(CannotUploadToWorkingTreeError,
 
628
                          upload.run, up_url, directory=remote_branch_url)
 
629
 
 
630
    def test_upload_without_working_tree(self):
 
631
        remote_branch_url = self.make_remote_branch_without_working_tree()
 
632
        self.do_full_upload(directory=remote_branch_url)
 
633
        self.assertUpFileEqual('foo', 'hello')
 
634