SyFi  0.3
test_tempdir_base.py
Go to the documentation of this file.
00001 
00002 import os, shutil
00003 import unittest
00004 import sfc
00005 
00006 class CompatibilityTestCase(unittest.TestCase):
00007     def __init__(self, *args, **kwargs):
00008         super(CompatibilityTestCase, self).__init__(*args, **kwargs)
00009 
00010     def setUp(self):
00011         super(CompatibilityTestCase, self).setUp()
00012 
00013     def tearDown(self):
00014         super(CompatibilityTestCase, self).tearDown()
00015 
00016     ### Asserts available in TestCase from python 2.7:
00017 
00018     def _assertIsInstance(self, obj, cl, msg=None):
00019         self.assertTrue(isinstance(obj, cl), msg=None)
00020 
00021     def _assertNotIsInstance(self, obj, cl, msg=None):
00022         self.assertFalse(isinstance(obj, cl), msg=msg)
00023 
00024     def _assertIs(self, obj, cl, msg=None):
00025         self.assertTrue(obj is cl, msg=None)
00026 
00027     def _assertIsNot(self, obj, cl, msg=None):
00028         self.assertTrue(obj is not cl, msg=msg)
00029 
00030     def _assertIsNone(self, obj, msg=None):
00031         self.assertTrue(obj is None, msg=msg)
00032 
00033     def _assertGreater(self, lhs, rhs, msg=None):
00034         self.assertTrue(lhs > rhs, msg=msg)
00035 
00036     def _assertLess(self, lhs, rhs, msg=None):
00037         self.assertTrue(lhs < rhs, msg=msg)
00038 
00039 # Hack for different versions of python unittest:
00040 for func in ('assertIsInstance', 'assertNotIsInstance',
00041              'assertIs', 'assertIsNot', 'assertIsNone',
00042              'assertGreater', 'assertLess'):
00043     if not hasattr(CompatibilityTestCase, func):
00044         setattr(CompatibilityTestCase, func, getattr(CompatibilityTestCase, '_'+func))
00045 
00046 
00047 class TempDirTestBase(CompatibilityTestCase):
00048     def __init__(self, *args, **kwargs):
00049         super(TempDirTestBase, self).__init__(*args, **kwargs)
00050         self.make_base_directories()
00051         self.make_suite_directories()
00052 
00053     def make_base_directories(self):
00054         "Make sure we have all base directories."
00055         for s in ('', 'running', 'done', 'cache'):
00056             p = os.path.join('.test', s)
00057             if not os.path.exists(p):
00058                 os.mkdir(p)
00059 
00060     def make_suite_directories(self):
00061         # Make custom directories for current suite
00062         name = type(self).__name__
00063         def ensure_dir(s):
00064             p = os.path.abspath(os.path.join(".test", s, name))
00065             if not os.path.exists(p):
00066                 os.mkdir(p)
00067             return p
00068         self._test_dir_running = ensure_dir('running')
00069         self._test_dir_done    = ensure_dir('done')
00070         self._test_dir_cache   = ensure_dir('cache')
00071 
00072     def setUp(self):
00073         super(TempDirTestBase, self).setUp()
00074 
00075         # Hook for modifying options across tests
00076         self.options = sfc.default_parameters()
00077         # Use local cache dir
00078         self.options.compilation.cache_dir = self._test_dir_cache
00079 
00080         # Create and enter a clean directory:
00081         shutil.rmtree(self._test_dir_running, ignore_errors=True)
00082         os.mkdir(self._test_dir_running)
00083         os.chdir(self._test_dir_running)
00084 
00085     def tearDown(self):
00086         super(TempDirTestBase, self).tearDown()
00087 
00088         # Leave clean directory
00089         os.chdir("..")
00090 
00091         cleanup = 0
00092 
00093         if cleanup:
00094             # Remove leftover files from previous test run
00095             shutil.rmtree(self._test_dir_done, ignore_errors=True)
00096 
00097             # Move running to done to keep files from this test run
00098             os.rename(self._test_dir_running, self._test_dir_done)
00099 
00100     def testSetup(self):
00101         "Just see that setUp and tearDown works."
00102         self.assertTrue("Did not crash in setUp.")
00103 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines