~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
# Authors: Robert Collins <robert.collins@canonical.com>
#          and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


"""Repository implementation tests for bzr.

These test the conformance of all the repository variations to the expected API.
Specific tests for individual formats are in the tests/test_repository.py file
rather than in tests/per_branch/*.py.
"""

from bzrlib import (
    repository,
    )
from bzrlib.revision import NULL_REVISION
from bzrlib.repofmt import (
    weaverepo,
    )
from bzrlib.remote import RemoteBzrDirFormat, RemoteRepositoryFormat
from bzrlib.tests import (
    default_transport,
    multiply_scenarios,
    multiply_tests,
    test_server,
    )
from bzrlib.tests.per_bzrdir.test_bzrdir import TestCaseWithBzrDir
from bzrlib.transport import memory


def formats_to_scenarios(formats, transport_server, transport_readonly_server,
    vfs_transport_factory=None):
    """Transform the input formats to a list of scenarios.

    :param formats: A list of (scenario_name_suffix, repo_format)
        where the scenario_name_suffix is to be appended to the format
        name, and the repo_format is a RepositoryFormat subclass
        instance.
    :returns: Scenarios of [(scenario_name, {parameter_name: value})]
    """
    result = []
    for scenario_name_suffix, repository_format in formats:
        scenario_name = repository_format.__class__.__name__
        scenario_name += scenario_name_suffix
        scenario = (scenario_name,
            {"transport_server":transport_server,
             "transport_readonly_server":transport_readonly_server,
             "bzrdir_format":repository_format._matchingbzrdir,
             "repository_format":repository_format,
             })
        # Only override the test's vfs_transport_factory if one was
        # specified, otherwise just leave the default in place.
        if vfs_transport_factory:
            scenario[1]['vfs_transport_factory'] = vfs_transport_factory
        result.append(scenario)
    return result


def all_repository_format_scenarios():
    """Return a list of test scenarios for parameterising repository tests.
    """
    registry = repository.format_registry
    all_formats = [registry.get(k) for k in registry.keys()]
    all_formats.extend(weaverepo._legacy_formats)
    # format_scenarios is all the implementations of Repository; i.e. all disk
    # formats plus RemoteRepository.
    format_scenarios = formats_to_scenarios(
        [('', format) for format in all_formats],
        default_transport,
        # None here will cause a readonly decorator to be created
        # by the TestCaseWithTransport.get_readonly_transport method.
        None)
    format_scenarios.extend(formats_to_scenarios(
        [('-default', RemoteRepositoryFormat())],
        test_server.SmartTCPServer_for_testing,
        test_server.ReadonlySmartTCPServer_for_testing,
        memory.MemoryServer))
    format_scenarios.extend(formats_to_scenarios(
        [('-v2', RemoteRepositoryFormat())],
        test_server.SmartTCPServer_for_testing_v2_only,
        test_server.ReadonlySmartTCPServer_for_testing_v2_only,
        memory.MemoryServer))
    return format_scenarios


class TestCaseWithRepository(TestCaseWithBzrDir):

    def make_repository(self, relpath, shared=False, format=None):
        if format is None:
            # Create a repository of the type we are trying to test.
            made_control = self.make_bzrdir(relpath)
            repo = self.repository_format.initialize(made_control,
                    shared=shared)
            if getattr(self, "repository_to_test_repository", None):
                repo = self.repository_to_test_repository(repo)
            return repo
        else:
            return super(TestCaseWithRepository, self).make_repository(
                relpath, shared=shared, format=format)


