~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_testament.py

  • Committer: Martin Pool
  • Date: 2005-05-30 01:39:13 UTC
  • Revision ID: mbp@sourcefrog.net-20050530013913-4ac43c29e1302170
- make sure any bzr output is flushed before 
  running external diff

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