~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

Refactor test setup/teardown into cleanup callbacks

Show diffs side-by-side

added added

removed removed

Lines of Context:
178
178
    def setUp(self):
179
179
        unittest.TestCase.setUp(self)
180
180
        self._cleanups = []
181
 
        self.oldenv = os.environ.get('HOME', None)
182
 
        os.environ['HOME'] = os.getcwd()
183
 
        self.bzr_email = os.environ.get('BZREMAIL')
184
 
        if self.bzr_email is not None:
185
 
            del os.environ['BZREMAIL']
186
 
        self.email = os.environ.get('EMAIL')
187
 
        if self.email is not None:
188
 
            del os.environ['EMAIL']
 
181
        self._cleanEnvironment()
189
182
        bzrlib.trace.disable_default_logging()
190
183
        self._startLogFile()
191
184
 
260
253
                    % (callable, self))
261
254
        self._cleanups.append(callable)
262
255
 
263
 
    def tearDown(self):
 
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):
264
268
        os.environ['HOME'] = self.oldenv
265
269
        if os.environ.get('BZREMAIL') is not None:
266
270
            del os.environ['BZREMAIL']
270
274
            del os.environ['EMAIL']
271
275
        if self.email is not None:
272
276
            os.environ['EMAIL'] = self.email
 
277
 
 
278
    def tearDown(self):
273
279
        logging.getLogger('').removeHandler(self._log_hdlr)
274
280
        bzrlib.trace.enable_default_logging()
275
281
        logging.debug('%s teardown', self.id())
458
464
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
459
465
 
460
466
    def setUp(self):
 
467
        super(TestCaseInTempDir, self).setUp()
461
468
        self._make_test_root()
462
 
        self._currentdir = os.getcwdu()
 
469
        _currentdir = os.getcwdu()
463
470
        short_id = self.id().replace('bzrlib.selftest.', '') \
464
471
                   .replace('__main__.', '')
465
472
        self.test_dir = os.path.join(self.TEST_ROOT, short_id)
466
473
        os.mkdir(self.test_dir)
467
474
        os.chdir(self.test_dir)
468
 
        super(TestCaseInTempDir, self).setUp()
 
475
        def _leaveDirectory():
 
476
            os.chdir(_currentdir)
 
477
        self.addCleanup(_leaveDirectory)
469
478
        
470
 
    def tearDown(self):
471
 
        os.chdir(self._currentdir)
472
 
        super(TestCaseInTempDir, self).tearDown()
473
 
 
474
479
    def build_tree(self, shape):
475
480
        """Build a test tree according to a pattern.
476
481