/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 bzrlib/tests/test_testament.py

Fix strict testaments, as_sha1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by 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
"""Test testaments for gpg signing."""
 
18
 
 
19
# TODO: Testaments with x-bits
 
20
 
 
21
import os
 
22
from sha import sha
 
23
import sys
 
24
 
 
25
from bzrlib.tests import TestCaseWithTransport
 
26
from bzrlib.branch import Branch
 
27
from bzrlib.testament import Testament, StrictTestament
 
28
from bzrlib.trace import mutter
 
29
from bzrlib.osutils import has_symlinks
 
30
 
 
31
 
 
32
class TestamentTests(TestCaseWithTransport):
 
33
 
 
34
    def setUp(self):
 
35
        super(TestamentTests, self).setUp()
 
36
        self.wt = self.make_branch_and_tree('.')
 
37
        b = self.b = self.wt.branch
 
38
        b.nick = "test branch"
 
39
        self.wt.commit(message='initial null commit',
 
40
                 committer='test@user',
 
41
                 timestamp=1129025423, # 'Tue Oct 11 20:10:23 2005'
 
42
                 timezone=0,
 
43
                 rev_id='test@user-1')
 
44
        self.build_tree_contents([('hello', 'contents of hello file'),
 
45
                             ('src/', ),
 
46
                             ('src/foo.c', 'int main()\n{\n}\n')])
 
47
        self.wt.add(['hello', 'src', 'src/foo.c'],
 
48
                             ['hello-id', 'src-id', 'foo.c-id'])
 
49
        self.wt.commit(message='add files and directories',
 
50
                 timestamp=1129025483,
 
51
                 timezone=36000,
 
52
                 rev_id='test@user-2',
 
53
                 committer='test@user')
 
54
 
 
55
    def test_null_testament(self):
 
56
        """Testament for a revision with no contents."""
 
57
        t = Testament.from_revision(self.b.repository, 'test@user-1')
 
58
        ass = self.assertTrue
 
59
        eq = self.assertEqual
 
60
        ass(isinstance(t, Testament))
 
61
        eq(t.revision_id, 'test@user-1')
 
62
        eq(t.committer, 'test@user')
 
63
        eq(t.timestamp, 1129025423)
 
64
        eq(t.timezone, 0)
 
65
 
 
66
    def test_testment_text_form(self):
 
67
        """Conversion of testament to canonical text form."""
 
68
        t = Testament.from_revision(self.b.repository, 'test@user-1')
 
69
        text_form = t.as_text()
 
70
        self.log('testament text form:\n' + text_form)
 
71
        self.assertEqual(text_form, REV_1_TESTAMENT)
 
72
 
 
73
    def test_strict_testment_text_form(self):
 
74
        """Conversion of testament to canonical text form."""
 
75
        t = StrictTestament.from_revision(self.b.repository, 'test@user-1')
 
76
        text_form = t.as_text()
 
77
        self.log('testament text form:\n' + text_form)
 
78
        self.assertEqualDiff(text_form, REV_1_STRICT_TESTAMENT)
 
79
 
 
80
    def test_testament_with_contents(self):
 
81
        """Testament containing a file and a directory."""
 
82
        t = Testament.from_revision(self.b.repository, 'test@user-2')
 
83
        text_form = t.as_text()
 
84
        self.log('testament text form:\n' + text_form)
 
85
        self.assertEqualDiff(text_form, REV_2_TESTAMENT)
 
86
        actual_short = t.as_short_text()
 
87
        self.assertEqualDiff(actual_short, REV_2_SHORT)
 
88
 
 
89
    def test_strict_testament_with_contents(self):
 
90
        """Testament containing a file and a directory."""
 
91
        t = StrictTestament.from_revision(self.b.repository, 'test@user-2')
 
92
        text_form = t.as_text()
 
93
        self.log('testament text form:\n' + text_form)
 
94
        self.assertEqualDiff(text_form, REV_2_STRICT_TESTAMENT)
 
95
        actual_short = t.as_short_text()
 
96
        self.assertEqualDiff(actual_short, REV_2_SHORT_STRICT)
 
