~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_testament.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-07 20:20:17 UTC
  • mto: This revision was merged to the branch mainline in revision 1990.
  • Revision ID: john@arbash-meinel.com-20060907202017-025a3518df901ea5
import sys in gpg.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Test testaments for gpg signing."""
18
18
 
19
19
# TODO: Testaments with x-bits
20
20
 
21
21
import os
 
22
from sha import sha
22
23
 
23
 
from bzrlib import osutils
24
 
from bzrlib.tests import SymlinkFeature, TestCaseWithTransport
25
 
from bzrlib.testament import (
26
 
    Testament,
27
 
    StrictTestament,
28
 
    StrictTestament3,
29
 
    )
 
24
from bzrlib.tests import TestCaseWithTransport
 
25
from bzrlib.testament import Testament, StrictTestament
30
26
from bzrlib.transform import TreeTransform
 
27
from bzrlib.osutils import has_symlinks
31
28
 
32
29
 
33
30
class TestamentSetup(TestCaseWithTransport):
34
31
 
35
32
    def setUp(self):
36
33
        super(TestamentSetup, self).setUp()
37
 
        self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
38
 
        self.wt.set_root_id('TREE_ROT')
 
34
        self.wt = self.make_branch_and_tree('.')
39
35
        b = self.b = self.wt.branch
40
36
        b.nick = "test branch"
41
37
        self.wt.commit(message='initial null commit',
61
57
 
62
58
class TestamentTests(TestamentSetup):
63
59
 
64
 
    def testament_class(self):
65
 
        return Testament
66
 
 
67
 
    def expected(self, key):
68
 
        return texts[self.testament_class()][key]
69
 
 
70
 
    def from_revision(self, repository, revision_id):
71
 
        return self.testament_class().from_revision(repository, revision_id)
72
 
 
73
60
    def test_null_testament(self):
74
61
        """Testament for a revision with no contents."""
75
 
        t = self.from_revision(self.b.repository, 'test@user-1')
 
62
        t = Testament.from_revision(self.b.repository, 'test@user-1')
76
63
        ass = self.assertTrue
77
64
        eq = self.assertEqual
78
65
        ass(isinstance(t, Testament))
83
70
 
84
71
    def test_testment_text_form(self):
85
72
        """Conversion of testament to canonical text form."""
86
 
        t = self.from_revision(self.b.repository, 'test@user-1')
87
 
        text_form = t.as_text()
88
 
        self.log('testament text form:\n' + text_form)
89
 
        self.assertEqualDiff(text_form, self.expected('rev_1'))
90
 
        short_text_form = t.as_short_text()
91
 
        self.assertEqualDiff(short_text_form, self.expected('rev_1_short'))
 
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)
92
84
 
93
85
    def test_testament_with_contents(self):
94
86
        """Testament containing a file and a directory."""
95
 
        t = self.from_revision(self.b.repository, 'test@user-2')
96
 
        text_form = t.as_text()
97
 
        self.log('testament text form:\n' + text_form)
98
 
        self.assertEqualDiff(text_form, self.expected('rev_2'))
99
 
        actual_short = t.as_short_text()
100
 
        self.assertEqualDiff(actual_short, self.expected('rev_2_short'))
 
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)
101
102
 
102
103
    def test_testament_symlinks(self):
103
104
        """Testament containing symlink (where possible)"""
104
 
        self.requireFeature(SymlinkFeature)
 
105
        if not has_symlinks():
 
106
            return
105
107
        os.symlink('wibble/linktarget', 'link')
106
108
        self.wt.add(['link'], ['link-id'])
107
109
        self.wt.commit(message='add symlink',
109
111
                 timezone=36000,
110
112
                 rev_id='test@user-3',
111
113
                 committer='test@user')
112
 
        t = self.from_revision(self.b.repository, 'test@user-3')
113
 
        self.assertEqualDiff(t.as_text(), self.expected('rev_3'))
 
114
        t = Testament.from_revision(self.b.repository, 'test@user-3')
 
115
        self.assertEqualDiff(t.as_text(), REV_3_TESTAMENT)
114
116
 
115
117
    def test_testament_revprops(self):
116
118
        """Testament to revision with extra properties"""
124
126
                      rev_id='test@user-3',
125
127
                      committer='test@user',
126
128
                      revprops=props)
127
 
        t = self.from_revision(self.b.repository, 'test@user-3')
128
 
        self.assertEqualDiff(t.as_text(), self.expected('rev_props'))
 
129
        t = Testament.from_revision(self.b.repository, 'test@user-3')
 
130
        self.assertEqualDiff(t.as_text(), REV_PROPS_TESTAMENT)
129
131
 
130
132
    def test_testament_unicode_commit_message(self):
131
133
        self.wt.commit(
133
135
            timestamp=1129025493,
134
136
            timezone=36000,
135
137
            rev_id='test@user-3',
136
 
            committer=u'Erik B\xe5gfors <test@user>',
 
138
            committer='Erik B\xe5gfors <test@user>',
137
139
            revprops={'uni':u'\xb5'}
138
140
            )
139
 
        t = self.from_revision(self.b.repository, 'test@user-3')
 
141
        t = Testament.from_revision(self.b.repository, 'test@user-3')
140
142
        self.assertEqualDiff(
141
 
            self.expected('sample_unicode').encode('utf-8'), t.as_text())
142
 
 
143
 
    def test_from_tree(self):
144
 
        tree = self.b.repository.revision_tree('test@user-2')
145
 
        testament = self.testament_class().from_revision_tree(tree)
146
 
        text_1 = testament.as_short_text()
147
 
        text_2 = self.from_revision(self.b.repository,
148
 
                                    'test@user-2').as_short_text()
149
 
        self.assertEqual(text_1, text_2)
 
143
            SAMPLE_UNICODE_TESTAMENT.encode('utf-8'), t.as_text())
150
144
 
151
145
    def test___init__(self):
152
146
        revision = self.b.repository.get_revision('test@user-2')
153
 
        tree = self.b.repository.revision_tree('test@user-2')
154
 
        testament_1 = self.testament_class()(revision, tree)
155
 
        text_1 = testament_1.as_short_text()
156
 
        text_2 = self.from_revision(self.b.repository,
157
 
                                    'test@user-2').as_short_text()
158
 
        self.assertEqual(text_1, text_2)
159
 
 
160
 
 
161
 
class TestamentTestsStrict(TestamentTests):
162
 
 
163
 
    def testament_class(self):
164
 
        return StrictTestament
165
 
 
166
 
 
167
 
class TestamentTestsStrict2(TestamentTests):
168
 
 
169
 
    def testament_class(self):
170
 
        return StrictTestament3
171
 
 
 
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
                    
172
153
 
173
154
REV_1_TESTAMENT = """\
174
155
bazaar-ng testament version 1
185
166
    test branch
