/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Michael Ellerman
  • Date: 2005-10-26 10:03:47 UTC
  • mfrom: (1185.16.116)
  • mto: (1185.16.126)
  • mto: This revision was merged to the branch mainline in revision 1488.
  • Revision ID: michael@ellerman.id.au-20051026100347-bb0b2bd42f7953f2
MergeĀ mainline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
from logging import debug, warning, error
42
42
 
43
43
 
44
 
 
45
44
class EarlyStoppingTestResultAdapter(object):
46
45
    """An adapter for TestResult to stop at the first first failure or error"""
47
46
 
159
158
 
160
159
    Error and debug log messages are redirected from their usual
161
160
    location into a temporary file, the contents of which can be
162
 
    retrieved by _get_log().
 
161
    retrieved by _get_log().  We use a real OS file, not an in-memory object,
 
162
    so that it can also capture file IO.  When the test completes this file
 
163
    is read into memory and removed from disk.
163
164
       
164
165
    There are also convenience functions to invoke bzr's command-line
165
 
    routine, and to build and check bzr trees."""
 
166
    routine, and to build and check bzr trees.
 
167
   
 
168
    In addition to the usual method of overriding tearDown(), this class also
 
169
    allows subclasses to register functions into the _cleanups list, which is
 
170
    run in order as the object is torn down.  It's less likely this will be
 
171
    accidentally overlooked.
 
172
    """
166
173
 
167
174
    BZRPATH = 'bzr'
168
175
    _log_file_name = None
 
176
    _log_contents = ''
169
177
 
170
178
    def setUp(self):
171
179
        unittest.TestCase.setUp(self)
172
 
        self.oldenv = os.environ.get('HOME', None)
173
 
        os.environ['HOME'] = os.getcwd()
174
 
        self.bzr_email = os.environ.get('BZREMAIL')
175
 
        if self.bzr_email is not None:
176
 
            del os.environ['BZREMAIL']
177
 
        self.email = os.environ.get('EMAIL')
178
 
        if self.email is not None:
179
 
            del os.environ['EMAIL']
 
180
        self._cleanups = []
 
181
        self._cleanEnvironment()
180
182
        bzrlib.trace.disable_default_logging()
181
 
        self._enable_file_logging()
 
183
        self._startLogFile()
182
184
 
183
185
    def _ndiff_strings(self, a, b):
184
186
        """Return ndiff between two strings containing lines.
212
214
        if not re.search(needle_re, haystack):
213
215
            raise AssertionError('pattern "%s" not found in "%s"'
214
216
                    % (needle_re, haystack))
215
 
        
216
 
    def _enable_file_logging(self):
 
217
 
 
218
    def _startLogFile(self):
 
219
        """Send bzr and test log messages to a temporary file.
 
220
 
 
221
        The file is removed as the test is torn down.
 
222
        """
217
223
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
218
 
 
219
224
        self._log_file = os.fdopen(fileno, 'w+')
220
 
 
221
225
        hdlr = logging.StreamHandler(self._log_file)
222
226
        hdlr.setLevel(logging.DEBUG)
223
227
        hdlr.setFormatter(logging.Formatter('%(levelname)8s  %(message)s'))
225
229
        logging.getLogger('').setLevel(logging.DEBUG)
226
230
        self._log_hdlr = hdlr
227
231
        debug('opened log file %s', name)
228
 
        
229
232
        self._log_file_name = name
230
 
 
231
 
    def tearDown(self):
 
233
        self.addCleanup(self._finishLogFile)
 
234
 
 
235
    def _finishLogFile(self):
 
236
        """Finished with the log file.
 
237
 
 
238
        Read contents into memory, close, and delete.
 
239
        """
 
240
        self._log_file.seek(0)
 
241
        self._log_contents = self._log_file.read()
 
242
        os.remove(self._log_file_name)
 
243
        self._log_file = self._log_file_name = None
 
244
 
 
245
    def addCleanup(self, callable):
 
246
        """Arrange to run a callable when this case is torn down.
 
247
 
 
248
        Callables are run in the reverse of the order they are registered, 
 
249
        ie last-in first-out.
 