class BrokenRepoScenario(object):
    """Base class for defining scenarios for testing check and reconcile.

    A subclass needs to define the following methods:
        :populate_repository: a method to use to populate a repository with
            sample revisions, inventories and file versions.
        :all_versions_after_reconcile: all the versions in repository after
            reconcile.  run_test verifies that the text of each of these
            versions of the file is unchanged by the reconcile.
        :populated_parents: a list of (parents list, revision).  Each version
            of the file is verified to have the given parents before running
            the reconcile.  i.e. this is used to assert that the repo from the
            factory is what we expect.
        :corrected_parents: a list of (parents list, revision).  Each version
            of the file is verified to have the given parents after the
            reconcile.  i.e. this is used to assert that reconcile made the
            changes we expect it to make.

    A subclass may define the following optional method as well:
        :corrected_fulltexts: a list of file versions that should be stored as
            fulltexts (not deltas) after reconcile.  run_test will verify that
            this occurs.
    """

    def __init__(self, test_case):
        self.test_case = test_case

    def make_one_file_inventory(self, repo, revision, parents,
                                inv_revision=None, root_revision=None,
                                file_contents=None, make_file_version=True):
        return self.test_case.make_one_file_inventory(
            repo, revision, parents, inv_revision=inv_revision,
            root_revision=root_revision, file_contents=file_contents,
            make_file_version=make_file_version)

    def add_revision(self, repo, revision_id, inv, parent_ids):
        return self.test_case.add_revision(repo, revision_id, inv, parent_ids)

    def corrected_fulltexts(self):
        return []

    def repository_text_key_index(self):
        result = {}
        if self.versioned_root:
            result.update(self.versioned_repository_text_keys())
        result.update(self.repository_text_keys())
        return result


class UndamagedRepositoryScenario(BrokenRepoScenario):
    """A scenario where the repository has no damage.

    It has a single revision, 'rev1a', with a single file.
    """

    def all_versions_after_reconcile(self):
        return ('rev1a', )

    def populated_parents(self):
        return (((), 'rev1a'), )

    def corrected_parents(self):
        # Same as the populated parents, because there was nothing wrong.
        return self.populated_parents()

    def check_regexes(self, repo):
        return ["0 unreferenced text versions"]

    def populate_repository(self, repo):
        # make rev1a: A well-formed revision, containing 'a-file'
        inv = self.make_one_file_inventory(
            repo, 'rev1a', [], root_revision='rev1a')
        self.add_revision(repo, 'rev1a', inv, [])
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'rev1a'): True})
        result.update({('a-file-id', 'rev1a'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'rev1a'):[NULL_REVISION]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'rev1a'):[NULL_REVISION]}


class FileParentIsNotInRevisionAncestryScenario(BrokenRepoScenario):
    """A scenario where a revision 'rev2' has 'a-file' with a
    parent 'rev1b' that is not in the revision ancestry.

    Reconcile should remove 'rev1b' from the parents list of 'a-file' in
    'rev2', preserving 'rev1a' as a parent.
    """

    def all_versions_after_reconcile(self):
        return ('rev1a', 'rev2')

    def populated_parents(self):
        return (
            ((), 'rev1a'),
            ((), 'rev1b'), # Will be gc'd
            (('rev1a', 'rev1b'), 'rev2')) # Will have parents trimmed

    def corrected_parents(self):
        return (
            ((), 'rev1a'),
            (None, 'rev1b'),
            (('rev1a',), 'rev2'))

    def check_regexes(self, repo):
        return [r"\* a-file-id version rev2 has parents \('rev1a', 'rev1b'\) "
                r"but should have \('rev1a',\)",
                "1 unreferenced text versions",
                ]

    def populate_repository(self, repo):
        # make rev1a: A well-formed revision, containing 'a-file'
        inv = self.make_one_file_inventory(
            repo, 'rev1a', [], root_revision='rev1a')
        self.add_revision(repo, 'rev1a', inv, [])

        # make rev1b, which has no Revision, but has an Inventory, and
        # a-file
        inv = self.make_one_file_inventory(
            repo, 'rev1b', [], root_revision='rev1b')
        repo.add_inventory('rev1b', inv, [])

        # make rev2, with a-file.
        # a-file has 'rev1b' as an ancestor, even though this is not
        # mentioned by 'rev1a', making it an unreferenced ancestor
        inv = self.make_one_file_inventory(
            repo, 'rev2', ['rev1a', 'rev1b'])
        self.add_revision(repo, 'rev2', inv, ['rev1a'])
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'rev1a'): True,
                           ('TREE_ROOT', 'rev2'): True})
        result.update({('a-file-id', 'rev1a'): True,
                       ('a-file-id', 'rev2'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'rev1a'):[NULL_REVISION],
                ('a-file-id', 'rev2'):[('a-file-id', 'rev1a')]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'rev1a'):[NULL_REVISION],
                ('TREE_ROOT', 'rev2'):[('TREE_ROOT', 'rev1a')]}


