File: rich_history_test.py

package info (click to toggle)
git-ubuntu 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,688 kB
  • sloc: python: 13,378; sh: 480; makefile: 2
file content (306 lines) | stat: -rw-r--r-- 10,424 bytes parent folder | download
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
import os

import pygit2
import pytest

from gitubuntu.repo_builder import Blob, Commit, Placeholder, Repo, Tree
import gitubuntu.spec as spec
from gitubuntu.test_fixtures import repo

import gitubuntu.rich_history as target


def test_preservation(tmpdir, repo):
    """An export followed by an import should preserve rich history

    Given a minimal repository that should be able to have upload tags be
    exported, when we import the export result into a similar repository with a
    subtly different upload tag in which the rich history should still apply,
    the result should be a correctly reconstructed upload tag.

    :param py.path tmpdir: the pytest standard tmpdir fixture.
    :param repo: our standard repo fixture.
    """
    Repo(
        commits=[
            Commit(
                name='1',
                tree=Tree({
                    'a': Blob(b'a'),
                }),
            ),
            Commit(
                name='2',
                parents=[Placeholder('1')],
                tree=Tree({
                    'a': Blob(b'ab'),
                }),
            ),
            Commit(
                name='3',
                parents=[Placeholder('2')],
                tree=Tree({
                    'a': Blob(b'abc'),
                }),
            ),
        ],
        tags={
            'importer/import/1': Placeholder('1'),
            'importer/upload/2': Placeholder('3'),
        }
    ).write(repo.raw_repo)
    target.export_all(repo, str(tmpdir))
    repo.delete_tags_in_namespace('importer')
    Repo(
        commits=[
            Commit(
                name='1',
                tree=Tree({
                    'a': Blob(b'a'),
                    'b': Blob(b'b'),
                }),
            )
        ],
        tags={'importer/import/1': Placeholder('1')},
    ).write(repo.raw_repo)
    target.import_single(repo, tmpdir, '2')
    new_tag = repo.raw_repo.lookup_reference(
        'refs/tags/importer/upload/2',
    ).peel(pygit2.Tag)
    assert new_tag.tagger.name == spec.SYNTHESIZED_COMMITTER_NAME
    assert new_tag.tagger.email == spec.SYNTHESIZED_COMMITTER_EMAIL
    new_commit = new_tag.peel(pygit2.Commit)
    assert new_commit.committer.name == spec.SYNTHESIZED_COMMITTER_NAME
    assert new_commit.committer.email == spec.SYNTHESIZED_COMMITTER_EMAIL
    new_tree = new_commit.peel(pygit2.Tree)
    assert repo.raw_repo.get(new_tree['a'].id).data == b'abc'
    assert repo.raw_repo.get(new_tree['b'].id).data == b'b'


def test_preservation_multiple_parents(tmpdir, repo):
    """An export of rich history should omit multiple parent cases

    If an upload tag leads to multiple parents before we reach an import tag,
    it should be excluded from the export as this type of upload tag is not
    supported for rich history preservation. This test also serves to verify
    code paths that handle the MultipleParentError exception in the export
    code.

    :param py.path tmpdir: the pytest standard tmpdir fixture.
    :param repo: our standard repo fixture.
    """
    Repo(
        commits=[
            Commit(name='1', message='a'),
            Commit(name='2', message='b'),
            Commit(name='child', parents=[Placeholder('1'), Placeholder('2')]),
        ],
        tags={
            'importer/import/1': Placeholder('1'),
            'importer/upload/2': Placeholder('child'),
        }
    ).write(repo.raw_repo)
    upload_tag = repo.raw_repo.lookup_reference('refs/tags/importer/upload/2')
    target.export_all(repo, str(tmpdir))
    assert not os.path.exists(tmpdir.join('2.base'))


def test_preservation_no_parents(tmpdir, repo):
    """An export of rich history should omit no parent cases

    If an upload tag leads to no parents before we reach an import tag,
    it should be excluded from the export as this type of upload tag is not
    supported for rich history preservation. This test also serves to verify
    code paths that handle the NoParentError exception in the export
    code.

    :param py.path tmpdir: the pytest standard tmpdir fixture.
    :param repo: our standard repo fixture.
    """
    Repo(
        commits=[
            Commit(name='1', message='a'),
            Commit(name='2', message='b'),
        ],
        tags={
            'importer/import/1': Placeholder('1'),
            'importer/upload/2': Placeholder('2'),
        }
    ).write(repo.raw_repo)
    upload_tag = repo.raw_repo.lookup_reference('refs/tags/importer/upload/2')
    target.export_all(repo, str(tmpdir))
    assert not os.path.exists(tmpdir.join('2.base'))


