~jelmer/brz-debian/setup

« back to all changes in this revision

Viewing changes to quilt/tests/test_merge.py

  • Committer: Jelmer Vernooij
  • Date: 2020-06-09 23:39:42 UTC
  • Revision ID: jelmer@jelmer.uk-20200609233942-98y54ecy7iznd97e
Drop the quilt code - it's been merged into breezy proper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    Copyright (C) 2011 Canonical Ltd
2
 
#
3
 
#    This file is part of bzr-builddeb.
4
 
#
5
 
#    bzr-builddeb is free software; you can redistribute it and/or modify
6
 
#    it under the terms of the GNU General Public License as published by
7
 
#    the Free Software Foundation; either version 2 of the License, or
8
 
#    (at your option) any later version.
9
 
#
10
 
#    bzr-builddeb is distributed in the hope that it will be useful,
11
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
#    GNU General Public License for more details.
14
 
#
15
 
#    You should have received a copy of the GNU General Public License
16
 
#    along with bzr-builddeb; if not, write to the Free Software
17
 
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
#
19
 
 
20
 
"""Tests for the merge_quilt code."""
21
 
 
22
 
from __future__ import absolute_import
23
 
 
24
 
import os
25
 
import shutil
26
 
 
27
 
from ..... import (
28
 
    config,
29
 
    errors,
30
 
    trace,
31
 
    )
32
 
from .....bedding import ensure_config_dir_exists
33
 
from .....merge import Merger
34
 
from .....mutabletree import MutableTree
35
 
 
36
 
from .. import (
37
 
    pre_merge_quilt,
38
 
    start_commit_check_quilt,
39
 
    post_build_tree_quilt,
40
 
    post_merge_quilt_cleanup,
41
 
    )
42
 
from ..quilt import QuiltPatches
43
 
from ..merge import tree_unapply_patches
44
 
 
45
 
from .test_wrapper import quilt_feature
46
 
 
47
 
from .....tests import (
48
 
    TestCaseWithTransport,
49
 
    TestSkipped,
50
 
    )
51
 
 
52
 
TRIVIAL_PATCH = """--- /dev/null        2012-01-02 01:09:10.986490031 +0100
53
 
+++ base/a      2012-01-02 20:03:59.710666215 +0100
54
 
@@ -0,0 +1 @@
55
 
+a
56
 
"""
57
 
 
58
 
 
59
 
def quilt_push_all(tree):
60
 
    QuiltPatches(tree, 'debian/patches').push_all()
61
 
 
62
 
 
63
 
class TestTreeUnapplyPatches(TestCaseWithTransport):
64
 
 
65
 
    _test_needs_features = [quilt_feature]
66
 
 
67
 
    def test_no_patches(self):
68
 
        tree = self.make_branch_and_tree('.')
69
 
        new_tree, target_dir = tree_unapply_patches(tree)
70
 
        self.assertIs(tree, new_tree)
71
 
        self.assertIs(None, target_dir)
72
 
 
73
 
    def test_unapply(self):
74
 
        orig_tree = self.make_branch_and_tree('source')
75
 
        self.build_tree(["source/debian/", "source/debian/patches/"])
76
 
        self.build_tree_contents([
77
 
            ("source/debian/patches/series", "patch1.diff\n"),
78
 
            ("source/debian/patches/patch1.diff", TRIVIAL_PATCH)])
79
 
        quilt_push_all(orig_tree)
80
 
        orig_tree.smart_add([orig_tree.basedir])
81
 
        tree, target_dir = tree_unapply_patches(orig_tree)
82
 
        self.addCleanup(shutil.rmtree, target_dir)
83
 
        self.assertPathExists("source/a")
84
 
        self.assertNotEqual(tree.basedir, orig_tree.basedir)
85
 
        self.assertPathDoesNotExist(tree.abspath("a"))
86
 
        self.assertPathExists(tree.abspath("debian/patches/series"))
87
 
 
88
 
    def test_unapply_nothing_applied(self):
89
 
        orig_tree = self.make_branch_and_tree('source')
90
 
        self.build_tree(["source/debian/", "source/debian/patches/"])
91
 
        self.build_tree_contents([
92
 
            ("source/debian/patches/series", "patch1.diff\n"),
93
 
            ("source/debian/patches/patch1.diff", TRIVIAL_PATCH)])
94
 
        orig_tree.smart_add([orig_tree.basedir])
95
 
        tree, target_dir = tree_unapply_patches(orig_tree)
96
 
        self.assertIs(tree, orig_tree)