class FileParentHasInaccessibleInventoryScenario(BrokenRepoScenario):
    """A scenario where a revision 'rev3' containing 'a-file' modified in
    'rev3', and with a parent which is in the revision ancestory, but whose
    inventory cannot be accessed at all.

    Reconcile should remove the file version parent whose inventory is
    inaccessbile (i.e. remove 'rev1c' from the parents of a-file's rev3).
    """

    def all_versions_after_reconcile(self):
        return ('rev2', 'rev3')

    def populated_parents(self):
        return (
            ((), 'rev2'),
            (('rev1c',), 'rev3'))

    def corrected_parents(self):
        return (
            ((), 'rev2'),
            ((), 'rev3'))

    def check_regexes(self, repo):
        return [r"\* a-file-id version rev3 has parents "
                r"\('rev1c',\) but should have \(\)",
                ]

    def populate_repository(self, repo):
        # make rev2, with a-file
        # a-file is sane
        inv = self.make_one_file_inventory(repo, 'rev2', [])
        self.add_revision(repo, 'rev2', inv, [])

        # make ghost revision rev1c, with a version of a-file present so
        # that we generate a knit delta against this version.  In real life
        # the ghost might never have been present or rev3 might have been
        # generated against a revision that was present at the time.  So
        # currently we have the full history of a-file present even though
        # the inventory and revision objects are not.
        self.make_one_file_inventory(repo, 'rev1c', [])

        # make rev3 with a-file
        # a-file refers to 'rev1c', which is a ghost in this repository, so
        # a-file cannot have rev1c as its ancestor.
        inv = self.make_one_file_inventory(repo, 'rev3', ['rev1c'])
        self.add_revision(repo, 'rev3', inv, ['rev1c', 'rev1a'])
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'rev2'): True,
                           ('TREE_ROOT', 'rev3'): True})
        result.update({('a-file-id', 'rev2'): True,
                       ('a-file-id', 'rev3'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'rev2'):[NULL_REVISION],
                ('a-file-id', 'rev3'):[NULL_REVISION]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'rev2'):[NULL_REVISION],
                ('TREE_ROOT', 'rev3'):[NULL_REVISION]}