97
 
 
98
    def test_testament_command(self):
 
99
        """Testament containing a file and a directory."""
 
100
        out, err = self.run_bzr_captured(['testament', '--long'])
 
101
        self.assertEqualDiff(err, '')
 
102
        self.assertEqualDiff(out, REV_2_TESTAMENT)
 
103
 
 
104
    def test_testament_command_2(self):
 
105
        """Command getting short testament of previous version."""
 
106
        out, err = self.run_bzr_captured(['testament', '-r1'])
 
107
        self.assertEqualDiff(err, '')
 
108
        self.assertEqualDiff(out, REV_1_SHORT)
 
109
 
 
110
    def test_testament_command_3(self):
 
111
        """Command getting short testament of previous version."""
 
112
        out, err = self.run_bzr_captured(['testament', '-r1', '--strict'])
 
113
        self.assertEqualDiff(err, '')
 
114
        self.assertEqualDiff(out, REV_1_SHORT_STRICT)
 
115
 
 
116
    def test_testament_symlinks(self):
 
117
        """Testament containing symlink (where possible)"""
 
118
        if not has_symlinks():
 
119
            return
 
120
        os.symlink('wibble/linktarget', 'link')
 
121
        self.wt.add(['link'], ['link-id'])
 
122
        self.wt.commit(message='add symlink',
 
123
                 timestamp=1129025493,
 
124
                 timezone=36000,
 
125
                 rev_id='test@user-3',
 
126
                 committer='test@user')
 
127
        t = Testament.from_revision(self.b.repository, 'test@user-3')
 
128
        self.assertEqualDiff(t.as_text(), REV_3_TESTAMENT)
 
129
 
 
130
    def test_testament_revprops(self):
 
131
        """Testament to revision with extra properties"""
 
132
        props = dict(flavor='sour cherry\ncream cheese',
 
133
                     size='medium')
 
134
        self.wt.commit(message='revision with properties',
 
135
                      timestamp=1129025493,
 
136
                      timezone=36000,
 
137
                      rev_id='test@user-3',
 
138
                      committer='test@user',
 
139
                      revprops=props)
 
140
        t = Testament.from_revision(self.b.repository, 'test@user-3')
 
141
        self.assertEqualDiff(t.as_text(), REV_PROPS_TESTAMENT)
 
142
 
 
143
    def test_testament_unicode_commit_message(self):
 
144
        self.wt.commit(
 
145
            message=u'non-ascii commit \N{COPYRIGHT SIGN} me',
 
146
            timestamp=1129025493,
 
147
            timezone=36000,
 
148
            rev_id='test@user-3',
 
149
            committer='test@user')
 
150
        t = Testament.from_revision(self.b.repository, 'test@user-3')
 
151
        self.assertEqualDiff(
 
152
            SAMPLE_UNICODE_TESTAMENT.encode('utf-8'), t.as_text())
 
153
 
 
154
    def test___init__(self):
 
155
        revision = self.b.repository.get_revision('test@user-2')
 
156
        inventory = self.b.repository.get_inventory('test@user-2')
 
157
        testament_1 = Testament(revision, inventory).as_short_text()
 
158
        testament_2 = Testament.from_revision(self.b.repository, 
 
159
                                              'test@user-2').as_short_text()
 
160
        self.assertEqual(testament_1, testament_2)
 
161
                    
 
162
 
 
163
REV_1_TESTAMENT = """\
 
164
bazaar-ng testament version 1
 
165
revision-id: test@user-1
 
166
committer: test@user
 
167
timestamp: 1129025423
 
168
timezone: 0
 
169
parents:
 
170
message:
 
171
  initial null commit
 
172
inventory:
 
173
properties:
 
174
  branch-nick:
 
175
    test branch
 
176
"""
 
177
 
 
178
REV_1_STRICT_TESTAMENT = """\
 
179
bazaar-ng testament version 2.1
 
180
revision-id: test@user-1
 
181
committer: test@user
 
182
timestamp: 1129025423
 
183
timezone: 0
 
184
parents:
 
185
message:
 
186
  initial null commit
 
187
inventory:
 
188
properties:
 
189
  branch-nick:
 
190
    test branch
 
191
"""
 