97
 
        self.assertIs(None, target_dir)
98
 
 
99
 
 
100
 
class TestMergeHook(TestCaseWithTransport):
101
 
 
102
 
    _test_needs_features = [quilt_feature]
103
 
 
104
 
    def enable_hooks(self):
105
 
        Merger.hooks.install_named_hook(
106
 
            'pre_merge', pre_merge_quilt,
107
 
            'Debian quilt patch (un)applying and ancestry fixing')
108
 
        Merger.hooks.install_named_hook(
109
 
            'post_merge', post_merge_quilt_cleanup,
110
 
            'Cleaning up quilt temporary directories')
111
 
        MutableTree.hooks.install_named_hook(
112
 
            "post_build_tree", post_build_tree_quilt,
113
 
            "Apply quilt trees.")
114
 
 
115
 
    def test_diverged_patches(self):
116
 
        self.enable_hooks()
117
 
 
118
 
        tree_a = self.make_branch_and_tree('a')
119
 
        self.build_tree(
120
 
            ['a/debian/', 'a/debian/patches/', 'a/debian/source/', 'a/.pc/'])
121
 
        self.build_tree_contents([
122
 
            ('a/.pc/.quilt_patches', 'debian/patches'),
123
 
            ('a/.pc/.version', '2'),
124
 
            ('a/debian/source/format', '3.0 (quilt)'),
125
 
            ('a/debian/patches/series', 'patch1\n'),
126
 
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
127
 
        tree_a.smart_add([tree_a.basedir])
128
 
        tree_a.commit('initial')
129
 
 
130
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
131
 
        self.build_tree_contents([
132
 
            ('a/debian/patches/patch1',
133
 
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+d\n"]))])
134
 
        quilt_push_all(tree_a)
135
 
        tree_a.smart_add([tree_a.basedir])
136
 
        tree_a.commit('apply patches')
137
 
        self.build_tree_contents([
138
 
            ('b/debian/patches/patch1',
139
 
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+c\n"]))])
140
 
        quilt_push_all(tree_b)
141
 
        tree_b.commit('apply patches')
142
 
        conflicts = tree_a.merge_from_branch(tree_b.branch)
143
 
        self.assertFileEqual("""\
144
 
--- /dev/null\t2012-01-02 01:09:10.986490031 +0100
145
 
+++ base/a\t2012-01-02 20:03:59.710666215 +0100
146
 
@@ -0,0 +1 @@
147
 
<<<<<<< TREE
148
 
+d
149
 
=======
150
 
+c
151
 
>>>>>>> MERGE-SOURCE
152
 
""", "a/debian/patches/patch1")
153
 
        # "a" should be unapplied again
154
 
        self.assertPathDoesNotExist("a/a")
155
 
        self.assertEquals(1, conflicts)
156
 
 
157
 
    def test_auto_apply_patches_after_checkout(self):
158
 
        self.enable_hooks()
159
 
 
160
 
        tree_a = self.make_branch_and_tree('a')
161
 
 
162
 
        self.build_tree(['a/debian/', 'a/debian/patches/'])
163
 
        self.build_tree_contents([
164
 
            ('a/debian/patches/series', 'patch1\n'),
165
 
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
166
 
        tree_a.smart_add([tree_a.basedir])
167
 
        tree_a.commit('initial')
168
 
 
169
 
        ensure_config_dir_exists()
170
 
        config.GlobalStack().set('quilt.tree_policy', 'applied')
171
 
 
172
 
        tree_a.branch.create_checkout("b")
173
 
        self.assertFileEqual("a\n", "b/a")
174
 
 
175
 
    def test_auto_apply_patches_after_update_format_1(self):
176
 
        self.enable_hooks()
177
 
 
178
 
        tree_a = self.make_branch_and_tree('a')
179
 
        tree_b = tree_a.branch.create_checkout("b")
180
 
 
181
 
        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/.pc/'])
182
 
        self.build_tree_contents([
183
 
            ('a/.pc/.quilt_patches', 'debian/patches'),
184
 
            ('a/.pc/.version', '2'),
185
 
            ('a/debian/patches/series', 'patch1\n'),
186
 
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
187
 
        tree_a.smart_add([tree_a.basedir])
188
 
        tree_a.commit('initial')
189
 
 
190
 
        self.build_tree(["b/.bzr-builddeb/", "b/debian/", "b/debian/source/"])
191
 
        tree_b.get_config_stack().set('quilt.tree_policy', 'applied')
192
 
        self.build_tree_contents([
193
 
            ("b/debian/source/format", "1.0")])
194
 
 
195
 
        tree_b.update()
196
 
        self.assertFileEqual("a\n", "b/a")
197
 
 
198
 
    def test_auto_apply_patches_after_update(self):
199
 
        self.enable_hooks()
200
 
 
201
 
        tree_a = self.make_branch_and_tree('a')
202
 
        tree_b = tree_a.branch.create_checkout("b")
203
 
 
204
 
        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/debian/source/', 'a/.pc/'])
205
 
        self.build_tree_contents([
206
 
            ('a/.pc/.quilt_patches', 'debian/patches'),
207
 
            ('a/.pc/.version', '2'),
208
 
            ('a/debian/source/format', '3.0 (quilt)'),
209
 
            ('a/debian/patches/series', 'patch1\n'),
210
 
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
211
 
        tree_a.smart_add([tree_a.basedir])
212
 
        tree_a.commit('initial')
213
 
 
214
 
        self.build_tree(["b/.bzr-builddeb/", "b/debian/", "b/debian/source/"])
215
 
        tree_b.get_config_stack().set('quilt.tree_policy', 'applied')
216
 
        self.build_tree_contents([
217
 
            ('b/debian/source/format', '3.0 (quilt)'),
218
 
            ])
219
 
 
220
 
        tree_b.update()
221
 
        self.assertFileEqual("a\n", "b/a")
222
 
 
223
 
    def test_auto_unapply_patches_after_update(self):
224
 
        self.enable_hooks()
225
 
 
226
 
        tree_a = self.make_branch_and_tree('a')
227
 
        tree_b = tree_a.branch.create_checkout("b")
228
 
 
229
 
        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/debian/source/', 'a/.pc/'])
230
 
        self.build_tree_contents([
231
 
            ('a/.pc/.quilt_patches', 'debian/patches'),
232
 
            ('a/.pc/.version', '2'),
233
 
            ('a/debian/source/format', '3.0 (quilt)'),
234
 
            ('a/debian/patches/series', 'patch1\n'),
235
 
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
236
 
        tree_a.smart_add([tree_a.basedir])
237
 
        tree_a.commit('initial')
238
 
 
239
 
        self.build_tree(["b/.bzr-builddeb/"])
240
 
        tree_b.get_config_stack().set('quilt.tree_policy', 'unapplied')
241
 
 
242
 
        tree_b.update()
243
 
        self.assertPathDoesNotExist("b/a")
244
 
 
245
 
    def test_disabled_hook(self):
246
 
        self.enable_hooks()
247
 
 
248
 
        tree_a = self.make_branch_and_tree('a')
249
 
        tree_a.get_config_stack().set('quilt.smart_merge', False)
250
 
        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/.pc/'])
251
 
        self.build_tree_contents([
252
 
            ('a/.pc/.quilt_patches', 'debian/patches'),
253
 
            ('a/.pc/.version', '2'),
254
 
            ('a/debian/patches/series', 'patch1\n'),
255
 
            ('a/debian/patches/patch1', TRIVIAL_PATCH),
256
 
            ("a/a", "")])
257
 
        tree_a.smart_add([tree_a.basedir])
258
 
        tree_a.commit('initial')
259
 
 
260
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
261
 
        self.build_tree_contents([
262
 
            ('a/debian/patches/patch1',
263
 
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+d\n"]))])
264
 
        quilt_push_all(tree_a)
265
 
        tree_a.smart_add([tree_a.basedir])
266
 
        tree_a.commit('apply patches')
267
 
        self.assertFileEqual("d\n", "a/a")
268
 
        self.build_tree_contents([
269
 
            ('b/debian/patches/patch1',
270
 
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+c\n"]))])
271
 
        quilt_push_all(tree_b)
272
 
        tree_b.commit('apply patches')
273
 
        self.assertFileEqual("c\n", "b/a")
274
 
        conflicts = tree_a.merge_from_branch(tree_b.branch)
275
 
        self.assertFileEqual("""\
276
 
--- /dev/null\t2012-01-02 01:09:10.986490031 +0100
277
 
+++ base/a\t2012-01-02 20:03:59.710666215 +0100
278
 
@@ -0,0 +1 @@
279
 
<<<<<<< TREE
280
 
+d
281
 
=======
282
 
+c
283
 
>>>>>>> MERGE-SOURCE
284
 
""", "a/debian/patches/patch1")
285
 
        self.assertFileEqual("""\
286
 
<<<<<<< TREE
287
 
d
288
 
=======
289
 
c
290
 
>>>>>>> MERGE-SOURCE
291
 
""", "a/a")
292
 
        self.assertEquals(2, conflicts)
293
 
 
294
 
 
295
 
 
296
 
class StartCommitMergeHookTests(TestCaseWithTransport):
297
 
 
298
 
    def enable_hooks(self):
299
 
        MutableTree.hooks.install_named_hook(
300
 
            'start_commit', start_commit_check_quilt,
301
 
            'Check for (un)applied quilt patches')
302
 
 
303
 
    def test_applied(self):
304
 
        self.enable_hooks()
305
 
        tree = self.make_branch_and_tree('source')
306
 
        tree.get_config_stack().set('quilt.commit_policy', 'applied')
307
 
        self.build_tree(['source/debian/', 'source/debian/patches/',
308
 
                         'source/debian/source/'])
309
 
        self.build_tree_contents([
310
 
            ('source/debian/source/format', '3.0 (quilt)'),
311
 
            ('source/debian/patches/series', 'patch1\n'),
312
 
            ('source/debian/patches/patch1', TRIVIAL_PATCH)])
313
 
        self.assertPathDoesNotExist("source/.pc/applied-patches")
314
 
        self.assertPathDoesNotExist("source/a")
315
 
        tree.smart_add([tree.basedir])
316
 
        tree.commit("foo")
317
 
        self.assertPathExists("source/.pc/applied-patches")
318
 
        self.assertPathExists("source/a")
319
 
 
320
 
    def test_unapplied(self):
321
 
        self.enable_hooks()
322
 
        tree = self.make_branch_and_tree('source')
323
 
        tree.get_config_stack().set('quilt.commit_policy', 'unapplied')
324
 
        self.build_tree(
325
 
            ['source/debian/', 'source/debian/patches/',
326
 
             'source/debian/source/'])
327
 
        self.build_tree_contents([
328
 
            ('source/debian/patches/series', 'patch1\n'),
329
 
            ('source/debian/patches/patch1', TRIVIAL_PATCH),
330
 
            ('source/debian/source/format', '3.0 (quilt)')])
331
 
        quilt_push_all(tree)
332
 
        self.assertPathExists("source/.pc/applied-patches")
333
 
        self.assertPathExists("source/a")
334
 
        tree.smart_add([tree.basedir])
335
 
        tree.commit("foo")
336
 
        self.assertPathDoesNotExist("source/.pc/applied-patches")
337
 
        self.assertPathDoesNotExist("source/a")
338
 
 
339
 
    def test_warning(self):
340
 
        self.enable_hooks()
341
 
        warnings = []
342
 
 
343
 
        def warning(*args):
344
 
            if len(args) > 1:
345
 
                warnings.append(args[0] % args[1:])
346
 
            else:
347
 
                warnings.append(args[0])
348
 
        _warning = trace.warning
349
 
        trace.warning = warning
350
 
        self.addCleanup(setattr, trace, "warning", _warning)
351
 
        tree = self.make_branch_and_tree('source')
352
 
        self.build_tree(['source/debian/', 'source/debian/patches/',
353
 
                         'source/debian/source/'])
354
 
        self.build_tree_contents([
355
 
            ('source/debian/patches/series', 'patch1\n'),
356
 
            ('source/debian/patches/patch1', TRIVIAL_PATCH)])
357
 
        quilt_push_all(tree)
358
 
        tree.smart_add([tree.basedir])
359
 
        tree.commit("initial")
360
 
        self.assertEquals([], warnings)
361
 
        self.assertPathExists("source/.pc/applied-patches")
362
 
        self.assertPathExists("source/a")
363
 
        self.build_tree_contents([
364
 
            ('source/debian/source/format', '3.0 (quilt)'),
365
 
            ('source/debian/patches/series', 'patch1\npatch2\n'),
366
 
            ('source/debian/patches/patch2',
367
 
                """--- /dev/null        2012-01-02 01:09:10.986490031 +0100
368
 
+++ base/b      2012-01-02 20:03:59.710666215 +0100
369
 
@@ -0,0 +1 @@
370
 
+a
371
 
""")])
372
 
        tree.smart_add([tree.basedir])
373
 
        tree.commit("foo")
374
 
        self.assertEquals(
375
 
            ['Committing with 1 patches applied and 1 patches unapplied.'],
376
 
            warnings)
377
 
        self.assertPathExists("source/.pc/applied-patches")
378
 
        self.assertPathExists("source/a")
379
 
        self.assertPathDoesNotExist("source/b")