~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to testsweet.py

  • Committer: Robert Collins
  • Date: 2005-08-23 08:18:05 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050823081805-f094248d196170be
move working dir creation into InTempDir and nuke the TestSuite subclass as unneeded

Show diffs side-by-side

added added

removed removed

Lines of Context:
218
218
 
219
219
 
220
220
class InTempDir(TestCase):
221
 
    """Base class for tests run in a temporary branch."""
 
221
    """Base class for tests that use disk resources - i.e to run in a temporary
 
222
    branch.
 
223
    """
 
224
 
 
225
    TEST_ROOT = None
 
226
    _TEST_NAME = 'test'
 
227
 
 
228
    def _make_test_root(self):
 
229
        import os
 
230
        import shutil
 
231
        import tempfile
 
232
        
 
233
        if InTempDir.TEST_ROOT is not None:
 
234
            return
 
235
        InTempDir.TEST_ROOT = os.path.abspath(
 
236
                                 tempfile.mkdtemp(suffix='.tmp',
 
237
                                                  prefix=self._TEST_NAME + '-',
 
238
                                                  dir=os.curdir))
 
239
    
 
240
        # print '%-30s %s\n' % ('running tests in', TestCase.TEST_ROOT)
 
241
    
 
242
        os.chdir(InTempDir.TEST_ROOT)
 
243
        # make a fake bzr directory there to prevent any tests propagating
 
244
        # up onto the source directory's real branch
 
245
        os.mkdir(os.path.join(InTempDir.TEST_ROOT, '.bzr'))
 
246
 
222
247
    def setUp(self):
223
248
        super(InTempDir, self).setUp()
224
249
        import os
 
250
        self._make_test_root()
225
251
        self.test_dir = os.path.join(self.TEST_ROOT, self.__class__.__name__)
226
252
        os.mkdir(self.test_dir)
227
253
        os.chdir(self.test_dir)
296
322
                print >>self.stream, test._get_log()
297
323
 
298
324
 
299
 
class TestSuite(unittest.TestSuite):
300
 
    
301
 
    def __init__(self, tests=(), name='test'):
302
 
        super(TestSuite, self).__init__(tests)
303
 
        self._name = name
304
 
 
305
 
    def run(self, result):
306
 
        self._setup_test_dir()
307
 
        return super(TestSuite,self).run(result)
308
 
 
309
 
    def _setup_test_dir(self):
310
 
        import os
311
 
        import shutil
312
 
        
313
 
        TestCase.ORIG_DIR = os.getcwdu()
314
 
        TestCase.TEST_ROOT = os.path.abspath(self._name + '.tmp')
315
 
    
316
 
        print '%-30s %s\n' % ('running tests in', TestCase.TEST_ROOT)
317
 
    
318
 
        if os.path.exists(TestCase.TEST_ROOT):
319
 
            shutil.rmtree(TestCase.TEST_ROOT)
320
 
        os.mkdir(TestCase.TEST_ROOT)
321
 
        os.chdir(TestCase.TEST_ROOT)
322
 
    
323
 
        # make a fake bzr directory there to prevent any tests propagating
324
 
        # up onto the source directory's real branch
325
 
        os.mkdir(os.path.join(TestCase.TEST_ROOT, '.bzr'))
326
 
 
327
325
 
328
326
class TextTestRunner(unittest.TextTestRunner):
329
327
 
338
336
    # we can override run() too.
339
337
 
340
338
 
341
 
def run_suite(a_suite, name='test', verbose=False):
342
 
    suite = TestSuite((a_suite,),name)
 
339
def run_suite(suite, name='test', verbose=False):
 
340
    import shutil
 
341
    InTempDir._TEST_NAME = name
343
342
    if verbose:
344
343
        style = 'verbose'
345
344
    else:
346
345
        style = 'progress'
347
346
    runner = TextTestRunner(stream=sys.stdout, style=style)
348
347
    result = runner.run(suite)
 
348
    # This is still a little bogus, 
 
349
    # but only a little. Folk not using our testrunner will
 
350
    # have to delete their temp directories themselves.
 
351
    if result.wasSuccessful():
 
352
        shutil.rmtree(InTempDir.TEST_ROOT) 
 
353
    else:
 
354
        print "Failed tests working directories are in '%s'\n" % InTempDir.TEST_ROOT
349
355
    return result.wasSuccessful()