/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1551.12.36 by Aaron Bentley
Fix failing tests
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
18
from bzrlib import (
19
    errors,
1551.12.16 by Aaron Bentley
Enable signing merge directives
20
    gpg,
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
21
    merge_directive,
22
    tests,
23
    )
24
1551.12.4 by Aaron Bentley
Add failing test
25
1551.12.45 by Aaron Bentley
Change format marker to not experimental
26
OUTPUT1 = """# Bazaar merge directive format 1
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
27
# revision_id: example:
28
# target_branch: http://example.com
29
# testament_sha1: sha
30
# timestamp: 1970-01-01 00:09:33 +0002
31
#\x20
32
booga"""
33
34
1551.12.45 by Aaron Bentley
Change format marker to not experimental
35
OUTPUT2 = """# Bazaar merge directive format 1
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
36
# revision_id: example:
37
# target_branch: http://example.com
38
# testament_sha1: sha
39
# timestamp: 1970-01-01 00:09:33 +0002
40
# source_branch: http://example.org
41
# message: Hi mom!
42
#\x20
43
booga"""
44
45
1551.12.51 by Aaron Bentley
Allow leading junk before merge directive header
46
INPUT1 = """
47
I was thinking today about creating a merge directive.
48
49
So I did.
50
51
Here it is.
52
53
(I've pasted it in the body of this message)
54
55
Aaron
56
57
# Bazaar merge directive format 1\r
58
# revision_id: example:
59
# target_branch: http://example.com
60
# testament_sha1: sha
61
# timestamp: 1970-01-01 00:09:33 +0002
62
# source_branch: http://example.org
63
# message: Hi mom!
64
#\x20
65
booga""".splitlines(True)
66
67
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
68
class TestMergeDirective(tests.TestCase):
69
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
70
    def test_merge_source(self):
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
71
        time = 500.0
72
        timezone = 5
73
        self.assertRaises(errors.NoMergeSource, merge_directive.MergeDirective,
74
            'example:', 'sha', time, timezone, 'http://example.com')
75
        self.assertRaises(errors.NoMergeSource, merge_directive.MergeDirective,
76
            'example:', 'sha', time, timezone, 'http://example.com',
77
            patch_type='diff')
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
78
        merge_directive.MergeDirective('example:', 'sha', time, timezone,
1551.12.13 by Aaron Bentley
Rename fields
79
            'http://example.com', source_branch='http://example.org')
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
80
        md = merge_directive.MergeDirective('null:', 'sha', time, timezone,
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
81
            'http://example.com', patch='blah', patch_type='bundle')
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
82
        self.assertIs(None, md.source_branch)
83
        md2 = merge_directive.MergeDirective('null:', 'sha', time, timezone,
84
            'http://example.com', patch='blah', patch_type='bundle',
85
            source_branch='bar')
86
        self.assertEqual('bar', md2.source_branch)
87
88
    def test_require_patch(self):
89
        time = 500.0
90
        timezone = 5
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
91
        self.assertRaises(errors.PatchMissing, merge_directive.MergeDirective,
1551.12.6 by Aaron Bentley
Force times to be floats
92
            'example:', 'sha', time, timezone, 'http://example.com',
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
93
            patch_type='bundle')
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
94
        md = merge_directive.MergeDirective('example:', 'sha1', time, timezone,
1551.12.13 by Aaron Bentley
Rename fields
95
            'http://example.com', source_branch="http://example.org",
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
96
            patch='', patch_type='diff')
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
97
        self.assertEqual(md.patch, '')
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
98
99
    def test_serialization(self):
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
100
        time = 501
101
        timezone = 72
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
102
        md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
103
            'http://example.com', patch='booga', patch_type='bundle')
104
        self.assertEqualDiff(OUTPUT1, ''.join(md.to_lines()))
105
        md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
106
            'http://example.com', source_branch="http://example.org",
107
            patch='booga', patch_type='diff', message="Hi mom!")
108
        self.assertEqualDiff(OUTPUT2, ''.join(md.to_lines()))
109
1551.12.49 by Aaron Bentley
Proper error when deserializing junk
110
    def test_deserialize_junk(self):
111
        self.assertRaises(errors.NotAMergeDirective,
112
                          merge_directive.MergeDirective.from_lines, 'lala')
113
1551.12.51 by Aaron Bentley
Allow leading junk before merge directive header
114
    def test_deserialize_leading_junk(self):
115
        md = merge_directive.MergeDirective.from_lines(INPUT1)
116
        self.assertEqual('example:', md.revision_id)
117
        self.assertEqual('sha', md.testament_sha1)
118
        self.assertEqual('http://example.com', md.target_branch)
119
        self.assertEqual('http://example.org', md.source_branch)
120
        self.assertEqual(501, md.time)
121
        self.assertEqual(72, md.timezone)
122
        self.assertEqual('booga', md.patch)
123
        self.assertEqual('diff', md.patch_type)
124
        self.assertEqual('Hi mom!', md.message)
1551.12.49 by Aaron Bentley
Proper error when deserializing junk
125
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
126
    def test_roundtrip(self):
127
        time = 501
