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

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
        ("find_repositoryV3", {
73
73
            "_request_class": smart_dir.SmartServerRequestFindRepositoryV3}),
74
74
        ]
75
 
    to_adapt, result = tests.split_suite_by_re(standard_tests,
76
 
        "TestSmartServerRequestFindRepository")
77
 
    v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt,
78
 
        "_v2")
 
75
    to_adapt, result = tests.split_suite_by_re(
 
76
        standard_tests, "TestSmartServerRequestFindRepository")
 
77
    v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt, "_v2")
79
78
    tests.multiply_tests(v1_and_2, scenarios, result)
80
79
    # The first scenario is only applicable to v1 protocols, it is deleted
81
80
    # since.
123
122
 
124
123
    def test_repeated_substreams_same_kind_are_one_stream(self):
125
124
        # Make a stream - an iterable of bytestrings.
126
 
        stream = [('text', [versionedfile.FulltextContentFactory((b'k1',), None,
127
 
            None, b'foo')]), ('text', [
128
 
            versionedfile.FulltextContentFactory((b'k2',), None, None, b'bar')])]
 
125
        stream = [
 
126
            ('text', [versionedfile.FulltextContentFactory((b'k1',), None,
 
127
             None, b'foo')]),
 
128
            ('text', [versionedfile.FulltextContentFactory((b'k2',), None,
 
129
             None, b'bar')])]
129
130
        fmt = controldir.format_registry.get('pack-0.92')().repository_format
130
131
        bytes = smart_repo._stream_to_byte_stream(stream, fmt)
131
132
        streams = []
142
143
 
143
144
    def test__eq__(self):
144
145
        self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
145
 
            smart_req.SmartServerResponse((b'ok', )))
 
146
                         smart_req.SmartServerResponse((b'ok', )))
146
147
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
147
 
            smart_req.SmartServerResponse((b'ok', ), b'body'))
 
148
                         smart_req.SmartServerResponse((b'ok', ), b'body'))
148
149
        self.assertNotEqual(smart_req.SmartServerResponse((b'ok', )),
149
 
            smart_req.SmartServerResponse((b'notok', )))
 
150
                            smart_req.SmartServerResponse((b'notok', )))
150
151
        self.assertNotEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
151
 
            smart_req.SmartServerResponse((b'ok', )))
 
152
                            smart_req.SmartServerResponse((b'ok', )))
152
153
        self.assertNotEqual(None,
153
 
            smart_req.SmartServerResponse((b'ok', )))
 
154
                            smart_req.SmartServerResponse((b'ok', )))
154
155
 
155
156
    def test__str__(self):
156
157
        """SmartServerResponses can be stringified."""
178
179
            errors.PathNotChild, request.translate_client_path, b'bar/')
179
180
        self.assertEqual('./baz', request.translate_client_path(b'foo/baz'))
180
181
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
181
 
        self.assertEqual(u'./' + urlutils.escape(e_acute),
182
 
                         request.translate_client_path(b'foo/' + e_acute.encode('utf-8')))
 
182
        self.assertEqual(
 
183
            u'./' + urlutils.escape(e_acute),
 
184
            request.translate_client_path(b'foo/' + e_acute.encode('utf-8')))
183
185
 
184
186
    def test_translate_client_path_vfs(self):
185
187
        """VfsRequests receive escaped paths rather than raw UTF-8."""
187
189
        request = vfs.VfsRequest(transport, 'foo/')
188
190
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
189
191
        escaped = urlutils.escape(u'foo/' + e_acute)
190
 
        self.assertEqual('./' + urlutils.escape(e_acute),
191
 
                         request.translate_client_path(escaped.encode('ascii')))
 
192
        self.assertEqual(
 
193
            './' + urlutils.escape(e_acute),
 
194
            request.translate_client_path(escaped.encode('ascii')))
192
195
 
193
196
    def test_transport_from_client_path(self):
194
197
        transport = self.get_transport()
199
202
 
200
203
 
201
204
class TestSmartServerBzrDirRequestCloningMetaDir(
202
 
    tests.TestCaseWithMemoryTransport):
 
205
        tests.TestCaseWithMemoryTransport):
203
206
    """Tests for BzrDir.cloning_metadir."""
204
207
 
205
208
    def test_cloning_metadir(self):
211
214
        request = request_class(backing)
212
215
        expected = smart_req.SuccessfulSmartServerResponse(
213
216
            (local_result.network_name(),
214
 
            local_result.repository_format.network_name(),
215
 
            (b'branch', local_result.get_branch_format().network_name())))
 
217
             local_result.repository_format.network_name(),
 
218
             (b'branch', local_result.get_branch_format().network_name())))
216
219
        self.assertEqual(expected, request.execute(b'', b'False'))
217
220
 
218
221
    def test_cloning_metadir_reference(self):
220
223
        backing = self.get_transport()
221
224
        referenced_branch = self.make_branch('referenced')
222
225
        dir = self.make_controldir('.')
223
 
        local_result = dir.cloning_metadir()
224
 
        reference = _mod_bzrbranch.BranchReferenceFormat().initialize(
 
226
        dir.cloning_metadir()
 
227
        _mod_bzrbranch.BranchReferenceFormat().initialize(
225
228
            dir, target_branch=referenced_branch)
226
 
        reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(dir)
 
229
        _mod_bzrbranch.BranchReferenceFormat().get_reference(dir)
227
230
        # The server shouldn't try to follow the branch reference, so it's fine
228
231
        # if the referenced branch isn't reachable.
229
232
        backing.rename('referenced', 'moved')
233
236
        self.assertEqual(expected, request.execute(b'', b'False'))
234
237
 
235
238
 
236
 
class TestSmartServerBzrDirRequestCloningMetaDir(
237
 
    tests.TestCaseWithMemoryTransport):
 
239
class TestSmartServerBzrDirRequestCheckoutMetaDir(
 
240
        tests.TestCaseWithMemoryTransport):
238
241
    """Tests for BzrDir.checkout_metadir."""
239
242
 
240
243
    def test_checkout_metadir(self):
241
244
        backing = self.get_transport()
242
245
        request = smart_dir.SmartServerBzrDirRequestCheckoutMetaDir(
243
246
            backing)
244
 
        branch = self.make_branch('.', format='2a')
 
247
        self.make_branch('.', format='2a')
245
248
        response = request.execute(b'')
246
249
        self.assertEqual(
247
250
            smart_req.SmartServerResponse(
252
255
 
253
256
 
254
257
class TestSmartServerBzrDirRequestDestroyBranch(
255
 
    tests.TestCaseWithMemoryTransport):
 
258
        tests.TestCaseWithMemoryTransport):
256
259
    """Tests for BzrDir.destroy_branch."""
257
260
 
258
261
    def test_destroy_branch_default(self):
259
262
        """The default branch can be removed."""
260
263
        backing = self.get_transport()
261
 
        dir = self.make_branch('.').controldir
 
264
        self.make_branch('.')
262
265
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
263
266
        request = request_class(backing)
264
267
        expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
277
280
    def test_destroy_branch_missing(self):
278
281
        """An error is raised if the branch didn't exist."""
279
282
        backing = self.get_transport()
280
 
        dir = self.make_controldir('.', format="development-colo")
 
283
        self.make_controldir('.', format="development-colo")
281
284
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
282
285
        request = request_class(backing)
283
286
        expected = smart_req.FailedSmartServerResponse((b'nobranch',), None)
285
288
 
286
289
 
287
290
class TestSmartServerBzrDirRequestHasWorkingTree(
288
 
    tests.TestCaseWithTransport):
 
291
        tests.TestCaseWithTransport):
289
292
    """Tests for BzrDir.has_workingtree."""
290
293
 
291
294
    def test_has_workingtree_yes(self):
292
295
        """A working tree is present."""
293
296
        backing = self.get_transport()
294
 
        dir = self.make_branch_and_tree('.').controldir
 
297
        self.make_branch_and_tree('.')
295
298
        request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
296
299
        request = request_class(backing)
297
300
        expected = smart_req.SuccessfulSmartServerResponse((b'yes',))
300
303
    def test_has_workingtree_no(self):
301
304
        """A working tree is missing."""
302
305
        backing = self.get_transport()
303
 
        dir = self.make_controldir('.')
 
306
        self.make_controldir('.')
304
307
        request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
305
308
        request = request_class(backing)
306
309
        expected = smart_req.SuccessfulSmartServerResponse((b'no',))
308
311
 
309
312
 
310
313
class TestSmartServerBzrDirRequestDestroyRepository(
311
 
    tests.TestCaseWithMemoryTransport):
 
314
        tests.TestCaseWithMemoryTransport):
312
315
    """Tests for BzrDir.destroy_repository."""
313
316
 
314
317
    def test_destroy_repository_default(self):
315
318
        """The repository can be removed."""
316
319
        backing = self.get_transport()
317
 
        dir = self.make_repository('.').controldir
 
320
        self.make_repository('.')
318
321
        request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
319
322
        request = request_class(backing)
320
323
        expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
323
326
    def test_destroy_repository_missing(self):
324
327
        """An error is raised if the repository didn't exist."""
325
328
        backing = self.get_transport()
326
 
        dir = self.make_controldir('.')
 
329
        self.make_controldir('.')
327
330
        request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
328
331
        request = request_class(backing)
329
332
        expected = smart_req.FailedSmartServerResponse(
331
334
        self.assertEqual(expected, request.execute(b''))
332
335
 
333
336
 
334
 
class TestSmartServerRequestCreateRepository(tests.TestCaseWithMemoryTransport):
 
337
class TestSmartServerRequestCreateRepository(
 
338
        tests.TestCaseWithMemoryTransport):
335
339
    """Tests for BzrDir.create_repository."""
336
340
 
337
341
    def test_makes_repository(self):
352
356
    """Tests for BzrDir.find_repository."""
353
357
 
354
358
    def test_no_repository(self):
355
 
        """When there is no repository to be found, ('norepository', ) is returned."""
 
359
        """If no repository is found, ('norepository', ) is returned."""
356
360
        backing = self.get_transport()
357
361
        request = self._request_class(backing)
358
362
        self.make_controldir('.')
359
363
        self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
360
 
            request.execute(b''))
 
364
                         request.execute(b''))
361
365
 
362
366
    def test_nonshared_repository(self):