def test_import_no_base(tmpdir, repo):
    """An import of rich history with a missing base should fail

    If a rich history export uses a base that doesn't exist in the target
    repository for rich history import, a BaseNotFoundError exception should be
    raised.

    :param py.path tmpdir: the pytest standard tmpdir fixture.
    :param repo: our standard repo fixture.
    """
    tmpdir.join('2.base').write("1\n")
    with pytest.raises(target.BaseNotFoundError):
        target.import_single(repo, str(tmpdir), '2')


@pytest.mark.parametrize(['before_export', 'before_import'], [
    (
        # Commit messages with patch-like contents should not fail

        # If a commit message in rich history contains text in a patch-like
        # format (such as a diff of some file), it should not prevent correct
        # round-tripping. "git format-patch" followed by "git am" fails this
        # test, for example.
        {
            'commits': [
                Commit(name='1', message='a', tree=Tree({'a': Blob(b'a')})),
                Commit(
                    name='2',
                    message='''First line

Inline patch that isn't part of the real patch starts here

--- a/foo
+++ b/foo
@@ -1 +1,2 @@
 foo
+bar
''',
                    tree=Tree({'a': Blob(b'ab')}),
                    parents=[Placeholder('1')],
                ),
            ],
            'tags': {
                'importer/import/1': Placeholder('1'),
                'importer/upload/2': Placeholder('2'),
            }
        },
        {
            'commits': [
                Commit(name='b1', message='c', tree=Tree({'a': Blob(b'a')})),
            ],
            'tags': {'importer/import/1': Placeholder('b1')},
        },
    ),
    (
        # Commits that have no changes should round trip
        {
            'commits': [
                Commit(
                    name='1',
                    message='a',
                ),
                Commit(
                    name='2',
                    message='b',
                    parents=[Placeholder('1')],
                ),
            ],
            'tags': {
                'importer/import/1': Placeholder('1'),
                'importer/upload/2': Placeholder('2'),
            },
        },
        {
            'commits':  [Commit(name='b1', message='c')],
            'tags': {'importer/import/1': Placeholder('b1')},
        },
    ),
    (
        # Commits that have no commit message should round trip
        {
            'commits': [
                Commit(
                    name='1',
                    message='a',
                ),
                Commit(
                    name='2',
                    message='',
                    tree=Tree({'a': Blob(b'a')}),
                    parents=[Placeholder('1')],
                ),
            ],
            'tags': {
                'importer/import/1': Placeholder('1'),
                'importer/upload/2': Placeholder('2'),
            }
        },
        {
            'commits': [Commit(name='b1', message='c')],
            'tags': {'importer/import/1': Placeholder('b1')},
        },
    ),
])
def test_input_edge_cases(tmpdir, repo, before_export, before_import):
    """
    Edge cases in input rich history should reimport without failure

    Check that rich history preservation completes without an exception in
    various cases. Details of each case are described in comments in the test
    parameters above.

    Since these tests generally cover the mutated case (where rich history has
    to be ported forward because parent commits have mutated), we must remove
    and recreate the 'importer/import/1' tag so that it is different before
    attempting reimport. This is done by deleting all ('importer/*') tags and
    then using the before_import parameter to recreate it again.

    :param py.path tmpdir: the pytest standard tmpdir fixture.
    :param GitUbuntuRepository repo: our standard repo fixture.
    :param dict before_export: the parameters to supply to Repo() to construct
        the repository that will be exported.
    :param dict before_import: the parametsrs to supply to Repo() to add to the
        repository before attempting reimport.
    """
    Repo(**before_export).write(repo.raw_repo)
    target.export_all(repo, str(tmpdir))
    repo.delete_tags_in_namespace('importer')
    Repo(**before_import).write(repo.raw_repo)

    # We already test in test_rich_history_preservation() that the import works
    # correctly; this test solely checks that the following call does not raise
    # an exception, so no further assertion is required.
    target.import_single(repo, tmpdir, '2')


def test_preservation_fast_forwards(tmpdir, repo):
    """Rich history that can be fast forwarded should not mutate

    :param py.path tmpdir: the pytest standard tmpdir fixture.
    :param GitUbuntuRepository repo: our standard repo fixture.
    """
    Repo(
        commits=[
            Commit(
                name='1',
                message='a',
            ),
            Commit(
                name='2',
                message='',
                tree=Tree({'a': Blob(b'a')}),
                parents=[Placeholder('1')],
            ),
        ],
        tags={
            'importer/import/1': Placeholder('1'),
            'importer/upload/2': Placeholder('2'),
        }
    ).write(repo.raw_repo)
    target.export_all(repo, str(tmpdir))
    old_upload_ref = repo.raw_repo.lookup_reference(
        'refs/tags/importer/upload/2',
    )
    old_upload_id = old_upload_ref.peel().id
    old_upload_ref.delete()
    target.import_single(repo, tmpdir, '2')
    new_upload_ref = repo.raw_repo.lookup_reference(
        'refs/tags/importer/upload/2',
    )
    new_upload_id = new_upload_ref.peel().id
    assert old_upload_id == new_upload_id