class FileParentsNotReferencedByAnyInventoryScenario(BrokenRepoScenario):
    """A scenario where a repository with file 'a-file' which has extra
    per-file versions that are not referenced by any inventory (even though
    they have the same ID as actual revisions).  The inventory of 'rev2'
    references 'rev1a' of 'a-file', but there is a 'rev2' of 'some-file' stored
    and erroneously referenced by later per-file versions (revisions 'rev4' and
    'rev5').

    Reconcile should remove the file parents that are not referenced by any
    inventory.
    """

    def all_versions_after_reconcile(self):
        return ('rev1a', 'rev2c', 'rev4', 'rev5')

    def populated_parents(self):
        return [
            (('rev1a',), 'rev2'),
            (('rev1a',), 'rev2b'),
            (('rev2',), 'rev3'),
            (('rev2',), 'rev4'),
            (('rev2', 'rev2c'), 'rev5')]

    def corrected_parents(self):
        return (
            # rev2 and rev2b have been removed.
            (None, 'rev2'),
            (None, 'rev2b'),
            # rev3's accessible parent inventories all have rev1a as the last
            # modifier.
            (('rev1a',), 'rev3'),
            # rev1a features in both rev4's parents but should only appear once
            # in the result
            (('rev1a',), 'rev4'),
            # rev2c is the head of rev1a and rev2c, the inventory provided
            # per-file last-modified revisions.
            (('rev2c',), 'rev5'))

    def check_regexes(self, repo):
        if repo.supports_rich_root():
            # TREE_ROOT will be wrong; but we're not testing it. so just adjust
            # the expected count of errors.
            count = 9
        else:
            count = 3
        return [
            # will be gc'd
            r"unreferenced version: {rev2} in a-file-id",
            r"unreferenced version: {rev2b} in a-file-id",
            # will be corrected
            r"a-file-id version rev3 has parents \('rev2',\) "
            r"but should have \('rev1a',\)",
            r"a-file-id version rev5 has parents \('rev2', 'rev2c'\) "
            r"but should have \('rev2c',\)",
            r"a-file-id version rev4 has parents \('rev2',\) "
            r"but should have \('rev1a',\)",
            "%d inconsistent parents" % count,
            ]

    def populate_repository(self, repo):
        # make rev1a: A well-formed revision, containing 'a-file'
        inv = self.make_one_file_inventory(
            repo, 'rev1a', [], root_revision='rev1a')
        self.add_revision(repo, 'rev1a', inv, [])

        # make rev2, with a-file.
        # a-file is unmodified from rev1a, and an unreferenced rev2 file
        # version is present in the repository.
        self.make_one_file_inventory(
            repo, 'rev2', ['rev1a'], inv_revision='rev1a')
        self.add_revision(repo, 'rev2', inv, ['rev1a'])

        # make rev3 with a-file
        # a-file has 'rev2' as its ancestor, but the revision in 'rev2' was
        # rev1a so this is inconsistent with rev2's inventory - it should
        # be rev1a, and at the revision level 1c is not present - it is a
        # ghost, so only the details from rev1a are available for
        # determining whether a delta is acceptable, or a full is needed,
        # and what the correct parents are.
        inv = self.make_one_file_inventory(repo, 'rev3', ['rev2'])
        self.add_revision(repo, 'rev3', inv, ['rev1c', 'rev1a'])

        # In rev2b, the true last-modifying-revision of a-file is rev1a,
        # inherited from rev2, but there is a version rev2b of the file, which
        # reconcile could remove, leaving no rev2b.  Most importantly,
        # revisions descending from rev2b should not have per-file parents of
        # a-file-rev2b.
        # ??? This is to test deduplication in fixing rev4
        inv = self.make_one_file_inventory(
            repo, 'rev2b', ['rev1a'], inv_revision='rev1a')
        self.add_revision(repo, 'rev2b', inv, ['rev1a'])

        # rev4 is for testing that when the last modified of a file in
        # multiple parent revisions is the same, that it only appears once
        # in the generated per file parents list: rev2 and rev2b both
        # descend from 1a and do not change the file a-file, so there should
        # be no version of a-file 'rev2' or 'rev2b', but rev4 does change
        # a-file, and is a merge of rev2 and rev2b, so it should end up with
        # a parent of just rev1a - the starting file parents list is simply
        # completely wrong.
        inv = self.make_one_file_inventory(repo, 'rev4', ['rev2'])
        self.add_revision(repo, 'rev4', inv, ['rev2', 'rev2b'])

        # rev2c changes a-file from rev1a, so the version it of a-file it
        # introduces is a head revision when rev5 is checked.
        inv = self.make_one_file_inventory(repo, 'rev2c', ['rev1a'])
        self.add_revision(repo, 'rev2c', inv, ['rev1a'])

        # rev5 descends from rev2 and rev2c; as rev2 does not alter a-file,
        # but rev2c does, this should use rev2c as the parent for the per
        # file history, even though more than one per-file parent is
        # available, because we use the heads of the revision parents for
        # the inventory modification revisions of the file to determine the
        # parents for the per file graph.
        inv = self.make_one_file_inventory(repo, 'rev5', ['rev2', 'rev2c'])
        self.add_revision(repo, 'rev5', inv, ['rev2', 'rev2c'])
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'rev1a'): True,
                           ('TREE_ROOT', 'rev2'): True,
                           ('TREE_ROOT', 'rev2b'): True,
                           ('TREE_ROOT', 'rev2c'): True,
                           ('TREE_ROOT', 'rev3'): True,
                           ('TREE_ROOT', 'rev4'): True,
                           ('TREE_ROOT', 'rev5'): True})
        result.update({('a-file-id', 'rev1a'): True,
                       ('a-file-id', 'rev2c'): True,
                       ('a-file-id', 'rev3'): True,
                       ('a-file-id', 'rev4'): True,
                       ('a-file-id', 'rev5'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'rev1a'): [NULL_REVISION],
                 ('a-file-id', 'rev2c'): [('a-file-id', 'rev1a')],
                 ('a-file-id', 'rev3'): [('a-file-id', 'rev1a')],
                 ('a-file-id', 'rev4'): [('a-file-id', 'rev1a')],
                 ('a-file-id', 'rev5'): [('a-file-id', 'rev2c')]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'rev1a'): [NULL_REVISION],
                ('TREE_ROOT', 'rev2'): [('TREE_ROOT', 'rev1a')],
                ('TREE_ROOT', 'rev2b'): [('TREE_ROOT', 'rev1a')],
                ('TREE_ROOT', 'rev2c'): [('TREE_ROOT', 'rev1a')],
                ('TREE_ROOT', 'rev3'): [('TREE_ROOT', 'rev1a')],
                ('TREE_ROOT', 'rev4'):
                    [('TREE_ROOT', 'rev2'), ('TREE_ROOT', 'rev2b')],
                ('TREE_ROOT', 'rev5'):
                    [('TREE_ROOT', 'rev2'), ('TREE_ROOT', 'rev2c')]}


