~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_trace.py

  • Committer: Martin Pool
  • Date: 2007-10-12 08:00:07 UTC
  • mto: This revision was merged to the branch mainline in revision 2913.
  • Revision ID: mbp@sourcefrog.net-20071012080007-vf80woayyom8s8e1
Rename update_to_one_parent_via_delta to more wieldy update_basis_by_delta

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
21
21
from cStringIO import StringIO
22
22
import errno
23
23
import os
24
 
import re
25
24
import sys
26
 
import tempfile
27
25
 
28
26
from bzrlib import (
29
27
    errors,
32
30
from bzrlib.trace import (
33
31
    mutter, mutter_callsite, report_exception,
34
32
    set_verbosity_level, get_verbosity_level, is_quiet, is_verbose, be_quiet,
35
 
    pop_log_file,
36
 
    push_log_file,
37
33
    _rollover_trace_maybe,
38
34
    )
39
35
 
93
89
        self.assertTrue(len(msg) > 0)
94
90
        self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: \"wibble\".\n')
95
91
 
96
 
    def test_report_external_import_error(self):
97
 
        """Short friendly message for missing system modules."""
98
 
        try:
99
 
            import ImaginaryModule
100
 
        except ImportError, e:
101
 
            pass
102
 
        else:
103
 
            self.fail("somehow succeeded in importing %r" % ImaginaryModule)
104
 
        msg = _format_exception()
105
 
        self.assertEqual(msg,
106
 
            'bzr: ERROR: No module named ImaginaryModule\n'
107
 
            'You may need to install this Python library separately.\n')
108
 
 
109
 
    def test_report_import_syntax_error(self):
110
 
        try:
111
 
            raise ImportError("syntax error")
112
 
        except ImportError, e:
113
 
            pass
114
 
        msg = _format_exception()
115
 
        self.assertContainsRe(msg,
116
 
            r"Traceback \(most recent call last\)")
117
 
 
118
92
    def test_trace_unicode(self):
119
93
        """Write Unicode to trace log"""
120
94
        self.log(u'the unicode character for benzene is \N{BENZENE RING}')
143
117
        else:
144
118
            self.fail("expected error not raised")
145
119
 
146
 
    def assertLogStartsWith(self, log, string):
147
 
        """Like assertStartsWith, but skips the log timestamp."""
148
 
        self.assertContainsRe(log,
149
 
            '^\\d+\\.\\d+  ' + re.escape(string))
150
 
 
151
120
    def test_mutter_callsite_1(self):
152
121
        """mutter_callsite can capture 1 level of stack frame."""
153
122
        mutter_callsite(1, "foo %s", "a string")
154
123
        log = self._get_log(keep_log_file=True)
155
124
        # begin with the message
156
 
        self.assertLogStartsWith(log, 'foo a string\nCalled from:\n')
 
125
        self.assertStartsWith(log, 'foo a string\nCalled from:\n')
157
126
        # should show two frame: this frame and the one above
158
127
        self.assertContainsRe(log,
159
 
            'test_trace\\.py", line \\d+, in test_mutter_callsite_1\n')
 
128
            'test_trace\.py", line \d+, in test_mutter_callsite_1\n')
160
129
        # this frame should be the final one
161
130
        self.assertEndsWith(log, ' "a string")\n')
162
131
 
165
134
        mutter_callsite(2, "foo %s", "a string")
166
135
        log = self._get_log(keep_log_file=True)
167
136
        # begin with the message
168
 
        self.assertLogStartsWith(log, 'foo a string\nCalled from:\n')
 
137
        self.assertStartsWith(log, 'foo a string\nCalled from:\n')
169
138
        # should show two frame: this frame and the one above
170
139
        self.assertContainsRe(log,
171
140
            'test_trace.py", line \d+, in test_mutter_callsite_2\n')
183
152
        self.assertContainsRe(log, "But fails in an ascii string")
184
153
        self.assertContainsRe(log, u"ascii argument: \xb5")
185
154
 
186
 
    def test_push_log_file(self):
187
 
        """Can push and pop log file, and this catches mutter messages.
188
 
 
189
 
        This is primarily for use in the test framework. 
190
 
        """
191
 
        tmp1 = tempfile.NamedTemporaryFile()
192
 
        tmp2 = tempfile.NamedTemporaryFile()
193
 
        try:
194
 
            memento1 = push_log_file(tmp1)
195
 
            mutter("comment to file1")
196
 
            try:
197
 
                memento2 = push_log_file(tmp2)
198
 
                try:
199
 
                    mutter("comment to file2")
200
 
                finally:
201
 
                    pop_log_file(memento2)
202
 
                mutter("again to file1")
203
 
            finally:
204
 
                pop_log_file(memento1)
205
 
            # the files were opened in binary mode, so should have exactly
206
 
            # these bytes.  and removing the file as the log target should
207
 
            # have caused them to be flushed out.  need to match using regexps
208
 
            # as there's a timestamp at the front.
209
 
            tmp1.seek(0)
210
 
            self.assertContainsRe(tmp1.read(),
211
 
                r"\d+\.\d+  comment to file1\n\d+\.\d+  again to file1\n")
212
 
            tmp2.seek(0)
213
 
            self.assertContainsRe(tmp2.read(),
214
 
                r"\d+\.\d+  comment to file2\n")
215
 
        finally:
216
 
            tmp1.close()
217
 
            tmp2.close()
218
 
 
219
155
 
220
156
class TestVerbosityLevel(TestCase):
221
157