186
167
"""
187
168
 
188
 
 
189
169
REV_1_STRICT_TESTAMENT = """\
190
170
bazaar-ng testament version 2.1
191
171
revision-id: test@user-1
202
182
"""
203
183
 
204
184
 
205
 
REV_1_STRICT_TESTAMENT3 = """\
206
 
bazaar testament version 3 strict
207
 
revision-id: test@user-1
208
 
committer: test@user
209
 
timestamp: 1129025423
210
 
timezone: 0
211
 
parents:
212
 
message:
213
 
  initial null commit
214
 
inventory:
215
 
  directory . TREE_ROT test@user-1 no
216
 
properties:
217
 
  branch-nick:
218
 
    test branch
219
 
"""
220
 
 
221
 
 
222
185
REV_1_SHORT = """\
223
186
bazaar-ng testament short form 1
224
187
revision-id: test@user-1
225
188
sha1: %s
226
 
""" % osutils.sha(REV_1_TESTAMENT).hexdigest()
 
189
""" % sha(REV_1_TESTAMENT).hexdigest()
227
190
 
228
191
 
229
192
REV_1_SHORT_STRICT = """\
230
193
bazaar-ng testament short form 2.1
231
194
revision-id: test@user-1
232
195
sha1: %s
233
 
""" % osutils.sha(REV_1_STRICT_TESTAMENT).hexdigest()
234
 
 
235
 
 
236
 