363
367
        # nonshared repositorys only allow 'find' to return a handle when the
369
373
        self.assertEqual(result, request.execute(b''))
370
374
        self.make_controldir('subdir')
371
375
        self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
372
 
            request.execute(b'subdir'))
 
376
                         request.execute(b'subdir'))
373
377
 
374
378
    def _make_repository_and_result(self, shared=False, format=None):
375
379
        """Convenience function to setup a repository.
390
394
        else:
391
395
            external = b'no'
392
396
        if (smart_dir.SmartServerRequestFindRepositoryV3 ==
393
 
            self._request_class):
 
397
                self._request_class):
394
398
            return smart_req.SuccessfulSmartServerResponse(
395
399
                (b'ok', b'', rich_root, subtrees, external,
396
400
                 repo._format.network_name()))
397
401
        elif (smart_dir.SmartServerRequestFindRepositoryV2 ==
398
 
            self._request_class):
 
402
              self._request_class):
399
403
            # All tests so far are on formats, and for non-external
400
404
            # repositories.
401
405
            return smart_req.SuccessfulSmartServerResponse(
405
409
                (b'ok', b'', rich_root, subtrees))
406
410
 
407
411
    def test_shared_repository(self):
408
 
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
 
412
        """for a shared repository, we get 'ok', 'relpath-to-repo'."""
409
413
        backing = self.get_transport()
410
414
        request = self._request_class(backing)
411
415
        result = self._make_repository_and_result(shared=True)
414
418
        result2 = smart_req.SmartServerResponse(
415
419
            result.args[0:1] + (b'..', ) + result.args[2:])
416
420
        self.assertEqual(result2,
417
 
            request.execute(b'subdir'))
 
421
                         request.execute(b'subdir'))
418
422
        self.make_controldir('subdir/deeper')
419
423
        result3 = smart_req.SmartServerResponse(
420
424
            result.args[0:1] + (b'../..', ) + result.args[2:])
421
425
        self.assertEqual(result3,
422
 
            request.execute(b'subdir/deeper'))
 
426
                         request.execute(b'subdir/deeper'))
423
427
 
424
428
    def test_rich_root_and_subtree_encoding(self):
425
429
        """Test for the format attributes for rich root and subtree support."""
444
448
 
445
449
 
446
450
class TestSmartServerBzrDirRequestGetConfigFile(
447
 
    tests.TestCaseWithMemoryTransport):
 
451
        tests.TestCaseWithMemoryTransport):
448
452
    """Tests for BzrDir.get_config_file."""
449
453
 
450
454
    def test_present(self):
459
463
 
460
464
    def test_missing(self):
461
465
        backing = self.get_transport()
462
 
        dir = self.make_controldir('.')
 
466
        self.make_controldir('.')
463
467
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
464
468
        request = request_class(backing)
465
469
        expected = smart_req.SuccessfulSmartServerResponse((), b'')
467
471
 
468
472
 
469
473
class TestSmartServerBzrDirRequestGetBranches(
470
 
    tests.TestCaseWithMemoryTransport):
 
474
        tests.TestCaseWithMemoryTransport):
471
475
    """Tests for BzrDir.get_branches."""
472
476
 
473
477
    def test_simple(self):
483
487
 
484
488
    def test_empty(self):
485
489
        backing = self.get_transport()
486
 
        dir = self.make_controldir('.')
 
490
        self.make_controldir('.')
487
491
        request_class = smart_dir.SmartServerBzrDirRequestGetBranches
488
492
        request = request_class(backing)
489
493
        local_result = bencode.bencode({})
492
496
        self.assertEqual(expected, request.execute(b''))
493
497
 
494
498
 
495
 
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
 
499
class TestSmartServerRequestInitializeBzrDir(
 
500
        tests.TestCaseWithMemoryTransport):
496
501
 
497
502
    def test_empty_dir(self):
498
503
        """Initializing an empty dir should succeed and do it."""
499
504
        backing = self.get_transport()
500
505
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
501
506
        self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
502
 
            request.execute(b''))
 
507
                         request.execute(b''))
503
508
        made_dir = controldir.ControlDir.open_from_transport(backing)
504
509
        # no branch, tree or repository is expected with the current
505
510
        # default formart.
512
517
        backing = self.get_transport()
513
518
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
514
519
        self.assertRaises(errors.NoSuchFile,
515
 
            request.execute, b'subdir')
 
520
                          request.execute, b'subdir')
516
521
 
517
522
    def test_initialized_dir(self):
518
523
        """Initializing an extant bzrdir should fail like the bzrdir api."""
520
525
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
521
526
        self.make_controldir('subdir')
522
527
        self.assertRaises(errors.AlreadyControlDirError,
523
 
            request.execute, b'subdir')
 
528
                          request.execute, b'subdir')
524
529
 
525
530
 
526
531
class TestSmartServerRequestBzrDirInitializeEx(
527
 
    tests.TestCaseWithMemoryTransport):
 
532
        tests.TestCaseWithMemoryTransport):
528
533
    """Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
529
534
 
530
535
    The main unit tests in test_bzrdir exercise the API comprehensively.
538
543
        self.assertEqual(
539
544
            smart_req.SmartServerResponse((b'', b'', b'', b'', b'', b'', name,
540
545
                                           b'False', b'', b'', b'')),
541
 
            request.execute(name, b'', b'True', b'False', b'False', b'', b'', b'', b'',
542
 
                            b'False'))
 
546
            request.execute(name, b'', b'True', b'False', b'False', b'', b'',
 
547
                            b'', b'', b'False'))
543
548
        made_dir = controldir.ControlDir.open_from_transport(backing)
544
549
        # no branch, tree or repository is expected with the current
545
550
        # default format.
553
558
        name = self.make_controldir('reference')._format.network_name()
554
559
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
555
560
        self.assertRaises(errors.NoSuchFile, request.execute, name,
556
 
            b'subdir/dir', b'False', b'False', b'False', b'', b'', b'', b'', b'False')
 
561
                          b'subdir/dir', b'False', b'False', b'False', b'',
 
562
                          b'', b'', b'', b'False')
557
563
 
558
564
    def test_initialized_dir(self):
559
565
        """Initializing an extant directory should fail like the bzrdir api."""
562
568
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
563
569
        self.make_controldir('subdir')
564
570
        self.assertRaises(errors.FileExists, request.execute, name, b'subdir',
565
 
            b'False', b'False', b'False', b'', b'', b'', b'', b'False')
 
571
                          b'False', b'False', b'False', b'', b'', b'', b'',
 
572
                          b'False')
566
573
 
567
574
 
568
575
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
571
578
        backing = self.get_transport()
572
579
        request = smart_dir.SmartServerRequestOpenBzrDir(backing)
573
580
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
574
 
            request.execute(b'does-not-exist'))
 
581
                         request.execute(b'does-not-exist'))
575
582
 
576
583
    def test_empty_directory(self):
577
584
        backing = self.get_transport()
578
585
        backing.mkdir('empty')
579
586
        request = smart_dir.SmartServerRequestOpenBzrDir(backing)
580
587
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
581
 
            request.execute(b'empty'))
 
588
                         request.execute(b'empty'))
582
589
 
583
590
    def test_outside_root_client_path(self):
584
591
        backing = self.get_transport()
585
 
        request = smart_dir.SmartServerRequestOpenBzrDir(backing,
586
 
            root_client_path='root')
 
592
        request = smart_dir.SmartServerRequestOpenBzrDir(
 
593
            backing, root_client_path='root')
587
594
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
588
 
            request.execute(b'not-root'))
 
595
                         request.execute(b'not-root'))
589
596
 
590
597
 
591
598
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
594
601
        backing = self.get_transport()
595
602
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
596
603
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
597
 
            request.execute(b'does-not-exist'))
 
604
                         request.execute(b'does-not-exist'))
598
605
 
599
606
    def test_empty_directory(self):
600
607
        backing = self.get_transport()
601
608
        backing.mkdir('empty')
602
609
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
603
610
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
604
 
            request.execute(b'empty'))
 
611
                         request.execute(b'empty'))
605
612
 
606
613
    def test_present_without_workingtree(self):
607
614
        backing = self.get_transport()
608
615
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
609
616
        self.make_controldir('.')
610
617
        self.assertEqual(smart_req.SmartServerResponse((b'yes', b'no')),
611
 
            request.execute(b''))
 
618
                         request.execute(b''))
612
619
 
613
620
    def test_outside_root_client_path(self):
614
621
        backing = self.get_transport()
615
 
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing,
616
 
            root_client_path='root')
 
622
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(
 
623
            backing, root_client_path='root')
617
624
        self.assertEqual(smart_req.SmartServerResponse((b'no',)),
618
 
            request.execute(b'not-root'))
 
625
                         request.execute(b'not-root'))
619
626
 
620
627
 
621
628
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
629
636
        bd.create_branch()
630
637
        bd.create_workingtree()
631
638
        self.assertEqual(smart_req.SmartServerResponse((b'yes', b'yes')),
632
 
            request.execute(b''))
 
639
                         request.execute(b''))
633
640
 
634
641
 
635
642
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
640
647
        request = smart_dir.SmartServerRequestOpenBranch(backing)
641
648
        self.make_controldir('.')
642
649
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
643
 
            request.execute(b''))
 
650
                         request.execute(b''))
644
651
 
645
652
    def test_branch(self):
646
653
        """When there is a branch, 'ok' is returned."""
648
655
        request = smart_dir.SmartServerRequestOpenBranch(backing)
649
656
        self.make_branch('.')
650
657
        self.assertEqual(smart_req.SmartServerResponse((b'ok', b'')),
651
 
            request.execute(b''))
 
658
                         request.execute(b''))
652
659
 
653
660
    def test_branch_reference(self):
654
661
        """When there is a branch reference, the reference URL is returned."""
661
668
            checkout.controldir).encode('utf-8')
662
669
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
663
670
        self.assertEqual(smart_req.SmartServerResponse((b'ok', reference_url)),
664
 
            request.execute(b'reference'))
 
671
                         request.execute(b'reference'))
665
672
 
666
673
    def test_notification_on_branch_from_repository(self):
667
674
        """When there is a repository, the error should return details."""
668
675
        backing = self.get_transport()
669
676
        request = smart_dir.SmartServerRequestOpenBranch(backing)
