~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testlog.py

  • Committer: Martin Pool
  • Date: 2005-08-12 15:41:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050812154144-bc98570a78b8f633
- merge in deferred revfile work

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
18
 
from cStringIO import StringIO
19
18
 
20
 
from bzrlib.tests import BzrTestBase, TestCaseWithTransport
21
 
from bzrlib.log import LogFormatter, show_log, LongLogFormatter, ShortLogFormatter
 
19
from bzrlib.selftest import BzrTestBase
 
20
from bzrlib.log import LogFormatter, show_log, LongLogFormatter
22
21
from bzrlib.branch import Branch
23
 
from bzrlib.errors import InvalidRevisionNumber
24
22
 
25
23
class _LogEntry(object):
26
24
    # should probably move into bzrlib.log?
49
47
        self.logs.append(le)
50
48
 
51
49
 
52
 
class SimpleLogTest(TestCaseWithTransport):
53
 
 
 
50
class SimpleLogTest(BzrTestBase):
54
51
    def checkDelta(self, delta, **kw):
55
52
        """Check the filenames touched by a delta are as expected."""
56
53
        for n in 'added', 'removed', 'renamed', 'modified', 'unchanged':
57
54
            expected = kw.get(n, [])
58
55
 
59
56
            # tests are written with unix paths; fix them up for windows
60
 
            #if os.sep != '/':
61
 
            #    expected = [x.replace('/', os.sep) for x in expected]
 
57
            if os.sep != '/':
 
58
                expected = [x.replace('/', os.sep) for x in expected]
62
59
 
63
60
            # strip out only the path components
64
61
            got = [x[0] for x in getattr(delta, n)]
65
62
            self.assertEquals(expected, got)
66
63
 
67
 
    def test_cur_revno(self):
68
 
        wt = self.make_branch_and_tree('.')
69
 
        b = wt.branch
70
 
 
71
 
        lf = LogCatcher()
72
 
        wt.commit('empty commit')
73
 
        show_log(b, lf, verbose=True, start_revision=1, end_revision=1)
74
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
75
 
                          start_revision=2, end_revision=1) 
76
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
77
 
                          start_revision=1, end_revision=2) 
78
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
79
 
                          start_revision=0, end_revision=2) 
80
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
81
 
                          start_revision=1, end_revision=0) 
82
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
83
 
                          start_revision=-1, end_revision=1) 
84
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
85
 
                          start_revision=1, end_revision=-1) 
86
 
 
87
 
    def test_cur_revno(self):
88
 
        wt = self.make_branch_and_tree('.')
89
 
        b = wt.branch
90
 
 
91
 
        lf = LogCatcher()
92
 
        wt.commit('empty commit')
93
 
        show_log(b, lf, verbose=True, start_revision=1, end_revision=1)
94
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
95
 
                          start_revision=2, end_revision=1) 
96
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
97
 
                          start_revision=1, end_revision=2) 
98
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
99
 
                          start_revision=0, end_revision=2) 
100
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
101
 
                          start_revision=1, end_revision=0) 
102
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
103
 
                          start_revision=-1, end_revision=1) 
104
 
        self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
105
 
                          start_revision=1, end_revision=-1) 
106
 
 
107
 
    def test_simple_log(self):
 
64
    
 
65
    def runTest(self):
108
66
        eq = self.assertEquals
 
67
        ass = self.assert_
109
68
        
110
 
        wt = self.make_branch_and_tree('.')
111
 
        b = wt.branch
 
69
        b = Branch('.', init=True)
112
70
 
113
71
        lf = LogCatcher()
114
72
        show_log(b, lf)
115
73
        # no entries yet
116
74
        eq(lf.logs, [])
117
75
 
118
 
        wt.commit('empty commit')
 
76
 
 
77
        b.commit('empty commit')
119
78
        lf = LogCatcher()
120
79
        show_log(b, lf, verbose=True)
121
80
        eq(len(lf.logs), 1)
125
84
        self.log('log delta: %r' % d)
126
85
        self.checkDelta(d)
127
86
 
 
87
 
128
88
        self.build_tree(['hello'])
129
 
        wt.add('hello')
130
 
        wt.commit('add one file')
131
 
 
132
 
        lf = StringIO()
 
89
        b.add('hello')
 
90
        b.commit('add one file')
133
91
        # log using regular thing
134
 
        show_log(b, LongLogFormatter(lf))
135
 
        lf.seek(0)
136
 
        for l in lf.readlines():
137
 
            self.log(l)
 
92
        show_log(b, LongLogFormatter(self.TEST_LOG))
138
93
 
139
94
        # get log as data structure
140
95
        lf = LogCatcher()
152
107
        self.log('log 2 delta: %r' % d)
153
108
        # self.checkDelta(d, added=['hello'])
154
109
        
155
 
        # commit a log message with control characters
156
 
        msg = "All 8-bit chars: " +  ''.join([unichr(x) for x in range(256)])
157
 
        self.log("original commit message: %r", msg)
158
 
        wt.commit(msg)
159
 
        lf = LogCatcher()
160
 
        show_log(b, lf, verbose=True)
