~bzr-pqm/bzr/bzr.dev

1185.16.12 by Martin Pool
- basic testament class
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
1185.16.25 by Martin Pool
- testament symlink support
19
# TODO: Testaments with x-bits
1185.16.23 by Martin Pool
- clean up imports
20
1185.16.12 by Martin Pool
- basic testament class
21
import os
1185.16.22 by Martin Pool
- more testament development
22
from sha import sha
1185.16.12 by Martin Pool
- basic testament class
23
import sys
24
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
25
from bzrlib.tests import TestCaseWithTransport
1185.16.12 by Martin Pool
- basic testament class
26
from bzrlib.branch import Branch
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
27
from bzrlib.testament import Testament, StrictTestament
1185.16.15 by Martin Pool
- test text form for testaments
28
from bzrlib.trace import mutter
1551.7.5 by Aaron Bentley
Make sure strict testaments indicate when an execute bit is true
29
from bzrlib.transform import TreeTransform
1185.16.25 by Martin Pool
- testament symlink support
30
from bzrlib.osutils import has_symlinks
1185.16.12 by Martin Pool
- basic testament class
31
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
32
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
33
class TestamentTests(TestCaseWithTransport):
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
34
1185.16.15 by Martin Pool
- test text form for testaments
35
    def setUp(self):
36
        super(TestamentTests, self).setUp()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
37
        self.wt = self.make_branch_and_tree('.')
38
        b = self.b = self.wt.branch
1185.35.16 by Aaron Bentley
Fixed tests
39
        b.nick = "test branch"
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
40
        self.wt.commit(message='initial null commit',
1185.16.12 by Martin Pool
- basic testament class
41
                 committer='test@user',
42
                 timestamp=1129025423, # 'Tue Oct 11 20:10:23 2005'
43
                 timezone=0,
44
                 rev_id='test@user-1')
1514 by Robert Collins
Unbreak self.build_tree_shape in tests.
45
        self.build_tree_contents([('hello', 'contents of hello file'),
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
46
                             ('src/', ),
1185.16.22 by Martin Pool
- more testament development
47
                             ('src/foo.c', 'int main()\n{\n}\n')])
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
48
        self.wt.add(['hello', 'src', 'src/foo.c'],
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
49
                             ['hello-id', 'src-id', 'foo.c-id'])
1551.7.5 by Aaron Bentley
Make sure strict testaments indicate when an execute bit is true
50
        tt = TreeTransform(self.wt)
51
        trans_id = tt.trans_id_tree_path('hello')
52
        tt.set_executability(True, trans_id)
53
        tt.apply()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
54
        self.wt.commit(message='add files and directories',
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
55
                 timestamp=1129025483,
56
                 timezone=36000,
57
                 rev_id='test@user-2',
58
                 committer='test@user')
1185.16.15 by Martin Pool
- test text form for testaments
59
60
    def test_null_testament(self):
61
        """Testament for a revision with no contents."""
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
62
        t = Testament.from_revision(self.b.repository, 'test@user-1')
1185.16.12 by Martin Pool
- basic testament class
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
1185.16.15 by Martin Pool
- test text form for testaments
71
    def test_testment_text_form(self):
72
        """Conversion of testament to canonical text form."""
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
73
        t = Testament.from_revision(self.b.repository, 'test@user-1')
1185.16.22 by Martin Pool
- more testament development
74
        text_form = t.as_text()
1185.16.15 by Martin Pool
- test text form for testaments
75
        self.log('testament text form:\n' + text_form)
1185.16.25 by Martin Pool
- testament symlink support
76
        self.assertEqual(text_form, REV_1_TESTAMENT)
1185.16.15 by Martin Pool
- test text form for testaments
77
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
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
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
85
    def test_testament_with_contents(self):
86
        """Testament containing a file and a directory."""
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
87
        t = Testament.from_revision(self.b.repository, 'test@user-2')
1185.16.22 by Martin Pool
- more testament development
88
        text_form = t.as_text()
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
89
        self.log('testament text form:\n' + text_form)
1185.16.24 by Martin Pool
- add and test 'testament' builtin command
90
        self.assertEqualDiff(text_form, REV_2_TESTAMENT)
91
        actual_short = t.as_short_text()
1185.16.25 by Martin Pool
- testament symlink support
92
        self.assertEqualDiff(actual_short, REV_2_SHORT)
1185.16.24 by Martin Pool
- add and test 'testament' builtin command
93
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
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
1185.16.24 by Martin Pool
- add and test 'testament' builtin command
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)
108
1185.16.25 by Martin Pool
- testament symlink support
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)
114
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
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)
120
1185.16.25 by Martin Pool
- testament symlink support
121
    def test_testament_symlinks(self):