670
 
        repo = self.make_repository('.')
 
677
        self.make_repository('.')
671
678
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
672
 
            request.execute(b''))
 
679
                         request.execute(b''))
673
680
 
674
681
 
675
682
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
680
687
        self.make_controldir('.')
681
688
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
682
689
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
683
 
            request.execute(b''))
 
690
                         request.execute(b''))
684
691
 
685
692
    def test_branch(self):
686
693
        """When there is a branch, 'ok' is returned."""
688
695
        expected = self.make_branch('.')._format.network_name()
689
696
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
690
697
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
691
 
                (b'branch', expected)),
692
 
                request.execute(b''))
 
698
            (b'branch', expected)),
 
699
            request.execute(b''))
693
700
 
694
701
    def test_branch_reference(self):
695
702
        """When there is a branch reference, the reference URL is returned."""
702
709
            checkout.controldir).encode('utf-8')
703
710
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
704
711
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
705
 
                (b'ref', reference_url)),
706
 
                request.execute(b'reference'))
 
712
            (b'ref', reference_url)),
 
713
            request.execute(b'reference'))
707
714
 
708
715
    def test_stacked_branch(self):
709
716
        """Opening a stacked branch does not open the stacked-on branch."""
722
729
            request.teardown_jail()
723
730
        expected_format = feature._format.network_name()
724
731
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
725
 
                (b'branch', expected_format)),
726
 
                         response)
 
732
            (b'branch', expected_format)),
 
733
            response)
727
734
        self.assertLength(1, opened_branches)
728
735
 
729
736
    def test_notification_on_branch_from_repository(self):
730
737
        """When there is a repository, the error should return details."""
731
738
        backing = self.get_transport()
732
739
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
733
 
        repo = self.make_repository('.')
 
740
        self.make_repository('.')
734
741
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
735
 
            request.execute(b''))
 
742
                         request.execute(b''))
736
743
 
737
744
 
738
745
class TestSmartServerRequestOpenBranchV3(TestCaseWithChrootedTransport):
743
750
        self.make_controldir('.')
744
751
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
745
752
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
746
 
            request.execute(b''))
 
753
                         request.execute(b''))
747
754
 
748
755
    def test_branch(self):
749
756
        """When there is a branch, 'ok' is returned."""
751
758
        expected = self.make_branch('.')._format.network_name()
752
759
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
753
760
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
754
 
                (b'branch', expected)),
755
 
                         request.execute(b''))
 
761
            (b'branch', expected)),
 
762
            request.execute(b''))
756
763
 
757
764
    def test_branch_reference(self):
758
765
        """When there is a branch reference, the reference URL is returned."""
765
772
            checkout.controldir).encode('utf-8')
766
773
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
767
774
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
768
 
                (b'ref', reference_url)),
769
 
                         request.execute(b'reference'))
 
775
            (b'ref', reference_url)),
 
776
            request.execute(b'reference'))
770
777
 
771
778
    def test_stacked_branch(self):
772
779
        """Opening a stacked branch does not open the stacked-on branch."""
785
792
            request.teardown_jail()
786
793
        expected_format = feature._format.network_name()
787
794
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
788
 
                (b'branch', expected_format)),
789
 
                         response)
 
795
            (b'branch', expected_format)),
 
796
            response)
790
797
        self.assertLength(1, opened_branches)
791
798
 
792
799
    def test_notification_on_branch_from_repository(self):
793
800
        """When there is a repository, the error should return details."""
794
801
        backing = self.get_transport()
795
802
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
796
 
        repo = self.make_repository('.')
 
803
        self.make_repository('.')
797
804
        self.assertEqual(smart_req.SmartServerResponse(
798
 
                (b'nobranch', b'location is a repository')),
799
 
                         request.execute(b''))
 
805
            (b'nobranch', b'location is a repository')),
 
806
            request.execute(b''))
800
807
 
801
808
 
802
809
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
807
814
        request = smart_branch.SmartServerRequestRevisionHistory(backing)
808
815
        self.make_branch('.')
809
816
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b''),
810
 
            request.execute(b''))
 
817
                         request.execute(b''))
811
818
 
812
819
    def test_not_empty(self):
813
820
        """For a non-empty branch, the body is empty."""
832
839
        request = smart_branch.SmartServerBranchRequest(backing)
833
840
        self.make_controldir('.')
834
841
        self.assertRaises(errors.NotBranchError,
835
 
            request.execute, b'')
 
842
                          request.execute, b'')
836
843
 
837
844
    def test_branch_reference(self):
838
845
        """When there is a branch reference, NotBranchError is raised."""
839
846
        backing = self.get_transport()
840
847
        request = smart_branch.SmartServerBranchRequest(backing)
841
848
        branch = self.make_branch('branch')
842
 
        checkout = branch.create_checkout('reference', lightweight=True)
 
849
        branch.create_checkout('reference', lightweight=True)
843
850
        self.assertRaises(errors.NotBranchError,
844
 
            request.execute, b'checkout')
 
851
                          request.execute, b'checkout')
845
852
 
846
853
 
847
854
class TestSmartServerBranchRequestLastRevisionInfo(
848
 
    tests.TestCaseWithMemoryTransport):
 
855
        tests.TestCaseWithMemoryTransport):
849
856
 
850
857
    def test_empty(self):
851
858
        """For an empty branch, the result is ('ok', '0', b'null:')."""
852
859
        backing = self.get_transport()
853
 
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
 
860
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
 
861
            backing)
854
862
        self.make_branch('.')
855
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0', b'null:')),
 
863
        self.assertEqual(
 
864
            smart_req.SmartServerResponse((b'ok', b'0', b'null:')),
856
865
            request.execute(b''))
857
866
 
858
867
    def test_ghost(self):
859
868
        """For an empty branch, the result is ('ok', '0', b'null:')."""
860
869
        backing = self.get_transport()
861
 
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
 
870
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
 
871
            backing)
862
872
        branch = self.make_branch('.')
 
873
 
863
874
        def last_revision_info():
864
875
            raise errors.GhostRevisionsHaveNoRevno(b'revid1', b'revid2')
865
876
        self.overrideAttr(branch, 'last_revision_info', last_revision_info)
866
877
        self.assertRaises(errors.GhostRevisionsHaveNoRevno,
867
 
            request.do_with_branch, branch)
 
878
                          request.do_with_branch, branch)
868
879
 
869
880
    def test_not_empty(self):
870
881
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
871
882
        backing = self.get_transport()
872
 
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
 
883
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
 
884
            backing)
873
885
        tree = self.make_branch_and_memory_tree('.')
874
886
        tree.lock_write()
875
887
        tree.add('')
876
888
        rev_id_utf8 = u'\xc8'.encode('utf-8')
877
 
        r1 = tree.commit('1st commit')
878
 
        r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
 
889
        tree.commit('1st commit')
 
890
        tree.commit('2nd commit', rev_id=rev_id_utf8)
879
891
        tree.unlock()
880
892
        self.assertEqual(
881
893
            smart_req.SmartServerResponse((b'ok', b'2', rev_id_utf8)),
883
895
 
884
896
 
885
897
class TestSmartServerBranchRequestRevisionIdToRevno(
886
 
    tests.TestCaseWithMemoryTransport):
 
898
        tests.TestCaseWithMemoryTransport):
887
899
 
888
900
    def test_null(self):
889
901
        backing = self.get_transport()
891
903
            backing)
892
904
        self.make_branch('.')
893
905
        self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0')),
894
 
            request.execute(b'', b'null:'))
 
906
                         request.execute(b'', b'null:'))
895
907
 
896
908
    def test_simple(self):
897
909
        backing = self.get_transport()
910
922
        backing = self.get_transport()
911
923
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
912
924
            backing)
913
 
        branch = self.make_branch('.')
 
925
        self.make_branch('.')
914
926
        self.assertEqual(
915
927
            smart_req.FailedSmartServerResponse(
916
928
                (b'NoSuchRevision', b'idontexist')),
918
930
 
919
931
 
920
932
class TestSmartServerBranchRequestGetConfigFile(
921
 
    tests.TestCaseWithMemoryTransport):
 
933
        tests.TestCaseWithMemoryTransport):
922
934
 
923
935
    def test_default(self):
924
936
        """With no file, we get empty content."""
925
937
        backing = self.get_transport()
926
938
        request = smart_branch.SmartServerBranchGetConfigFile(backing)
927
 
        branch = self.make_branch('.')
 
939
        self.make_branch('.')
928
940
        # there should be no file by default
929
941
        content = b''
930
942
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), content),
931
 
            request.execute(b''))
 
943
                         request.execute(b''))
932
944
 
933
945
    def test_with_content(self):
934
946
        # SmartServerBranchGetConfigFile should return the content from
935
 
        # branch.control_files.get('branch.conf') for now - in the future it may
936
 
        # perform more complex processing.
 
947
        # branch.control_files.get('branch.conf') for now - in the future it
 
948
        # may perform more complex processing.
937
949
        backing = self.get_transport()
938
950
        request = smart_branch.SmartServerBranchGetConfigFile(backing)
939
951
        branch = self.make_branch('.')
940
952
        branch._transport.put_bytes('branch.conf', b'foo bar baz')
941
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'foo bar baz'),
 
953
        self.assertEqual(
 
954
            smart_req.SmartServerResponse((b'ok', ), b'foo bar baz'),
942
955
            request.execute(b''))
943
956
 
944
957
 
977
990
        branch_token, repo_token = self.get_lock_tokens(branch)
978
991
        config = branch._get_config()
979
992
        result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
980
 
            b'')
 
993
                                 b'')
981
994
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
982
995
        self.assertEqual('bar', config.get_option('foo'))
983
996
        # Cleanup
990
1003
        branch_token, repo_token = self.get_lock_tokens(branch)
991
1004
        config = branch._get_config()
992
1005
        result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
993
 
            b'gam')
 
1006
                                 b'gam')
994
1007
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
995
1008
        self.assertEqual('bar', config.get_option('foo', 'gam'))
996
1009
        # Cleanup
1015
1028
        branch_token, repo_token = self.get_lock_tokens(branch)
1016
1029
        config = branch._get_config()
1017
1030
        result = request.execute(b'', branch_token, repo_token,
1018
 
            self.encoded_value_dict, b'foo', b'')
 