161
 
        committed_msg = lf.logs[0].rev.message
162
 
        self.log("escaped commit message: %r", committed_msg)
163
 
        self.assert_(msg != committed_msg)
164
 
        self.assert_(len(committed_msg) > len(msg))
165
 
 
166
 
        # Check that log message with only XML-valid characters isn't
167
 
        # escaped.  As ElementTree apparently does some kind of
168
 
        # newline conversion, neither LF (\x0A) nor CR (\x0D) are
169
 
        # included in the test commit message, even though they are
170
 
        # valid XML 1.0 characters.
171
 
        msg = "\x09" + ''.join([unichr(x) for x in range(0x20, 256)])
172
 
        self.log("original commit message: %r", msg)
173
 
        wt.commit(msg)
174
 
        lf = LogCatcher()
175
 
        show_log(b, lf, verbose=True)
176
 
        committed_msg = lf.logs[0].rev.message
177
 
        self.log("escaped commit message: %r", committed_msg)
178
 
        self.assert_(msg == committed_msg)
179
 
 
180
 
    def test_trailing_newlines(self):
181
 
        wt = self.make_branch_and_tree('.')
182
 
        b = wt.branch
183
 
        b.nick='test'
184
 
        open('a', 'wb').write('hello moto\n')
185
 
        wt.add('a')
186
 
        wt.commit('simple log message', rev_id='a1'
187
 
                , timestamp=1132586655.459960938, timezone=-6*3600
188
 
                , committer='Joe Foo <joe@foo.com>')
189
 
        open('b', 'wb').write('goodbye\n')
190
 
        wt.add('b')
191
 
        wt.commit('multiline\nlog\nmessage\n', rev_id='a2'
192
 
                , timestamp=1132586842.411175966, timezone=-6*3600
193
 
                , committer='Joe Foo <joe@foo.com>')
194
 
 
195
 
        open('c', 'wb').write('just another manic monday\n')
196
 
        wt.add('c')
197
 
        wt.commit('single line with trailing newline\n', rev_id='a3'
198
 
                , timestamp=1132587176.835228920, timezone=-6*3600
199
 
                , committer = 'Joe Foo <joe@foo.com>')
200
 
 
201
 
        sio = StringIO()
202
 
        lf = ShortLogFormatter(to_file=sio)
203
 
        show_log(b, lf)
204
 
        self.assertEquals(sio.getvalue(), """\
205
 
    3 Joe Foo\t2005-11-21
206
 
      single line with trailing newline
207
 
 
208
 
    2 Joe Foo\t2005-11-21
209
 
      multiline
210
 
      log
211
 
      message
212
 
 
213
 
    1 Joe Foo\t2005-11-21
214
 
      simple log message
215
 
 
216
 
""")
217
 
 
218
 
        sio = StringIO()
219
 
        lf = LongLogFormatter(to_file=sio)
220
 
        show_log(b, lf)
221
 
        self.assertEquals(sio.getvalue(), """\
222
 
------------------------------------------------------------
223
 
revno: 3
224
 
committer: Joe Foo <joe@foo.com>
225
 
branch nick: test
226
 
timestamp: Mon 2005-11-21 09:32:56 -0600
227
 
message:
228
 
  single line with trailing newline
229
 
------------------------------------------------------------
230
 
revno: 2
231
 
committer: Joe Foo <joe@foo.com>
232
 
branch nick: test
233
 
timestamp: Mon 2005-11-21 09:27:22 -0600
234
 
message:
235
 
  multiline
236
 
  log
237
 
  message
238
 
------------------------------------------------------------
239
 
revno: 1
240
 
committer: Joe Foo <joe@foo.com>
241
 
branch nick: test
242
 
timestamp: Mon 2005-11-21 09:24:15 -0600
243
 
message:
244
 
  simple log message
245
 
""")
246
 
        
247
 
    def test_verbose_log(self):
248
 
        """Verbose log includes changed files
249
 
        
250
 
        bug #4676
251
 
        """
252
 
        wt = self.make_branch_and_tree('.')
253
 
        b = wt.branch
254
 
        self.build_tree(['a'])
255
 
        wt.add('a')
256
 
        # XXX: why does a longer nick show up?
257
 
        b.nick = 'test_verbose_log'
258
 
        wt.commit(message='add a', 
259
 
                  timestamp=1132711707, 
260
 
                  timezone=36000,
261
 
                  committer='Lorem Ipsum <test@example.com>')
262
 
        logfile = file('out.tmp', 'w+')
263
 
        formatter = LongLogFormatter(to_file=logfile)
264
 
        show_log(b, formatter, verbose=True)
265
 
        logfile.flush()
266
 
        logfile.seek(0)
267
 
        log_contents = logfile.read()
268
 
        self.assertEqualDiff(log_contents, '''\
269
 
------------------------------------------------------------
270
 
revno: 1
271
 
committer: Lorem Ipsum <test@example.com>
272
 
branch nick: test_verbose_log
273
 
timestamp: Wed 2005-11-23 12:08:27 +1000
274
 
message:
275
 
  add a
276
 
added:
277
 
  a
278
 
''')