REV_1_SHORT_STRICT3 = """\
237
 
bazaar testament short form 3 strict
238
 
revision-id: test@user-1
239
 
sha1: %s
240
 
""" % osutils.sha(REV_1_STRICT_TESTAMENT3).hexdigest()
 
196
""" % sha(REV_1_STRICT_TESTAMENT).hexdigest()
241
197
 
242
198
 
243
199
REV_2_TESTAMENT = """\
280
236
"""
281
237
 
282
238
 
283
 
REV_2_STRICT_TESTAMENT3 = """\
284
 
bazaar testament version 3 strict
285
 
revision-id: test@user-2
286
 
committer: test@user
287
 
timestamp: 1129025483
288
 
timezone: 36000
289
 
parents:
290
 
  test@user-1
291
 
message:
292
 
  add files and directories
293
 
inventory:
294
 
  directory . TREE_ROT test@user-1 no
295
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
296
 
  directory src src-id test@user-2 no
297
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
298
 
properties:
299
 
  branch-nick:
300
 
    test branch
301
 
"""
302
 
 
303
 
 
304
239
REV_2_SHORT = """\
305
240
bazaar-ng testament short form 1
306
241
revision-id: test@user-2
307
242
sha1: %s
308
 
""" % osutils.sha(REV_2_TESTAMENT).hexdigest()
 
243
""" % sha(REV_2_TESTAMENT).hexdigest()
309
244
 
310
245
 
311
246
REV_2_SHORT_STRICT = """\
312
247
bazaar-ng testament short form 2.1
313
248
revision-id: test@user-2
314
249
sha1: %s
315
 
""" % osutils.sha(REV_2_STRICT_TESTAMENT).hexdigest()
316
 
 
317
 
 
318
 
REV_2_SHORT_STRICT3 = """\
319
 
bazaar testament short form 3 strict
320
 
revision-id: test@user-2
321
 
sha1: %s
322
 
""" % osutils.sha(REV_2_STRICT_TESTAMENT3).hexdigest()
 
250
""" % sha(REV_2_STRICT_TESTAMENT).hexdigest()
323
251
 
324
252
 
325
253
REV_PROPS_TESTAMENT = """\
348
276
"""
349
277
 
350
278
 
351
 
REV_PROPS_TESTAMENT_STRICT = """\
352
 
bazaar-ng testament version 2.1
353
 
revision-id: test@user-3
354
 
committer: test@user
355
 
timestamp: 1129025493
356
 
timezone: 36000
357
 
parents:
358
 
  test@user-2
359
 
message:
360
 
  revision with properties
361
 
inventory:
362
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
363
 
  directory src src-id test@user-2 no
364
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
365
 
properties:
366
 
  branch-nick:
367
 
    test branch
368
 
  empty:
369
 
  flavor:
370
 
    sour cherry
371
 
    cream cheese
372
 
  size:
373
 
    medium
374
 
"""
375
 
 
376
 
 
377
 
REV_PROPS_TESTAMENT_STRICT3 = """\
378
 
bazaar testament version 3 strict
379
 
revision-id: test@user-3
380
 
committer: test@user
381
 
timestamp: 1129025493
382
 
timezone: 36000
383
 
parents:
384
 
  test@user-2
385
 
message:
386
 
  revision with properties
387
 
inventory:
388
 
  directory . TREE_ROT test@user-1 no
389
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
390
 
  directory src src-id test@user-2 no
391
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
392
 
properties:
393
 
  branch-nick:
394
 
    test branch
395
 
  empty:
396
 
  flavor:
397
 
    sour cherry
398
 
    cream cheese
399
 
  size:
400
 
    medium
401
 
"""
402
 
 
403
 
 
404
279
REV_3_TESTAMENT = """\
405
280
bazaar-ng testament version 1
406
281
revision-id: test@user-3
422
297
"""
423
298
 
424
299
 
425
 
REV_3_TESTAMENT_STRICT = """\
426
 
bazaar-ng testament version 2.1
427
 
revision-id: test@user-3
428
 
committer: test@user
429
 
timestamp: 1129025493
430
 
timezone: 36000
431
 
parents:
432
 
  test@user-2
433
 
message:
434
 
  add symlink
435
 
inventory:
436
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
437
 
  symlink link link-id wibble/linktarget test@user-3 no