1031
                                 self.encoded_value_dict, b'foo', b'')
1019
1032
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1020
1033
        self.assertEqual(self.value_dict, config.get_option('foo'))
1021
1034
        # Cleanup
1028
1041
        branch_token, repo_token = self.get_lock_tokens(branch)
1029
1042
        config = branch._get_config()
1030
1043
        result = request.execute(b'', branch_token, repo_token,
1031
 
            self.encoded_value_dict, b'foo', b'gam')
 
1044
                                 self.encoded_value_dict, b'foo', b'gam')
1032
1045
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1033
1046
        self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
1034
1047
        # Cleanup
1062
1075
        request = smart_branch.SmartServerBranchSetTagsBytes(
1063
1076
            self.get_transport())
1064
1077
        self.assertRaises(errors.TokenMismatch, request.execute,
1065
 
            b'base', b'wrong token', b'wrong token')
 
1078
                          b'base', b'wrong token', b'wrong token')
1066
1079
        # The request handler will keep processing the message parts, so even
1067
1080
        # if the request fails immediately do_chunk and do_end are still
1068
1081
        # called.
1071
1084
        base_branch.unlock()
1072
1085
 
1073
1086
 
1074
 
 
1075
1087
class SetLastRevisionTestBase(TestLockedBranch):
1076
1088
    """Base test case for verbs that implement set_last_revision."""
1077
1089
 
1111
1123
        """If the revision_id is not present, the verb returns NoSuchRevision.
1112
1124
        """
1113
1125
        revision_id = b'non-existent revision'
1114
 
        self.assertEqual(smart_req.FailedSmartServerResponse((b'NoSuchRevision',
1115
 
                                                              revision_id)),
1116
 
                         self.set_last_revision(revision_id, 1))
 
1126
        self.assertEqual(
 
1127
            smart_req.FailedSmartServerResponse(
 
1128
                (b'NoSuchRevision', revision_id)),
 
1129
            self.set_last_revision(revision_id, 1))
1117
1130
 
1118
1131
    def make_tree_with_two_commits(self):
1119
1132
        self.tree.lock_write()
1120
1133
        self.tree.add('')
1121
1134
        rev_id_utf8 = u'\xc8'.encode('utf-8')
1122
 
        r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
1123
 
        r2 = self.tree.commit('2nd commit', rev_id=b'rev-2')
 
1135
        self.tree.commit('1st commit', rev_id=rev_id_utf8)
 
1136
        self.tree.commit('2nd commit', rev_id=b'rev-2')
1124
1137
        self.tree.unlock()
1125
1138
 
1126
1139
    def test_branch_last_revision_info_is_updated(self):
1157
1170
        returns TipChangeRejected.
1158
1171
        """
1159
1172
        rejection_message = u'rejection message\N{INTERROBANG}'
 
1173
 
1160
1174
        def hook_that_rejects(params):
1161
1175
            raise errors.TipChangeRejected(rejection_message)
1162
1176
        _mod_branch.Branch.hooks.install_named_hook(
1208
1222
    def assertRequestSucceeds(self, revision_id, revno):
1209
1223
        response = self.set_last_revision(revision_id, revno)
1210
1224
        self.assertEqual(
1211
 
            smart_req.SuccessfulSmartServerResponse((b'ok', revno, revision_id)),
 
1225
            smart_req.SuccessfulSmartServerResponse(
 
1226
                (b'ok', revno, revision_id)),
1212
1227
            response)
1213
1228
 
1214
1229
    def test_branch_last_revision_info_rewind(self):
1249
1264
        """
1250
1265
        self.tree.lock_write()
1251
1266
        self.tree.add('')
1252
 
        r1 = self.tree.commit('1st commit')
 
1267
        self.tree.commit('1st commit')
1253
1268
        revno_1, revid_1 = self.tree.branch.last_revision_info()
1254
 
        r2 = self.tree.commit('2nd commit', rev_id=b'child-1')
 
1269
        self.tree.commit('2nd commit', rev_id=b'child-1')
1255
1270
        # Undo the second commit
1256
1271
        self.tree.branch.set_last_revision_info(revno_1, revid_1)
1257
1272
        self.tree.set_parent_ids([revid_1])
1258
1273
        # Make a new second commit, child-2.  child-2 has diverged from
1259
1274
        # child-1.
1260
 
        new_r2 = self.tree.commit('2nd commit', rev_id=b'child-2')
 
1275
        self.tree.commit('2nd commit', rev_id=b'child-2')
1261
1276
        self.tree.unlock()
1262
1277
 
1263
1278
    def test_not_allow_diverged(self):
1299
1314
            request.execute(b'base'))
1300
1315
 
1301
1316
    def test_nothing_to_break(self):
1302
 
        base_branch = self.make_branch('base')
 
1317
        self.make_branch('base')
1303
1318
        request = smart_branch.SmartServerBranchBreakLock(
1304
1319
            self.get_transport())
1305
1320
        self.assertEqual(
1310
1325
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1311
1326
 
1312
1327
    def test_get_parent_none(self):
1313
 
        base_branch = self.make_branch('base')
 
1328
        self.make_branch('base')
1314
1329
        request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1315
1330
        response = request.execute(b'base')
1316
1331
        self.assertEqual(
1361
1376
 
1362
1377
 
1363
1378
class TestSmartServerBranchRequestGetTagsBytes(
1364
 
    tests.TestCaseWithMemoryTransport):
 
1379
        tests.TestCaseWithMemoryTransport):
1365
1380
    # Only called when the branch format and tags match [yay factory
1366
1381
    # methods] so only need to test straight forward cases.
1367
1382
 
1368
1383
    def test_get_bytes(self):
1369
 
        base_branch = self.make_branch('base')
 
1384
        self.make_branch('base')
1370
1385
        request = smart_branch.SmartServerBranchGetTagsBytes(
1371
1386
            self.get_transport())
1372
1387
        response = request.execute(b'base')
1374
1389
            smart_req.SuccessfulSmartServerResponse((b'',)), response)
1375
1390
 
1376
1391
 
1377
 
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
 
1392
class TestSmartServerBranchRequestGetStackedOnURL(
 
1393
        tests.TestCaseWithMemoryTransport):
1378
1394
 
1379
1395
    def test_get_stacked_on_url(self):
1380
 
        base_branch = self.make_branch('base', format='1.6')
 
1396
        self.make_branch('base', format='1.6')
1381
1397
        stacked_branch = self.make_branch('stacked', format='1.6')
1382
1398
        # typically should be relative
1383
1399
        stacked_branch.set_stacked_on_url('../base')
1400
1416
        branch_nonce = branch.control_files._lock.peek().get('nonce')
1401
1417
        repository_nonce = repository.control_files._lock.peek().get('nonce')
1402
1418
        self.assertEqual(smart_req.SmartServerResponse(
1403
 
                (b'ok', branch_nonce, repository_nonce)),
1404
 
                         response)
 
1419
            (b'ok', branch_nonce, repository_nonce)),
 
1420
            response)
1405
1421
        # The branch (and associated repository) is now locked.  Verify that
1406
1422
        # with a new branch object.
1407
1423
        new_branch = repository.controldir.open_branch()
1455
1471
        branch.repository.leave_lock_in_place()
1456
1472
        branch.unlock()
1457
1473
        response = request.execute(b'',
1458
 
                                   branch_token+b'xxx', repo_token)
 
1474
                                   branch_token + b'xxx', repo_token)
1459
1475
        self.assertEqual(
1460
1476
            smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1461
1477
        # Cleanup
1485
1501
    def test_lock_write_on_readonly_transport(self):
1486
1502
        backing = self.get_readonly_transport()
1487
1503
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1488
 
        branch = self.make_branch('.')
 
1504
        self.make_branch('.')
1489
1505
        root = self.get_transport().clone('/')
1490
1506
        path = urlutils.relative_url(root.base, self.get_transport().base)
1491
1507
        response = request.execute(path.encode('utf-8'))
1545
1561
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
1546
1562
        backing = self.get_transport()
1547
1563
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1548
 
        branch = self.make_branch('.', format='knit')
 
1564
        self.make_branch('.', format='knit')
1549
1565
        response = request.execute(
1550
1566
            b'', b'branch token', b'repo token')
1551
1567
        self.assertEqual(
1583
1599
        self.make_repository('.', shared=True)
1584
1600
        self.make_controldir('subdir')
1585
1601
        self.assertRaises(errors.NoRepositoryPresent,
1586
 
            request.execute, b'subdir')
1587
 
 
1588
 
 
1589
 
class TestSmartServerRepositoryAddSignatureText(tests.TestCaseWithMemoryTransport):
 
1602
                          request.execute, b'subdir')
 
1603
 
 
1604
 
 
1605
class TestSmartServerRepositoryAddSignatureText(
 
1606
        tests.TestCaseWithMemoryTransport):
1590
1607
 
1591
1608
    def test_add_text(self):
1592
1609
        backing = self.get_transport()
1598
1615
        tree.commit("Message", rev_id=b'rev1')
1599
1616
        tree.branch.repository.start_write_group()
1600
1617
        write_group_tokens = tree.branch.repository.suspend_write_group()
1601
 
        self.assertEqual(None, request.execute(b'', write_token,
1602
 
            b'rev1', *[token.encode('utf-8') for token in write_group_tokens]))
 
1618
        self.assertEqual(
 
1619
            None, request.execute(
 
1620
                b'', write_token, b'rev1',
 
1621
                *[token.encode('utf-8') for token in write_group_tokens]))
1603
1622
        response = request.do_body(b'somesignature')
1604
1623
        self.assertTrue(response.is_successful())
1605
1624
        self.assertEqual(response.args[0], b'ok')
1606
 
        write_group_tokens = [token.decode('utf-8') for token in response.args[1:]]
 
1625
        write_group_tokens = [token.decode('utf-8')
 
1626
                              for token in response.args[1:]]
1607
1627
        tree.branch.repository.resume_write_group(write_group_tokens)
1608
1628
        tree.branch.repository.commit_write_group()
1609
1629
        tree.unlock()
1610
1630
        self.assertEqual(b"somesignature",
1611
 
            tree.branch.repository.get_signature_text(b"rev1"))
 
1631
                         tree.branch.repository.get_signature_text(b"rev1"))