class UnreferencedFileParentsFromNoOpMergeScenario(BrokenRepoScenario):
    """
    rev1a and rev1b with identical contents
    rev2 revision has parents of [rev1a, rev1b]
    There is a a-file:rev2 file version, not referenced by the inventory.
    """

    def all_versions_after_reconcile(self):
        return ('rev1a', 'rev1b', 'rev2', 'rev4')

    def populated_parents(self):
        return (
            ((), 'rev1a'),
            ((), 'rev1b'),
            (('rev1a', 'rev1b'), 'rev2'),
            (None, 'rev3'),
            (('rev2',), 'rev4'),
            )

    def corrected_parents(self):
        return (
            ((), 'rev1a'),
            ((), 'rev1b'),
            ((), 'rev2'),
            (None, 'rev3'),
            (('rev2',), 'rev4'),
            )

    def corrected_fulltexts(self):
        return ['rev2']

    def check_regexes(self, repo):
        return []

    def populate_repository(self, repo):
        # make rev1a: A well-formed revision, containing 'a-file'
        inv1a = self.make_one_file_inventory(
            repo, 'rev1a', [], root_revision='rev1a')
        self.add_revision(repo, 'rev1a', inv1a, [])

        # make rev1b: A well-formed revision, containing 'a-file'
        # rev1b of a-file has the exact same contents as rev1a.
        file_contents = repo.revision_tree('rev1a').get_file_text('a-file-id')
        inv = self.make_one_file_inventory(
            repo, 'rev1b', [], root_revision='rev1b',
            file_contents=file_contents)
        self.add_revision(repo, 'rev1b', inv, [])

        # make rev2, a merge of rev1a and rev1b, with a-file.
        # a-file is unmodified from rev1a and rev1b, but a new version is
        # wrongly present anyway.
        inv = self.make_one_file_inventory(
            repo, 'rev2', ['rev1a', 'rev1b'], inv_revision='rev1a',
            file_contents=file_contents)
        self.add_revision(repo, 'rev2', inv, ['rev1a', 'rev1b'])

        # rev3: a-file unchanged from rev2, but wrongly referencing rev2 of the
        # file in its inventory.
        inv = self.make_one_file_inventory(
            repo, 'rev3', ['rev2'], inv_revision='rev2',
            file_contents=file_contents, make_file_version=False)
        self.add_revision(repo, 'rev3', inv, ['rev2'])

        # rev4: a modification of a-file on top of rev3.
        inv = self.make_one_file_inventory(repo, 'rev4', ['rev2'])
        self.add_revision(repo, 'rev4', inv, ['rev3'])
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'rev1a'): True,
                           ('TREE_ROOT', 'rev1b'): True,
                           ('TREE_ROOT', 'rev2'): True,
                           ('TREE_ROOT', 'rev3'): True,
                           ('TREE_ROOT', 'rev4'): True})
        result.update({('a-file-id', 'rev1a'): True,
                       ('a-file-id', 'rev1b'): True,
                       ('a-file-id', 'rev2'): False,
                       ('a-file-id', 'rev4'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'rev1a'): [NULL_REVISION],
                ('a-file-id', 'rev1b'): [NULL_REVISION],
                ('a-file-id', 'rev2'): [NULL_REVISION],
                ('a-file-id', 'rev4'): [('a-file-id', 'rev2')]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'rev1a'): [NULL_REVISION],
                ('TREE_ROOT', 'rev1b'): [NULL_REVISION],
                ('TREE_ROOT', 'rev2'):
                    [('TREE_ROOT', 'rev1a'), ('TREE_ROOT', 'rev1b')],
                ('TREE_ROOT', 'rev3'): [('TREE_ROOT', 'rev2')],
                ('TREE_ROOT', 'rev4'): [('TREE_ROOT', 'rev3')]}