438
 
  directory src src-id test@user-2 no
439
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
440
 
properties:
441
 
  branch-nick:
442
 
    test branch
443
 
"""
444
 
 
445
 
 
446
 
REV_3_TESTAMENT_STRICT3 = """\
447
 
bazaar testament version 3 strict
448
 
revision-id: test@user-3
449
 
committer: test@user
450
 
timestamp: 1129025493
451
 
timezone: 36000
452
 
parents:
453
 
  test@user-2
454
 
message:
455
 
  add symlink
456
 
inventory:
457
 
  directory . TREE_ROT test@user-1 no
458
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
459
 
  symlink link link-id wibble/linktarget test@user-3 no
460
 
  directory src src-id test@user-2 no
461
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
462
 
properties:
463
 
  branch-nick:
464
 
    test branch
465
 
"""
466
 
 
467
 
 
468
300
SAMPLE_UNICODE_TESTAMENT = u"""\
469
301
bazaar-ng testament version 1
470
302
revision-id: test@user-3
485
317
  uni:
486
318
    \xb5
487
319
"""
488
 
 
489
 
 
490
 
SAMPLE_UNICODE_TESTAMENT_STRICT = u"""\
491
 
bazaar-ng testament version 2.1
492
 
revision-id: test@user-3
493
 
committer: Erik B\xe5gfors <test@user>
494
 
timestamp: 1129025493
495
 
timezone: 36000
496
 
parents:
497
 
  test@user-2
498
 
message:
499
 
  non-ascii commit \N{COPYRIGHT SIGN} me
500
 
inventory:
501
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
502
 
  directory src src-id test@user-2 no
503
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
504
 
properties:
505
 
  branch-nick:
506
 
    test branch
507
 
  uni:
508
 
    \xb5
509
 
"""
510
 
 
511
 
 
512
 
SAMPLE_UNICODE_TESTAMENT_STRICT3 = u"""\
513
 
bazaar testament version 3 strict
514
 
revision-id: test@user-3
515
 
committer: Erik B\xe5gfors <test@user>
516
 
timestamp: 1129025493
517
 
timezone: 36000
518
 
parents:
519
 
  test@user-2
520
 
message:
521
 
  non-ascii commit \N{COPYRIGHT SIGN} me
522
 
inventory:
523
 
  directory . TREE_ROT test@user-1 no
524
 
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
525
 
  directory src src-id test@user-2 no
526
 
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
527
 
properties:
528
 
  branch-nick:
529
 
    test branch
530
 
  uni:
531
 
    \xb5
532
 
"""
533
 
 
534
 
 
535
 
texts = {
536
 
    Testament: { 'rev_1': REV_1_TESTAMENT,
537
 
                 'rev_1_short': REV_1_SHORT,
538
 
                 'rev_2': REV_2_TESTAMENT,
539
 
                 'rev_2_short': REV_2_SHORT,
540
 
                 'rev_3': REV_3_TESTAMENT,
541
 
                 'rev_props': REV_PROPS_TESTAMENT,
542
 
                 'sample_unicode': SAMPLE_UNICODE_TESTAMENT,
543
 
    },
544
 
    StrictTestament: {'rev_1': REV_1_STRICT_TESTAMENT,
545
 
                      'rev_1_short': REV_1_SHORT_STRICT,
546
 
                      'rev_2': REV_2_STRICT_TESTAMENT,
547
 
                      'rev_2_short': REV_2_SHORT_STRICT,
548
 
                      'rev_3': REV_3_TESTAMENT_STRICT,
549
 
                      'rev_props': REV_PROPS_TESTAMENT_STRICT,
550
 
                      'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT,
551
 
    },
552
 
    StrictTestament3: {'rev_1': REV_1_STRICT_TESTAMENT3,
553
 
                      'rev_1_short': REV_1_SHORT_STRICT3,
554
 
                      'rev_2': REV_2_STRICT_TESTAMENT3,
555
 
                      'rev_2_short': REV_2_SHORT_STRICT3,
556
 
                      'rev_3': REV_3_TESTAMENT_STRICT3,
557
 
                      'rev_props': REV_PROPS_TESTAMENT_STRICT3,
558
 
                      'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT3,
559
 
    },
560
 
}