1612
1632
 
1613
1633
 
1614
1634
class TestSmartServerRepositoryAllRevisionIds(
1615
 
    tests.TestCaseWithMemoryTransport):
 
1635
        tests.TestCaseWithMemoryTransport):
1616
1636
 
1617
1637
    def test_empty(self):
1618
1638
        """An empty body should be returned for an empty repository."""
1635
1655
        tree.unlock()
1636
1656
        self.assertIn(
1637
1657
            request.execute(b''),
1638
 
            [smart_req.SuccessfulSmartServerResponse((b"ok", ),
1639
 
                b"origineel\nnog-een-revisie"),
1640
 
             smart_req.SuccessfulSmartServerResponse((b"ok", ),
1641
 
                b"nog-een-revisie\norigineel")])
 
1658
            [smart_req.SuccessfulSmartServerResponse(
 
1659
                (b"ok", ), b"origineel\nnog-een-revisie"),
 
1660
             smart_req.SuccessfulSmartServerResponse(
 
1661
                 (b"ok", ), b"nog-een-revisie\norigineel")])
1642
1662
 
1643
1663
 
1644
1664
class TestSmartServerRepositoryBreakLock(tests.TestCaseWithMemoryTransport):
1655
1675
    def test_nothing_to_break(self):
1656
1676
        backing = self.get_transport()
1657
1677
        request = smart_repo.SmartServerRepositoryBreakLock(backing)
1658
 
        tree = self.make_branch_and_memory_tree('.')
 
1678
        self.make_branch_and_memory_tree('.')
1659
1679
        self.assertEqual(
1660
1680
            smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1661
1681
            request.execute(b''))
1667
1687
        # This tests that the wire encoding is actually bzipped
1668
1688
        backing = self.get_transport()
1669
1689
        request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1670
 
        tree = self.make_branch_and_memory_tree('.')
 
1690
        self.make_branch_and_memory_tree('.')
1671
1691
 
1672
1692
        self.assertEqual(None,
1673
 
            request.execute(b'', b'missing-id'))
 
1693
                         request.execute(b'', b'missing-id'))
1674
1694
        # Note that it returns a body that is bzipped.
1675
1695
        self.assertEqual(
1676
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ), bz2.compress(b'')),
 
1696
            smart_req.SuccessfulSmartServerResponse(
 
1697
                (b'ok', ), bz2.compress(b'')),
1677
1698
            request.do_body(b'\n\n0\n'))
1678
1699
 
1679
1700
    def test_trivial_include_missing(self):
1680
1701
        backing = self.get_transport()
1681
1702
        request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1682
 
        tree = self.make_branch_and_memory_tree('.')
 
1703
        self.make_branch_and_memory_tree('.')
1683
1704
 
1684
 
        self.assertEqual(None,
1685
 
            request.execute(b'', b'missing-id', b'include-missing:'))
1686
 
        self.assertEqual(
1687
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ),
1688
 
                bz2.compress(b'missing:missing-id')),
 
1705
        self.assertEqual(
 
1706
            None, request.execute(b'', b'missing-id', b'include-missing:'))
 
1707
        self.assertEqual(
 
1708
            smart_req.SuccessfulSmartServerResponse(
 
1709
                (b'ok', ), bz2.compress(b'missing:missing-id')),
1689
1710
            request.do_body(b'\n\n0\n'))
1690
1711
 
1691
1712
 
1692
1713
class TestSmartServerRepositoryGetRevisionGraph(
1693
 
    tests.TestCaseWithMemoryTransport):
 
1714
        tests.TestCaseWithMemoryTransport):
1694
1715
 
1695
1716
    def test_none_argument(self):
1696
1717
        backing = self.get_transport()
1709
1730
        response.body = b'\n'.join(sorted(response.body.split(b'\n')))
1710
1731
 
1711
1732
        self.assertEqual(
1712
 
            smart_req.SmartServerResponse((b'ok', ), b'\n'.join(lines)), response)
 
1733
            smart_req.SmartServerResponse((b'ok', ), b'\n'.join(lines)),
 
1734
            response)
1713
1735
 
1714
1736
    def test_specific_revision_argument(self):
1715
1737
        backing = self.get_transport()
1718
1740
        tree.lock_write()
1719
1741
        tree.add('')
1720
1742
        rev_id_utf8 = u'\xc9'.encode('utf-8')
1721
 
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
1722
 
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
 
1743
        tree.commit('1st commit', rev_id=rev_id_utf8)
 
1744
        tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1723
1745
        tree.unlock()
1724
1746
 
1725
1747
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), rev_id_utf8),
1726
 
            request.execute(b'', rev_id_utf8))
 
1748
                         request.execute(b'', rev_id_utf8))
1727
1749
 
1728
1750
    def test_no_such_revision(self):
1729
1751
        backing = self.get_transport()
1731
1753
        tree = self.make_branch_and_memory_tree('.')
1732
1754
        tree.lock_write()
1733
1755
        tree.add('')
1734
 
        r1 = tree.commit('1st commit')
 
1756
        tree.commit('1st commit')
1735
1757
        tree.unlock()
1736
1758
 
1737
1759
        # Note that it still returns body (of zero bytes).
1738
1760
        self.assertEqual(smart_req.SmartServerResponse(
1739
 
                (b'nosuchrevision', b'missingrevision', ), b''),
1740
 
                         request.execute(b'', b'missingrevision'))
 
1761
            (b'nosuchrevision', b'missingrevision', ), b''),
 
1762
            request.execute(b'', b'missingrevision'))
1741
1763
 
1742
1764
 
1743
1765
class TestSmartServerRepositoryGetRevIdForRevno(
1744
 
    tests.TestCaseWithMemoryTransport):
 
1766
        tests.TestCaseWithMemoryTransport):
1745
1767
 
1746
1768
    def test_revno_found(self):
1747
1769
        backing = self.get_transport()
1756
1778
        tree.unlock()
1757
1779
 
1758
1780
        self.assertEqual(smart_req.SmartServerResponse((b'ok', rev1_id_utf8)),
1759
 
            request.execute(b'', 1, (2, rev2_id_utf8)))
 
1781
                         request.execute(b'', 1, (2, rev2_id_utf8)))
1760
1782
 
1761
1783
    def test_known_revid_missing(self):
1762
1784
        backing = self.get_transport()
1763
1785
        request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1764
 
        repo = self.make_repository('.')
 
1786
        self.make_repository('.')
1765
1787
        self.assertEqual(
1766
1788
            smart_req.FailedSmartServerResponse((b'nosuchrevision', b'ghost')),
1767
1789
            request.execute(b'', 1, (2, b'ghost')))
1772
1794
        parent = self.make_branch_and_memory_tree('parent', format='1.9')
1773
1795
        parent.lock_write()
1774
1796
        parent.add([''], [b'TREE_ROOT'])
1775
 
        r1 = parent.commit(message='first commit')
 
1797
        parent.commit(message='first commit')
1776
1798
        r2 = parent.commit(message='second commit')
1777
1799
        parent.unlock()
1778
1800
        local = self.make_branch_and_memory_tree('local', format='1.9')
1787
1809
 
1788
1810
 
1789
1811
class TestSmartServerRepositoryIterRevisions(
1790
 
    tests.TestCaseWithMemoryTransport):
 
1812
        tests.TestCaseWithMemoryTransport):
1791
1813
 
1792
1814
    def test_basic(self):
1793
1815
        backing = self.get_transport()
1807
1829
 
1808
1830
        self.addCleanup(tree.branch.lock_read().unlock)
1809
1831
        entries = [zlib.compress(record.get_bytes_as("fulltext")) for record in
1810
 
            tree.branch.repository.revisions.get_record_stream(
 
1832
                   tree.branch.repository.revisions.get_record_stream(
1811
1833
            [(b"rev1", ), (b"rev2", )], "unordered", True)]
1812
1834
 
1813
1835
        contents = b"".join(response.body_stream)
1818
1840
    def test_missing(self):
1819
1841
        backing = self.get_transport()
1820
1842
        request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1821
 
        tree = self.make_branch_and_memory_tree('.', format='2a')
 
1843
        self.make_branch_and_memory_tree('.', format='2a')
1822
1844
 
1823
1845
        self.assertIs(None, request.execute(b''))
1824
1846
        response = request.do_body(b"rev1\nrev2")
1892
1914
        request = smart_repo.SmartServerRequestHasRevision(backing)
1893
1915
        self.make_repository('.')
1894
1916
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1895
 
            request.execute(b'', b'revid'))
 
1917
                         request.execute(b'', b'revid'))
1896
1918
 
1897
1919
    def test_present_revision(self):
1898
1920
        """For a present revision, ('yes', ) is returned."""
1902
1924
        tree.lock_write()
1903
1925
        tree.add('')
1904
1926
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1905
 
        r1 = tree.commit('a commit', rev_id=rev_id_utf8)
 
1927
        tree.commit('a commit', rev_id=rev_id_utf8)
1906
1928
        tree.unlock()
1907
1929
        self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
1908
1930
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1909
 
            request.execute(b'', rev_id_utf8))
 
1931
                         request.execute(b'', rev_id_utf8))
1910
1932
 
1911
1933
 
1912
1934
class TestSmartServerRepositoryIterFilesBytes(tests.TestCaseWithTransport):
1924
1946
        self.assertTrue(response.is_successful())
1925
1947
        self.assertEqual(response.args, (b"ok", ))
1926
1948
        self.assertEqual(b"".join(response.body_stream),
1927
 
            b"ok\x000\n" + zlib.compress(b"somecontents"))
 
1949
                         b"ok\x000\n" + zlib.compress(b"somecontents"))
1928
1950
 
1929
1951
    def test_missing(self):
1930
1952
        backing = self.get_transport()
1936
1958
        self.assertTrue(response.is_successful())
1937
1959
        self.assertEqual(response.args, (b"ok", ))
1938
1960
        self.assertEqual(b"".join(response.body_stream),
1939
 
            b"absent\x00thefileid\x00revision\x000\n")
 
1961
                         b"absent\x00thefileid\x00revision\x000\n")
1940
1962
 
1941
1963
 
