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
17
17
"""Test testaments for gpg signing."""
19
19
# TODO: Testaments with x-bits
23
from bzrlib import osutils
24
from bzrlib.tests import TestCaseWithTransport
25
from bzrlib.testament import (
30
from bzrlib.transform import TreeTransform
31
from bzrlib.tests.features import (
36
class TestamentSetup(TestCaseWithTransport):
25
from bzrlib.selftest import TestCaseInTempDir
26
from bzrlib.selftest.treeshape import build_tree_contents
27
from bzrlib.branch import Branch
28
from bzrlib.testament import Testament
29
from bzrlib.trace import mutter
30
from bzrlib.osutils import has_symlinks
32
class TestamentTests(TestCaseInTempDir):
39
super(TestamentSetup, self).setUp()
40
self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
41
self.wt.set_root_id('TREE_ROT')
42
b = self.b = self.wt.branch
43
b.nick = "test branch"
44
self.wt.commit(message='initial null commit',
34
super(TestamentTests, self).setUp()
35
b = self.b = Branch.initialize('.')
36
b.commit(message='initial null commit',
45
37
committer='test@user',
46
38
timestamp=1129025423, # 'Tue Oct 11 20:10:23 2005'
48
40
rev_id='test@user-1')
49
self.build_tree_contents([('hello', 'contents of hello file'),
41
build_tree_contents([('hello', 'contents of hello file'),
51
43
('src/foo.c', 'int main()\n{\n}\n')])
52
self.wt.add(['hello', 'src', 'src/foo.c'],
53
['hello-id', 'src-id', 'foo.c-id'])
54
tt = TreeTransform(self.wt)
55
trans_id = tt.trans_id_tree_path('hello')
56
tt.set_executability(True, trans_id)
58
self.wt.commit(message='add files and directories',
44
b.add(['hello', 'src', 'src/foo.c'],
45
['hello-id', 'src-id', 'foo.c-id'])
46
b.commit(message='add files and directories',
59
47
timestamp=1129025483,
61
49
rev_id='test@user-2',
62
50
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)
76
52
def test_null_testament(self):
77
53
"""Testament for a revision with no contents."""
78
t = self.from_revision(self.b.repository, 'test@user-1')
54
t = Testament.from_revision(self.b, 'test@user-1')
79
55
ass = self.assertTrue
80
56
eq = self.assertEqual
81
57
ass(isinstance(t, Testament))
87
63
def test_testment_text_form(self):
88
64
"""Conversion of testament to canonical text form."""
89
t = self.from_revision(self.b.repository, 'test@user-1')
65
t = Testament.from_revision(self.b, 'test@user-1')
90
66
text_form = t.as_text()
91
67
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'))
68
self.assertEqual(text_form, REV_1_TESTAMENT)
96
70
def test_testament_with_contents(self):
97
71
"""Testament containing a file and a directory."""
98
t = self.from_revision(self.b.repository, 'test@user-2')
72
t = Testament.from_revision(self.b, 'test@user-2')
99
73
text_form = t.as_text()
100
74
self.log('testament text form:\n' + text_form)
101
self.assertEqualDiff(text_form, self.expected('rev_2'))
75
self.assertEqualDiff(text_form, REV_2_TESTAMENT)
102
76
actual_short = t.as_short_text()
103
self.assertEqualDiff(actual_short, self.expected('rev_2_short'))
77
self.assertEqualDiff(actual_short, REV_2_SHORT)
79
def test_testament_command(self):
80
"""Testament containing a file and a directory."""
81
out, err = self.run_bzr_captured(['testament', '--long'])
82
self.assertEqualDiff(err, '')
83
self.assertEqualDiff(out, REV_2_TESTAMENT)
85
def test_testament_command_2(self):
86
"""Command getting short testament of previous version."""
87
out, err = self.run_bzr_captured(['testament', '-r1'])
88
self.assertEqualDiff(err, '')
89
self.assertEqualDiff(out, REV_1_SHORT)
105
91
def test_testament_symlinks(self):
106
92
"""Testament containing symlink (where possible)"""
107
self.requireFeature(SymlinkFeature)
93
if not has_symlinks():
108
95
os.symlink('wibble/linktarget', 'link')
109
self.wt.add(['link'], ['link-id'])
110
self.wt.commit(message='add symlink',
96
self.b.add(['link'], ['link-id'])
97
self.b.commit(message='add symlink',
111
98
timestamp=1129025493,
113
100
rev_id='test@user-3',
114
101
committer='test@user')
115
t = self.from_revision(self.b.repository, 'test@user-3')
116
self.assertEqualDiff(t.as_text(), self.expected('rev_3'))
102
t = Testament.from_revision(self.b, 'test@user-3')
103
self.assertEqualDiff(t.as_text(), REV_3_TESTAMENT)
118
105
def test_testament_revprops(self):
119
106
"""Testament to revision with extra properties"""
120
107
props = dict(flavor='sour cherry\ncream cheese',
124
self.wt.commit(message='revision with properties',
109
self.b.commit(message='revision with properties',
125
110
timestamp=1129025493,
127
112
rev_id='test@user-3',
128
113
committer='test@user',
130
t = self.from_revision(self.b.repository, 'test@user-3')
131
self.assertEqualDiff(t.as_text(), self.expected('rev_props'))
133
def test_testament_unicode_commit_message(self):
135
message=u'non-ascii commit \N{COPYRIGHT SIGN} me',
136
timestamp=1129025493,
138
rev_id='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')
143
self.assertEqualDiff(
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)
154
def test___init__(self):
155
revision = self.b.repository.get_revision('test@user-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
115
t = Testament.from_revision(self.b, 'test@user-3')
116
self.assertEqualDiff(t.as_text(), REV_PROPS_TESTAMENT)
176
119
REV_1_TESTAMENT = """\
177
120
bazaar-ng testament version 1
257
149
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
258
150
directory src src-id
259
151
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
266
REV_2_STRICT_TESTAMENT = """\
267
bazaar-ng testament version 2.1
268
revision-id: test@user-2
270
timestamp: 1129025483
275
add files and directories
277
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
278
directory src src-id test@user-2 no
279
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
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
419
197
symlink link link-id wibble/linktarget
420
198
directory src src-id
421
199
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
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
471
SAMPLE_UNICODE_TESTAMENT = u"""\
472
bazaar-ng testament version 1
473
revision-id: test@user-3
474
committer: Erik B\xe5gfors <test@user>
475
timestamp: 1129025493
480
non-ascii commit \N{COPYRIGHT SIGN} me
482
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
484
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
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,