128
        timezone = 72
129
        md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
1551.12.13 by Aaron Bentley
Rename fields
130
            'http://example.com', source_branch="http://example.org",
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
131
            patch='booga', patch_type='diff')
132
        md2 = merge_directive.MergeDirective.from_lines(md.to_lines())
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
133
        self.assertEqual('example:', md2.revision_id)
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
134
        self.assertEqual('sha', md2.testament_sha1)
1551.12.13 by Aaron Bentley
Rename fields
135
        self.assertEqual('http://example.com', md2.target_branch)
136
        self.assertEqual('http://example.org', md2.source_branch)
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
137
        self.assertEqual(time, md2.time)
138
        self.assertEqual(timezone, md2.timezone)
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
139
        self.assertEqual('diff', md2.patch_type)
140
        self.assertEqual('booga', md2.patch)
1551.12.26 by Aaron Bentley
Get email working, with optional message
141
        self.assertEqual(None, md2.message)
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
142
        md.patch = "# Bazaar revision bundle v0.9\n#\n"
1551.12.26 by Aaron Bentley
Get email working, with optional message
143
        md.message = "Hi mom!"
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
144
        md3 = merge_directive.MergeDirective.from_lines(md.to_lines())
145
        self.assertEqual("# Bazaar revision bundle v0.9\n#\n", md3.patch)
146
        self.assertEqual("bundle", md3.patch_type)
1551.12.12 by Aaron Bentley
Add format header
147
        self.assertContainsRe(md3.to_lines()[0],
148
            '^# Bazaar merge directive format ')
1551.12.26 by Aaron Bentley
Get email working, with optional message
149
        self.assertEqual("Hi mom!", md3.message)
150
151
152
EMAIL1 = """To: pqm@example.com
153
From: J. Random Hacker <jrandom@example.com>
154
Subject: Commit of rev2a
155
1551.12.45 by Aaron Bentley
Change format marker to not experimental
156
# Bazaar merge directive format 1
1551.12.26 by Aaron Bentley
Get email working, with optional message
157
# revision_id: rev2a
158
# target_branch: (.|\n)*
159
# testament_sha1: .*
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
160
# timestamp: 1970-01-01 00:08:56 \\+0001
1551.12.26 by Aaron Bentley
Get email working, with optional message
161
# source_branch: (.|\n)*
162
"""
163
164
165
EMAIL2 = """To: pqm@example.com
166
From: J. Random Hacker <jrandom@example.com>
167
Subject: Commit of rev2a with special message
168
1551.12.45 by Aaron Bentley
Change format marker to not experimental
169
# Bazaar merge directive format 1
1551.12.26 by Aaron Bentley
Get email working, with optional message
170
# revision_id: rev2a
171
# target_branch: (.|\n)*
172
# testament_sha1: .*
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
173
# timestamp: 1970-01-01 00:08:56 \\+0001
1551.12.26 by Aaron Bentley
Get email working, with optional message
174
# source_branch: (.|\n)*
175
# message: Commit of rev2a with special message
176
"""
1551.12.4 by Aaron Bentley
Add failing test
177
178
179
class TestMergeDirectiveBranch(tests.TestCaseWithTransport):
180
1551.12.26 by Aaron Bentley
Get email working, with optional message
181
    def make_trees(self):
1551.12.4 by Aaron Bentley
Add failing test
182
        tree_a = self.make_branch_and_tree('tree_a')
1551.12.26 by Aaron Bentley
Get email working, with optional message
183
        tree_a.branch.get_config().set_user_option('email',
184
            'J. Random Hacker <jrandom@example.com>')
1551.12.4 by Aaron Bentley
Add failing test
185
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_b\n')])
186
        tree_a.add('file')
187
        tree_a.commit('message', rev_id='rev1')
188
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
189
        branch_c = tree_a.bzrdir.sprout('branch_c').open_branch()
1551.12.4 by Aaron Bentley
Add failing test
190
        tree_b.commit('message', rev_id='rev2b')
191
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c\n')])
1551.12.26 by Aaron Bentley
Get email working, with optional message
192
        tree_a.commit('Commit of rev2a', rev_id='rev2a')
193
        return tree_a, tree_b, branch_c
194
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
195
    def test_generate_patch(self):
196
        tree_a, tree_b, branch_c = self.make_trees()
197
        md2 = merge_directive.MergeDirective.from_objects(
198
            tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
199
            patch_type='diff', public_branch=tree_a.branch.base)
200
        self.assertNotContainsRe(md2.patch, 'Bazaar revision bundle')
201
        self.assertContainsRe(md2.patch, '\\+content_c')
202
        self.assertNotContainsRe(md2.patch, '\\+\\+\\+ b/')
203
        self.assertContainsRe(md2.patch, '\\+\\+\\+ file')
204
205
    def test_public_branch(self):
1551.12.26 by Aaron Bentley
Get email working, with optional message
206
        tree_a, tree_b, branch_c = self.make_trees()
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
207
        self.assertRaises(errors.PublicBranchOutOfDate,
208
            merge_directive.MergeDirective.from_objects,
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
209
            tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
1551.12.34 by Aaron Bentley
Check public branch only if not using a bundle
210
            public_branch=branch_c.base, patch_type='diff')
