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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Test testaments for gpg signing."""
19
19
# TODO: Testaments with x-bits
23
from bzrlib import osutils
25
24
from bzrlib.tests import TestCaseWithTransport
26
from bzrlib.branch import Branch
27
from bzrlib.testament import Testament, StrictTestament
28
from bzrlib.trace import mutter
25
from bzrlib.testament import (
29
30
from bzrlib.transform import TreeTransform
30
from bzrlib.osutils import has_symlinks
33
class TestamentTests(TestCaseWithTransport):
31
from bzrlib.tests.features import (
36
class TestamentSetup(TestCaseWithTransport):
36
super(TestamentTests, self).setUp()
37
self.wt = self.make_branch_and_tree('.')
39
super(TestamentSetup, self).setUp()
40
self.wt = self.make_branch_and_tree('.', format='development-subtree')
41
self.wt.set_root_id('TREE_ROT')
38
42
b = self.b = self.wt.branch
39
43
b.nick = "test branch"
40
44
self.wt.commit(message='initial null commit',
57
61
rev_id='test@user-2',
58
62
committer='test@user')
65
class TestamentTests(TestamentSetup):
67
def testament_class(self):
70
def expected(self, key):
71
return texts[self.testament_class()][key]
73
def from_revision(self, repository, revision_id):
74
return self.testament_class().from_revision(repository, revision_id)
60
76
def test_null_testament(self):
61
77
"""Testament for a revision with no contents."""
62
t = Testament.from_revision(self.b.repository, 'test@user-1')
78
t = self.from_revision(self.b.repository, 'test@user-1')
63
79
ass = self.assertTrue
64
80
eq = self.assertEqual
65
81
ass(isinstance(t, Testament))
71
87
def test_testment_text_form(self):
72
88
"""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)
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)
89
t = self.from_revision(self.b.repository, 'test@user-1')
90
text_form = t.as_text()
91
self.log('testament text form:\n' + text_form)
92
self.assertEqualDiff(text_form, self.expected('rev_1'))
93
short_text_form = t.as_short_text()
94
self.assertEqualDiff(short_text_form, self.expected('rev_1_short'))
85
96
def test_testament_with_contents(self):
86
97
"""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)
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)
103
def test_testament_command(self):
104
"""Testament containing a file and a directory."""
105
out, err = self.run_bzr_captured(['testament', '--long'])
106
self.assertEqualDiff(err, '')
107
self.assertEqualDiff(out, REV_2_TESTAMENT)
109
def test_testament_command_2(self):
110
"""Command getting short testament of previous version."""
111
out, err = self.run_bzr_captured(['testament', '-r1'])
112
self.assertEqualDiff(err, '')
113
self.assertEqualDiff(out, REV_1_SHORT)
115
def test_testament_command_3(self):
116
"""Command getting short testament of previous version."""
117
out, err = self.run_bzr_captured(['testament', '-r1', '--strict'])
118
self.assertEqualDiff(err, '')
119
self.assertEqualDiff(out, REV_1_SHORT_STRICT)
98
t = self.from_revision(self.b.repository, 'test@user-2')
99
text_form = t.as_text()
100
self.log('testament text form:\n' + text_form)
101
self.assertEqualDiff(text_form, self.expected('rev_2'))
102
actual_short = t.as_short_text()
103
self.assertEqualDiff(actual_short, self.expected('rev_2_short'))
121
105
def test_testament_symlinks(self):
122
106
"""Testament containing symlink (where possible)"""
123
if not has_symlinks():
107
self.requireFeature(SymlinkFeature)
125
108
os.symlink('wibble/linktarget', 'link')
126
109
self.wt.add(['link'], ['link-id'])
127
110
self.wt.commit(message='add symlink',
130
113
rev_id='test@user-3',
131
114
committer='test@user')
132
t = Testament.from_revision(self.b.repository, 'test@user-3')
133
self.assertEqualDiff(t.as_text(), REV_3_TESTAMENT)
115
t = self.from_revision(self.b.repository, 'test@user-3')
116
self.assertEqualDiff(t.as_text(), self.expected('rev_3'))
135
118
def test_testament_revprops(self):
136
119
"""Testament to revision with extra properties"""
137
120
props = dict(flavor='sour cherry\ncream cheese',
139
124
self.wt.commit(message='revision with properties',
140
125
timestamp=1129025493,
142
127
rev_id='test@user-3',
143
128
committer='test@user',
145
t = Testament.from_revision(self.b.repository, 'test@user-3')
146
self.assertEqualDiff(t.as_text(), REV_PROPS_TESTAMENT)
130
t = self.from_revision(self.b.repository, 'test@user-3')
131
self.assertEqualDiff(t.as_text(), self.expected('rev_props'))
148
133
def test_testament_unicode_commit_message(self):
151
136
timestamp=1129025493,
153
138
rev_id='test@user-3',
154
committer='test@user')
155
t = Testament.from_revision(self.b.repository, 'test@user-3')
139
committer=u'Erik B\xe5gfors <test@user>',
140
revprops={'uni':u'\xb5'}
142
t = self.from_revision(self.b.repository, 'test@user-3')
156
143
self.assertEqualDiff(
157
SAMPLE_UNICODE_TESTAMENT.encode('utf-8'), t.as_text())
144
self.expected('sample_unicode').encode('utf-8'), t.as_text())
146
def test_from_tree(self):
147
tree = self.b.repository.revision_tree('test@user-2')
148
testament = self.testament_class().from_revision_tree(tree)
149
text_1 = testament.as_short_text()
150
text_2 = self.from_revision(self.b.repository,
151
'test@user-2').as_short_text()
152
self.assertEqual(text_1, text_2)
159
154
def test___init__(self):
160
155
revision = self.b.repository.get_revision('test@user-2')
161
inventory = self.b.repository.get_inventory('test@user-2')
162
testament_1 = Testament(revision, inventory).as_short_text()
163
testament_2 = Testament.from_revision(self.b.repository,
164
'test@user-2').as_short_text()
165
self.assertEqual(testament_1, testament_2)
156
tree = self.b.repository.revision_tree('test@user-2')
157
testament_1 = self.testament_class()(revision, tree)
158
text_1 = testament_1.as_short_text()
159
text_2 = self.from_revision(self.b.repository,
160
'test@user-2').as_short_text()
161
self.assertEqual(text_1, text_2)
164
class TestamentTestsStrict(TestamentTests):
166
def testament_class(self):
167
return StrictTestament
170
class TestamentTestsStrict2(TestamentTests):
172
def testament_class(self):
173
return StrictTestament3
168
176
REV_1_TESTAMENT = """\
169
177
bazaar-ng testament version 1
208
REV_1_STRICT_TESTAMENT3 = """\
209
bazaar testament version 3 strict
210
revision-id: test@user-1
212
timestamp: 1129025423
218
directory . TREE_ROT test@user-1 no
199
225
REV_1_SHORT = """\
200
226
bazaar-ng testament short form 1
201
227
revision-id: test@user-1
203
""" % sha(REV_1_TESTAMENT).hexdigest()
229
""" % osutils.sha_string(REV_1_TESTAMENT)
206
232
REV_1_SHORT_STRICT = """\
207
233
bazaar-ng testament short form 2.1
208
234
revision-id: test@user-1
210
""" % sha(REV_1_STRICT_TESTAMENT).hexdigest()
236
""" % osutils.sha_string(REV_1_STRICT_TESTAMENT)
239
REV_1_SHORT_STRICT3 = """\
240
bazaar testament short form 3 strict
241
revision-id: test@user-1
243
""" % osutils.sha_string(REV_1_STRICT_TESTAMENT3)
213
246
REV_2_TESTAMENT = """\
286
REV_2_STRICT_TESTAMENT3 = """\
287
bazaar testament version 3 strict
288
revision-id: test@user-2
290
timestamp: 1129025483
295
add files and directories
297
directory . TREE_ROT test@user-1 no
298
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
299
directory src src-id test@user-2 no
300
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
253
307
REV_2_SHORT = """\
254
308
bazaar-ng testament short form 1
255
309
revision-id: test@user-2
257
""" % sha(REV_2_TESTAMENT).hexdigest()
311
""" % osutils.sha_string(REV_2_TESTAMENT)
260
314
REV_2_SHORT_STRICT = """\
261
315
bazaar-ng testament short form 2.1
262
316
revision-id: test@user-2
264
""" % sha(REV_2_STRICT_TESTAMENT).hexdigest()
318
""" % osutils.sha_string(REV_2_STRICT_TESTAMENT)
321
REV_2_SHORT_STRICT3 = """\
322
bazaar testament short form 3 strict
323
revision-id: test@user-2
325
""" % osutils.sha_string(REV_2_STRICT_TESTAMENT3)
267
328
REV_PROPS_TESTAMENT = """\
354
REV_PROPS_TESTAMENT_STRICT = """\
355
bazaar-ng testament version 2.1
356
revision-id: test@user-3
358
timestamp: 1129025493
363
revision with properties
365
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
366
directory src src-id test@user-2 no
367
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
380
REV_PROPS_TESTAMENT_STRICT3 = """\
381
bazaar testament version 3 strict
382
revision-id: test@user-3
384
timestamp: 1129025493
389
revision with properties
391
directory . TREE_ROT test@user-1 no
392
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
393
directory src src-id test@user-2 no
394
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
428
REV_3_TESTAMENT_STRICT = """\
429
bazaar-ng testament version 2.1
430
revision-id: test@user-3
432
timestamp: 1129025493
439
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
440
symlink link link-id wibble/linktarget test@user-3 no
441
directory src src-id test@user-2 no
442
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
449
REV_3_TESTAMENT_STRICT3 = """\
450
bazaar testament version 3 strict
451
revision-id: test@user-3
453
timestamp: 1129025493
460
directory . TREE_ROT test@user-1 no
461
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
462
symlink link link-id wibble/linktarget test@user-3 no
463
directory src src-id test@user-2 no
464
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
313
471
SAMPLE_UNICODE_TESTAMENT = u"""\
314
472
bazaar-ng testament version 1
315
473
revision-id: test@user-3
474
committer: Erik B\xe5gfors <test@user>
317
475
timestamp: 1129025493
493
SAMPLE_UNICODE_TESTAMENT_STRICT = u"""\
494
bazaar-ng testament version 2.1
495
revision-id: test@user-3
496
committer: Erik B\xe5gfors <test@user>
497
timestamp: 1129025493
502
non-ascii commit \N{COPYRIGHT SIGN} me
504
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
505
directory src src-id test@user-2 no
506
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
515
SAMPLE_UNICODE_TESTAMENT_STRICT3 = u"""\
516
bazaar testament version 3 strict
517
revision-id: test@user-3
518
committer: Erik B\xe5gfors <test@user>
519
timestamp: 1129025493
524
non-ascii commit \N{COPYRIGHT SIGN} me
526
directory . TREE_ROT test@user-1 no
527
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
528
directory src src-id test@user-2 no
529
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
539
Testament: { 'rev_1': REV_1_TESTAMENT,
540
'rev_1_short': REV_1_SHORT,
541
'rev_2': REV_2_TESTAMENT,
542
'rev_2_short': REV_2_SHORT,
543
'rev_3': REV_3_TESTAMENT,
544
'rev_props': REV_PROPS_TESTAMENT,
545
'sample_unicode': SAMPLE_UNICODE_TESTAMENT,
547
StrictTestament: {'rev_1': REV_1_STRICT_TESTAMENT,
548
'rev_1_short': REV_1_SHORT_STRICT,
549
'rev_2': REV_2_STRICT_TESTAMENT,
550
'rev_2_short': REV_2_SHORT_STRICT,
551
'rev_3': REV_3_TESTAMENT_STRICT,
552
'rev_props': REV_PROPS_TESTAMENT_STRICT,
553
'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT,
555
StrictTestament3: {'rev_1': REV_1_STRICT_TESTAMENT3,
556
'rev_1_short': REV_1_SHORT_STRICT3,
557
'rev_2': REV_2_STRICT_TESTAMENT3,
558
'rev_2_short': REV_2_SHORT_STRICT3,
559
'rev_3': REV_3_TESTAMENT_STRICT3,
560
'rev_props': REV_PROPS_TESTAMENT_STRICT3,
561
'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT3,