1942
1964
class TestSmartServerRequestHasSignatureForRevisionId(
1961
1983
        tree = self.make_branch_and_memory_tree('.')
1962
1984
        tree.lock_write()
1963
1985
        tree.add('')
1964
 
        r1 = tree.commit('a commit', rev_id=b'A')
 
1986
        tree.commit('a commit', rev_id=b'A')
1965
1987
        tree.unlock()
1966
1988
        self.assertTrue(tree.branch.repository.has_revision(b'A'))
1967
1989
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1968
 
            request.execute(b'', b'A'))
 
1990
                         request.execute(b'', b'A'))
1969
1991
 
1970
1992
    def test_present_signature(self):
1971
1993
        """For a present signature, ('yes', ) is returned."""
1976
1998
        tree = self.make_branch_and_memory_tree('.')
1977
1999
        tree.lock_write()
1978
2000
        tree.add('')
1979
 
        r1 = tree.commit('a commit', rev_id=b'A')
 
2001
        tree.commit('a commit', rev_id=b'A')
1980
2002
        tree.branch.repository.start_write_group()
1981
2003
        tree.branch.repository.sign_revision(b'A', strategy)
1982
2004
        tree.branch.repository.commit_write_group()
1983
2005
        tree.unlock()
1984
2006
        self.assertTrue(tree.branch.repository.has_revision(b'A'))
1985
2007
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1986
 
            request.execute(b'', b'A'))
 
2008
                         request.execute(b'', b'A'))
1987
2009
 
1988
2010
 
1989
2011
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
1993
2015
        backing = self.get_transport()
1994
2016
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
1995
2017
        repository = self.make_repository('.')
1996
 
        stats = repository.gather_stats()
 
2018
        repository.gather_stats()
1997
2019
        expected_body = b'revisions: 0\n'
1998
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
1999
 
                         request.execute(b'', b'', b'no'))
 
2020
        self.assertEqual(
 
2021
            smart_req.SmartServerResponse((b'ok', ), expected_body),
 
2022
            request.execute(b'', b'', b'no'))
2000
2023
 
2001
2024
    def test_revid_with_committers(self):
2002
2025
        """For a revid we get more infos."""
2012
2035
                    rev_id=rev_id_utf8)
2013
2036
        tree.unlock()
2014
2037
 
2015
 
        stats = tree.branch.repository.gather_stats()
 
2038
        tree.branch.repository.gather_stats()
2016
2039
        expected_body = (b'firstrev: 123456.200 3600\n'
2017
2040
                         b'latestrev: 654321.400 0\n'
2018
2041
                         b'revisions: 2\n')
2019
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
2020
 
                         request.execute(b'', rev_id_utf8, b'no'))
 
2042
        self.assertEqual(
 
2043
            smart_req.SmartServerResponse((b'ok', ), expected_body),
 
2044
            request.execute(b'', rev_id_utf8, b'no'))
2021
2045
 
2022
2046
    def test_not_empty_repository_with_committers(self):
2023
2047
        """For a revid and requesting committers we get the whole thing."""
2033
2057
        tree.commit('a commit', timestamp=654321.4, timezone=0,
2034
2058
                    committer='bar', rev_id=rev_id_utf8)
2035
2059
        tree.unlock()
2036
 
        stats = tree.branch.repository.gather_stats()
 
2060
        tree.branch.repository.gather_stats()
2037
2061
 
2038
2062
        expected_body = (b'committers: 2\n'
2039
2063
                         b'firstrev: 123456.200 3600\n'
2040
2064
                         b'latestrev: 654321.400 0\n'
2041
2065
                         b'revisions: 2\n')
2042
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
2043
 
                         request.execute(b'',
2044
 
                                         rev_id_utf8, b'yes'))
 
2066
        self.assertEqual(
 
2067
            smart_req.SmartServerResponse((b'ok', ), expected_body),
 
2068
            request.execute(b'', rev_id_utf8, b'yes'))
2045
2069
 
2046
2070
    def test_unknown_revid(self):
2047
2071
        """An unknown revision id causes a 'nosuchrevision' error."""
2048
2072
        backing = self.get_transport()
2049
2073
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
2050
 
        repository = self.make_repository('.')
2051
 
        expected_body = b'revisions: 0\n'
 
2074
        self.make_repository('.')
2052
2075
        self.assertEqual(
2053
2076
            smart_req.FailedSmartServerResponse(
2054
2077
                (b'nosuchrevision', b'mia'), None),
2063
2086
        request = smart_repo.SmartServerRepositoryIsShared(backing)
2064
2087
        self.make_repository('.', shared=True)
2065
2088
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2066
 
            request.execute(b'', ))
 
2089
                         request.execute(b'', ))
2067
2090
 
2068
2091
    def test_is_not_shared(self):
2069
2092
        """For a shared repository, ('no', ) is returned."""
2071
2094
        request = smart_repo.SmartServerRepositoryIsShared(backing)
2072
2095
        self.make_repository('.', shared=False)
2073
2096
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2074
 
            request.execute(b'', ))
 
2097
                         request.execute(b'', ))
2075
2098
 
2076
2099
 
2077
2100
class TestSmartServerRepositoryGetRevisionSignatureText(
2108
2131
        r = self.make_repository('.')
2109
2132
        r.set_make_working_trees(True)
2110
2133
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2111
 
            request.execute(b'', ))
 
2134
                         request.execute(b'', ))
2112
2135
 
2113
2136
    def test_is_not_shared(self):
2114
2137
        """For a repository with working trees, ('no', ) is returned."""
2117
2140
        r = self.make_repository('.')
2118
2141
        r.set_make_working_trees(False)
2119
2142
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2120
 
            request.execute(b'', ))
 
2143
                         request.execute(b'', ))
2121
2144
 
2122
2145
 
2123
2146
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
2128
2151
        repository = self.make_repository('.', format='knit')
2129
2152
        response = request.execute(b'')
2130
2153
        nonce = repository.control_files._lock.peek().get('nonce')
2131
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', nonce)), response)
 
2154
        self.assertEqual(smart_req.SmartServerResponse(
 
2155
            (b'ok', nonce)), response)
2132
2156
        # The repository is now locked.  Verify that with a new repository
2133
2157
        # object.
2134
2158
        new_repo = repository.controldir.open_repository()
2155
2179
    def test_lock_write_on_readonly_transport(self):
2156
2180
        backing = self.get_readonly_transport()
2157
2181
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
2158
 
        repository = self.make_repository('.', format='knit')
 
2182
        self.make_repository('.', format='knit')
2159
2183
        response = request.execute(b'')
2160
2184
        self.assertFalse(response.is_successful())
2161
2185
        self.assertEqual(b'LockFailed', response.args[0])
2203
2227
        request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2204
2228
            backing)
2205
2229
        repository = self.make_repository('.', format='knit')
2206
 
        lock_token = repository.lock_write().repository_token
2207
 
        self.assertRaises(
2208
 
            errors.TokenMismatch, request.execute, b'', b'', b'wrong-token')
2209
 
        repository.unlock()
 
2230
        with repository.lock_write():
 
2231
            self.assertRaises(
 
2232
                errors.TokenMismatch, request.execute, b'', b'',
 
2233
                b'wrong-token')
2210
2234
 
2211
2235
 
2212
2236
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
2230
2254
    def test_unlock_on_unlocked_repo(self):
2231
2255
        backing = self.get_transport()
2232
2256
        request = smart_repo.SmartServerRepositoryUnlock(backing)
2233
 
        repository = self.make_repository('.', format='knit')
 
2257
        self.make_repository('.', format='knit')
2234
2258
        response = request.execute(b'', b'some token')
2235
2259
        self.assertEqual(
2236
2260
            smart_req.SmartServerResponse((b'TokenMismatch',)), response)
2237
2261
 
2238
2262
 
2239
2263
class TestSmartServerRepositoryGetPhysicalLockStatus(
2240
 
    tests.TestCaseWithTransport):
 
2264
        tests.TestCaseWithTransport):
2241
2265
 
2242
2266
    def test_with_write_lock(self):
2243
2267
        backing = self.get_transport()
2252
2276
        request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2253
2277
        request = request_class(backing)
2254
2278
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((expected,)),
2255
 
            request.execute(b'', ))
 
2279
                         request.execute(b'', ))
2256
2280
 
2257
2281
    def test_without_write_lock(self):
2258
2282
        backing = self.get_transport()
2261
2285
        request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2262
2286
        request = request_class(backing)
2263
2287
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'no',)),
2264
 
            request.execute(b'', ))
 
2288
                         request.execute(b'', ))
2265
2289
 
2266
2290
 
2267
2291
class TestSmartServerRepositoryReconcile(tests.TestCaseWithTransport):
2275
2299
        request = request_class(backing)
2276
2300
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
2277
2301
            (b'ok', ),
2278
 
             b'garbage_inventories: 0\n'
2279
 
             b'inconsistent_parents: 0\n'),
 
2302
            b'garbage_inventories: 0\n'
 
2303
            b'inconsistent_parents: 0\n'),
2280
2304
            request.execute(b'', token))
2281
2305
 
2282
2306
 
2298
2322
 
2299
2323
 
2300
2324
class TestSmartServerRepositorySetMakeWorkingTrees(
2301
 
    tests.TestCaseWithMemoryTransport):
 
2325
        tests.TestCaseWithMemoryTransport):
2302
2326
 
2303
2327
    def test_set_false(self):
2304
2328
        backing = self.get_transport()
2307
2331
        request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2308
2332
        request = request_class(backing)
2309
2333
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2310
 
            request.execute(b'', b'False'))
 
2334
                         request.execute(b'', b'False'))
2311
2335
        repo = repo.controldir.open_repository()
2312
2336
        self.assertFalse(repo.make_working_trees())
2313
2337
 
2318
2342
        request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2319
2343
        request = request_class(backing)
2320
2344
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2321
 
            request.execute(b'', b'True'))
 
2345
                         request.execute(b'', b'True'))
2322
2346
        repo = repo.controldir.open_repository()
2323
2347
        self.assertTrue(repo.make_working_trees())
2324
2348
 
2325
2349
 
2326
2350
class TestSmartServerRepositoryGetSerializerFormat(
2327
 
    tests.TestCaseWithMemoryTransport):
 
2351
        tests.TestCaseWithMemoryTransport):
2328
2352
 
2329
2353
    def test_get_serializer_format(self):
2330
2354
        backing = self.get_transport()
2337
2361
 