class TooManyParentsScenario(BrokenRepoScenario):
    """A scenario where 'broken-revision' of 'a-file' claims to have parents
    ['good-parent', 'bad-parent'].  However 'bad-parent' is in the ancestry of
    'good-parent', so the correct parent list for that file version are is just
    ['good-parent'].
    """

    def all_versions_after_reconcile(self):
        return ('bad-parent', 'good-parent', 'broken-revision')

    def populated_parents(self):
        return (
            ((), 'bad-parent'),
            (('bad-parent',), 'good-parent'),
            (('good-parent', 'bad-parent'), 'broken-revision'))

    def corrected_parents(self):
        return (
            ((), 'bad-parent'),
            (('bad-parent',), 'good-parent'),
            (('good-parent',), 'broken-revision'))

    def check_regexes(self, repo):
        if repo.supports_rich_root():
            # TREE_ROOT will be wrong; but we're not testing it. so just adjust
            # the expected count of errors.
            count = 3
        else:
            count = 1
        return (
            '     %d inconsistent parents' % count,
            (r"      \* a-file-id version broken-revision has parents "
             r"\('good-parent', 'bad-parent'\) but "
             r"should have \('good-parent',\)"))

    def populate_repository(self, repo):
        inv = self.make_one_file_inventory(
            repo, 'bad-parent', (), root_revision='bad-parent')
        self.add_revision(repo, 'bad-parent', inv, ())

        inv = self.make_one_file_inventory(
            repo, 'good-parent', ('bad-parent',))
        self.add_revision(repo, 'good-parent', inv, ('bad-parent',))

        inv = self.make_one_file_inventory(
            repo, 'broken-revision', ('good-parent', 'bad-parent'))
        self.add_revision(repo, 'broken-revision', inv, ('good-parent',))
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'bad-parent'): True,
                           ('TREE_ROOT', 'broken-revision'): True,
                           ('TREE_ROOT', 'good-parent'): True})
        result.update({('a-file-id', 'bad-parent'): True,
                       ('a-file-id', 'broken-revision'): True,
                       ('a-file-id', 'good-parent'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'bad-parent'): [NULL_REVISION],
                ('a-file-id', 'broken-revision'):
                    [('a-file-id', 'good-parent')],
                ('a-file-id', 'good-parent'): [('a-file-id', 'bad-parent')]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'bad-parent'): [NULL_REVISION],
                ('TREE_ROOT', 'broken-revision'):
                    [('TREE_ROOT', 'good-parent')],
                ('TREE_ROOT', 'good-parent'): [('TREE_ROOT', 'bad-parent')]}