122
        """Testament containing symlink (where possible)"""
123
        if not has_symlinks():
124
            return
125
        os.symlink('wibble/linktarget', 'link')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
126
        self.wt.add(['link'], ['link-id'])
127
        self.wt.commit(message='add symlink',
1185.16.25 by Martin Pool
- testament symlink support
128
                 timestamp=1129025493,
129
                 timezone=36000,
130
                 rev_id='test@user-3',
131
                 committer='test@user')
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
132
        t = Testament.from_revision(self.b.repository, 'test@user-3')
1185.16.25 by Martin Pool
- testament symlink support
133
        self.assertEqualDiff(t.as_text(), REV_3_TESTAMENT)
134
1185.16.59 by mbp at sourcefrog
- store revprops in testaments
135
    def test_testament_revprops(self):
136
        """Testament to revision with extra properties"""
137
        props = dict(flavor='sour cherry\ncream cheese',
138
                     size='medium')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
139
        self.wt.commit(message='revision with properties',
1185.16.59 by mbp at sourcefrog
- store revprops in testaments
140
                      timestamp=1129025493,
141
                      timezone=36000,
142
                      rev_id='test@user-3',
143
                      committer='test@user',
144
                      revprops=props)
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
145
        t = Testament.from_revision(self.b.repository, 'test@user-3')
1185.16.59 by mbp at sourcefrog
- store revprops in testaments
146
        self.assertEqualDiff(t.as_text(), REV_PROPS_TESTAMENT)
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
147
1553.3.1 by Marien Zwart
Make Testament.as_text_lines return utf-8 instead of unicode objects and add a test for this.
148
    def test_testament_unicode_commit_message(self):
1558.1.3 by Aaron Bentley
Fixed deprecated op use in test suite
149
        self.wt.commit(
1553.3.1 by Marien Zwart
Make Testament.as_text_lines return utf-8 instead of unicode objects and add a test for this.
150
            message=u'non-ascii commit \N{COPYRIGHT SIGN} me',
151
            timestamp=1129025493,
152
            timezone=36000,
153
            rev_id='test@user-3',
154
            committer='test@user')
155
        t = Testament.from_revision(self.b.repository, 'test@user-3')
1553.3.4 by Marien Zwart
Move the encoding step into the test to make it more obvious we are comparing bytestrings.
156
        self.assertEqualDiff(
1185.50.79 by John Arbash Meinel
small cleanup of test
157
            SAMPLE_UNICODE_TESTAMENT.encode('utf-8'), t.as_text())
1553.3.1 by Marien Zwart
Make Testament.as_text_lines return utf-8 instead of unicode objects and add a test for this.
158
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
159
    def test___init__(self):
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
160
        revision = self.b.repository.get_revision('test@user-2')
161
        inventory = self.b.repository.get_inventory('test@user-2')
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
162
        testament_1 = Testament(revision, inventory).as_short_text()
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
163
        testament_2 = Testament.from_revision(self.b.repository, 
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
164
                                              'test@user-2').as_short_text()
165
        self.assertEqual(testament_1, testament_2)
1185.16.59 by mbp at sourcefrog
- store revprops in testaments
166
                    