2338
2362
 
2339
2363
class TestSmartServerRepositoryWriteGroup(
2340
 
    tests.TestCaseWithMemoryTransport):
 
2364
        tests.TestCaseWithMemoryTransport):
2341
2365
 
2342
2366
    def test_start_write_group(self):
2343
2367
        backing = self.get_transport()
2347
2371
        request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2348
2372
        request = request_class(backing)
2349
2373
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok', [])),
2350
 
            request.execute(b'', lock_token))
 
2374
                         request.execute(b'', lock_token))
2351
2375
 
2352
2376
    def test_start_write_group_unsuspendable(self):
2353
2377
        backing = self.get_transport()
2370
2394
        request_class = smart_repo.SmartServerRepositoryCommitWriteGroup
2371
2395
        request = request_class(backing)
2372
2396
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2373
 
            request.execute(b'', lock_token, tokens))
 
2397
                         request.execute(b'', lock_token, tokens))
2374
2398
 
2375
2399
    def test_abort_write_group(self):
2376
2400
        backing = self.get_transport()
2382
2406
        request_class = smart_repo.SmartServerRepositoryAbortWriteGroup
2383
2407
        request = request_class(backing)
2384
2408
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2385
 
            request.execute(b'', lock_token, tokens))
 
2409
                         request.execute(b'', lock_token, tokens))
2386
2410
 
2387
2411
    def test_check_write_group(self):
2388
2412
        backing = self.get_transport()
2394
2418
        request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2395
2419
        request = request_class(backing)
2396
2420
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2397
 
            request.execute(b'', lock_token, tokens))
 
2421
                         request.execute(b'', lock_token, tokens))
2398
2422
 
2399
2423
    def test_check_write_group_invalid(self):
2400
2424
        backing = self.get_transport()
2470
2494
        request = vfs.GetRequest(backing)
2471
2495
        backing.put_bytes_non_atomic(filename_escaped, b'contents')
2472
2496
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'contents'),
2473
 
            request.execute(filename_escaped.encode('ascii')))
 
2497
                         request.execute(filename_escaped.encode('ascii')))
2474
2498
 
2475
2499
 
2476
2500
class TestHandlers(tests.TestCase):
2492
2516
    def test_registered_methods(self):
2493
2517
        """Test that known methods are registered to the correct object."""
2494
2518
        self.assertHandlerEqual(b'Branch.break_lock',
2495
 
            smart_branch.SmartServerBranchBreakLock)
 
2519
                                smart_branch.SmartServerBranchBreakLock)
2496
2520
        self.assertHandlerEqual(b'Branch.get_config_file',
2497
 
            smart_branch.SmartServerBranchGetConfigFile)
 
2521
                                smart_branch.SmartServerBranchGetConfigFile)
2498
2522
        self.assertHandlerEqual(b'Branch.put_config_file',
2499
 
            smart_branch.SmartServerBranchPutConfigFile)
 
2523
                                smart_branch.SmartServerBranchPutConfigFile)
2500
2524
        self.assertHandlerEqual(b'Branch.get_parent',
2501
 
            smart_branch.SmartServerBranchGetParent)
 
2525
                                smart_branch.SmartServerBranchGetParent)
2502
2526
        self.assertHandlerEqual(b'Branch.get_physical_lock_status',
2503
 
            smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
 
2527
                                smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
2504
2528
        self.assertHandlerEqual(b'Branch.get_tags_bytes',
2505
 
            smart_branch.SmartServerBranchGetTagsBytes)
 
2529
                                smart_branch.SmartServerBranchGetTagsBytes)
2506
2530
        self.assertHandlerEqual(b'Branch.lock_write',
2507
 
            smart_branch.SmartServerBranchRequestLockWrite)
 
2531
                                smart_branch.SmartServerBranchRequestLockWrite)
2508
2532
        self.assertHandlerEqual(b'Branch.last_revision_info',
2509
 
            smart_branch.SmartServerBranchRequestLastRevisionInfo)
 
2533
                                smart_branch.SmartServerBranchRequestLastRevisionInfo)
2510
2534
        self.assertHandlerEqual(b'Branch.revision_history',
2511
 
            smart_branch.SmartServerRequestRevisionHistory)
 
2535
                                smart_branch.SmartServerRequestRevisionHistory)
2512
2536
        self.assertHandlerEqual(b'Branch.revision_id_to_revno',
2513
 
            smart_branch.SmartServerBranchRequestRevisionIdToRevno)
 
2537
                                smart_branch.SmartServerBranchRequestRevisionIdToRevno)
2514
2538
        self.assertHandlerEqual(b'Branch.set_config_option',
2515
 
            smart_branch.SmartServerBranchRequestSetConfigOption)
 
2539
                                smart_branch.SmartServerBranchRequestSetConfigOption)
2516
2540
        self.assertHandlerEqual(b'Branch.set_last_revision',
2517
 
            smart_branch.SmartServerBranchRequestSetLastRevision)
 
2541
                                smart_branch.SmartServerBranchRequestSetLastRevision)
2518
2542
        self.assertHandlerEqual(b'Branch.set_last_revision_info',
2519
 
            smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
 
2543
                                smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
2520
2544
        self.assertHandlerEqual(b'Branch.set_last_revision_ex',
2521
 
            smart_branch.SmartServerBranchRequestSetLastRevisionEx)
 
2545
                                smart_branch.SmartServerBranchRequestSetLastRevisionEx)
2522
2546
        self.assertHandlerEqual(b'Branch.set_parent_location',
2523
 
            smart_branch.SmartServerBranchRequestSetParentLocation)
 
2547
                                smart_branch.SmartServerBranchRequestSetParentLocation)
2524
2548
        self.assertHandlerEqual(b'Branch.unlock',
2525
 
            smart_branch.SmartServerBranchRequestUnlock)
 
2549
                                smart_branch.SmartServerBranchRequestUnlock)
2526
2550
        self.assertHandlerEqual(b'BzrDir.destroy_branch',
2527
 
            smart_dir.SmartServerBzrDirRequestDestroyBranch)
 
2551
                                smart_dir.SmartServerBzrDirRequestDestroyBranch)
2528
2552
        self.assertHandlerEqual(b'BzrDir.find_repository',
2529
 
            smart_dir.SmartServerRequestFindRepositoryV1)
 
2553
                                smart_dir.SmartServerRequestFindRepositoryV1)
2530
2554
        self.assertHandlerEqual(b'BzrDir.find_repositoryV2',
2531
 
            smart_dir.SmartServerRequestFindRepositoryV2)
 
2555
                                smart_dir.SmartServerRequestFindRepositoryV2)
2532
2556
        self.assertHandlerEqual(b'BzrDirFormat.initialize',
2533
 
            smart_dir.SmartServerRequestInitializeBzrDir)
 
2557
                                smart_dir.SmartServerRequestInitializeBzrDir)
2534
2558
        self.assertHandlerEqual(b'BzrDirFormat.initialize_ex_1.16',
2535
 
            smart_dir.SmartServerRequestBzrDirInitializeEx)
 
2559
                                smart_dir.SmartServerRequestBzrDirInitializeEx)
2536
2560
        self.assertHandlerEqual(b'BzrDir.checkout_metadir',
2537
 
            smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
 
2561
                                smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
2538
2562
        self.assertHandlerEqual(b'BzrDir.cloning_metadir',
2539
 
            smart_dir.SmartServerBzrDirRequestCloningMetaDir)
 
2563
                                smart_dir.SmartServerBzrDirRequestCloningMetaDir)
2540
2564
        self.assertHandlerEqual(b'BzrDir.get_branches',
2541
 
            smart_dir.SmartServerBzrDirRequestGetBranches)
 
2565
                                smart_dir.SmartServerBzrDirRequestGetBranches)
2542
2566
        self.assertHandlerEqual(b'BzrDir.get_config_file',
2543
 
            smart_dir.SmartServerBzrDirRequestConfigFile)
 
2567
                                smart_dir.SmartServerBzrDirRequestConfigFile)
2544
2568
        self.assertHandlerEqual(b'BzrDir.open_branch',
2545
 
            smart_dir.SmartServerRequestOpenBranch)
 
2569
                                smart_dir.SmartServerRequestOpenBranch)
2546
2570
        self.assertHandlerEqual(b'BzrDir.open_branchV2',
2547
 
            smart_dir.SmartServerRequestOpenBranchV2)
 
2571
                                smart_dir.SmartServerRequestOpenBranchV2)
2548
2572
        self.assertHandlerEqual(b'BzrDir.open_branchV3',
2549
 
            smart_dir.SmartServerRequestOpenBranchV3)
 
2573
                                smart_dir.SmartServerRequestOpenBranchV3)
2550
2574
        self.assertHandlerEqual(b'PackRepository.autopack',
2551
 
            smart_packrepo.SmartServerPackRepositoryAutopack)
 
2575
                                smart_packrepo.SmartServerPackRepositoryAutopack)
2552
2576
        self.assertHandlerEqual(b'Repository.add_signature_text',
2553
 
            smart_repo.SmartServerRepositoryAddSignatureText)
 
2577
                                smart_repo.SmartServerRepositoryAddSignatureText)
2554
2578
        self.assertHandlerEqual(b'Repository.all_revision_ids',
2555
 
            smart_repo.SmartServerRepositoryAllRevisionIds)
 
2579
                                smart_repo.SmartServerRepositoryAllRevisionIds)
2556
2580
        self.assertHandlerEqual(b'Repository.break_lock',
2557
 
            smart_repo.SmartServerRepositoryBreakLock)
 
2581
                                smart_repo.SmartServerRepositoryBreakLock)
2558
2582
        self.assertHandlerEqual(b'Repository.gather_stats',
2559
 
            smart_repo.SmartServerRepositoryGatherStats)
 
2583
                                smart_repo.SmartServerRepositoryGatherStats)
2560
2584
        self.assertHandlerEqual(b'Repository.get_parent_map',
2561
 
            smart_repo.SmartServerRepositoryGetParentMap)
 
2585
                                smart_repo.SmartServerRepositoryGetParentMap)
2562
2586
        self.assertHandlerEqual(b'Repository.get_physical_lock_status',
2563
 
            smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
 
2587
                                smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
2564
2588
        self.assertHandlerEqual(b'Repository.get_rev_id_for_revno',
2565
 
            smart_repo.SmartServerRepositoryGetRevIdForRevno)
 
2589
                                smart_repo.SmartServerRepositoryGetRevIdForRevno)