class ClaimedFileParentDidNotModifyFileScenario(BrokenRepoScenario):
    """A scenario where the file parent is the same as the revision parent, but
    should not be because that revision did not modify the file.

    Specifically, the parent revision of 'current' is
    'modified-something-else', which does not modify 'a-file', but the
    'current' version of 'a-file' erroneously claims that
    'modified-something-else' is the parent file version.
    """

    def all_versions_after_reconcile(self):
        return ('basis', 'current')

    def populated_parents(self):
        return (
            ((), 'basis'),
            (('basis',), 'modified-something-else'),
            (('modified-something-else',), 'current'))

    def corrected_parents(self):
        return (
            ((), 'basis'),
            (None, 'modified-something-else'),
            (('basis',), 'current'))

    def check_regexes(self, repo):
        if repo.supports_rich_root():
            # TREE_ROOT will be wrong; but we're not testing it. so just adjust
            # the expected count of errors.
            count = 3
        else:
            count = 1
        return (
            "%d inconsistent parents" % count,
            r"\* a-file-id version current has parents "
            r"\('modified-something-else',\) but should have \('basis',\)",
            )

    def populate_repository(self, repo):
        inv = self.make_one_file_inventory(repo, 'basis', ())
        self.add_revision(repo, 'basis', inv, ())

        # 'modified-something-else' is a correctly recorded revision, but it
        # does not modify the file we are looking at, so the inventory for that
        # file in this revision points to 'basis'.
        inv = self.make_one_file_inventory(
            repo, 'modified-something-else', ('basis',), inv_revision='basis')
        self.add_revision(repo, 'modified-something-else', inv, ('basis',))

        # The 'current' revision has 'modified-something-else' as its parent,
        # but the 'current' version of 'a-file' should have 'basis' as its
        # parent.
        inv = self.make_one_file_inventory(
            repo, 'current', ('modified-something-else',))
        self.add_revision(repo, 'current', inv, ('modified-something-else',))
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'basis'): True,
                           ('TREE_ROOT', 'current'): True,
                           ('TREE_ROOT', 'modified-something-else'): True})
        result.update({('a-file-id', 'basis'): True,
                       ('a-file-id', 'current'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'basis'): [NULL_REVISION],
                ('a-file-id', 'current'): [('a-file-id', 'basis')]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'basis'): ['null:'],
                ('TREE_ROOT', 'current'):
                    [('TREE_ROOT', 'modified-something-else')],
                ('TREE_ROOT', 'modified-something-else'):
                    [('TREE_ROOT', 'basis')]}


class IncorrectlyOrderedParentsScenario(BrokenRepoScenario):
    """A scenario where the set parents of a version of a file are correct, but
    the order of those parents is incorrect.

    This defines a 'broken-revision-1-2' and a 'broken-revision-2-1' which both
    have their file version parents reversed compared to the revision parents,
    which is invalid.  (We use two revisions with opposite orderings of the
    same parents to make sure that accidentally relying on dictionary/set
    ordering cannot make the test pass; the assumption is that while dict/set
    iteration order is arbitrary, it is also consistent within a single test).
    """

    def all_versions_after_reconcile(self):
        return ['parent-1', 'parent-2', 'broken-revision-1-2',
                'broken-revision-2-1']

    def populated_parents(self):
        return (
            ((), 'parent-1'),
            ((), 'parent-2'),
            (('parent-2', 'parent-1'), 'broken-revision-1-2'),
            (('parent-1', 'parent-2'), 'broken-revision-2-1'))

    def corrected_parents(self):
        return (
            ((), 'parent-1'),
            ((), 'parent-2'),
            (('parent-1', 'parent-2'), 'broken-revision-1-2'),
            (('parent-2', 'parent-1'), 'broken-revision-2-1'))

    def check_regexes(self, repo):
        if repo.supports_rich_root():
            # TREE_ROOT will be wrong; but we're not testing it. so just adjust
            # the expected count of errors.
            count = 4
        else:
            count = 2
        return (
            "%d inconsistent parents" % count,
            r"\* a-file-id version broken-revision-1-2 has parents "
            r"\('parent-2', 'parent-1'\) but should have "
            r"\('parent-1', 'parent-2'\)",
            r"\* a-file-id version broken-revision-2-1 has parents "
            r"\('parent-1', 'parent-2'\) but should have "
            r"\('parent-2', 'parent-1'\)")

    def populate_repository(self, repo):
        inv = self.make_one_file_inventory(repo, 'parent-1', [])
        self.add_revision(repo, 'parent-1', inv, [])

        inv = self.make_one_file_inventory(repo, 'parent-2', [])
        self.add_revision(repo, 'parent-2', inv, [])

        inv = self.make_one_file_inventory(
            repo, 'broken-revision-1-2', ['parent-2', 'parent-1'])
        self.add_revision(
            repo, 'broken-revision-1-2', inv, ['parent-1', 'parent-2'])

        inv = self.make_one_file_inventory(
            repo, 'broken-revision-2-1', ['parent-1', 'parent-2'])
        self.add_revision(
            repo, 'broken-revision-2-1', inv, ['parent-2', 'parent-1'])
        self.versioned_root = repo.supports_rich_root()

    def repository_text_key_references(self):
        result = {}
        if self.versioned_root:
            result.update({('TREE_ROOT', 'broken-revision-1-2'): True,
                           ('TREE_ROOT', 'broken-revision-2-1'): True,
                           ('TREE_ROOT', 'parent-1'): True,
                           ('TREE_ROOT', 'parent-2'): True})
        result.update({('a-file-id', 'broken-revision-1-2'): True,
                       ('a-file-id', 'broken-revision-2-1'): True,
                       ('a-file-id', 'parent-1'): True,
                       ('a-file-id', 'parent-2'): True})
        return result

    def repository_text_keys(self):
        return {('a-file-id', 'broken-revision-1-2'):
                    [('a-file-id', 'parent-1'), ('a-file-id', 'parent-2')],
                ('a-file-id', 'broken-revision-2-1'):
                    [('a-file-id', 'parent-2'), ('a-file-id', 'parent-1')],
                ('a-file-id', 'parent-1'): [NULL_REVISION],
                ('a-file-id', 'parent-2'): [NULL_REVISION]}

    def versioned_repository_text_keys(self):
        return {('TREE_ROOT', 'broken-revision-1-2'):
                    [('TREE_ROOT', 'parent-1'), ('TREE_ROOT', 'parent-2')],
                ('TREE_ROOT', 'broken-revision-2-1'):
                    [('TREE_ROOT', 'parent-2'), ('TREE_ROOT', 'parent-1')],
                ('TREE_ROOT', 'parent-1'): [NULL_REVISION],
                ('TREE_ROOT', 'parent-2'): [NULL_REVISION]}