1185.16.25 by Martin Pool
- testament symlink support
167
168
REV_1_TESTAMENT = """\
169
bazaar-ng testament version 1
170
revision-id: test@user-1
171
committer: test@user
172
timestamp: 1129025423
173
timezone: 0
174
parents:
175
message:
176
  initial null commit
177
inventory:
1185.35.16 by Aaron Bentley
Fixed tests
178
properties:
179
  branch-nick:
180
    test branch
1185.16.25 by Martin Pool
- testament symlink support
181
"""
182
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
183
REV_1_STRICT_TESTAMENT = """\
184
bazaar-ng testament version 2.1
185
revision-id: test@user-1
186
committer: test@user
187
timestamp: 1129025423
188
timezone: 0
189
parents:
190
message:
191
  initial null commit
192
inventory:
193
properties:
194
  branch-nick:
195
    test branch
196
"""
197
198
1185.16.25 by Martin Pool
- testament symlink support
199
REV_1_SHORT = """\
200
bazaar-ng testament short form 1
201
revision-id: test@user-1
202
sha1: %s
203
""" % sha(REV_1_TESTAMENT).hexdigest()
204
1185.16.24 by Martin Pool
- add and test 'testament' builtin command
205
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
206
REV_1_SHORT_STRICT = """\
207
bazaar-ng testament short form 2.1
208
revision-id: test@user-1
209
sha1: %s
210
""" % sha(REV_1_STRICT_TESTAMENT).hexdigest()
211
212
1185.16.24 by Martin Pool
- add and test 'testament' builtin command
213
REV_2_TESTAMENT = """\
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
214
bazaar-ng testament version 1
215
revision-id: test@user-2
216
committer: test@user
1185.16.22 by Martin Pool
- more testament development
217
timestamp: 1129025483
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
218
timezone: 36000
219
parents:
220
  test@user-1
221
message:
222
  add files and directories
223
inventory:
1185.16.22 by Martin Pool
- more testament development
224
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
225
  directory src src-id
226
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
1185.35.16 by Aaron Bentley
Fixed tests
227
properties:
228
  branch-nick:
229
    test branch
1185.16.19 by Martin Pool
- testament now contains summary of parents and inventory
230
"""
1185.16.25 by Martin Pool
- testament symlink support
231
232
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
233
REV_2_STRICT_TESTAMENT = """\
234
bazaar-ng testament version 2.1
235
revision-id: test@user-2
236
committer: test@user
237
timestamp: 1129025483
238
timezone: 36000
239
parents:
240
  test@user-1
241
message:
242
  add files and directories
243
inventory:
1551.7.5 by Aaron Bentley
Make sure strict testaments indicate when an execute bit is true
244
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
245
  directory src src-id test@user-2 no
246
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
247
properties:
248
  branch-nick:
249
    test branch
250
"""
251
252
1185.16.25 by Martin Pool
- testament symlink support
253
REV_2_SHORT = """\
254
bazaar-ng testament short form 1
255
revision-id: test@user-2
256
sha1: %s
257
""" % sha(REV_2_TESTAMENT).hexdigest()
258
259
1551.7.1 by Aaron Bentley
Implement --strict at commandline, fix up strict format
260
REV_2_SHORT_STRICT = """\
261
bazaar-ng testament short form 2.1
262
revision-id: test@user-2
263
sha1: %s
264
""" % sha(REV_2_STRICT_TESTAMENT).hexdigest()
265
266
1185.16.59 by mbp at sourcefrog
- store revprops in testaments
267
REV_PROPS_TESTAMENT = """\
268
bazaar-ng testament version 1
269
revision-id: test@user-3
270
committer: test@user
271
timestamp: 1129025493
272
timezone: 36000
273
parents:
274
  test@user-2
275
message:
276
  revision with properties
277
inventory:
278
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
279
  directory src src-id
280
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
281
properties:
1185.35.16 by Aaron Bentley
Fixed tests
282
  branch-nick:
283
    test branch
1185.16.59 by mbp at sourcefrog
- store revprops in testaments
284
  flavor:
285
    sour cherry
286
    cream cheese
287
  size:
288
    medium
289
"""
290
291
1185.16.25 by Martin Pool
- testament symlink support
292
REV_3_TESTAMENT = """\
293
bazaar-ng testament version 1
294
revision-id: test@user-3
295
committer: test@user
296
timestamp: 1129025493
297
timezone: 36000
298
parents:
299
  test@user-2
300
message:
301
  add symlink
302
inventory:
303
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
304
  symlink link link-id wibble/linktarget
305
  directory src src-id
306
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
1185.35.16 by Aaron Bentley
Fixed tests
307
properties:
308
  branch-nick:
309
    test branch
1185.16.25 by Martin Pool
- testament symlink support
310
"""
1553.3.1 by Marien Zwart
Make Testament.as_text_lines return utf-8 instead of unicode objects and add a test for this.
311
1553.3.3 by Marien Zwart
Whitespace cleanup (pep 3)
312
1553.3.4 by Marien Zwart
Move the encoding step into the test to make it more obvious we are comparing bytestrings.
313
SAMPLE_UNICODE_TESTAMENT = u"""\
1553.3.1 by Marien Zwart
Make Testament.as_text_lines return utf-8 instead of unicode objects and add a test for this.
314
bazaar-ng testament version 1
315
revision-id: test@user-3
316
committer: test@user
317
timestamp: 1129025493
318
timezone: 36000
319
parents:
320
  test@user-2
321
message:
322
  non-ascii commit \N{COPYRIGHT SIGN} me
323
inventory:
324
  file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
325
  directory src src-id
326
  file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
327
properties:
328
  branch-nick:
329
    test branch
1553.3.4 by Marien Zwart
Move the encoding step into the test to make it more obvious we are comparing bytestrings.
330
"""