2566
2590
        self.assertHandlerEqual(b'Repository.get_revision_graph',
2567
 
            smart_repo.SmartServerRepositoryGetRevisionGraph)
 
2591
                                smart_repo.SmartServerRepositoryGetRevisionGraph)
2568
2592
        self.assertHandlerEqual(b'Repository.get_revision_signature_text',
2569
 
            smart_repo.SmartServerRepositoryGetRevisionSignatureText)
 
2593
                                smart_repo.SmartServerRepositoryGetRevisionSignatureText)
2570
2594
        self.assertHandlerEqual(b'Repository.get_stream',
2571
 
            smart_repo.SmartServerRepositoryGetStream)
 
2595
                                smart_repo.SmartServerRepositoryGetStream)
2572
2596
        self.assertHandlerEqual(b'Repository.get_stream_1.19',
2573
 
            smart_repo.SmartServerRepositoryGetStream_1_19)
 
2597
                                smart_repo.SmartServerRepositoryGetStream_1_19)
2574
2598
        self.assertHandlerEqual(b'Repository.iter_revisions',
2575
 
            smart_repo.SmartServerRepositoryIterRevisions)
 
2599
                                smart_repo.SmartServerRepositoryIterRevisions)
2576
2600
        self.assertHandlerEqual(b'Repository.has_revision',
2577
 
            smart_repo.SmartServerRequestHasRevision)
 
2601
                                smart_repo.SmartServerRequestHasRevision)
2578
2602
        self.assertHandlerEqual(b'Repository.insert_stream',
2579
 
            smart_repo.SmartServerRepositoryInsertStream)
 
2603
                                smart_repo.SmartServerRepositoryInsertStream)
2580
2604
        self.assertHandlerEqual(b'Repository.insert_stream_locked',
2581
 
            smart_repo.SmartServerRepositoryInsertStreamLocked)
 
2605
                                smart_repo.SmartServerRepositoryInsertStreamLocked)
2582
2606
        self.assertHandlerEqual(b'Repository.is_shared',
2583
 
            smart_repo.SmartServerRepositoryIsShared)
 
2607
                                smart_repo.SmartServerRepositoryIsShared)
2584
2608
        self.assertHandlerEqual(b'Repository.iter_files_bytes',
2585
 
            smart_repo.SmartServerRepositoryIterFilesBytes)
 
2609
                                smart_repo.SmartServerRepositoryIterFilesBytes)
2586
2610
        self.assertHandlerEqual(b'Repository.lock_write',
2587
 
            smart_repo.SmartServerRepositoryLockWrite)
 
2611
                                smart_repo.SmartServerRepositoryLockWrite)
2588
2612
        self.assertHandlerEqual(b'Repository.make_working_trees',
2589
 
            smart_repo.SmartServerRepositoryMakeWorkingTrees)
 
2613
                                smart_repo.SmartServerRepositoryMakeWorkingTrees)
2590
2614
        self.assertHandlerEqual(b'Repository.pack',
2591
 
            smart_repo.SmartServerRepositoryPack)
 
2615
                                smart_repo.SmartServerRepositoryPack)
2592
2616
        self.assertHandlerEqual(b'Repository.reconcile',
2593
 
            smart_repo.SmartServerRepositoryReconcile)
 
2617
                                smart_repo.SmartServerRepositoryReconcile)
2594
2618
        self.assertHandlerEqual(b'Repository.tarball',
2595
 
            smart_repo.SmartServerRepositoryTarball)
 
2619
                                smart_repo.SmartServerRepositoryTarball)
2596
2620
        self.assertHandlerEqual(b'Repository.unlock',
2597
 
            smart_repo.SmartServerRepositoryUnlock)
 
2621
                                smart_repo.SmartServerRepositoryUnlock)
2598
2622
        self.assertHandlerEqual(b'Repository.start_write_group',
2599
 
            smart_repo.SmartServerRepositoryStartWriteGroup)
 
2623
                                smart_repo.SmartServerRepositoryStartWriteGroup)
2600
2624
        self.assertHandlerEqual(b'Repository.check_write_group',
2601
 
            smart_repo.SmartServerRepositoryCheckWriteGroup)
 
2625
                                smart_repo.SmartServerRepositoryCheckWriteGroup)
2602
2626
        self.assertHandlerEqual(b'Repository.commit_write_group',
2603
 
            smart_repo.SmartServerRepositoryCommitWriteGroup)
 
2627
                                smart_repo.SmartServerRepositoryCommitWriteGroup)
2604
2628
        self.assertHandlerEqual(b'Repository.abort_write_group',
2605
 
            smart_repo.SmartServerRepositoryAbortWriteGroup)
 
2629
                                smart_repo.SmartServerRepositoryAbortWriteGroup)
2606
2630
        self.assertHandlerEqual(b'VersionedFileRepository.get_serializer_format',
2607
 
            smart_repo.SmartServerRepositoryGetSerializerFormat)
 
2631
                                smart_repo.SmartServerRepositoryGetSerializerFormat)
2608
2632
        self.assertHandlerEqual(b'VersionedFileRepository.get_inventories',
2609
 
            smart_repo.SmartServerRepositoryGetInventories)
 
2633
                                smart_repo.SmartServerRepositoryGetInventories)
2610
2634
        self.assertHandlerEqual(b'Transport.is_readonly',
2611
 
            smart_req.SmartServerIsReadonly)
 
2635
                                smart_req.SmartServerIsReadonly)
2612
2636
 
2613
2637
 
2614
2638
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
2622
2646
        """Test the server started hooks get fired properly."""
2623
2647
        started_calls = []
2624
2648
        server.SmartTCPServer.hooks.install_named_hook('server_started',
2625
 
            lambda backing_urls, url: started_calls.append((backing_urls, url)),
2626
 
            None)
 
2649
                                                       lambda backing_urls, url: started_calls.append(
 
2650
                                                           (backing_urls, url)),
 
2651
                                                       None)
2627
2652
        started_ex_calls = []
2628
2653
        server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
2629
 
            lambda backing_urls, url: started_ex_calls.append((backing_urls, url)),
2630
 
            None)
 
2654
                                                       lambda backing_urls, url: started_ex_calls.append(
 
2655
                                                           (backing_urls, url)),
 
2656
                                                       None)
2631
2657
        self.server._sockname = ('example.com', 42)
2632
2658
        self.server.run_server_started_hooks()
2633
2659
        self.assertEqual(started_calls,
2634
 
            [([self.get_transport().base], 'bzr://example.com:42/')])
 
2660
                         [([self.get_transport().base], 'bzr://example.com:42/')])
2635
2661
        self.assertEqual(started_ex_calls,
2636
 
            [([self.get_transport().base], self.server)])
 
2662
                         [([self.get_transport().base], self.server)])
2637
2663
 
2638
2664
    def test_run_server_started_hooks_ipv6(self):
2639
2665
        """Test that socknames can contain 4-tuples."""
2640
2666
        self.server._sockname = ('::', 42, 0, 0)
2641
2667
        started_calls = []
2642
2668
        server.SmartTCPServer.hooks.install_named_hook('server_started',
2643
 
            lambda backing_urls, url: started_calls.append((backing_urls, url)),
2644
 
            None)
 
2669
                                                       lambda backing_urls, url: started_calls.append(
 
2670
                                                           (backing_urls, url)),
 
2671
                                                       None)
2645
2672
        self.server.run_server_started_hooks()
2646
2673
        self.assertEqual(started_calls,
2647
 
                [([self.get_transport().base], 'bzr://:::42/')])
 
2674
                         [([self.get_transport().base], 'bzr://:::42/')])
2648
2675
 
2649
2676
    def test_run_server_stopped_hooks(self):
2650
2677
        """Test the server stopped hooks."""
2651
2678
        self.server._sockname = ('example.com', 42)
2652
2679
        stopped_calls = []
2653
2680
        server.SmartTCPServer.hooks.install_named_hook('server_stopped',
2654
 
            lambda backing_urls, url: stopped_calls.append((backing_urls, url)),
2655
 
            None)
 
2681
                                                       lambda backing_urls, url: stopped_calls.append(
 
2682
                                                           (backing_urls, url)),
 
2683
                                                       None)
2656
2684
        self.server.run_server_stopped_hooks()
2657
2685
        self.assertEqual(stopped_calls,
2658
 
            [([self.get_transport().base], 'bzr://example.com:42/')])
 
2686
                         [([self.get_transport().base], 'bzr://example.com:42/')])
2659
2687
 
2660
2688
 
2661
2689
class TestSmartServerRepositoryPack(tests.TestCaseWithMemoryTransport):
2696
2724
        self.assertEqual(response.args, (b"ok", ))
2697
2725
        stream = [('inventory-deltas', [
2698
2726
            versionedfile.FulltextContentFactory(b'somerev', None, None,
2699
 
                self._get_serialized_inventory_delta(
2700
 
                    t.branch.repository, b'null:', b'somerev'))])]
 
2727
                                                 self._get_serialized_inventory_delta(
 
2728
                                                     t.branch.repository, b'null:', b'somerev'))])]
2701
2729
        fmt = controldir.format_registry.get('2a')().repository_format
2702
2730
        self.assertEqual(
2703
2731
            b"".join(response.body_stream),
2716
2744
        self.assertTrue(response.is_successful())
2717
2745
        self.assertEqual(response.args, (b"ok", ))
2718
2746
        self.assertEqual(b"".join(response.body_stream),
2719
 
            b"Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")
 
2747
                         b"Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")
2720
2748
 
2721
2749
 
2722
2750
class TestSmartServerRepositoryGetStreamForMissingKeys(GetStreamTestBase):
2776
2804
        self.assertTrue(response.is_successful())
2777
2805
        self.assertEqual(response.args, (b"ok", ))
2778
2806
        self.assertEqual(
2779
 
                [[b'somerev', b'somecontents\n'], [b'somerev', b'morecontents\n']],
2780
 
                bencode.bdecode(response.body))
 
2807
            [[b'somerev', b'somecontents\n'], [b'somerev', b'morecontents\n']],
 
2808
            bencode.bdecode(response.body))