all_broken_scenario_classes = [
    UndamagedRepositoryScenario,
    FileParentIsNotInRevisionAncestryScenario,
    FileParentHasInaccessibleInventoryScenario,
    FileParentsNotReferencedByAnyInventoryScenario,
    TooManyParentsScenario,
    ClaimedFileParentDidNotModifyFileScenario,
    IncorrectlyOrderedParentsScenario,
    UnreferencedFileParentsFromNoOpMergeScenario,
    ]


def load_tests(standard_tests, module, loader):
    prefix = 'bzrlib.tests.per_repository.'
    test_repository_modules = [
        'test_add_fallback_repository',
        'test_add_inventory_by_delta',
        'test_break_lock',
        'test_check',
        # test_check_reconcile is intentionally omitted, see below.
        'test_commit_builder',
        'test_fetch',
        'test_fileid_involved',
        'test_find_text_key_references',
        'test__generate_text_key_index',
        'test_get_parent_map',
        'test_has_same_location',
        'test_has_revisions',
        'test_is_write_locked',
        'test_iter_reverse_revision_history',
        'test_merge_directive',
        'test_pack',
        'test_reconcile',
        'test_refresh_data',
        'test_repository',
        'test_revision',
        'test_statistics',
        'test_write_group',
        ]
    # Parameterize per_repository test modules by format.
    submod_tests = loader.loadTestsFromModuleNames(
        [prefix + module_name for module_name in test_repository_modules])
    format_scenarios = all_repository_format_scenarios()
    multiply_tests(submod_tests, format_scenarios, standard_tests)

    # test_check_reconcile needs to be parameterized by format *and* by broken
    # repository scenario.
    broken_scenarios = [(s.__name__, {'scenario_class': s})
                        for s in all_broken_scenario_classes]
    broken_scenarios_for_all_formats = multiply_scenarios(
        format_scenarios, broken_scenarios)
    return multiply_tests(
        loader.loadTestsFromModuleNames([prefix + 'test_check_reconcile']),
        broken_scenarios_for_all_formats, standard_tests)