192
 
 
193
 
 
194
REV_1_SHORT = """\
 
195
bazaar-ng testament short form 1
 
196
revision-id: test@user-1
 
197
sha1: %s
 
198
""" % sha(REV_1_TESTAMENT).hexdigest()
 
199
 
 
200
 
 
201
REV_1_SHORT_STRICT = """\
 
202
bazaar-ng testament short form 2.1
 
203
revision-id: test@user-1
 
204
sha1: %s
 
205
""" % sha(REV_1_STRICT_TESTAMENT).hexdigest()
 
206
 
 
207
 
 
208
REV_2_TESTAMENT = """\
 
209
bazaar-ng testament version 1
 
210
revision-id: test@user-2
 
211
committer: test@user
 
212
timestamp: 1129025483
 
213
timezone: 36000
 
214
parents:
 
215
  test@user-1
 
216
message:
 
217
  add files and directories
 
218
inventory:
 
219
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
 
220
  directory src src-id
 
221
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
 
222
properties:
 
223
  branch-nick:
 
224
    test branch
 
225
"""
 
226
 
 
227
 
 
228
REV_2_STRICT_TESTAMENT = """\
 
229
bazaar-ng testament version 2.1
 
230
revision-id: test@user-2
 
231
committer: test@user
 
232
timestamp: 1129025483
 
233
timezone: 36000
 
234
parents:
 
235
  test@user-1
 
236
message:
 
237
  add files and directories
 
238
inventory:
 
239
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 no
 
240
  directory src src-id test@user-2 no
 
241
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
 
242
properties:
 
243
  branch-nick:
 
244
    test branch
 
245
"""
 
246
 
 
247
 
 
248
REV_2_SHORT = """\
 
249
bazaar-ng testament short form 1
 
250
revision-id: test@user-2
 
251
sha1: %s
 
252
""" % sha(REV_2_TESTAMENT).hexdigest()
 
253
 
 
254
 
 
255
REV_2_SHORT_STRICT = """\
 
256
bazaar-ng testament short form 2.1
 
257
revision-id: test@user-2
 
258
sha1: %s
 
259
""" % sha(REV_2_STRICT_TESTAMENT).hexdigest()
 
260
 
 
261
 
 
262
REV_PROPS_TESTAMENT = """\
 
263
bazaar-ng testament version 1
 
264
revision-id: test@user-3
 
265
committer: test@user
 
266
timestamp: 1129025493
 
267
timezone: 36000
 
268
parents:
 
269
  test@user-2
 
270
message:
 
271
  revision with properties
 
272
inventory:
 
273
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
 
274
  directory src src-id
 
275
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
 
276
properties:
 
277
  branch-nick:
 
278
    test branch
 
279
  flavor:
 
280
    sour cherry
 
281
    cream cheese
 
282
  size:
 
283
    medium
 
284
"""
 
285
 
 
286
 
 
287
REV_3_TESTAMENT = """\
 
288
bazaar-ng testament version 1
 
289
revision-id: test@user-3
 
290
committer: test@user
 
291
timestamp: 1129025493
 
292
timezone: 36000
 
293
parents:
 
294
  test@user-2
 
295
message:
 
296
  add symlink
 
297
inventory:
 
298
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
 
299
  symlink link link-id wibble/linktarget
 
300
  directory src src-id
 
301
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
 
302
properties:
 
303
  branch-nick:
 
304
    test branch
 
305
"""
 
306
 
 
307
 
 
308
SAMPLE_UNICODE_TESTAMENT = u"""\
 
309
bazaar-ng testament version 1
 
310
revision-id: test@user-3
 
311
committer: test@user
 
312
timestamp: 1129025493
 
313
timezone: 36000
 
314
parents:
 
315
  test@user-2
 
316
message:
 
317
  non-ascii commit \N{COPYRIGHT SIGN} me
 
318
inventory:
 
319
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
 
320
  directory src src-id
 
321
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
 
322
properties:
 
323
  branch-nick:
 
324
    test branch
 
325
"""