250
        """
 
251
        if callable in self._cleanups:
 
252
            raise ValueError("cleanup function %r already registered on %s" 
 
253
                    % (callable, self))
 
254
        self._cleanups.append(callable)
 
255
 
 
256
    def _cleanEnvironment(self):
 
257
        self.oldenv = os.environ.get('HOME', None)
 
258
        os.environ['HOME'] = os.getcwd()
 
259
        self.bzr_email = os.environ.get('BZREMAIL')
 
260
        if self.bzr_email is not None:
 
261
            del os.environ['BZREMAIL']
 
262
        self.email = os.environ.get('EMAIL')
 
263
        if self.email is not None:
 
264
            del os.environ['EMAIL']
 
265
        self.addCleanup(self._restoreEnvironment)
 
266
 
 
267
    def _restoreEnvironment(self):
232
268
        os.environ['HOME'] = self.oldenv
233
269
        if os.environ.get('BZREMAIL') is not None:
234
270
            del os.environ['BZREMAIL']
238
274
            del os.environ['EMAIL']
239
275
        if self.email is not None:
240
276
            os.environ['EMAIL'] = self.email
 
277
 
 
278
    def tearDown(self):
241
279
        logging.getLogger('').removeHandler(self._log_hdlr)
242
280
        bzrlib.trace.enable_default_logging()
243
281
        logging.debug('%s teardown', self.id())
244
 
        self._log_file.close()
 
282
        self._runCleanups()
245
283
        unittest.TestCase.tearDown(self)
246
284
 
 
285
    def _runCleanups(self):
 
286
        """Run registered cleanup functions. 
 
287
 
 
288
        This should only be called from TestCase.tearDown.
 
289
        """
 
290
        for callable in reversed(self._cleanups):
 
291
            callable()
 
292
 
247
293
    def log(self, *args):
248
294
        logging.debug(*args)
249
295
 
252
298
        if self._log_file_name:
253
299
            return open(self._log_file_name).read()
254
300
        else:
255
 
            return ''
 
301
            return self._log_contents
256
302
 
257
303
    def capture(self, cmd):
258
304
        """Shortcut that splits cmd into words, runs, and returns stdout"""
418
464
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
419
465
 
420
466
    def setUp(self):
 
467
        super(TestCaseInTempDir, self).setUp()
421
468
        self._make_test_root()
422
 
        self._currentdir = os.getcwdu()
 
469
        _currentdir = os.getcwdu()
423
470
        short_id = self.id().replace('bzrlib.selftest.', '') \
424
471
                   .replace('__main__.', '')
425
472
        self.test_dir = os.path.join(self.TEST_ROOT, short_id)
426
473
        os.mkdir(self.test_dir)
427
474
        os.chdir(self.test_dir)
428
 
        super(TestCaseInTempDir, self).setUp()
 
475
        def _leaveDirectory():
 
476
            os.chdir(_currentdir)
 
477
        self.addCleanup(_leaveDirectory)
429
478
        
430
 
    def tearDown(self):
431
 
        os.chdir(self._currentdir)
432
 
        super(TestCaseInTempDir, self).tearDown()
433
 
 
434
479
    def build_tree(self, shape):
435
480
        """Build a test tree according to a pattern.
436
481
 
456
501
        """Fail unless path, which may be abs or relative, exists."""
457
502
        self.failUnless(osutils.lexists(path))
458
503
        
 
504
    def assertFileEqual(self, content, path):
 
505
        """Fail if path does not contain 'content'."""
 
506
        self.failUnless(osutils.lexists(path))
 
507
        self.assertEqualDiff(content, open(path, 'r').read())
 
508
        
459
509
 
460
510
class MetaTestLog(TestCase):
461
511
    def test_logging(self):
521
571
                   'bzrlib.selftest.testinv',
522
572
                   'bzrlib.selftest.test_ancestry',
523
573
                   'bzrlib.selftest.test_commit',
 
574
                   'bzrlib.selftest.test_command',
524
575
                   'bzrlib.selftest.test_commit_merge',
525
576
                   'bzrlib.selftest.testconfig',
526
577
                   'bzrlib.selftest.versioning',
557
608
                   'bzrlib.selftest.testoptions',
558
609
                   'bzrlib.selftest.testhttp',
559
610
                   'bzrlib.selftest.testnonascii',
 
611
                   'bzrlib.selftest.testreweave',
 
612
                   'bzrlib.selftest.testtsort',
560
613
                   ]
561
614
 
562
615
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,