211
        # public branch is not checked if patch format is bundle.
212
        md1 = merge_directive.MergeDirective.from_objects(
213
            tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
1551.12.33 by Aaron Bentley
Take public_branch as a string, not object
214
            public_branch=branch_c.base)
1551.12.34 by Aaron Bentley
Check public branch only if not using a bundle
215
        # public branch is provided with a bundle, despite possibly being out
216
        # of date, because it's not required if a bundle is present.
217
        self.assertEqual(md1.source_branch, branch_c.base)
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
218
        # Once we update the public branch, we can generate a diff.
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
219
        branch_c.pull(tree_a.branch)
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
220
        md3 = merge_directive.MergeDirective.from_objects(
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
221
            tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
222
            patch_type=None, public_branch=branch_c.base)
223
1551.12.50 by Aaron Bentley
Use public location of submit branch if possible
224
    def test_use_public_submit_branch(self):
225
        tree_a, tree_b, branch_c = self.make_trees()
226
        branch_c.pull(tree_a.branch)
227
        md = merge_directive.MergeDirective.from_objects(
228
             tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
229
             patch_type=None, public_branch=branch_c.base)
230
        self.assertEqual(md.target_branch, tree_b.branch.base)
231
        tree_b.branch.set_public_branch('http://example.com')
232
        md2 = merge_directive.MergeDirective.from_objects(
233
              tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
234
              patch_type=None, public_branch=branch_c.base)
235
        self.assertEqual(md2.target_branch, 'http://example.com')
236
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
237
    def test_message(self):
238
        tree_a, tree_b, branch_c = self.make_trees()
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
239
        md3 = merge_directive.MergeDirective.from_objects(
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
240
            tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
1551.12.33 by Aaron Bentley
Take public_branch as a string, not object
241
            patch_type=None, public_branch=branch_c.base,
242
            message='Merge message')
1551.12.7 by Aaron Bentley
Fix use of public location/branch
243
        md3.to_lines()
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
244
        self.assertIs(None, md3.patch)
1551.12.27 by Aaron Bentley
support custom message everywhere
245
        self.assertEqual('Merge message', md3.message)
1551.12.16 by Aaron Bentley
Enable signing merge directives
246
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
247
    def test_generate_bundle(self):
1551.12.40 by Aaron Bentley
Do not show prefixes in diffs
248
        tree_a, tree_b, branch_c = self.make_trees()
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
249
        md1 = merge_directive.MergeDirective.from_objects(
1551.12.40 by Aaron Bentley
Do not show prefixes in diffs
250
            tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
1551.12.41 by Aaron Bentley
Clean up tests, add serialization text test
251
            public_branch=branch_c.base)
252
        self.assertContainsRe(md1.patch, 'Bazaar revision bundle')
253
        self.assertContainsRe(md1.patch, '\\+content_c')
254
        self.assertNotContainsRe(md1.patch, '\\+content_a')
255
        self.assertContainsRe(md1.patch, '\\+content_c')
256
        self.assertNotContainsRe(md1.patch, '\\+content_a')
1551.12.40 by Aaron Bentley
Do not show prefixes in diffs
257
1551.12.16 by Aaron Bentley
Enable signing merge directives
258
    def test_signing(self):
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
259
        time = 501
260
        timezone = 72
1551.12.16 by Aaron Bentley
Enable signing merge directives
261
        class FakeBranch(object):
262
            def get_config(self):
263
                return self
264
            def gpg_signing_command(self):
265
                return 'loopback'
266
        md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
267
            'http://example.com', source_branch="http://example.org",
268
            patch='booga', patch_type='diff')
269
        old_strategy = gpg.GPGStrategy
270
        gpg.GPGStrategy = gpg.LoopbackGPGStrategy
271
        try:
272
            signed = md.to_signed(FakeBranch())
273
        finally:
274
            gpg.GPGStrategy = old_strategy
275
        self.assertContainsRe(signed, '^-----BEGIN PSEUDO-SIGNED CONTENT')
276
        self.assertContainsRe(signed, 'example.org')
277
        self.assertContainsRe(signed, 'booga')
1551.12.26 by Aaron Bentley
Get email working, with optional message
278
279
    def test_email(self):
280
        tree_a, tree_b, branch_c = self.make_trees()
281
        md = merge_directive.MergeDirective.from_objects(
1551.12.30 by Aaron Bentley
Use patch-style dates for timestamps in merge directives
282
            tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
1551.12.33 by Aaron Bentley
Take public_branch as a string, not object
283
            patch_type=None, public_branch=tree_a.branch.base)
1551.12.26 by Aaron Bentley
Get email working, with optional message
284
        message = md.to_email('pqm@example.com', tree_a.branch)
285
        self.assertContainsRe(message.as_string(), EMAIL1)
286
        md.message = 'Commit of rev2a with special message'
287
        message = md.to_email('pqm@example.com', tree_a.branch)
288
        self.assertContainsRe(message.as_string(), EMAIL2)